EventAnalysis  1.3.0
Factory.h
Go to the documentation of this file.
1 /*
2  * Factory.h
3  *
4  * Created on: 07 May 2014
5  * Author: Nicola Mori
6  */
7 
10 #ifndef FACTORY_H_
11 #define FACTORY_H_
12 
13 #include "utils/Demangler.h"
14 #include "utils/SmartLog.h"
15 
16 #include <cxxabi.h>
17 #include <map>
18 #include <typeinfo>
19 #include <vector>
20 #include <memory>
21 
22 namespace EA {
23 
33 template <class T, typename... ConstructorArgs> class Factory {
34 
35 public:
40  static Factory &GetInstance();
41 
48  bool RegisterBuilder(const std::string &builderName, T *(*objectBuilder)(ConstructorArgs...));
49 
58  std::unique_ptr<T> CreateObject(const std::string &name, ConstructorArgs... arguments);
59 
64  const std::vector<std::string> &GetListOfRegisteredBuilders();
65 
66 private:
67  Factory();
68  std::string _factoryName;
69  typedef std::map<std::string, T *(*)(ConstructorArgs...)> BuildersMap;
70  BuildersMap _builders;
71  std::vector<std::string> _builderNames;
72 };
73 
74 template <class T, typename... ConstructorArgs>
76  static Factory<T, ConstructorArgs...> instance;
77  return instance;
78 }
79 
80 // Private default constructor
81 template <class T, typename... ConstructorArgs>
82 Factory<T, ConstructorArgs...>::Factory() : _factoryName(std::string("Factory<").append(Demangler::ClassName<T>())) {
83  const std::string routineName(_factoryName + "::Factory");
84  COUT(DEEPDEB) << routineName << ENDL;
85 }
86 
87 template <class T, typename... ConstructorArgs>
88 bool Factory<T, ConstructorArgs...>::RegisterBuilder(const std::string &builderName,
89  T *(*objectBuilder)(ConstructorArgs...)) {
90  const std::string &routineName(_factoryName + "::RegisterBuilder");
91 
92  std::pair<typename BuildersMap::iterator, bool> insertResult =
93  _builders.insert(std::pair<std::string, T *(*)(ConstructorArgs...)>(builderName, objectBuilder));
94  if (insertResult.second) {
95  _builderNames.push_back(builderName);
96  return true;
97  } else {
98  COUT(ERROR) << "Impossible to register class builder " << builderName
99  << ": a class builder with the same name is already registered." << ENDL;
100  return false;
101  }
102 }
103 
104 template <class T, typename... ConstructorArgs>
106  return _builderNames;
107 }
108 
109 template <class T, typename... ConstructorArgs>
110 std::unique_ptr<T> Factory<T, ConstructorArgs...>::CreateObject(const std::string &builderName,
111  ConstructorArgs... arguments) {
112  const std::string &routineName(_factoryName + "::CreateObject");
113 
114  // 1. Search for a suitable builder
115  typename BuildersMap::iterator builderIter = _builders.find(builderName);
116  if (builderIter == _builders.end()) {
117  COUT(ERROR) << "No available class builder with name " << builderName << "." << ENDL;
118  return std::unique_ptr<T>((T *)(NULL));
119  }
120 
121  // 2. Build the object and return it
122  return std::unique_ptr<T>((*(builderIter->second))(arguments...));
123 }
124 
125 } // namespace EA
126 
127 #endif /* FACTORY_H_ */
bool RegisterBuilder(const std::string &builderName, T *(*objectBuilder)(ConstructorArgs...))
Register a builder for a class.
Definition: Factory.h:88
std::map< std::string, T *(*)(ConstructorArgs...)> BuildersMap
Definition: Factory.h:69
const std::vector< std::string > & GetListOfRegisteredBuilders()
Gets a vector containing the names of available registered builders.
Definition: Factory.h:105
#define COUT(level)
Definition: SmartLog.h:88
std::vector< std::string > _builderNames
Definition: Factory.h:71
std::string ClassName(const T &obj)
Function which returns the name of the class.
Definition: Demangler.h:30
IncludeFileExc.h IncludeFileExc class declaration.
Definition: Algorithm.h:21
std::unique_ptr< T > CreateObject(const std::string &name, ConstructorArgs... arguments)
Create an object.
Definition: Factory.h:110
static Factory & GetInstance()
Getter method for singleton pointer.
Definition: Factory.h:75
#define ENDL
Definition: SmartLog.h:117
Template factory class.
Definition: Factory.h:33
std::string _factoryName
Definition: Factory.h:68
BuildersMap _builders
Definition: Factory.h:70
Factory()
Definition: Factory.h:82
Aliased object is an alias.