Path Integral Quantum Monte Carlo
factory.h
Go to the documentation of this file.
1 
9 #ifndef FACTORY_H
10 #define FACTORY_H
11 
24 template <typename CtorSignature> class Factory;
25 
26 template<class BaseType, class ...ParamType>
27 class Factory<BaseType (ParamType...)>
28 {
29  protected:
30  /* The constructor signature */
31  using CreateObjectFunc = BaseType (*)(ParamType...);
32 
33  public:
34 
36  vector<string> getNames() const {
37  std::vector<string> names;
38  for(auto const& createIter: _create)
39  names.push_back(createIter.first);
40  return names;
41  }
42 
44  Factory<BaseType (ParamType...)> *Instance()
45  {
46  static Factory<BaseType (ParamType...)> fact;
47  return &fact;
48 
49  }
50 
52  const Factory<BaseType (ParamType...)> * operator() () const { return Instance();}
53  Factory<BaseType (ParamType...)> * operator() () { return Instance();}
54 
56  BaseType Create(string name, ParamType ...param) {
57  typename map<string,CreateObjectFunc>::const_iterator objItr = _create.find(name);
58  if (objItr != _create.end()) {
59  return (objItr->second)(param...);
60  /* auto created = (objItr->second)(param...); */
61  /* created->name1 = name; */
62  /* return created; */
63  }
64  return nullptr;
65  }
66 
68  template<class DerivedType>
69  bool Register(string name)
70  {
71  _create[name] = &createObj<DerivedType>;
72  return true;
73  }
74 
75  protected:
76  map<string,CreateObjectFunc> _create; // The name->constructor map
77 
78  /* Create the new object */
79  template <class DerivedType>
80  static BaseType createObj(ParamType ...param)
81  { return new DerivedType(param...);}
82 
83 };
84 
85 class EstimatorBase;
86 class MoveBase;
87 class ActionBase;
88 class Path;
89 class MTRand;
90 
91 /* Typedefs used for actually creating factories */
92 typedef Factory<EstimatorBase* (Path &, ActionBase *, MTRand &, double)> EstimatorFactory;
93 typedef Factory<EstimatorBase* (Path &, Path &, ActionBase *, ActionBase *, MTRand &, double)> MultiEstimatorFactory;
94 typedef Factory<MoveBase* (Path &, ActionBase *, MTRand &)> MoveFactory;
95 
96 /* template<typename BaseType, class DerivedType, class ...ParamType> */
97 /* BaseType CreateObject(ParamType ...param) */
98 /* { */
99 /* return new DerivedType(param...); */
100 /* } */
101 
102 #endif
Holds a base class that all action classes will be derived from.
Definition: action.h:29
The base class that all estimator classes will be derived from.
Definition: estimator.h:28
Factory< BaseType(ParamType...)> * Instance()
Singleton access.
Definition: factory.h:44
vector< string > getNames() const
The names of all objects instatiated by the factory.
Definition: factory.h:36
BaseType Create(string name, ParamType ...param)
Return an instantiated object with a given name.
Definition: factory.h:56
bool Register(string name)
Register the derived type with a descriptive name in the map.
Definition: factory.h:69
An abstract factory class which creates new object instances based on a string descripter and constru...
Definition: factory.h:24
The base class that all moves will be derived from.
Definition: move.h:30
The space-time trajectories.
Definition: path.h:29