C++ mpi module for stochmagnet_main Package
CORE_ClassFactory.h
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 "shared_pointer.h"
15 
16 //list of options
17 #include "CORE_OptionsList.h"
18 
24 class CORE_ClassFactory : public virtual CORE_Object { // class
25 
26 
27  // ASSOCIATION
28 
29  //list of sub classes
30 
31  // ATTRIBUTES
32 
33 private:
34 
35 
36  // ASSOCIATIONS
37 
38  //list of factory classes
39  std::vector<CORE_UniquePointer<CORE_ClassFactory> > mFactoryClasses;
40 
41  // METHODS
42 
43 
44 protected:
45  // CONSTRUCTORS
49 
50 
51 
52 
53  // DESTRUCTORS
57  virtual ~CORE_ClassFactory(void);
58 
59 
60 
61 
62 
63 public:
64 
72  virtual tMemSize getMemorySize() const override{
73  return sizeof(*this)+getContentsMemorySize();
74  }
75 
84  virtual tMemSize getContentsMemorySize() const override {
86  std::for_each(mFactoryClasses.cbegin(),mFactoryClasses.cend(),
87  [&](const auto& cf) {mem+=cf->getMemorySize();});
88  return mem;
89  }
90 public:
91 
92  // SET
93 
97  inline void addClassFactory(CORE_UniquePointer<CORE_ClassFactory> child) {
98  mFactoryClasses.push_back(std::move(child));
99 
100  }
101 
102 
105  inline void clearClassFactories() {
106  mFactoryClasses.clear();
107  }
108 
109 
110 public:
111 
112  //New class Factory
116  inline static CORE_UniquePointer<CORE_ClassFactory> New() {
117  return CORE_UniquePointer<CORE_ClassFactory>(new CORE_ClassFactory(),CORE_ClassFactory::Delete());
118  }
119 
120  // OTHERS
121 
122 
129  virtual CORE_UniquePointer<CORE_Object> NewInstance(const tString& name,
130  const CORE_OptionsList& arguments) const;
131 
137  inline CORE_UniquePointer<CORE_Object> NewInstance(const tString& name) const {
138  CORE_OptionsList arguments;
139  return NewInstance(name,arguments);
140  }
141 
142 
149  template<class T>
150  inline CORE_UniquePointer<T> NewInstance(const tString& name,
151  const CORE_OptionsList& arguments) const {
152  return shared_pointer::dynamic_up_cast<T>(NewInstance(name,arguments));
153  }
154 
160  template<class T>
161  inline CORE_UniquePointer<T> NewInstance(const tString& name) const {
162  CORE_OptionsList nullArguments;
163  return NewInstance<T>(name,nullArguments);
164  }
165 
166 
173  template<class T>
174  inline CORE_SharedPointer<T> NewSharedPointer(const tString& name,
175  const CORE_OptionsList& arguments) const {
176  CORE_UniquePointer<T> up=NewInstance<T>(name,arguments);
177  if (up.get()!=null) return up->setThis(up);
178  else {
179  CORE_UniquePointer<T> nullPtr;
180  return nullPtr;
181  }
182  }
188  template<class T>
189  inline CORE_SharedPointer<T> NewSharedPointer(const tString& name) const {
190  CORE_OptionsList nullArguments;
191  return NewSharedPointer<T>(name,nullArguments);
192  }
197  template<class T>
198  inline CORE_SharedPointer<T> NewSharedPointer() const {
199  CORE_OptionsList nullArguments;
200  return NewSharedPointer<T>(nullArguments);
201  }
207  template<class T>
208  inline CORE_SharedPointer<T> NewSharedPointer(const CORE_OptionsList& arguments) const {
209  return NewSharedPointer<T>(functions_type::getTypeName<T>(),arguments);
210  }
211 
216  template<class T>
217  static inline CORE_SharedPointer<T> NewSharedPointer(CORE_UniquePointer<T>& up) {
218  if (up.get()==null) {
219  CORE_SharedPointer<T> nullPtr;
220  return nullPtr;
221  }
222  return up->setThis(up);
223  }
228  template<class T>
229  static inline CORE_SharedPointer<T> NewSharedPointer(CORE_UniquePointer<T>&& up) {
230  if (up.get()!=null) return up->setThis(up);
231  CORE_SharedPointer<T> nullPtr;
232  return nullPtr;
233  }
234 
235 
239  virtual tString toString() const override {
240 
241  std::stringstream ret;
242  ret<<"class factories:"<<getIdentityString()<<"\n";
243  std::vector<CORE_UniquePointer<CORE_ClassFactory> >::const_iterator iter=mFactoryClasses.begin();
244  std::vector<CORE_UniquePointer<CORE_ClassFactory> >::const_iterator iterE=mFactoryClasses.end();
245  while (iter!=iterE) {
246  //try to create a new instance of the class with name
247  ret<<"\t"<<(*iter)->getIdentityString()<<"\n";
248  //next classes factory
249  iter++;
250  }
251 
252  return ret.str();
253  }
254 
255 
256 
257 
258 
259 
260 
261 };
262 
263 //UniquePointer = Instance definition
264 #define NewUniquePointer NewInstance
265 #define NewUniqueInstance NewInstance
266 #define NewSharedInstance NewSharedPointer
267 #endif
this class describes a class factory to generate classes
Definition: CORE_ClassFactory.h:24
virtual CORE_UniquePointer< CORE_Object > NewInstance(const tString &name, const CORE_OptionsList &arguments) const
create an unique instance of a class as a general CORE_Object
Definition: CORE_ClassFactory.cpp:18
CORE_ClassFactory()
build a CORE_ClassFactory
Definition: CORE_ClassFactory.cpp:11
CORE_UniquePointer< T > NewInstance(const tString &name, const CORE_OptionsList &arguments) const
create an unique instance of a class T
Definition: CORE_ClassFactory.h:150
static CORE_SharedPointer< T > NewSharedPointer(CORE_UniquePointer< T > &up)
create a shared instance of an unique pointer
Definition: CORE_ClassFactory.h:217
CORE_SharedPointer< T > NewSharedPointer(const tString &name) const
create a shared instance of a class with no argument
Definition: CORE_ClassFactory.h:189
virtual ~CORE_ClassFactory(void)
destroy a CORE_ClassFactory
Definition: CORE_ClassFactory.cpp:15
virtual tMemSize getContentsMemorySize() const override
return the memory size in byte
Definition: CORE_ClassFactory.h:84
CORE_SharedPointer< T > NewSharedPointer(const CORE_OptionsList &arguments) const
create a shared instance of a class with same type as T
Definition: CORE_ClassFactory.h:208
static CORE_UniquePointer< CORE_ClassFactory > New()
build a new instance of default class factory
Definition: CORE_ClassFactory.h:116
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:137
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:72
static CORE_SharedPointer< T > NewSharedPointer(CORE_UniquePointer< T > &&up)
create a shared instance of an unique pointer as a lvalue
Definition: CORE_ClassFactory.h:229
CORE_UniquePointer< T > NewInstance(const tString &name) const
create an unique instance of a class T with no arguments
Definition: CORE_ClassFactory.h:161
void clearClassFactories()
clear the class factories list
Definition: CORE_ClassFactory.h:105
virtual tString toString() const override
get the string representation of the class
Definition: CORE_ClassFactory.h:239
CORE_SharedPointer< T > NewSharedPointer(const tString &name, const CORE_OptionsList &arguments) const
create a shared instance of a class
Definition: CORE_ClassFactory.h:174
CORE_SharedPointer< T > NewSharedPointer() const
create a shared instance of a class with same type as T with no argument
Definition: CORE_ClassFactory.h:198
void addClassFactory(CORE_UniquePointer< CORE_ClassFactory > child)
add to class factory
Definition: CORE_ClassFactory.h:97
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:113
abstract base class for most classes.
Definition: CORE_Object.h:65
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:278
tString getIdentityString() const
retrun the string identification of the class
Definition: CORE_Object.h:321
This class is an list of options.
Definition: CORE_OptionsList.h:36