IActivator.h
1
26
27#ifndef COSMO_ACTIVATOR_INTERFACE_H
28#define COSMO_ACTIVATOR_INTERFACE_H
29
30#include <Pt/Cosmo/Api.h>
31
32#include <string>
33#include <stdexcept>
34
35namespace Pt {
36
37namespace Cosmo {
38
39class Component;
40
50{
51 public:
55 {
56 }
57
60 virtual ~IActivator()
61 {}
62
67 Component* get(const std::string& featureId) const;
68
73 template<typename I>
74 I* get(const std::string& id) const;
75
80 Component* query(const std::string& featureId) const;
81
86 template<typename I>
87 I* query(const std::string& id) const;
88
89 protected:
92 virtual Component* onQuery(const std::string& featureId) const = 0;
93};
94
95} // namespace Cosmo
96
97} // namespace Pt
98
99#include <Pt/Cosmo/Component.h>
100
101namespace Pt {
102
103namespace Cosmo {
104
105inline Component* IActivator::get(const std::string& featureId) const
106{
107 Component* c = onQuery(featureId);
108 if( ! c )
109 throw std::invalid_argument(featureId);
110
111 return c;
112}
113
114
115template<typename I>
116I* IActivator::get(const std::string& featureId) const
117{
118 Component* c = get(featureId);
119
120 I* i = c->queryInterface<I>();
121 if( ! i )
122 throw std::invalid_argument(featureId);
123
124 return i;
125}
126
127
128inline Component* IActivator::query(const std::string& featureId) const
129{
130 return onQuery(featureId);
131}
132
133
134template<typename I>
135I* IActivator::query(const std::string& featureId) const
136{
137 Component* c = query(featureId);
138 if(c)
139 return c->queryInterface<I>();
140
141 return 0;
142}
143
144} // namespace Cosmo
145
146} // namespace Pt
147
148#endif
Component base class.
Definition Component.h:52
I * queryInterface()
Returns the interface or null if not available.
Component * query(const std::string &featureId) const
Returns the component by feature ID or null.
Definition IActivator.h:128
IActivator()
Constructor.
Definition IActivator.h:54
Component * get(const std::string &featureId) const
Returns the component by feature ID or throws.
Definition IActivator.h:105
virtual ~IActivator()
Destructor.
Definition IActivator.h:60
virtual Component * onQuery(const std::string &featureId) const =0
Gets the component by feature ID or null if not available.
Cosmo Component Model.
Definition Api.h:53
Core module.
Definition Allocator.h:33