17178457a6bea914767d13a0cd332ff3589d5d35
[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 #include <lib/base/elock.h>
13
14 typedef int RESULT;
15
16 class iObject
17 {
18 private:
19                 /* we don't allow the default operator here, as it would break the refcount. */
20         void operator=(const iObject &);
21 protected:
22         virtual ~iObject() { }
23 public:
24         virtual void AddRef()=0;
25         virtual void Release()=0;
26 };
27
28 class oRefCount
29 {
30         int ref;
31 public:
32         oRefCount(): ref(0) { }
33         operator int&() { return ref; }
34         ~oRefCount() { 
35 #ifdef OBJECT_DEBUG
36                 if (ref) eDebug("OBJECT_DEBUG FATAL: %p has %d references!", this, ref); else eDebug("OBJECT_DEBUG refcount ok! (%p)", this); 
37 #endif
38         }
39 };
40
41 #ifndef SWIG
42 #define DECLARE_REF(x) private: eSingleLock ref_lock; oRefCount ref; public: void AddRef(); void Release();
43 #ifdef OBJECT_DEBUG
44 extern int object_total_remaining;
45 #define DEFINE_REF(c) void c::AddRef() { eSingleLocker l(ref_lock); ++object_total_remaining; ++ref; eDebug("OBJECT_DEBUG " #c "+%p now %d", this, (int)ref); } void c::Release() { { eSingleLocker l(ref_lock); --object_total_remaining; --ref; eDebug("OBJECT_DEBUG " #c "-%p now %d", this, ref); } if (!ref) delete this; }
46 #error fix locking for debug
47 #else
48 #define DEFINE_REF(c) void c::AddRef() { eSingleLocker l(ref_lock); ++ref; } void c::Release() { { eSingleLocker l(ref_lock); --ref; } if (!ref) delete this; }
49 #endif
50 #else
51 #define DECLARE_REF(x) private: void AddRef(); void Release();
52 #endif
53
54 #ifdef SWIG
55 class Object
56 {
57 };
58 #endif
59
60 #endif