VR-Forces 4.6.1 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
objectCounter.h
Go to the documentation of this file.
1 
2 
3 
4 
5 #pragma once
6 
8 
9 #include <tbb/atomic.h>
10 #include <string>
11 #include <assert.h>
12 #include <boost/function.hpp>
13 
14 #ifndef USE_VLD_MEMORY_TRACKING
15 #define DT_OBJECT_DEBUGGING_ENABLED
16 #endif
17 
18 //#define DT_OBJECT_TRACKING_ENABLED
19 
20 #ifdef DT_OBJECT_TRACKING_ENABLED
21 #include <tbb/spin_mutex.h>
22 #include <list>
23 #endif
24 
26 #ifdef _DEBUG
27 template <typename D, bool Enable = false>
28 class DtObjectDebugger;
29 #else
30 template <typename D, bool Enable = false>
31 class DtObjectDebugger;
32 #endif
33 
34 #ifndef DT_OBJECT_DEBUGGING_ENABLED
35 #define DT_DEFINE_OBJECT_DEBUGGER_NAME(type)
36 #define DT_DEFINE_OBJECT_DEBUGGER_NAME_TEMPLATE(type)
37 #define DT_DEFINE_OBJECT_DEBUGGER_NAME_TEMPLATE2(type)
38 
40 template <typename D, bool Enable>
41 class DtObjectDebugger
42 {
43 public:
44  DtObjectDebugger() {};
45 };
46 
47 #else
48 
49 class DtObjectContainer;
50 
53 {
54 public:
55  template <typename D, bool Enable>
56  friend class DtObjectDebugger;
57 
58  typedef unsigned long long CounterType;
59  typedef tbb::atomic<CounterType> AtomicType;
60 
61  typedef boost::function<std::string ()> NameFunction;
62 
64  static void Init();
65 
66  static std::string Print(std::string regex = "");
67  static std::string PrettyPrintSize(size_t);
68  static std::string DetectLeaks();
69 
71  explicit DtObjectCounter(
72  const NameFunction&,
73  size_t size = 1,
74  bool enable = true,
75  const DtObjectContainer* container = 0);
76 
78  explicit DtObjectCounter(
79  const std::string&,
80  size_t size = 1,
81  bool enable = true,
82  const DtObjectContainer* container = 0);
83 
85  ~DtObjectCounter();
86 
88  CounterType value() const { return myCount; }
89 
91  CounterType total() const { return myTotal; }
92 
94  size_t size() const { return mySize; }
95 
97  size_t totalSize() const { return value() * size(); }
98 
100  const DtObjectContainer* objects() const { return myObjects; }
101 
102 private:
104  CounterType increment() { ++myCount; return myTotal++; }
105 
107  void decrement() { --myCount; }
108 
110  AtomicType myCount, myTotal;
111  const size_t mySize;
112  const bool myEnableFlag;
113 
115  const DtObjectContainer* myObjects;
116 
120  void operator=(const DtObjectCounter&);
121 };
122 
123 #ifdef DT_OBJECT_TRACKING_ENABLED
124 
125 class DtObjectTracker;
126 
127 class DT_DLL_vrfutil DtObjectContainer
128 {
129 public:
130  typedef tbb::spin_mutex Mutex;
131  typedef std::list<const DtObjectTracker*> TrackableList;
132  typedef TrackableList::iterator Handle;
133 
134  typedef boost::function<void (const DtObjectTracker&)> IterateFunction;
135 
136  Handle add(const DtObjectTracker& obj);
137  void remove(Handle h);
138 
139  void iterate(const IterateFunction&) const;
140  void print(std::ostream&, unsigned width = 0) const;
141 
142 private:
143  mutable Mutex myMutex;
144  TrackableList myTrackables;
145 };
146 
148 {
149 public:
150  virtual std::string debugText() const;
151 
152  DtObjectCounter::CounterType objectIndex() const { return myObjectIndex; }
153 
154 protected:
155  DtObjectTracker() : myObjectIndex(0) { }
156 
157  virtual ~DtObjectTracker();
158 
159 private:
160  template <typename D, bool Enable>
161  friend class DtObjectDebugger;
162 
163  DtObjectContainer::Handle myHandle;
164  DtObjectCounter::CounterType myObjectIndex;
165 };
166 
167 #else
168 
169 class DtObjectTracker { };
170 
171 #endif
172 
173 template <typename D, bool Enable>
175 {
176 public:
178  {
179  return theStaticCount.value();
180  }
181 
182  static const DtObjectContainer* ObjectContainer()
183  {
184  return ObjectContainerMutable();
185  }
186 
187 protected:
188  explicit DtObjectDebugger()
189  {
190  if (Enable)
191  {
192 #ifdef DT_OBJECT_TRACKING_ENABLED
193  myObjectIndex = theStaticCount.increment();
194  myHandle = ObjectContainerMutable()->add(*this);
195 #else
197 #endif
198  }
199  }
200 
202  {
203  if (Enable)
204  {
205 #ifdef DT_OBJECT_TRACKING_ENABLED
206  myObjectIndex = theStaticCount.increment();
207  myHandle = ObjectContainerMutable()->add(*this);
208 #else
210 #endif
211  }
212  }
213 
215  {
216 #ifdef DT_OBJECT_TRACKING_ENABLED
217  if (Enable)
218  ObjectContainerMutable()->remove(myHandle);
219 #endif
220 
221  if (Enable)
223  }
224 
225 private:
227 
228  static DtObjectContainer* ObjectContainerMutable()
229  {
230 #ifdef DT_OBJECT_TRACKING_ENABLED
231  static DtObjectContainer theObjectContainer;
232  return &theObjectContainer;
233 #else
234  return 0;
235 #endif
236  }
237 };
238 
239 template <typename D>
241 {
242  static inline std::string value()
243  {
244  return "Unnamed";
245  }
246 };
247 
248 template <typename D, bool Enabled>
251  sizeof(D),
252  Enabled,
254 
255 #define DT_NAME_OBJECT_DEBUGGER(type, name) \
256  template <> \
257  struct DtObjectDebuggerLabel< type > \
258  { \
259  static inline std::string value() \
260  { \
261  return name ; \
262  } \
263  }; \
264 
265 #define DT_DEFINE_OBJECT_DEBUGGER_NAME(type) \
266  template <> \
267  struct DtObjectDebuggerLabel< type > \
268  { \
269  static inline std::string value() \
270  { \
271  return #type ; \
272  } \
273  }; \
274 
275 #define DT_DEFINE_OBJECT_DEBUGGER_NAME_TEMPLATE(type) \
276  template <typename T> \
277  struct DtObjectDebuggerLabel< type <T> > \
278  { \
279  static inline std::string value() \
280  { \
281  std::string name( #type "<" ); \
282  name += DtObjectDebuggerLabel<T>::value(); \
283  name += ">"; \
284  return name; \
285  } \
286  }; \
287 
288 #define DT_DEFINE_OBJECT_DEBUGGER_NAME_TEMPLATE2(type) \
289  template <typename T1, typename T2> \
290  struct DtObjectDebuggerLabel< type <T1,T2> > \
291  { \
292  static inline std::string value() \
293  { \
294  std::string name( #type "<" ); \
295  name += DtObjectDebuggerLabel<T1>::value(); \
296  name += ","; \
297  name += DtObjectDebuggerLabel<T2>::value(); \
298  name += ">"; \
299  return name; \
300  } \
301  }; \
302 
303 #endif

Document ID: Generated on Wed Jul 25 16:57:45 EDT 2018 from SVN revision 190790
Copyright © 2005-2018 VT MÄK. All Rights Reserved (www.mak.com)