dynamic-graph  4.1.0-8-gdab7-dirty
Dynamic graph library
factory.h
1 // -*- mode: c++ -*-
2 // Copyright 2010, François Bleibel, Thomas Moulard, Olivier Stasse,
3 // JRL, CNRS/AIST.
4 //
5 
6 #ifndef DYNAMIC_GRAPH_FACTORY_HH
7 #define DYNAMIC_GRAPH_FACTORY_HH
8 #include <map>
9 #include <string>
10 #include <vector>
11 
12 #include <boost/noncopyable.hpp>
13 
14 #include <dynamic-graph/dynamic-graph-api.h>
15 #include <dynamic-graph/exception-factory.h>
16 #include <dynamic-graph/fwd.hh>
17 
27 #define DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CLASSTYPE, CLASSNAME) \
28  const std::string CLASSTYPE::CLASS_NAME = CLASSNAME; \
29  extern "C" { \
30  ::dynamicgraph::Entity * \
31  EntityMaker_##CLASSTYPE(const std::string &objname) { \
32  return new CLASSTYPE(objname); \
33  } \
34  ::dynamicgraph::EntityRegisterer reg_##CLASSTYPE(CLASSNAME, \
35  &EntityMaker_##CLASSTYPE); \
36  } \
37  struct e_n_d__w_i_t_h__s_e_m_i_c_o_l_o_n
38 
39 namespace dynamicgraph {
82 class DYNAMIC_GRAPH_DLLAPI FactoryStorage : private boost::noncopyable {
83 public:
86  typedef Entity *(*EntityConstructor_ptr)(const std::string &);
87 
88  ~FactoryStorage();
89 
91  static FactoryStorage *getInstance();
92 
94  static void destroy();
95 
108  void registerEntity(const std::string &entname, EntityConstructor_ptr ent);
109 
117  void deregisterEntity(const std::string &entname);
118 
137  Entity *newEntity(const std::string &classname,
138  const std::string &objname) const;
139 
145  bool existEntity(const std::string &name) const;
146 
152  void listEntities(std::vector<std::string> &list) const;
153 
154 private:
160  explicit FactoryStorage();
161 
166  typedef std::map<std::string, EntityConstructor_ptr> EntityMap;
167 
170  EntityMap entityMap;
171 
173  static FactoryStorage *instance_;
174 };
175 
185 class DYNAMIC_GRAPH_DLLAPI EntityRegisterer : private boost::noncopyable {
186 public:
188  explicit EntityRegisterer(const std::string &entityClassName,
190 
192  ~EntityRegisterer();
193 
194 private:
197  const std::string entityName;
198 };
199 } // end of namespace dynamicgraph
200 
201 #endif
202 
203 // LocalWords: unregister
This class represents an entity, i.e. a generic computational unit that provides input and output sig...
Definition: entity.h:53
Entity *(* EntityConstructor_ptr)(const std::string &)
Function pointer providing an entity instance from its name.
Definition: factory.h:86
This class automatically register an Entity to the global factory at initialization and unregister it...
Definition: factory.h:185
Provides a way to create Entity objects from their class name.
Definition: factory.h:82