[364] | 1 | /* |
---|
[1018] | 2 | * ====================================================================== |
---|
[364] | 3 | * Rappture::Ptr<type> |
---|
| 4 | * |
---|
| 5 | * AUTHOR: Michael McLennan, Purdue University |
---|
[3177] | 6 | * Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
[1018] | 7 | * ---------------------------------------------------------------------- |
---|
[364] | 8 | * See the file "license.terms" for information on usage and |
---|
| 9 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
| 10 | * ====================================================================== |
---|
| 11 | */ |
---|
[1018] | 12 | #ifndef RAPPTURE_PTR_H |
---|
| 13 | #define RAPPTURE_PTR_H |
---|
[364] | 14 | |
---|
| 15 | #include <assert.h> |
---|
[1031] | 16 | #include <stddef.h> |
---|
[364] | 17 | |
---|
| 18 | namespace Rappture { |
---|
| 19 | |
---|
[1018] | 20 | /** |
---|
| 21 | * This is the core of a smart pointer, built to keep a reference |
---|
| 22 | * count and do most of the work, so the template class can be lean |
---|
| 23 | * and mean. |
---|
| 24 | */ |
---|
[364] | 25 | class PtrCore { |
---|
| 26 | public: |
---|
[1018] | 27 | explicit PtrCore(void* ptr=NULL); |
---|
[364] | 28 | ~PtrCore(); |
---|
| 29 | |
---|
[1018] | 30 | void* pointer() const; |
---|
| 31 | void attach(); |
---|
| 32 | void* detach(); |
---|
| 33 | |
---|
| 34 | private: |
---|
[364] | 35 | // copying the core is not allowed |
---|
| 36 | PtrCore(const PtrCore& pc) { assert(0); } |
---|
[1018] | 37 | PtrCore& operator=(const PtrCore& pc) { assert(0); return *this; } |
---|
[364] | 38 | |
---|
[1018] | 39 | void *_ptr; |
---|
[364] | 40 | int _refcount; |
---|
| 41 | }; |
---|
| 42 | |
---|
[1018] | 43 | /** |
---|
| 44 | * This is a smart pointer with reference counting. Once one of |
---|
| 45 | * these pointers is constructed with an object of the underlying |
---|
| 46 | * type, that object becomes property of the pointer. Other |
---|
| 47 | * pointers can point to the same object. When all such pointers |
---|
| 48 | * have been destroyed, the underlying object goes away. |
---|
| 49 | */ |
---|
[364] | 50 | template <class Type> |
---|
| 51 | class Ptr { |
---|
| 52 | public: |
---|
[370] | 53 | explicit Ptr(Type* ptr=NULL); |
---|
[364] | 54 | Ptr(const Ptr& ptr); |
---|
| 55 | Ptr& operator=(Type* ptr); |
---|
| 56 | Ptr& operator=(const Ptr& ptr); |
---|
| 57 | ~Ptr(); |
---|
| 58 | |
---|
[370] | 59 | int isNull() const; |
---|
[364] | 60 | Type* operator->() const; |
---|
| 61 | Type* pointer() const; |
---|
[370] | 62 | Type& operator*() const; |
---|
[364] | 63 | void clear(); |
---|
| 64 | |
---|
| 65 | private: |
---|
[1018] | 66 | PtrCore *_pc; |
---|
[364] | 67 | }; |
---|
| 68 | |
---|
| 69 | template <class Type> |
---|
| 70 | Ptr<Type>::Ptr(Type* ptr) |
---|
| 71 | : _pc(NULL) |
---|
| 72 | { |
---|
| 73 | if (ptr) { |
---|
[1018] | 74 | _pc = new PtrCore(ptr); |
---|
[364] | 75 | } |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | template <class Type> |
---|
| 79 | Ptr<Type>::Ptr(const Ptr<Type>& ptr) |
---|
| 80 | : _pc(NULL) |
---|
| 81 | { |
---|
| 82 | if (ptr._pc) { |
---|
| 83 | ptr._pc->attach(); |
---|
| 84 | _pc = ptr._pc; |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | template <class Type> |
---|
| 89 | Ptr<Type>::~Ptr() |
---|
| 90 | { |
---|
[1018] | 91 | clear(); |
---|
[364] | 92 | } |
---|
| 93 | |
---|
| 94 | template <class Type> |
---|
| 95 | Ptr<Type>& |
---|
| 96 | Ptr<Type>::operator=(Type* ptr) |
---|
| 97 | { |
---|
[1018] | 98 | clear(); |
---|
| 99 | _pc = new PtrCore(ptr); |
---|
| 100 | return *this; |
---|
[364] | 101 | } |
---|
| 102 | |
---|
| 103 | template <class Type> |
---|
| 104 | Ptr<Type>& |
---|
| 105 | Ptr<Type>::operator=(const Ptr<Type>& ptr) |
---|
| 106 | { |
---|
| 107 | if (ptr._pc) { |
---|
| 108 | ptr._pc->attach(); |
---|
| 109 | } |
---|
[1018] | 110 | clear(); |
---|
[364] | 111 | _pc = ptr._pc; |
---|
[596] | 112 | return *this; |
---|
[364] | 113 | } |
---|
| 114 | |
---|
| 115 | template <class Type> |
---|
| 116 | int |
---|
[370] | 117 | Ptr<Type>::isNull() const |
---|
[364] | 118 | { |
---|
| 119 | if (_pc) { |
---|
| 120 | return (_pc->pointer() == NULL); |
---|
| 121 | } |
---|
| 122 | return 1; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | template <class Type> |
---|
| 126 | Type* |
---|
| 127 | Ptr<Type>::operator->() const |
---|
| 128 | { |
---|
| 129 | if (_pc) { |
---|
[1018] | 130 | return static_cast<Type*>(_pc->pointer()); |
---|
[364] | 131 | } |
---|
| 132 | return NULL; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | template <class Type> |
---|
| 136 | Type* |
---|
| 137 | Ptr<Type>::pointer() const |
---|
| 138 | { |
---|
| 139 | if (_pc) { |
---|
[1018] | 140 | return (Type*)_pc->pointer(); |
---|
[364] | 141 | } |
---|
| 142 | return NULL; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | template <class Type> |
---|
[370] | 146 | Type& |
---|
| 147 | Ptr<Type>::operator*() const |
---|
| 148 | { |
---|
| 149 | assert(_pc != NULL); |
---|
| 150 | return *(Type*)_pc->pointer(); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | template <class Type> |
---|
[364] | 154 | void |
---|
| 155 | Ptr<Type>::clear() |
---|
| 156 | { |
---|
| 157 | if (_pc) { |
---|
[1018] | 158 | Type* ptr = (Type*)_pc->detach(); |
---|
| 159 | if (ptr) { |
---|
| 160 | // If we get back a pointer, then it is fully detached. |
---|
| 161 | // Clean it up. |
---|
| 162 | delete ptr; |
---|
| 163 | delete _pc; |
---|
| 164 | } |
---|
[364] | 165 | _pc = NULL; |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | } // namespace Rappture |
---|
| 170 | |
---|
[1018] | 171 | #endif // RAPPTURE_PTR_H |
---|