wxFreeChart
refobject.h
1 #ifndef REFOBJECT_H_
2 #define REFOBJECT_H_
3 
8 class WXDLLIMPEXP_FREECHART RefObject
9 {
10 public:
11  RefObject()
12  {
13  refCount = 0;
14  }
15 
16  virtual ~RefObject()
17  {
18  if (refCount > 0) {
19  wxLogError(wxT("RefObject::~RefObject: object still have references. refCount=%i"), refCount);
20  }
21  }
22 
23  void AddRef()
24  {
25  refCount++;
26  }
27 
28  void Unref()
29  {
30  refCount--;
31  if (refCount < 0) {
32  wxLogError(wxT("RefObject::Unref: refCount < 0"));
33  }
34  }
35 
36  int RefCount()
37  {
38  return refCount;
39  }
40 
41 private:
42  int refCount;
43 };
44 
45 #define SAFE_UNREF(ptr) do { \
46  if (ptr != NULL) { \
47  ptr->Unref(); \
48  if (ptr->RefCount() <= 0) { \
49  delete ptr; \
50  } \
51  } \
52  ptr = NULL; \
53 } while (0)
54 
55 #define SAFE_REPLACE_UNREF(dst, src) do { \
56  SAFE_UNREF(dst); \
57  if (src != NULL) \
58  ((RefObject *)src)->AddRef(); \
59  dst = src; \
60 } while (0)
61 
62 #define SAFE_UNREF_ELEMENTS(arrPtr, arrSize) do { \
63  if (arrPtr != NULL) { \
64  for (int n = 0; n < arrSize; n++) { \
65  SAFE_UNREF(arrPtr[n]); \
66  } \
67  wxDELETEA(arrPtr); \
68  } \
69 } while (0)
70 
71 #endif /*REFOBJECT_H_*/
Object with reference counter.
Definition: refobject.h:8