OpenAB  1.0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Plugin.hpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5  */
10 #ifndef PLUGIN_TEMPLATE_HPP_
11 #define PLUGIN_TEMPLATE_HPP_
12 
13 #include <map>
14 #include <typeinfo>
15 #include <helpers/Log.hpp>
16 
17 namespace OpenAB_Plugin{
18 
19 template<typename __C>
20 class Node
21 {
22  public:
23  Node(__C*p,std::string& name):p(p),next(NULL),name(name){};
24  Node(__C*p,std::string& name,Node*n):p(p),next(n),name(name){};
25  __C* getElement(){return p;};
26  Node*getNext(){return next;};
27  void setNext(Node*n){next=n;};
28 
29  private:
30  __C* p;
31  Node* next;
32  std::string name;
33 };
34 
35 template<typename __C>
37 {
38  public:
39  NodeIterator(Node<__C>*c):current(c){};
40 
42  {
43  if (NULL != current){
44  current = current->getNext();
45  }
46  return *this;
47  };
48  bool operator!=(NodeIterator<__C> it){ return current != it.current; };
49  __C* operator->(){return current->getElement();};
50 
51  private:
52  Node<__C>*current;
53 };
54 
55 template<typename __C>
57 {
58  public:
61 
63  {};
65  {};
66  __C* operator[](std::string name)
67  {
68  typeNode * node = findNode(name);
69  if (NULL == node)
70  return NULL;
71  else
72  return node->getElement();
73  };
74 
76  {
77  return iterator(first);
78  };
80  {
81  return iterator(NULL);
82  };
83 
84  void push_front(__C*elem,std::string& name){
85  typeNode * node = new typeNode(elem,name,first);
86  first = node;
87  };
88 
89  typeNode * findNode(std::string name)
90  {
91  typeNode * l = first;
92  while(NULL != l){
93  if (0 == l->getElement()->getName().compare(name))
94  {
95  return l;
96  }
97  l = l->getNext();
98  }
99  return NULL;
100  };
101 
102  void eraseNode(std::string name)
103  {
104  LOG_FUNC() << " Erase: " << " \"" << name << "\""<<std::endl;
105  typeNode * l = first;
106  typeNode * prev = first;
107  while(NULL != l){
108  if (0 == l->getElement()->getName().compare(name))
109  {
110  if (l==prev){
111  first = l->getNext();
112  }else{
113  prev->setNext(l->getNext());
114  }
115  delete l;
116  return;
117  }
118  if (l!=prev)
119  prev=l;
120  l = l->getNext();
121  }
122  return;
123  };
124  private:
125  typeNode* first;
126 };
127 
132 template<typename __C,typename __P>
133 class Factory
134 {
135  public:
136 
141 
145  Factory(std::string name):name(name){
146  LOG_FUNC() << " Adding new " << typeid(__C).name() << " \"" << name << "\""<<std::endl;
147  if (NULL == factories[name])
148  {
149  factories.push_front(this,name);
150  if (NULL != loadedCb)
151  {
152  loadedCb(name);
153  }
154  }
155  };
160 
161  virtual __C * newIstance(const __P &) = 0;
162 
163  std::string getName(){return name;};
164 
165  typedef void (*PluginLoadedCallback) (const std::string& pluginName);
166  private:
167 
168  std::string name;
169  static PluginLoadedCallback loadedCb;
170 
171  public:
173  static void setPluginLoadedCallback(PluginLoadedCallback callback) {loadedCb = callback;}
174 };
175 
177 {
178  public:
180  virtual ~Parameters(){};
181 
182  virtual bool fromJSON(const std::string& json) = 0;
183  virtual std::string toJSON() const = 0;
184 };
185 
193 #define EXPORT_PLUGIN_INTERFACE(PluginNamespace, PluginInterface, PluginParameters) \
194  namespace PluginNamespace { \
195  Factory::typeList factories; \
196  template<> \
197  Factory::PluginLoadedCallback Factory::loadedCb = NULL; \
198  \
199  template<> \
200  Factory::typeList Factory::factories = PluginNamespace::factories;\
201  }
202 
210 #define DECLARE_PLUGIN_INTERFACE(PluginNamespace, PluginInterface, PluginParameters) \
211  namespace PluginNamespace { \
212  typedef OpenAB_Plugin::Factory< PluginInterface , PluginParameters > Factory; \
213  }
214 
221 #define REGISTER_PLUGIN_FACTORY(PluginFactory) \
222  namespace { \
223  PluginFactory factory; \
224  }
225 
226 } // namespace OpenAB_Plugin
227 
228 #endif // PLUGIN_TEMPLATE_HPP_
bool operator!=(NodeIterator< __C > it)
Definition: Plugin.hpp:48
Definition: Plugin.hpp:176
virtual __C * newIstance(const __P &)=0
NodeIterator(Node< __C > *c)
Definition: Plugin.hpp:39
virtual bool fromJSON(const std::string &json)=0
LinkedList()
Definition: Plugin.hpp:62
static void setPluginLoadedCallback(PluginLoadedCallback callback)
Definition: Plugin.hpp:173
Factory(std::string name)
Constructor.
Definition: Plugin.hpp:145
~LinkedList()
Definition: Plugin.hpp:64
typeNode * findNode(std::string name)
Definition: Plugin.hpp:89
Node * getNext()
Definition: Plugin.hpp:26
NodeIterator< __C > iterator
Definition: Plugin.hpp:60
std::string getName()
Definition: Plugin.hpp:163
void eraseNode(std::string name)
Definition: Plugin.hpp:102
virtual ~Parameters()
Definition: Plugin.hpp:180
Definition: Plugin.hpp:20
static typeList factories
Definition: Plugin.hpp:172
void(* PluginLoadedCallback)(const std::string &pluginName)
Definition: Plugin.hpp:165
Definition: Plugin.hpp:36
virtual std::string toJSON() const =0
void push_front(__C *elem, std::string &name)
Definition: Plugin.hpp:84
iterator begin()
Definition: Plugin.hpp:75
Definition: Plugin.hpp:133
void setNext(Node *n)
Definition: Plugin.hpp:27
__C * operator[](std::string name)
Definition: Plugin.hpp:66
#define LOG_FUNC()
Definition: Log.hpp:147
Node(__C *p, std::string &name)
Definition: Plugin.hpp:23
__C * operator->()
Definition: Plugin.hpp:49
NodeIterator & operator++()
Definition: Plugin.hpp:41
__C * getElement()
Definition: Plugin.hpp:25
~Factory()
Destructor, virtual by default.
Definition: Plugin.hpp:159
LinkedList< Factory< __C, __P > > typeList
List of the specialized Factories.
Definition: Plugin.hpp:140
Node< __C > typeNode
Definition: Plugin.hpp:59
iterator end()
Definition: Plugin.hpp:79
Definition: Plugin.hpp:56
Node(__C *p, std::string &name, Node *n)
Definition: Plugin.hpp:24
Parameters()
Definition: Plugin.hpp:179