C++ main module for stochmagnet Package  1.0
CORE_ClassFactory.h
Go to the documentation of this file.
1 #ifndef CORE_ClassFactory_H
2 #define CORE_ClassFactory_H
3 
4 //vector header
5 #include <vector>
6 
7 //map header
8 #include <map>
9 
10 //include the base class of the package
11 #include "CORE_Object.h"
12 
13 //shared pointer functions
14 #include "sp.h"
15 
16 
22 class CORE_ClassFactory : public virtual CORE_Object { // class
23 
24 
25  // ASSOCIATION
26 
27  //list of sub classes
28 
29  // ATTRIBUTES
30 
31 private:
32 
33 
34  // ASSOCIATIONS
35 
36  //list of factory classes
37  std::vector<CORE_UniquePointer<CORE_ClassFactory> > mFactoryClasses;
38 
39  // METHODS
40 
41 
42 protected:
43  // CONSTRUCTORS
47 
48 
49 
50 
51  // DESTRUCTORS
55  virtual ~CORE_ClassFactory(void);
56 
57 
58 
59 
60 
61 public:
62 
70  virtual tMemSize getMemorySize() const override{
71  return sizeof(*this)+getContentsMemorySize();
72  }
73 
82  virtual tMemSize getContentsMemorySize() const override {
84  std::for_each(mFactoryClasses.cbegin(),mFactoryClasses.cend(),
85  [&](const auto& cf) {mem+=cf->getMemorySize();});
86  return mem;
87  }
88 public:
89 
90  // SET
91 
96  mFactoryClasses.push_back(std::move(child));
97 
98  }
99 
100 
103  inline void clearClassFactories() {
104  mFactoryClasses.clear();
105  }
106 
107 
108 public:
109 
110  //New class Factory
116  }
117 
118  // OTHERS
119 
120 
128  const std::map<tString,tString>& arguments) const;
129 
136  std::map<tString,tString> arguments;
137  return NewInstance(name,arguments);
138  }
139 
140 
147  template<class T>
149  const std::map<tString,tString>& arguments) const {
150  return sp::dynamic_upointer_cast<T>(NewInstance(name,arguments));
151  }
152 
158  template<class T>
159  inline CORE_UniquePointer<T> NewUniqueInstance(const tString& name) const {
160  std::map<tString,tString> nullArguments;
161  return sp::dynamic_upointer_cast<T>(NewInstance(name,nullArguments));
162  }
163 
164 
171  template<class T>
173  const std::map<tString,tString>& arguments) const {
174  CORE_UniquePointer<T> up=sp::dynamic_upointer_cast<T>(NewInstance(name,arguments));
175  return up->setThis(up);
176  }
182  template<class T>
183  inline CORE_SharedPointer<T> NewSharedInstance(const tString& name) const {
184  std::map<tString,tString> nullArguments;
185  CORE_UniquePointer<T> up=sp::dynamic_upointer_cast<T>(NewInstance(name,nullArguments));
186  return up->setThis(up);
187  }
192  template<class T>
194  std::map<tString,tString> nullArguments;
195  return NewSharedInstance<T>(nullArguments);
196  }
202  template<class T>
203  inline CORE_SharedPointer<T> NewSharedInstance(const std::map<tString,tString>& arguments) const {
204  std::map<tString,tString> nullArguments;
205  CORE_UniquePointer<T> up=sp::dynamic_upointer_cast<T>(NewInstance(core_functions::getTypeName<T>(),arguments));
206  return up->setThis(up);
207  }
208 
213  template<class T>
215  if (up.get()==null) {
216  CORE_SharedPointer<T> nullPtr;
217  return nullPtr;
218  }
219  return up->setThis(up);
220  }
225  template<class T>
227  return up->setThis(up);
228  }
229 
230 
234  virtual tString toString() const override {
235 
236  std::stringstream ret;
237  ret<<"class factories:"<<getIdentityString()<<"\n";
238  std::vector<CORE_UniquePointer<CORE_ClassFactory> >::const_iterator iter=mFactoryClasses.begin();
239  std::vector<CORE_UniquePointer<CORE_ClassFactory> >::const_iterator iterE=mFactoryClasses.end();
240  while (iter!=iterE) {
241  //try to create a new instance of the class with name
242  ret<<"\t"<<(*iter)->getIdentityString()<<"\n";
243  //next classes factory
244  iter++;
245  }
246 
247  return ret.str();
248  }
249 
250 
251 
252 
253 
254 
255 
256 };
257 
258 #endif
this class describes a class factory to generate classes
Definition: CORE_ClassFactory.h:22
CORE_SharedPointer< T > NewSharedInstance(const std::map< tString, tString > &arguments) const
create a shared instance of a class with same type as T
Definition: CORE_ClassFactory.h:203
std::vector< CORE_UniquePointer< CORE_ClassFactory > > mFactoryClasses
Definition: CORE_ClassFactory.h:37
CORE_ClassFactory()
build a CORE_ClassFactory
Definition: CORE_ClassFactory.cpp:9
virtual CORE_UniquePointer< CORE_Object > NewInstance(const tString &name, const std::map< tString, tString > &arguments) const
create an unique instance of a class as a general CORE_Object
Definition: CORE_ClassFactory.cpp:16
CORE_UniquePointer< T > NewUniqueInstance(const tString &name, const std::map< tString, tString > &arguments) const
create an unique instance of a class T
Definition: CORE_ClassFactory.h:148
CORE_SharedPointer< T > NewSharedInstance(const tString &name, const std::map< tString, tString > &arguments) const
create a shared instance of a class
Definition: CORE_ClassFactory.h:172
virtual ~CORE_ClassFactory(void)
destroy a CORE_ClassFactory
Definition: CORE_ClassFactory.cpp:13
static CORE_SharedPointer< T > NewSharedInstance(CORE_UniquePointer< T > &up)
create a shared instance of an unique pointer
Definition: CORE_ClassFactory.h:214
CORE_SharedPointer< T > NewSharedInstance() const
create a shared instance of a class with same type as T with no argument
Definition: CORE_ClassFactory.h:193
virtual tMemSize getContentsMemorySize() const override
return the memory size in byte
Definition: CORE_ClassFactory.h:82
CORE_UniquePointer< T > NewUniqueInstance(const tString &name) const
create an unique instance of a class T with no arguments
Definition: CORE_ClassFactory.h:159
static CORE_UniquePointer< CORE_ClassFactory > New()
build a new instance of default class factory
Definition: CORE_ClassFactory.h:114
CORE_UniquePointer< CORE_Object > NewInstance(const tString &name) const
create an unique instance of a class as a general CORE_Object
Definition: CORE_ClassFactory.h:135
static CORE_SharedPointer< T > NewSharedInstance(CORE_UniquePointer< T > &&up)
create a shared instance of an unique pointer as a lvalue
Definition: CORE_ClassFactory.h:226
virtual tMemSize getMemorySize() const override
return the memory size of the class and the memory size of all its attributes/associations
Definition: CORE_ClassFactory.h:70
void clearClassFactories()
clear the class factories list
Definition: CORE_ClassFactory.h:103
virtual tString toString() const override
get the string representation of the class
Definition: CORE_ClassFactory.h:234
CORE_SharedPointer< T > NewSharedInstance(const tString &name) const
create a shared instance of a class with no argument
Definition: CORE_ClassFactory.h:183
void addClassFactory(CORE_UniquePointer< CORE_ClassFactory > child)
add to class factory
Definition: CORE_ClassFactory.h:95
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:94
abstract base class for most classes.
Definition: CORE_Object.h:48
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:259
tString getIdentityString() const
retrun the string identification of the class
Definition: CORE_Object.h:302
typename std::unique_ptr< T, CORE_Object::Delete > CORE_UniquePointer
Definition: sp.h:8
typename std::shared_ptr< T > CORE_SharedPointer
Definition: sp.h:13
#define tString
Definition: types.h:147
#define tMemSize
Definition: types.h:166