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