| 1 | // This may look like C code, but it is really -*- C++ -*- | 
|---|
| 2 |  | 
|---|
| 3 | #ifndef RCPTR_H | 
|---|
| 4 | #define RCPTR_H | 
|---|
| 5 |  | 
|---|
| 6 | // This implements a very simple "smart pointer" with reference counting. | 
|---|
| 7 | // Many refinements are possible | 
|---|
| 8 |  | 
|---|
| 9 | // Principle :  use RCPtr<T> instead of T* | 
|---|
| 10 | // To use with type X, define | 
|---|
| 11 | //  typedef RCPtr<X> Xp; | 
|---|
| 12 | //  When creating a new object, use | 
|---|
| 13 | //    Xp xp = new X(...); | 
|---|
| 14 | // xp can be used like a X*   xp->field, xp->method(), *xp ... | 
|---|
| 15 | //    can be passed by reference, copied, etc | 
|---|
| 16 | // the object is destroyed when the last Xp is destroyed. | 
|---|
| 17 |  | 
|---|
| 18 | // The class RCCPtr<X> is a const smart pointer, to be used like | 
|---|
| 19 | // a X const *. | 
|---|
| 20 |  | 
|---|
| 21 | // Eric Aubourg  1999   CEA/DAPNIA/SPP | 
|---|
| 22 |  | 
|---|
| 23 |  | 
|---|
| 24 | template <class T> class RCCPtr; | 
|---|
| 25 |  | 
|---|
| 26 | template <class T> | 
|---|
| 27 | class RCPtr { | 
|---|
| 28 | public: | 
|---|
| 29 | RCPtr(T* obj=0) { | 
|---|
| 30 | x = obj; | 
|---|
| 31 | cnt = new int; | 
|---|
| 32 | *cnt = 1; | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 | RCPtr(RCPtr<T> const& other) { | 
|---|
| 36 | x = other.x; | 
|---|
| 37 | cnt = other.cnt; | 
|---|
| 38 | (*cnt)++; | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | RCPtr<T>& operator = (RCPtr<T> const& other) { | 
|---|
| 42 | (*cnt)--; | 
|---|
| 43 | if (!*cnt) { | 
|---|
| 44 | delete x; | 
|---|
| 45 | delete cnt; | 
|---|
| 46 | } | 
|---|
| 47 | x = other.x; | 
|---|
| 48 | cnt = other.cnt; | 
|---|
| 49 | (*cnt)++; | 
|---|
| 50 | return *this; | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | ~RCPtr() { | 
|---|
| 54 | (*cnt)--; | 
|---|
| 55 | if (!*cnt) { | 
|---|
| 56 | delete x; | 
|---|
| 57 | delete cnt; | 
|---|
| 58 | } | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | T* operator->() {return x;} | 
|---|
| 62 | T& operator*()  {return *x;} | 
|---|
| 63 |  | 
|---|
| 64 | private: | 
|---|
| 65 | T* x;                  // the object we are referring | 
|---|
| 66 | int* cnt;              // how many smart pointers ? | 
|---|
| 67 | friend class RCCPtr<T>; | 
|---|
| 68 | }; | 
|---|
| 69 |  | 
|---|
| 70 |  | 
|---|
| 71 | // A const smart pointer | 
|---|
| 72 | template <class T> | 
|---|
| 73 | class RCCPtr { | 
|---|
| 74 | public: | 
|---|
| 75 | RCCPtr(T const* obj=0) { | 
|---|
| 76 | x = obj; | 
|---|
| 77 | cnt = new int; | 
|---|
| 78 | *cnt = 1; | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | RCCPtr(RCCPtr<T> const& other) { | 
|---|
| 82 | x = other.x; | 
|---|
| 83 | cnt = other.cnt; | 
|---|
| 84 | (*cnt)++; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | RCCPtr(RCPtr<T> const& other) { | 
|---|
| 88 | x = other.x; | 
|---|
| 89 | cnt = other.cnt; | 
|---|
| 90 | (*cnt)++; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | RCCPtr<T>& operator = (RCCPtr<T> const& other) { | 
|---|
| 94 | (*cnt)--; | 
|---|
| 95 | if (!*cnt) { | 
|---|
| 96 | delete (T*) x; | 
|---|
| 97 | delete cnt; | 
|---|
| 98 | } | 
|---|
| 99 | x = other.x; | 
|---|
| 100 | cnt = other.cnt; | 
|---|
| 101 | (*cnt)++; | 
|---|
| 102 | return *this; | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | ~RCCPtr() { | 
|---|
| 106 | (*cnt)--; | 
|---|
| 107 | if (!*cnt) { | 
|---|
| 108 | delete (T*) x; | 
|---|
| 109 | delete cnt; | 
|---|
| 110 | } | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | T const* operator->() {return x;} | 
|---|
| 114 | T const& operator*()  {return *x;} | 
|---|
| 115 |  | 
|---|
| 116 | private: | 
|---|
| 117 | T const * x;           // the object we are referring | 
|---|
| 118 | int* cnt;              // how many smart pointers ? | 
|---|
| 119 | }; | 
|---|
| 120 |  | 
|---|
| 121 | #endif | 
|---|