Changeset 7373 for code/branches/doc/src/libraries/core/WeakPtr.h
- Timestamp:
- Sep 7, 2010, 11:24:47 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/doc/src/libraries/core/WeakPtr.h
r7363 r7373 32 32 @file 33 33 @ingroup Object SmartPtr 34 @brief Definition of WeakPtr<T>. 35 36 @anchor WeakPtrExample 37 38 A WeakPtr wraps a pointer to an object. If the object gets deleted, the WeakPtr becomes 39 NULL. This can be used to store pointers to objects without knowing when they will be 40 destroyed. 41 42 WeakPtr works only with objects that are derived from orxonox::OrxonoxClass, because 43 WeakPtr is intrusive and registers itself in the stored object, to get a notification if 44 the object is being deleted. 45 46 Example: 47 @code 48 MyClass* object = new MyClass(); // create an instance of MyClass 49 50 WeakPtr<MyClass> pointer = object; // create a WeakPtr and assign the object 51 52 if (pointer) // checks if pointer is not NULL (which is true) 53 pointer->someFunction(); // calls MyClass::someFunction() 54 55 object->destroy(); // calls destroy() which deletes the object 56 57 if (pointer) // checks if pointer is not NULL (which is now false) 58 pointer->someFunction(); // this will not be executed 59 @endcode 60 In this example we assumed that MyClass is derived of OrxonoxClass (otherwise it couldn't 61 be used with a WeakPtr). 62 63 A callback can be registerd with the WeakPtr that will be called if the object gets deleted. 64 @code 65 void myCallback() // definition of the callback function 66 { 67 COUT(0) << "Object destroyed" << std::endl; 68 } 69 70 MyClass* object = new MyClass(); // create an instance of MyClass 71 72 WeakPtr<MyClass> pointer = object; // create a WeakPtr and assign the object 73 74 pointer.setCallback(createFunctor(&myCallback)); // defines a callback 75 76 object->destroy(); // calls destroy() which deletes the object. prints "Object destroyed" to the console 77 @endcode 34 78 */ 35 79 … … 46 90 namespace orxonox 47 91 { 92 /** 93 @brief WeakPtr wraps a pointer to an object, which becomes NULL if the object is deleted. 94 95 @see See @ref WeakPtrExample "this description" for more information and an example. 96 */ 48 97 template <class T> 49 98 class WeakPtr … … 52 101 53 102 public: 103 /// Constructor: Initializes the weak pointer with a null pointer. 54 104 inline WeakPtr() : pointer_(0), base_(0), callback_(0) 55 105 { 56 106 } 57 107 108 /// Constructor: Used to explicitly initialize the weak pointer with a null pointer 58 109 inline WeakPtr(int) : pointer_(0), base_(0), callback_(0) 59 110 { 60 111 } 61 112 113 /// Constructor: Initializes the weak pointer with a pointer to an object. 62 114 inline WeakPtr(T* pointer) : pointer_(pointer), base_(pointer), callback_(0) 63 115 { … … 66 118 } 67 119 120 /// Copy-constructor 68 121 inline WeakPtr(const WeakPtr& other) : pointer_(other.pointer_), base_(other.base_), callback_(0) 69 122 { … … 72 125 } 73 126 127 /// Copy-constructor for weak pointers to objects of another class. 74 128 template <class O> 75 129 inline WeakPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.base_), callback_(0) … … 79 133 } 80 134 135 /// Destructor 81 136 inline ~WeakPtr() 82 137 { … … 86 141 } 87 142 143 /// Used to assign a null pointer. 88 144 inline WeakPtr& operator=(int) 89 145 { … … 92 148 } 93 149 150 /// Assigns a new pointer. 94 151 inline WeakPtr& operator=(T* pointer) 95 152 { … … 98 155 } 99 156 157 /// Assigns the wrapped pointer of another WeakPtr. 100 158 inline WeakPtr& operator=(const WeakPtr& other) 101 159 { … … 104 162 } 105 163 164 /// Assigns the wrapped pointer of a WeakPtr of another class 106 165 template <class O> 107 166 inline WeakPtr& operator=(const WeakPtr<O>& other) … … 111 170 } 112 171 172 /// Returns the wrapped pointer as @c T* 113 173 inline T* get() const 114 174 { … … 116 176 } 117 177 178 /// Returns the wrapped pointer as @c OrxonoxClass* 118 179 inline OrxonoxClass* getBase() const 119 180 { … … 121 182 } 122 183 184 /// Implicitly converts the WeakPtr to a pointer of type @c T* 123 185 inline operator T*() const 124 186 { … … 126 188 } 127 189 190 /// Overloaded operator, returns a pointer to the stored object. 128 191 inline T* operator->() const 129 192 { … … 132 195 } 133 196 197 /// Overloaded operator, returns a reference to the stored object. 134 198 inline T& operator*() const 135 199 { … … 138 202 } 139 203 204 /// Returns true if the wrapped pointer is NULL. 140 205 inline bool operator!() const 141 206 { … … 143 208 } 144 209 210 /// Swaps the contents of two weak pointers. 145 211 inline void swap(WeakPtr& other) 146 212 { … … 167 233 } 168 234 235 /// Resets the weak pointer (equivalent to assigning a NULL pointer). 169 236 inline void reset() 170 237 { … … 172 239 } 173 240 241 /// Registers a callback that will be executed if the stored object is destroyed. 174 242 inline void setCallback(const FunctorPtr& callback) 175 243 { … … 177 245 } 178 246 247 /// Returns the registered callback. 179 248 inline const FunctorPtr& getCallback() const 180 249 { … … 183 252 184 253 private: 254 /// Will be called by OrxonoxClass::~OrxonoxClass() if the stored object is deleted. Resets the wrapped pointer and executes the callback. 185 255 inline void objectDeleted() 186 256 { … … 191 261 } 192 262 193 T* pointer_; 194 OrxonoxClass* base_; 195 FunctorPtr callback_; 263 T* pointer_; ///< The wrapped pointer to an object of type @a T 264 OrxonoxClass* base_; ///< The wrapped pointer, casted up to OrxonoxClass (this is needed because with just a T* pointer, WeakPtr couln't be used with forward declarations) 265 FunctorPtr callback_; ///< This callback will be executed if the stored object is deleted 196 266 }; 197 267 268 /// Swaps the contents of two weak pointers. 198 269 template <class T> 199 270 void swap(WeakPtr<T>& a, WeakPtr<T>& b) … … 202 273 } 203 274 275 /// Uses a static_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new WeakPtr<T>. 204 276 template <class T, class U> 205 277 WeakPtr<T> static_pointer_cast(const WeakPtr<U>& p) … … 208 280 } 209 281 282 /// Uses a const_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new WeakPtr<T>. 210 283 template <class T, class U> 211 284 WeakPtr<T> const_pointer_cast(const WeakPtr<U>& p) … … 214 287 } 215 288 289 /// Uses a dynamic_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new WeakPtr<T>. 216 290 template <class T, class U> 217 291 WeakPtr<T> dynamic_pointer_cast(const WeakPtr<U>& p)
Note: See TracChangeset
for help on using the changeset viewer.