C++ main module for stochmagnet Package  1.0
CORE_Object.h
Go to the documentation of this file.
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 //include the types
13 #include "types.h"
14 
15 //include core functions header
16 #include "functions.h"
17 
18 //include constants header
19 #include "constants.h"
20 
21 //exception
22 #include "CORE_Exception.h"
23 
24 //MEMORY STACK
25 #ifdef DEBUG
26 #define MEMORY_STACK_ENABLED
27 #else
28 #undef MEMORY_STACK_ENABLED
29 #endif
30 
31 
32 
33 //enable the memory stack only in debug mode
34 #ifdef MEMORY_STACK_ENABLED
35 #include "CORE_MemoryStack.h"
36 #endif
37 
38 
39 class CORE_ClassFactory;
40 
48 class CORE_Object {
49 
50  // ATTRIBUTES
51 
52 
53 public:
54 
55 
56 private:
57 
58  //friend class setThis is only called by the CORE_ClassFactory::NewSharedInstance() method
59  friend class CORE_ClassFactory;
60 
61  // ASSOCIATIONS
62 private:
63 
64 
65 
66 #ifdef MEMORY_STACK_ENABLED
67  static std::unique_ptr<CORE_MemoryStack<CORE_Object> > mMemoryStack;
68 #endif
69 
70 
71  //registed shared pointer of this
72  std::weak_ptr<CORE_Object> mThis;
73 
74  //size 24 bits : weak Pointer 3*8 wirtual methods
75 
76 protected:
77  // CONSTRUCTORS
80  CORE_Object();
81 
82  // DESTRUCTORS
85  virtual ~CORE_Object();
86 
87 public:
88 
89  // for using the protected destructor within the smart pointers
90  class Delete;
91  friend class Delete;
94  class Delete {
95  public:
98  void operator()(const CORE_Object* p){delete p;}
99  };
100 
101 public:
102 
103 
104  //operator methods
105  //================
111  friend std::ostream& operator << (std::ostream& out,const CORE_Object& obj) {
112  out << obj.toString();
113  return out;
114  };
115 
116 
117 
118 
119  //shared pointer methods
120  //=======================
121 
122 
123 
124 private:
131  template<class T>
132  std::shared_ptr<T> setThis(std::unique_ptr<T,CORE_Object::Delete>& up) {
133  std::shared_ptr<T> sp=std::move(up);
134  mThis=sp;
135  return sp;
136  }
137 
138 public:
142  template<class T>
143  std::shared_ptr<T> getSharedPointer() {
144  //return std::dynamic_pointer_cast<T>(shared_from_this());
145  return std::dynamic_pointer_cast<T>(mThis.lock());
146  }
147 
151  template<class T>
152  std::shared_ptr<const T> getConstSharedPointer() const {
153  //return std::dynamic_pointer_cast<const T>(shared_from_this());
154  return std::dynamic_pointer_cast<T>(mThis.lock());
155  }
156 
157 
158  //memory methods
159  //==============
164  static inline tBoolean EnableMemoryStack(const tBoolean& isMemoryChecked) {
165  if (isMemoryChecked) EnableMemoryStack();
166  else DisableMemoryStack();
167  return IsMemoryStackEnabled();
168  }
169 
172  static inline void EnableMemoryStack() {
173 #ifdef MEMORY_STACK_ENABLED
174  if (mMemoryStack.get()==null) {
175  mMemoryStack=std::make_unique<CORE_MemoryStack<CORE_Object> >();
176  }
177 #endif
178  }
181  static inline void DisableMemoryStack() {
182 #ifdef MEMORY_STACK_ENABLED
183  if (mMemoryStack.get()!=null) {
184  mMemoryStack.reset();
185  }
186 #endif
187  }
190  static inline tBoolean IsMemoryStackEnabled() {
191 #ifdef MEMORY_STACK_ENABLED
192  return (mMemoryStack.get()!=null);
193 #else
194  return false;
195 #endif
196  }
200  static inline tString MemoryStackToString() {
201 #ifdef MEMORY_STACK_ENABLED
202  if (mMemoryStack.get()!=null) {
203  return mMemoryStack->toString();
204  }
205  return "memory stack is disabled";
206 #else
207  return "memory stack is disabled";
208 #endif
209  }
210 
215 #ifdef MEMORY_STACK_ENABLED
216  if (mMemoryStack.get()!=null) {
217  return mMemoryStack->getRegistredClassesNumber();
218  }
219  return 0;
220 #else
221  return 0;
222 #endif
223  }
224 
225 
226 #ifdef MEMORY_STACK_ENABLED
229  static inline const CORE_MemoryStack<CORE_Object>* GetRegisteredClasses() {
230  return mMemoryStack.get();
231 
232  }
233 #endif
234 
248  virtual tMemSize getMemorySize() const {
249  return sizeof(*this)+getContentsMemorySize();
250  }
259  virtual tMemSize getContentsMemorySize() const {
260  return 0;
261  }
262 
263 
264  //types methods
265  //=============
266 public:
270  template<class T>
271  inline tBoolean isInstanceOf() const {
272  return (dynamic_cast<const T*>(this)!=NULL);
273  }
274 
275 
279  inline tString getClassName() const {
280  return typeid(*this).name();
281  }
282 
283 
284 
285 
286 
287 
288 
289  //String representation of the class
290  //=================================
291 
295  inline tString getPointerString() const {
296  return core_functions::pointerToString(this);
297  }
298 
302  inline tString getIdentityString() const {
303  std::stringstream cstr;
304  cstr<<getClassName();
305  cstr<<"@";
307  return cstr.str();
308  }
309 
310 
314  virtual tString toString() const {
315  return getIdentityString();
316  }
317 };
318 #endif
this class describes a class factory to generate classes
Definition: CORE_ClassFactory.h:22
CORE_MemoryStack is the class to register all the created classes to detect not destroyed classes.
Definition: CORE_MemoryStack.h:13
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:94
void operator()(const CORE_Object *p)
operator to delete the class
Definition: CORE_Object.h:98
abstract base class for most classes.
Definition: CORE_Object.h:48
virtual ~CORE_Object()
destroy the instance of object std
Definition: CORE_Object.cpp:18
std::shared_ptr< T > setThis(std::unique_ptr< T, CORE_Object::Delete > &up)
set the shared pointer from an unique pointer
Definition: CORE_Object.h:132
virtual tMemSize getMemorySize() const
return the memory size of the class and the memory size of all its attributes/associations
Definition: CORE_Object.h:248
std::weak_ptr< CORE_Object > mThis
Definition: CORE_Object.h:72
friend std::ostream & operator<<(std::ostream &out, const CORE_Object &obj)
print the class object
Definition: CORE_Object.h:111
tString getClassName() const
return the name of the class
Definition: CORE_Object.h:279
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:259
static void DisableMemoryStack()
disable the memory stack
Definition: CORE_Object.h:181
tString getPointerString() const
retrun the pointer of the class as a string
Definition: CORE_Object.h:295
static tBoolean IsMemoryStackEnabled()
return trur if the memory stack is enabled
Definition: CORE_Object.h:190
static tIndex GetRegisteredClassesNumber()
get the memory stack in string
Definition: CORE_Object.h:214
std::shared_ptr< const T > getConstSharedPointer() const
return a const shared pointer for this
Definition: CORE_Object.h:152
std::shared_ptr< T > getSharedPointer()
return the shared pointer for this
Definition: CORE_Object.h:143
static tString MemoryStackToString()
get the memory stack in string
Definition: CORE_Object.h:200
tBoolean isInstanceOf() const
test if the clas T is an instance of this class
Definition: CORE_Object.h:271
tString getIdentityString() const
retrun the string identification of the class
Definition: CORE_Object.h:302
static void EnableMemoryStack()
enable the memory stack
Definition: CORE_Object.h:172
virtual tString toString() const
return the string representation of the object node
Definition: CORE_Object.h:314
static tBoolean EnableMemoryStack(const tBoolean &isMemoryChecked)
enable the memory stack
Definition: CORE_Object.h:164
CORE_Object()
build an instance of the object
Definition: CORE_Object.cpp:8
tString pointerToString(const T *ptr)
return the pointer of the class as a string
Definition: functions.h:183
Definition: sp.h:21
#define tIndex
Definition: types.h:157
#define tString
Definition: types.h:147
#define tMemSize
Definition: types.h:166
#define tBoolean
Definition: types.h:151