C++ mpi module for stochmagnet_main Package
CORE_Object.h
1 #ifndef CORE_Object_H
2 #define CORE_Object_H
3 
4 //shared pointer library
5 #include<memory>
6 
7 //include std::cout header
8 #include <iostream>
9 //include std::setprecision header
10 #include <iomanip>
11 
12 //algorithm header std::each etc...
13 #include <algorithm>
14 
15 //include the chrono header
16 #include <chrono>
17 //C time header
18 #include <ctime>
19 
20 //map header
21 #include <map>
22 //vector header
23 #include <vector>
24 
25 //include the types
26 #include "types.h"
27 //required functions of type
28 #include "functions_type.h"
29 
30 //constans
31 #include "CORE_Exception.h"
32 
33 //exception
34 #include "CORE_Constants.h"
35 
36 //profiler class header
37 #include "CORE_Profiler.h"
38 
39 //MEMORY STACK
40 #ifdef DEBUG
41 #define MEMORY_STACK_ENABLED
42 #else
43 #undef MEMORY_STACK_ENABLED
44 #endif
45 
46 
47 
48 
49 //enable the memory stack only in debug mode
50 #ifdef MEMORY_STACK_ENABLED
51 #include "CORE_MemoryStack.h"
52 #endif
53 
54 
55 class CORE_ClassFactory;
56 
57 
65 class CORE_Object {
66 
67  // ATTRIBUTES
68 
69 
70 public:
71 
72 
73 private:
74 
75 
76 
77  //friend class setThis is only called by the CORE_ClassFactory::NewSharedInstance() method
78  friend class CORE_ClassFactory;
79 
80  // ASSOCIATIONS
81 
82 
83 private:
84 
85 #ifdef MEMORY_STACK_ENABLED
86  static std::unique_ptr<CORE_MemoryStack<CORE_Object> > mMemoryStack;
87 #endif
88 
89 
90  //registed shared pointer of this
91  std::weak_ptr<CORE_Object> mThis;
92 
93  //size 24 bits : weak Pointer 3*8 wirtual methods
94 
95 protected:
96  // CONSTRUCTORS
99  CORE_Object();
100 
101  // DESTRUCTORS
104  virtual ~CORE_Object();
105 
106 public:
107 
108  // for using the protected destructor within the smart pointers
109  class Delete;
110  friend class Delete;
113  class Delete {
114  public:
117  void operator()(const CORE_Object* p){delete p;}
118  };
119 
120 public:
121 
122 
123  //operator methods
124  //================
130  friend std::ostream& operator << (std::ostream& out,const CORE_Object& obj) {
131  out << obj.toString();
132  return out;
133  };
134 
135 
136 
137 
138  //shared pointer methods
139  //=======================
140 
141 
142 
143 private:
150  template<class T>
151  std::shared_ptr<T> setThis(std::unique_ptr<T,CORE_Object::Delete>& up) {
152  std::shared_ptr<T> sp=std::move(up);
153  mThis=sp;
154  return sp;
155  }
156 
157 public:
161  template<class T>
162  std::shared_ptr<T> getSharedPointer() {
163  //return std::dynamic_pointer_cast<T>(shared_from_this());
164  return std::dynamic_pointer_cast<T>(mThis.lock());
165  }
166 
170  template<class T>
171  std::shared_ptr<const T> getConstSharedPointer() const {
172  //return std::dynamic_pointer_cast<const T>(shared_from_this());
173  return std::dynamic_pointer_cast<T>(mThis.lock());
174  }
175 
176 
177  //memory methods
178  //==============
183  static inline tBoolean EnableMemoryStack(const tBoolean& isMemoryChecked) {
184  if (isMemoryChecked) EnableMemoryStack();
185  else DisableMemoryStack();
186  return IsMemoryStackEnabled();
187  }
188 
191  static inline void EnableMemoryStack() {
192 #ifdef MEMORY_STACK_ENABLED
193  if (mMemoryStack.get()==null) {
194  mMemoryStack=std::make_unique<CORE_MemoryStack<CORE_Object> >();
195  }
196 #endif
197  }
200  static inline void DisableMemoryStack() {
201 #ifdef MEMORY_STACK_ENABLED
202  if (mMemoryStack.get()!=null) {
203  mMemoryStack.reset();
204  }
205 #endif
206  }
209  static inline tBoolean IsMemoryStackEnabled() {
210 #ifdef MEMORY_STACK_ENABLED
211  return (mMemoryStack.get()!=null);
212 #else
213  return false;
214 #endif
215  }
219  static inline tString MemoryStackToString() {
220 #ifdef MEMORY_STACK_ENABLED
221  if (mMemoryStack.get()!=null) {
222  return mMemoryStack->toString();
223  }
224  return "memory stack is disabled";
225 #else
226  return "memory stack is disabled";
227 #endif
228  }
229 
233  static inline tIndex GetRegisteredClassesNumber() {
234 #ifdef MEMORY_STACK_ENABLED
235  if (mMemoryStack.get()!=null) {
236  return mMemoryStack->getRegistredClassesNumber();
237  }
238  return 0;
239 #else
240  return 0;
241 #endif
242  }
243 
244 
245 #ifdef MEMORY_STACK_ENABLED
248  static inline const CORE_MemoryStack<CORE_Object>* GetRegisteredClasses() {
249  return mMemoryStack.get();
250 
251  }
252 #endif
253 
267  virtual tMemSize getMemorySize() const {
268  return sizeof(*this)+getContentsMemorySize();
269  }
278  virtual tMemSize getContentsMemorySize() const {
279  return 0;
280  }
281 
282 
283  //types methods
284  //=============
285 public:
289  template<class T>
290  inline tBoolean isInstanceOf() const {
291  return (dynamic_cast<const T*>(this)!=NULL);
292  }
293 
294 
298  inline tString getClassName() const {
299  return typeid(*this).name();
300  }
301 
302 
303 
304 
305 
306 
307 
308  //String representation of the class
309  //=================================
310 
314  inline tString getPointerString() const {
315  return functions_type::pointerToString(this);
316  }
317 
321  inline tString getIdentityString() const {
322  std::stringstream cstr;
323  cstr<<getClassName();
324  cstr<<"@";
325  cstr<<functions_type::pointerToString(this);
326  return cstr.str();
327  }
328 
329 
333  virtual tString toString() const {
334  return getIdentityString();
335  }
336 };
337 #endif
this class describes a class factory to generate classes
Definition: CORE_ClassFactory.h:24
CORE_MemoryStack is the class to register all the created classes to detect not destroyed classes.
Definition: CORE_MemoryStack.h:14
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:113
void operator()(const CORE_Object *p)
operator to delete the class
Definition: CORE_Object.h:117
abstract base class for most classes.
Definition: CORE_Object.h:65
virtual ~CORE_Object()
destroy the instance of object std
Definition: CORE_Object.cpp:19
virtual tMemSize getMemorySize() const
return the memory size of the class and the memory size of all its attributes/associations
Definition: CORE_Object.h:267
friend std::ostream & operator<<(std::ostream &out, const CORE_Object &obj)
print the class object
Definition: CORE_Object.h:130
tString getClassName() const
return the name of the class
Definition: CORE_Object.h:298
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:278
static void DisableMemoryStack()
disable the memory stack
Definition: CORE_Object.h:200
tString getPointerString() const
retrun the pointer of the class as a string
Definition: CORE_Object.h:314
static tBoolean IsMemoryStackEnabled()
return trur if the memory stack is enabled
Definition: CORE_Object.h:209
static tIndex GetRegisteredClassesNumber()
get the memory stack in string
Definition: CORE_Object.h:233
std::shared_ptr< const T > getConstSharedPointer() const
return a const shared pointer for this
Definition: CORE_Object.h:171
std::shared_ptr< T > getSharedPointer()
return the shared pointer for this
Definition: CORE_Object.h:162
static tString MemoryStackToString()
get the memory stack in string
Definition: CORE_Object.h:219
tBoolean isInstanceOf() const
test if the clas T is an instance of this class
Definition: CORE_Object.h:290
tString getIdentityString() const
retrun the string identification of the class
Definition: CORE_Object.h:321
static void EnableMemoryStack()
enable the memory stack
Definition: CORE_Object.h:191
virtual tString toString() const
return the string representation of the object node
Definition: CORE_Object.h:333
static tBoolean EnableMemoryStack(const tBoolean &isMemoryChecked)
enable the memory stack
Definition: CORE_Object.h:183
CORE_Object()
build an instance of the object
Definition: CORE_Object.cpp:9