Nymph  v1.5.2
Flow-Based Data Processing Framework
KTExtensibleStructFactory.hh
Go to the documentation of this file.
1 /*
2  * KTExtensibleStructFactory.hh
3  *
4  * Created on: Sept 22, 2014
5  * Author: nsoblath
6  */
7 
8 #ifndef KTEXTENSIBLESTRUCTFACTORY_HH_
9 #define KTEXTENSIBLESTRUCTFACTORY_HH_
10 
11 #include "KTExtensibleStruct.hh"
12 #include "KTLogger.hh"
13 
14 #include "singleton.hh"
15 
16 #include <map>
17 #include <string>
18 
19 namespace Nymph
20 {
21  KTLOGGER(utillog_esfactory, "KTExtensibleStructFactory");
22 
23  template< class XBaseType >
25 
26  template< class XBaseType >
28  {
29  public:
32 
33  public:
34  friend class KTExtensibleStructFactory< XBaseType >;
35 
36  protected:
37  virtual KTExtensibleStructCore< XBaseType >* Create() const = 0;
39 
40  };
41 
42  template< class XBaseType, class XDerivedType >
44  {
45  public:
46  KTExtensibleStructRegistrar(const std::string& className);
47  virtual ~KTExtensibleStructRegistrar();
48 
49  protected:
50  void Register(const std::string& className) const;
51 
54 
55  };
56 
57 
58  template< class XBaseType >
59  class KTExtensibleStructFactory : public scarab::singleton< KTExtensibleStructFactory< XBaseType > >
60  {
61  public:
62  typedef std::map< std::string, const KTExtensibleStructRegistrarBase< XBaseType >* > FactoryMap;
63  typedef typename FactoryMap::value_type FactoryEntry;
64  typedef typename FactoryMap::iterator FactoryIt;
65  typedef typename FactoryMap::const_iterator FactoryCIt;
66 
67  public:
68  KTExtensibleStructCore< XBaseType >* Create(const std::string& className);
69  KTExtensibleStructCore< XBaseType >* Create(const FactoryCIt& iter);
70 
73 
74  void Register(const std::string& className, const KTExtensibleStructRegistrarBase< XBaseType >* registrar);
75 
76  FactoryCIt GetFactoryMapBegin() const;
77  FactoryCIt GetFactoryMapEnd() const;
78 
79  protected:
80  FactoryMap* fMap;
81 
82 
83  protected:
84  friend class scarab::singleton< KTExtensibleStructFactory >;
85  friend class scarab::destroyer< KTExtensibleStructFactory >;
88  };
89 
90  template< class XBaseType >
92  {
93  return iter->second->Create();
94  }
95 
96  template< class XBaseType >
98  {
99  FactoryCIt it = fMap->find(className);
100  if (it == fMap->end())
101  {
102  KTERROR(utillog_esfactory, "Did not find factory for <" << className << ">.");
103  return NULL;
104  }
105 
106  return it->second->Create();
107  }
108 
109  template< class XBaseType >
111  {
112  return iter->second->Create(object);
113  }
114 
115  template< class XBaseType >
117  {
118  FactoryCIt it = fMap->find(className);
119  if (it == fMap->end())
120  {
121  KTERROR(utillog_esfactory, "Did not find factory for <" << className << ">.");
122  return NULL;
123  }
124 
125  return it->second->Create(object);
126  }
127 
128  template< class XBaseType >
130  {
131  // A local (static) logger is created inside this function to avoid static initialization order problems
132  KTLOGGER(utillog_esfactory_reg, "KTExtensibleStructFactory-Register");
133 
134  FactoryCIt it = fMap->find(className);
135  if (it != fMap->end())
136  {
137  KTERROR(utillog_esfactory_reg, "Already have factory registered for <" << className << ">.");
138  return;
139  }
140  fMap->insert(std::pair< std::string, const KTExtensibleStructRegistrarBase< XBaseType >* >(className, registrar));
141  KTDEBUG(utillog_esfactory_reg, "Registered a factory for class " << className << ", factory #" << fMap->size()-1);
142  }
143 
144  template< class XBaseType >
146  fMap(new FactoryMap())
147  {}
148 
149  template< class XBaseType >
151  {
152  delete fMap;
153  }
154 
155  template< class XBaseType >
157  {
158  return fMap->begin();
159  }
160 
161  template< class XBaseType >
163  {
164  return fMap->end();
165  }
166 
167 
168 
169 
170  template< class XBaseType, class XDerivedType >
172  KTExtensibleStructRegistrarBase< XBaseType >()
173  {
174  Register(className);
175  }
176 
177  template< class XBaseType, class XDerivedType >
179  {}
180 
181  template< class XBaseType, class XDerivedType >
183  {
185  return;
186  }
187 
188  template< class XBaseType, class XDerivedType >
190  {
191  return dynamic_cast< KTExtensibleStructCore< XBaseType >* >(new XDerivedType());
192  }
193 
194  template< class XBaseType, class XDerivedType >
196  {
197  return dynamic_cast< KTExtensibleStructCore< XBaseType >* >(&object->template Of< XDerivedType >());
198  }
199 
200 } /* namespace Nymph */
201 #endif /* KTEXTENSIBLESTRUCTFACTORY_HH_ */
std::map< std::string, const KTExtensibleStructRegistrarBase< XBaseType > *> FactoryMap
KTExtensibleStructCore< XBaseType > * Create(const std::string &className)
void Register(const std::string &className, const KTExtensibleStructRegistrarBase< XBaseType > *registrar)
void Register(const std::string &className) const
KTExtensibleStructRegistrar(const std::string &className)
#define KTDEBUG(...)
Definition: KTLogger.hh:343
KTLOGGER(applog, "KTApplication")
KTExtensibleStructCore< XBaseType > * Create() const
::Nymph::KTLogger utillog_esfactory("KTExtensibleStructFactory")
#define KTERROR(...)
Definition: KTLogger.hh:347
Contains the logger class and macros, based on Kasper&#39;s KLogger class.
virtual KTExtensibleStructCore< XBaseType > * Create() const =0