objectFactory.hpp
Go to the documentation of this file.
1 #ifndef JULIAN_OBJECTFACTORY_HPP
2 #define JULIAN_OBJECTFACTORY_HPP
3 
4 #include <map>
5 #include <string>
6 #include <typeinfo>
7 #include <utils/smartPointer.hpp>
8 
9 
10 namespace julian {
11 
25  template<class T>
26  class ObjectFactory {
27  public:
30  typedef SmartPointer<T> (*CreateT)();
31 
32  static ObjectFactory& instance();
33  SmartPointer<T> getObject(std::string name);
34  void registerObject(std::string, CreateT );
35 
39  private:
40  std::map<std::string, CreateT > creators;
46 
52 
57  ObjectFactory& operator = (const ObjectFactory&){ return *this;}
58  };
59 
65  template<class T>
66  void ObjectFactory<T>::registerObject(std::string name, CreateT f) {
67  creators.insert(std::pair<std::string,CreateT>(name,f) );
68  }
69 
74  template<class T>
76  auto i = creators.find(name);
77  if ( i == creators.end() ) {
78  std::cout << name << " is an unknown name" << std::endl;
79  return (creators.begin()->second)();
80  }
81  return (i->second)();
82  }
83 
86  template<class T>
88  static ObjectFactory theFactory;
89  return theFactory;
90  }
91 
96  template <class Base,class Derived>
98  public:
99  ObjectFactoryHelper(std::string);
100  static SmartPointer<Base> create();
101  };
102 
107  template <class Base,class Derived>
109  Derived a;
110  return a;
111  }
112 
117  template <class Base,class Derived>
121  }
122 }
123 #endif
static SmartPointer< Base > create()
creator
Definition: objectFactory.hpp:108
File contains template of deep-coping smart pointer.
ObjectFactoryHelper(std::string)
constructor
Definition: objectFactory.hpp:118
factory helper
Definition: objectFactory.hpp:97
Definition: cadHoliday.cpp:3
ObjectFactory(const ObjectFactory &)
copy constructor
Definition: objectFactory.hpp:51
Template of deep-coping smart pointer.
Definition: smartPointer.hpp:14
SmartPointer< T > getObject(std::string name)
returns the a SmartPointer pointing new object of type dependent on string provided ...
Definition: objectFactory.hpp:75
void registerObject(std::string, CreateT)
registers an object
Definition: objectFactory.hpp:66
ObjectFactory()
constructor
Definition: objectFactory.hpp:45
SmartPointer< T >(* CreateT)()
auxiliary definition of smart pointer
Definition: objectFactory.hpp:30
ObjectFactory & operator=(const ObjectFactory &)
assignment constructor
Definition: objectFactory.hpp:57
Class implements a factory pattern.
Definition: objectFactory.hpp:26
static ObjectFactory & instance()
returns reference to instance of singleton factory
Definition: objectFactory.hpp:87
std::map< std::string, CreateT > creators
Map holding the creators of class and their string codes.
Definition: objectFactory.hpp:38
~ObjectFactory()
destructor
Definition: objectFactory.hpp:38