ComponentPlugin.h
1
26
27#ifndef COSMO_COMPONENT_PLUGIN_H
28#define COSMO_COMPONENT_PLUGIN_H
29
30#include <Pt/Cosmo/ComponentMode.h>
31#include <Pt/Cosmo/Enumerable.h>
32#include <Pt/Cosmo/AutoReleasePool.h>
33#include <Pt/Cosmo/IComponentPlugin.h>
34
35#include <string>
36#include <vector>
37#include <map>
38#include <typeindex>
39#include <typeinfo>
40
41namespace Pt {
42
43namespace Cosmo {
44
45class Component;
47
50template <typename T>
51class ComponentFactory
52{
53 public:
56 ComponentFactory()
57 {}
58
61 Component* create()
62 {
63 return new T();
64 }
65
68 void destroy(Component* instance)
69 {
70 delete instance;
71 }
72};
73
76template <typename T>
77class FromReference
78{
79 public:
80 explicit FromReference(T& instance)
81 : _instance(&instance)
82 {}
83
84 Cosmo::Component* create()
85 { return _instance; }
86
87 void destroy(Cosmo::Component* /*instance*/)
88 {}
89
90 private:
91 T* _instance;
92};
93
103template <typename T, typename Factory = ComponentFactory<T> >
104class ComponentPlugin : public IComponentPlugin
105{
106 public:
107 class IdListEnumeration : public IEnumeration<const char*>
108 {
109 public:
110 IdListEnumeration(const std::vector<std::string>& list)
111 : _list(list)
112 , _it( list.begin() )
113 { }
114
115 protected:
116 virtual bool onIsValid() const override
117 {
118 return _it != _list.end();
119 }
120
121 virtual void onAdvance() override
122 {
123 ++_it;
124 }
125
126 virtual const char* onGet() const override
127 {
128 return _it->c_str();
129 }
130
131 private:
132 const std::vector<std::string>& _list;
133 typename std::vector<std::string>::const_iterator _it;
134 };
135
136 class IdList : public IEnumerable<const char*>
137 {
138 public:
139 IdList(IAutoReleasePool& pool,
140 const std::vector<std::string>& strings)
141 : _pool(pool)
142 , _strings(strings)
143 { }
144
145 protected:
146 Cosmo::Enumerator<const char*> onEnumerate() const
147 {
148 IdListEnumeration* e = new IdListEnumeration(_strings);
149 return Cosmo::Enumerator<const char*>(_pool, e);
150 }
151
152 private:
153 IAutoReleasePool& _pool;
154 const std::vector<std::string>& _strings;
155 };
156
157 public:
158 ComponentPlugin(const char* featureId, const char* info,
159 ComponentMode mode = ComponentMode::None,
160 const Factory& factory = ComponentFactory<T>());
161
168 ComponentPlugin(const char* featureId, const char* classId, const char* info,
169 ComponentMode mode = ComponentMode::None,
170 const Factory& factory = ComponentFactory<T>());
171
175
178 template <typename I>
180
183 template <typename I>
184 void registerInterface( I* (T::*getter)() );
185
188 void registerDependency(const char* featureId);
189
192 void registerFeatureId(const char* featureId);
193
194 protected:
195 // inherit docs
196 virtual bool onHasFeatureId(const char* featureId) const override;
197
198 // inherit docs
199 virtual const IEnumerable<const char*>& onGetFeatureIds() const override;
200
201 // inherit docs
202 virtual const char* onGetClassId() const override;
203
204 // inherit docs
205 bool onHasInterface(const std::type_info& ti) const override;
206
207 // inherit docs
209 const std::type_info& ti) const override;
210
211 protected:
212 // inherit docs
213 virtual void onAttach(IComponentManager& cm,
214 const std::string& instance) override;
215
216 // inherit docs
217 virtual void onDetach(IComponentManager& cm,
218 const std::string& instance) override;
219
220 // inherit docs
221 virtual void onDetach(IComponentManager& cm) override;
222
223 // inherit docs
224 virtual const IEnumerable<const char*>& onGetDependencies() const override;
225
226 // inherit docs
227 virtual ComponentMode onGetMode() const override;
228
229 // inherit docs
230 virtual const char* onGetInfo() const override;
231
232 // inherit docs
233 virtual Component* onCreate() override;
234
235 // inherit docs
236 virtual void onDestroy(Component* instance) override;
237
238 private:
239 class IThunk;
240
241 template <typename I>
242 class GetAggregated;
243
244 template <typename I>
245 class GetInherited;
246
247 typedef std::map<std::type_index, IThunk*> ThunkMap;
248 ThunkMap _thunks;
249
250 Factory _factory;
251 std::string _classId;
252 std::vector<std::string> _featureIds;
253 std::string _info;
254 ComponentMode _mode;
255 std::vector<std::string> _dependencies;
256
257 std::multimap<IComponentManager*,
258 std::string> _componentManager;
259
260 AutoReleasePool _pool;
261 IdList _featureIdsEnum;
262 IdList _dependenciesEnum;
263};
264
265} // namespace Cosmo
266
267} // namespace Pt
268
269#include <Pt/Cosmo/ComponentPlugin.tpp>
270
271#endif
Auto-release pool implementation.
Definition AutoReleasePool.h:45
Component flags.
Definition ComponentMode.h:45
virtual bool onHasFeatureId(const char *featureId) const override
Returns true if the given feature ID is implemented.
void registerDependency(const char *featureId)
Registers the dependency to another component.
virtual const char * onGetClassId() const override
Returns the class ID of the component.
virtual void onDetach(IComponentManager &cm) override
The plugin is detached from a component manager.
void registerFeatureId(const char *featureId)
Registers a feature ID.
virtual const IEnumerable< const char * > & onGetFeatureIds() const override
Returns the implemented feature IDs.
virtual Component * onCreate() override
Creates a component instance.
virtual IUnknown * onQueryInterface(Component &c, const std::type_info &ti) const override
Returns the interface or null if the interface is not implemented.
void registerInterface()
Registers a component interface.
virtual void onDestroy(Component *instance) override
Destroys a component instance.
virtual ComponentMode onGetMode() const override
Returns the component mode.
void registerInterface(I *(T::*getter)())
Registers a component interface.
virtual const char * onGetInfo() const override
Returns the info string of the component.
bool onHasInterface(const std::type_info &ti) const override
Returns true if the interface is implemented.
ComponentPlugin(const char *featureId, const char *classId, const char *info, ComponentMode mode=ComponentMode::None, const Factory &factory=ComponentFactory< T >())
Constructor.
virtual const IEnumerable< const char * > & onGetDependencies() const override
Returns the feature IDs of the depencies.
virtual void onAttach(IComponentManager &cm, const std::string &instance) override
The plugin is attached to a component manager.
virtual void onDetach(IComponentManager &cm, const std::string &instance) override
The plugin is detached from a component manager.
Component base class.
Definition Component.h:52
Enumerator for IEnumerables.
Definition IEnumerable.h:283
Auto-release pool interface.
Definition IAutoReleasePool.h:46
Component manager interface.
Definition IComponentManager.h:55
Component plugin interface.
Definition IComponentPlugin.h:51
ComponentMode mode() const
Returns the component mode.
Definition IComponentPlugin.h:130
const char * info() const
Returns the info string of the component.
Definition IComponentPlugin.h:136
const char * classId() const
Returns the class ID of the component.
Definition IComponentType.h:128
Enumerable interface.
Definition IEnumerable.h:58
Enumerator implementation.
Definition IEnumerable.h:118
Interface base class.
Definition IUnknown.h:44
Cosmo Component Model.
Definition Api.h:53
Core module.
Definition Allocator.h:33