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

Document ID: Generated on Thu Oct 23 22:29:17 EDT 2025 from SVN revision 280951
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)