/home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/util/include/RAT/Factory.hh Source File

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/util/include/RAT/Factory.hh Source File
Ratpac-two
Factory.hh
1 #ifndef __RAT_Factory__
2 #define __RAT_Factory__
3 
4 #include <map>
5 #include <string>
6 
7 namespace RAT {
8 
9 template <class T>
10 class AllocBase {
11  public:
12  virtual T *New() = 0;
13 };
14 
15 template <class T, class TDerived>
16 class Alloc : public AllocBase<T> {
17  public:
18  virtual T *New() { return new TDerived; };
19 };
20 
22  public:
23  FactoryUnknownID(const std::string &_id) { id = _id; };
24  std::string id;
25 };
26 
27 template <class T>
28 class AllocTable : public std::map<std::string, AllocBase<T> *> {};
29 
30 template <class T>
31 class Factory {
32  public:
33  T *New(const std::string &id) {
34  if (table.count(id) == 0)
35  throw FactoryUnknownID(id);
36  else
37  return table[id]->New();
38  };
39 
40  void Register(const std::string &id, AllocBase<T> *allocator) { table[id] = allocator; };
41 
42  protected:
43  AllocTable<T> table;
44 };
45 
46 template <class T>
48  public:
49  static T *New(const std::string &id) { return factory.New(id); };
50  static void Register(const std::string &id, AllocBase<T> *allocator) { factory.Register(id, allocator); };
51 
52  protected:
53  static Factory<T> factory;
54 };
55 
56 template <class T>
58 
59 } // namespace RAT
60 
61 #endif
Definition: Factory.hh:10
Definition: Factory.hh:28
Definition: Factory.hh:16
Definition: Factory.hh:21
Definition: Factory.hh:31
Definition: Factory.hh:47
Definition: CCCrossSecMessenger.hh:29