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