C++ main module for stochmagnet Package  1.0
CORE_MemoryStack.h
Go to the documentation of this file.
1 #ifndef CORE_MemoryStack_H
2 #define CORE_MemoryStack_H
3 
4 //include map
5 #include<map>
6 
7 
12 template<class T>
13 class CORE_MemoryStack { // class MemoryStack
14 
15  // ATTRIBUTES
16 
17 
18 
19 
20 private:
21 
22 
23 
24  // ASSOCIATIONS
25 private:
26 
27  //list of objects
28  std::map<std::string,const T*> mObjects;
29 
30 
31 
32 public:
33  // CONSTRUCTORS
37  }
38 
39  // DESTRUCTORS
42  virtual ~CORE_MemoryStack(){
43  }
44 
45 
46 
47 
48 
49 public:
50 
51  //operator methods
52  //================
53 
62  inline tMemSize getMemorySize() const {
63  return sizeof(*this)+getContentsMemorySize();
64  }
74  return mObjects.size()*(sizeof(tString)+sizeof(T*));
75  }
76 public:
80  inline void registerClass(const T* obj) {
81  ASSERT_IN(obj!=null);
83  }
84 
88  inline void unregisterClass(const T* obj) {
89  ASSERT_IN(obj!=null);
90  typename std::map<std::string,const T*>::iterator iter=mObjects.find(core_functions::pointerToString(obj));
91  if (iter!=mObjects.end()) {
92  mObjects.erase(iter);
93  }
94  }
95 
96 
97  //get methods
98  //===========
99 
104  return mObjects.size();
105  }
106 
107  //string representation
108  //====================
112  inline std::string toString() const {
113  std::stringstream cstr;
114 
115  cstr<<"registered objects:"<<std::endl;
116 
117  //number of registered objects
118  tInteger n=0;
119  std::for_each(mObjects.cbegin(),mObjects.cend(),
120  [&](const auto& obj) {
121  cstr<<"\t"<<obj.first<<"->"<<obj.second->getIdentityString();
122  cstr<<std::endl;
123  n++;
124  });
125  cstr<<"number of registered objects :"<<n<<std::endl;
126 
127 
128  return cstr.str();
129  }
130 };
131 
132 #endif
CORE_MemoryStack is the class to register all the created classes to detect not destroyed classes.
Definition: CORE_MemoryStack.h:13
void unregisterClass(const T *obj)
unregister the object
Definition: CORE_MemoryStack.h:88
tMemSize getContentsMemorySize() const
return the memory size of the included associations
Definition: CORE_MemoryStack.h:73
void registerClass(const T *obj)
register the object
Definition: CORE_MemoryStack.h:80
std::string toString() const
print all the registred classes
Definition: CORE_MemoryStack.h:112
tMemSize getMemorySize() const
return the memory size in byte
Definition: CORE_MemoryStack.h:62
CORE_MemoryStack()
build an instance of the object
Definition: CORE_MemoryStack.h:36
tIndex getRegistredClassesNumber() const
retunr the number of registred elements
Definition: CORE_MemoryStack.h:103
std::map< std::string, const T * > mObjects
Definition: CORE_MemoryStack.h:28
virtual ~CORE_MemoryStack()
destroy the instance of object
Definition: CORE_MemoryStack.h:42
#define ASSERT_IN(a)
Definition: functions.h:601
tString pointerToString(const T *ptr)
return the pointer of the class as a string
Definition: functions.h:183
#define tIndex
Definition: types.h:157
#define tString
Definition: types.h:147
#define tMemSize
Definition: types.h:166
#define tInteger
Definition: types.h:114