| 1 | // This may look like C code, but it is really -*- C++ -*-
|
|---|
| 2 |
|
|---|
| 3 | #ifndef RCPTR_H
|
|---|
| 4 | #define RCPTR_H
|
|---|
| 5 |
|
|---|
| 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
|
|---|
| 12 |
|
|---|
| 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.
|
|---|
| 22 |
|
|---|
| 23 | The class RCCPtr<X> is a const smart pointer, to be used like
|
|---|
| 24 | a X const *.
|
|---|
| 25 | \endverbatim
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | template <class T> class RCCPtr;
|
|---|
| 29 |
|
|---|
| 30 | template <class T>
|
|---|
| 31 | class RCPtr {
|
|---|
| 32 | public:
|
|---|
| 33 | RCPtr(T* obj=0) {
|
|---|
| 34 | x = obj;
|
|---|
| 35 | cnt = new int;
|
|---|
| 36 | *cnt = 1;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | RCPtr(RCPtr<T> const& other) {
|
|---|
| 40 | x = other.x;
|
|---|
| 41 | cnt = other.cnt;
|
|---|
| 42 | (*cnt)++;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | RCPtr<T>& operator = (RCPtr<T> const& other) {
|
|---|
| 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 | }
|
|---|
| 56 |
|
|---|
| 57 | ~RCPtr() {
|
|---|
| 58 | (*cnt)--;
|
|---|
| 59 | if (!*cnt) {
|
|---|
| 60 | delete x;
|
|---|
| 61 | delete cnt;
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | T* operator->() {return x;}
|
|---|
| 66 | T& operator*() {return *x;}
|
|---|
| 67 |
|
|---|
| 68 | private:
|
|---|
| 69 | T* x; // the object we are referring
|
|---|
| 70 | int* cnt; // how many smart pointers ?
|
|---|
| 71 | friend class RCCPtr<T>;
|
|---|
| 72 | };
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 | //! A const smart pointer class
|
|---|
| 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 |
|
|---|
| 125 | #endif
|
|---|