add some comments
[enigma2.git] / lib / base / object.h
1 #ifndef __base_object_h
2 #define __base_object_h
3
4 #include <assert.h>
5
6 // #define OBJECT_DEBUG
7
8 #include <lib/base/smartptr.h>
9 #ifdef OBJECT_DEBUG
10 #include <lib/base/eerror.h>
11 #endif
12
13 typedef int RESULT;
14
15 class iObject
16 {
17 private:
18                 /* we don't allow the default operator here, as it would break the refcount. */
19         void operator=(const iObject &);
20 protected:
21         virtual ~iObject() { }
22 public:
23         virtual void AddRef()=0;
24         virtual void Release()=0;
25 };
26
27 class oRefCount
28 {
29         int ref;
30 public:
31         oRefCount(): ref(0) { }
32         operator int&() { return ref; }
33         ~oRefCount() { 
34 #ifdef OBJECT_DEBUG
35                 if (ref) eDebug("OBJECT_DEBUG FATAL: %p has %d references!", this, ref); else eDebug("OBJECT_DEBUG refcount ok! (%p)", this); 
36 #endif
37         }
38 };
39
40 #ifndef SWIG
41 #define DECLARE_REF(x) private: oRefCount ref; public: void AddRef(); void Release();
42 #ifdef OBJECT_DEBUG
43 extern int object_total_remaining;
44 #define DEFINE_REF(c) void c::AddRef() { ++object_total_remaining; ++ref; eDebug("OBJECT_DEBUG " #c "+%p now %d", this, (int)ref); } void c::Release() { --object_total_remaining; eDebug("OBJECT_DEBUG " #c "-%p now %d", this, ref-1); if (!--ref) delete this; }
45 #else
46 #define DEFINE_REF(c) void c::AddRef() { ++ref; } void c::Release() { if (!--ref) delete this; }
47 #endif
48 #else
49 #define DECLARE_REF(x) private: void AddRef(); void Release();
50 #endif
51
52 #ifdef SWIG
53 class Object
54 {
55 };
56 #endif
57
58 #endif