-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathObjectExtension.h
224 lines (195 loc) · 9.38 KB
/
ObjectExtension.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
This file is part of the Util library.
Copyright (C) 2007-2012 Benjamin Eikel <[email protected]>
Copyright (C) 2007-2012 Claudius J�hn <[email protected]>
Copyright (C) 2007-2012 Ralf Petring <[email protected]>
This library is subject to the terms of the Mozilla Public License, v. 2.0.
You should have received a copy of the MPL along with this library; see the
file LICENSE. If not, you can obtain one at http://mozilla.org/MPL/2.0/.
*/
#ifndef OBJECTEXTENSION_H_INCLUDED
#define OBJECTEXTENSION_H_INCLUDED
#include "AttributeProvider.h"
#include "GenericAttribute.h"
#include "StringIdentifier.h"
#include <memory>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
namespace Util {
//! @addtogroup util_helper
//! @{
/*! @defgroup objext ObjectExtensions
ObjectExtensions can be used to dynamically extend objects inheriting from AttributeProvider
with data and functionality at runtime. Internally, it is a unified interface
for several different GenericAttributes.
There are two types of extensions: 1. non polymorphic and 2. polymorphic.
1. non polymorphic extensions ...
- are based on ordinary classes (without any virtual function).
- must use the PROVIDES_TYPE_NAME_NV(NameOfTheExtension) macro,
OR implement a static function getExtensionName() that returns a StringIdentifier with the extension's name.
OR the extension's name must be provided for each function
- must provide a copy constructor.
- do not introduce additional overhead in comparison to a normal GenericAttribute for
storing a value.
- may provide constructors with arbitrary many parameters.
\code
class MyExtension{
PROVIDES_TYPE_NAME_NV(MyExtension)
int someValue;
public:
MyExtension(int _someValue) : someValue(_someValue){}
int getValue()const { return someValue; }
}
...
MyObject * a = new MyObject; // MyObject---|>AttributeProvider
Util::addObjectExtension<MyExtension>(42);
...
Util::requireObjectExtension<MyExtension>(a)->getValue();
\endcode
2. polymorphic extensions ...
- introduce an additional memory overhead (pointer + polymorphic object header)
- may also provide constructors with arbitrary many parameters.
- must provide a base class that contains:
- the PROVIDES_TYPE_NAME(NameOfTheExtensionBase) macro (or getExtensionName(), see above)
- a typedef extentsionBase_t referening to the class itself
- a (possibly abstract) clone() method returning a pointer of extentsionBase_t
- Specific extension classes ...
- inherit from extentsionBase_t.
- must implement the clone() method.
- should NOT contain the PROVIDES_TYPE_NAME macro.
\code
class MyExtensionBase{
PROVIDES_TYPE_NAME(MyExtensionBase)
public:
typedef MyExtensionBase extentsionBase_t;
virtual float calcSomething()const = 0;
virtual MyExtensionBase * clone()const = 0;
};
//! ---|> MyExtensionBase
class MyExt1 : public MyExtensionBase{
public:
//! ---|> MyExtensionBase
float calcSomething() const override { return 27.0f; };
//! ---|> MyExtensionBase
MyExt1 * clone()const { return new MyExt1; }
};
...
MyObject * a = new MyObject; // MyObject---|>AttributeProvider
Util::addObjectExtension<MyExt1>();
...
Util::requireObjectExtension<MyExtensionBase>(a)->calcSomething();
\endcode
@{
*/
// ------------------------------------------------------------------------------
//! @internal
namespace _Internals{
template<typename extension_t>
Util::StringIdentifier getObjectExtensionName(decltype(extension_t::getExtensionName) = nullptr){
return extension_t::getExtensionName();
}
template<typename extension_t>
Util::StringIdentifier getObjectExtensionName(decltype(extension_t::getClassId) = nullptr){
return extension_t::getClassId();
}
// ---------------------------
//// non polymorphic object extension
template<typename extension_t,typename object_t,typename ...args>
extension_t * addObjectExtension(typename std::enable_if<!std::is_polymorphic<extension_t>::value>::type*,
const Util::StringIdentifier & extName,object_t * obj,args&&... params){
typedef Util::WrapperAttribute<extension_t> attr_t;
attr_t * attr( new attr_t(std::forward<args>(params)...) );
obj->setAttribute(extName,attr);
return &attr->ref();
}
template<typename extension_t>
extension_t * getObjectExtension(typename std::enable_if<!std::is_polymorphic<extension_t>::value>::type*,
const Util::StringIdentifier & extName,const Util::AttributeProvider * obj){
auto attr = obj->getAttribute<Util::WrapperAttribute<extension_t>>(extName);
return attr ? &attr->ref() : nullptr;
}
// ---------------------------
//// polymorphic object extension
template<class extension_t>
struct PolymorphicObjectExtensionWrapper : public GenericAttribute{
std::unique_ptr<extension_t> ext;
explicit PolymorphicObjectExtensionWrapper(extension_t * _ext) : ext(_ext){}
//! ---|> GenericAttribute
PolymorphicObjectExtensionWrapper * clone() const override{
return new PolymorphicObjectExtensionWrapper(ext.get()->clone());
}
extension_t * ref()const { return ext.get(); }
};
template<typename extension_t,typename object_t,typename ...args>
extension_t * addObjectExtension(typename std::enable_if<std::is_polymorphic<extension_t>::value>::type*,
const Util::StringIdentifier & extName,object_t * obj,args&&... params){
auto attr = new PolymorphicObjectExtensionWrapper<typename extension_t::extentsionBase_t>(
new extension_t(std::forward<args>(params)...));
obj->setAttribute(extName,attr);
return static_cast<extension_t*>(attr->ref());
}
template<typename extension_t>
extension_t * getObjectExtension(typename std::enable_if<std::is_polymorphic<extension_t>::value>::type*,
const Util::StringIdentifier & extName,const Util::AttributeProvider * obj){
auto attr = obj->getAttribute<PolymorphicObjectExtensionWrapper<typename extension_t::extentsionBase_t>>(extName);
return attr ? attr->ref() : nullptr;
}
}
// ---------------------------------------------------------------------------
/*! Create and add an extension of type extension_t to the given @p object.
- Additional parameters are passed to the extension's constructor.
- The extension's name is inquired by calling extension_t::getObjectExtensionName() or extension_t::getClassId()
- The created extension is returned. */
template<typename extension_t,typename object_t,typename ...args>
extension_t * addObjectExtension(object_t * obj,args&&... params){
return _Internals::addObjectExtension<extension_t,object_t>(nullptr,
_Internals::getObjectExtensionName<extension_t>(),obj,std::forward<args>(params)...);
}
/*! Create and add an extension of type extension_t to the given @p object named @p extName.
@see addObjectExtension(obj, params...) */
template<typename extension_t,typename object_t,typename ...args>
extension_t * addObjectExtension(const StringIdentifier & extName,object_t * obj,args&&... params){
return _Internals::addObjectExtension<extension_t,object_t>(nullptr,extName,obj,std::forward<args>(params)...);
}
/*! If the given @p object has a extension of type extension_t, it is returned; nullptr otherwise.
- The extension's name is inquired by calling extension_t::getObjectExtensionName() or extension_t::getClassId() */
template<typename extension_t>
extension_t * getObjectExtension(const Util::AttributeProvider * obj){
return getObjectExtension<extension_t>(_Internals::getObjectExtensionName<extension_t>(),obj);
}
/*! If the given @p object has a extension of type extension_t named @p extName, it is returned; nullptr otherwise.
@see getObjectExtension(obj) */
template<typename extension_t>
extension_t * getObjectExtension(const StringIdentifier & extName,const Util::AttributeProvider * obj){
return _Internals::getObjectExtension<extension_t>(nullptr,extName,obj);
}
/*! If the given @p object has a extension of type extension_t, it is returned; otherwise, an exception is thrown.
- The extension's name is inquired by calling extension_t::getObjectExtensionName() or extension_t::getClassId(). */
template<typename extension_t>
extension_t * requireObjectExtension(const Util::AttributeProvider * obj){
return requireObjectExtension<extension_t>(_Internals::getObjectExtensionName<extension_t>(),obj);
}
/*! If the given @p object has a extension of type extension_t named @p extName, it is returned; otherwise, an exception is thrown.
- The extension's name is inquired by calling extension_t::getObjectExtensionName() or extension_t::getClassId(). */
template<typename extension_t>
extension_t * requireObjectExtension(const StringIdentifier & extName,const Util::AttributeProvider * obj){
extension_t * ext = getObjectExtension<extension_t>(extName,obj);
if(!ext)
throw std::invalid_argument("Required object extension '"+extName.toString()+"' not found.");
return ext;
}
/*! Returns true iff the @p object has a extension of type extension_t.
- The extension's name is inquired by calling extension_t::getObjectExtensionName() or extension_t::getClassId(). */
template<typename extension_t> bool hasObjectExtension(const Util::AttributeProvider * obj){
return getObjectExtension<extension_t>(obj)!=nullptr;
}
//! Returns true iff the @p object has a extension of type extension_t named @p extName.
template<typename extension_t> bool hasObjectExtension(const StringIdentifier & extName,const Util::AttributeProvider * obj){
return getObjectExtension<extension_t>(extName,obj)!=nullptr;
}
//! @}
//! @}
}
#endif // OBJECTEXTENSION_H_INCLUDED