00001 #ifndef REFOBJECT_H_
00002 #define REFOBJECT_H_
00003
00007 class WXDLLIMPEXP_FREECHART RefObject
00008 {
00009 public:
00010 RefObject()
00011 {
00012 refCount = 0;
00013 }
00014
00015 virtual ~RefObject()
00016 {
00017 if (refCount > 0) {
00018 wxLogError(wxT("RefObject::~RefObject: object still have references. refCount=%i"), refCount);
00019 }
00020 }
00021
00022 void AddRef()
00023 {
00024 refCount++;
00025 }
00026
00027 void Unref()
00028 {
00029 refCount--;
00030 if (refCount < 0) {
00031 wxLogError(wxT("RefObject::Unref: refCount < 0"));
00032 }
00033 }
00034
00035 int RefCount()
00036 {
00037 return refCount;
00038 }
00039
00040 private:
00041 int refCount;
00042 };
00043
00044 #define SAFE_UNREF(ptr) do { \
00045 if (ptr != NULL) { \
00046 ptr->Unref(); \
00047 if (ptr->RefCount() <= 0) { \
00048 delete ptr; \
00049 } \
00050 } \
00051 ptr = NULL; \
00052 } while (0)
00053
00054 #define SAFE_REPLACE_UNREF(dst, src) do { \
00055 SAFE_UNREF(dst); \
00056 if (src != NULL) \
00057 ((RefObject *)src)->AddRef(); \
00058 dst = src; \
00059 } while (0)
00060
00061 #define SAFE_UNREF_ELEMENTS(arrPtr, arrSize) do { \
00062 if (arrPtr != NULL) { \
00063 for (int n = 0; n < arrSize; n++) { \
00064 SAFE_UNREF(arrPtr[n]); \
00065 } \
00066 wxDELETEA(arrPtr); \
00067 } \
00068 } while (0)
00069
00070 #endif