Changeset 7401 for code/trunk/src/libraries/core/SmartPtr.h
- Timestamp:
- Sep 11, 2010, 12:34:00 AM (15 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/doc (added) merged: 7290-7292,7296-7300,7302-7304,7306-7312,7315-7318,7323,7325,7327,7331-7332,7334-7335,7345-7347,7352-7353,7356-7357,7361,7363-7367,7371-7375,7388
- Property svn:mergeinfo changed
-
code/trunk/src/libraries/core/SmartPtr.h
r7268 r7401 29 29 // Inspired by boost::intrusive_ptr by Peter Dimov 30 30 31 /** 32 @defgroup SmartPtr SmartPtr<T> and WeakPtr<T> 33 @ingroup Object 34 */ 35 36 /** 37 @file 38 @ingroup Object SmartPtr 39 @brief Definition of SmartPtr<T>, wraps a pointer to an object and keeps it alive. 40 41 @anchor SmartPtrExample 42 43 orxonox::SmartPtr is an implementation of a smart pointer - it wraps a pointer to an 44 object and keeps this object alive until no SmartPtr points to this object anymore. 45 In contrast to orxonox::SharedPtr, SmartPtr works only with classes that are derived 46 from orxonox::OrxonoxClass, because it's an intrusive implementation, meaning the 47 reference counter is stored in the object itself. 48 49 It's possible to use normal pointers and smart pointers to an object simultaneously. 50 You don't have to use SmartPtr all the time, you can create a SmartPtr for an object 51 at any time and also convert it back to a normal pointer if you like. This is possible 52 because the reference counter is stored in the object itself and not in SmartPtr (in 53 contrast to SharedPtr). 54 55 @b Important: If you want to delete an object, you must not use @c delete @c object but 56 rather @c object->destroy(). This function will check if there are smart pointers 57 pointing to the object. If yes, the object will be kept alive until all smart pointes 58 are destroyed. If no, the object is deleted instantly. 59 60 If all smart pointers that point to an object are destroyed, but you never called 61 @c object->destroy() before, the object will not be deleted! All a SmartPtr will do 62 is to really just keep an object alive, but it will not delete it automatically 63 unless you tried to destroy it before. 64 65 Example: 66 @code 67 class MyClass // class declaration 68 { 69 public: 70 void setObject(OtherClass* object) // passes a normal pointer which will be stored in a SmartPtr 71 { this->object_ = object; } 72 73 OtherClass* getObject() const // converts the SmartPtr to a normal pointer and returns it 74 { return this->object_; } 75 76 private: 77 SmartPtr<OtherClass> object_; // a pointer to an instance of OtherClass is stored in a SmartPtr 78 }; 79 @endcode 80 In this example we assume that OtherClass is a child of OrxonoxClass. We don't care 81 about the inheritance of MyClass though. 82 83 Now we create an instance of MyClass and assign a pointer to an instance of OtherClass: 84 @code 85 MyClass* myclass = new MyClass(); // create an instance of MyClass 86 OtherClass* object = new OtherClass(); // create an instance of OtherClass 87 myclass->setObject(object); // the object is now stored in a SmartPtr inside myclass 88 89 object->destroy(); // we try to destroy object, but there's still a SmartPtr pointing at it. 90 91 # object still exists at this point (because a SmartPtr points at it) 92 93 delete myclass; // now we delete myclass, which also destroys the SmartPtr 94 95 # object doesn't exist anymore (because the SmartPtr is now destroyed) 96 @endcode 97 98 Now we look at the same example, but we first delete myclass, then destroy object: 99 @code 100 MyClass* myclass = new MyClass(); // create an instance of MyClass 101 OtherClass* object = new OtherClass(); // create an instance of OtherClass 102 myclass->setObject(object); // the object is now stored in a SmartPtr inside myclass 103 104 delete myclass; // we delete myclass, which also destroys the SmartPtr 105 106 # object still exists at this point (because destroy() was not called yet) 107 108 object->destroy(); // now we try to destroy object, which works instantly 109 110 # object doesn't exist anymore (because we just destroyed it) 111 @endcode 112 113 Note that in any case @c object->destroy() has to be called to delete the object. 114 However if a SmartPtr points at it, the destruction is delayed until all SmartPtr 115 are destroyed. 116 */ 117 31 118 #ifndef _SmartPtr_H__ 32 119 #define _SmartPtr_H__ … … 42 129 namespace orxonox 43 130 { 131 /** 132 @brief A smart pointer which wraps a pointer to an object and keeps this object alive as long as the smart pointer exists. 133 134 @see See @ref SmartPtrExample "this description" for more information and an example. 135 */ 44 136 template <class T> 45 137 class SmartPtr 46 138 { 47 139 public: 140 /// Constructor: Initializes the smart pointer with a null pointer. 48 141 inline SmartPtr() : pointer_(0), base_(0) 49 142 { 50 143 } 51 144 145 /// Constructor: Used to explicitly initialize the smart pointer with a null pointer 52 146 inline SmartPtr(int) : pointer_(0), base_(0) 53 147 { 54 148 } 55 149 150 /// Constructor: Initializes the smart pointer with a pointer to an object. @param pointer The pointer @param bAddRef If true, the reference counter is increased. Don't set this to false unless you know exactly what you're doing! (for example to avoid circular references if the @c this pointer of the possessing object is stored) 56 151 inline SmartPtr(T* pointer, bool bAddRef = true) : pointer_(pointer), base_(pointer) 57 152 { … … 60 155 } 61 156 157 /// Copy-constructor 62 158 inline SmartPtr(const SmartPtr& other) : pointer_(other.pointer_), base_(other.base_) 63 159 { … … 66 162 } 67 163 164 /// Copy-constructor for smart pointers to objects of another class. 68 165 template <class O> 69 166 inline SmartPtr(const SmartPtr<O>& other) : pointer_(other.get()), base_(other.base_) … … 73 170 } 74 171 172 /// Constructor: Initializes the smart pointer with the pointer that is stored in a WeakPtr. 75 173 template <class O> 76 174 inline SmartPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.getBase()) … … 80 178 } 81 179 180 /// Destructor: Decrements the reference counter. 82 181 inline ~SmartPtr() 83 182 { … … 86 185 } 87 186 187 /// Used to assign a null pointer. 88 188 inline SmartPtr& operator=(int) 89 189 { … … 92 192 } 93 193 194 /// Assigns a new pointer. 94 195 inline SmartPtr& operator=(T* pointer) 95 196 { … … 98 199 } 99 200 201 /// Assigns the wrapped pointer of another SmartPtr. 100 202 inline SmartPtr& operator=(const SmartPtr& other) 101 203 { … … 104 206 } 105 207 208 /// Assigns the wrapped pointer of a SmartPtr of another class 106 209 template <class O> 107 210 inline SmartPtr& operator=(const SmartPtr<O>& other) … … 111 214 } 112 215 216 /// Assigns the wrapped pointer of a WeakPtr. 113 217 template <class O> 114 218 inline SmartPtr& operator=(const WeakPtr<O>& other) … … 118 222 } 119 223 224 /// Returns the wrapped pointer as @c T* 120 225 inline T* get() const 121 226 { … … 123 228 } 124 229 230 /// Returns the wrapped pointer as @c OrxonoxClass* 125 231 inline OrxonoxClass* getBase() const 126 232 { … … 128 234 } 129 235 236 /// Implicitly converts the SmartPtr to a pointer of type @c T* 130 237 inline operator T*() const 131 238 { … … 133 240 } 134 241 242 /// Overloaded operator, returns a pointer to the stored object. 135 243 inline T* operator->() const 136 244 { … … 139 247 } 140 248 249 /// Overloaded operator, returns a reference to the stored object. 141 250 inline T& operator*() const 142 251 { … … 145 254 } 146 255 256 /// Returns true if the wrapped pointer is NULL. 147 257 inline bool operator!() const 148 258 { … … 150 260 } 151 261 262 /// Swaps the contents of two smart pointers. 152 263 inline void swap(SmartPtr& other) 153 264 { … … 164 275 } 165 276 277 /// Resets the smart pointer (equivalent to assigning a NULL pointer). 166 278 inline void reset() 167 279 { … … 170 282 171 283 private: 172 T* pointer_; 173 OrxonoxClass* base_; 284 T* pointer_; ///< The wrapped pointer to an object of type @a T 285 OrxonoxClass* base_; ///< The wrapped pointer, casted up to OrxonoxClass (this is needed because with just a T* pointer, SmartPtr couln't be used with forward declarations) 174 286 }; 175 287 288 /// Swaps the contents of two smart pointers. 176 289 template <class T> 177 290 void swap(SmartPtr<T>& a, SmartPtr<T>& b) … … 180 293 } 181 294 295 /// Uses a static_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new SmartPtr<T>. 182 296 template <class T, class U> 183 297 SmartPtr<T> static_pointer_cast(const SmartPtr<U>& p) … … 186 300 } 187 301 302 /// Uses a const_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new SmartPtr<T>. 188 303 template <class T, class U> 189 304 SmartPtr<T> const_pointer_cast(const SmartPtr<U>& p) … … 192 307 } 193 308 309 /// Uses a dynamic_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new SmartPtr<T>. 194 310 template <class T, class U> 195 311 SmartPtr<T> dynamic_pointer_cast(const SmartPtr<U>& p)
Note: See TracChangeset
for help on using the changeset viewer.