IActivator.h
1 
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 
35 namespace Pt {
36 
37 namespace Cosmo {
38 
39 class 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 
101 namespace Pt {
102 
103 namespace Cosmo {
104 
105 inline 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 
115 template<typename I>
116 I* 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 
128 inline Component* IActivator::query(const std::string& featureId) const
129 {
130  return onQuery(featureId);
131 }
132 
133 
134 template<typename I>
135 I* 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
virtual Component * onQuery(const std::string &featureId) const =0
Gets the component by feature ID or null if not available.
I * queryInterface()
Returns the interface or null if not available.
Core module.
Definition: Allocator.h:33
Dependency resolver.
Definition: IActivator.h:50
IActivator()
Constructor.
Definition: IActivator.h:54
Component base class.
Definition: Component.h:52
virtual ~IActivator()
Destructor.
Definition: IActivator.h:60
Component * query(const std::string &featureId) const
Returns the component by feature ID or null.
Definition: IActivator.h:128
Component * get(const std::string &featureId) const
Returns the component by feature ID or throws.
Definition: IActivator.h:105