Changeset 7401 for code/trunk/src/libraries/core/WeakPtr.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/WeakPtr.h
r7284 r7401 29 29 // Inspired by boost::intrusive_ptr by Peter Dimov 30 30 31 /** 32 @file 33 @ingroup Object SmartPtr 34 @brief Definition of WeakPtr<T>, wraps a pointer to an object. 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 78 */ 79 31 80 #ifndef _WeakPtr_H__ 32 81 #define _WeakPtr_H__ … … 41 90 namespace orxonox 42 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 */ 43 97 template <class T> 44 98 class WeakPtr … … 47 101 48 102 public: 103 /// Constructor: Initializes the weak pointer with a null pointer. 49 104 inline WeakPtr() : pointer_(0), base_(0), callback_(0) 50 105 { 51 106 } 52 107 108 /// Constructor: Used to explicitly initialize the weak pointer with a null pointer 53 109 inline WeakPtr(int) : pointer_(0), base_(0), callback_(0) 54 110 { 55 111 } 56 112 113 /// Constructor: Initializes the weak pointer with a pointer to an object. 57 114 inline WeakPtr(T* pointer) : pointer_(pointer), base_(pointer), callback_(0) 58 115 { … … 61 118 } 62 119 120 /// Copy-constructor 63 121 inline WeakPtr(const WeakPtr& other) : pointer_(other.pointer_), base_(other.base_), callback_(0) 64 122 { … … 67 125 } 68 126 127 /// Copy-constructor for weak pointers to objects of another class. 69 128 template <class O> 70 129 inline WeakPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.base_), callback_(0) … … 74 133 } 75 134 135 /// Destructor 76 136 inline ~WeakPtr() 77 137 { … … 81 141 } 82 142 143 /// Used to assign a null pointer. 83 144 inline WeakPtr& operator=(int) 84 145 { … … 87 148 } 88 149 150 /// Assigns a new pointer. 89 151 inline WeakPtr& operator=(T* pointer) 90 152 { … … 93 155 } 94 156 157 /// Assigns the wrapped pointer of another WeakPtr. 95 158 inline WeakPtr& operator=(const WeakPtr& other) 96 159 { … … 99 162 } 100 163 164 /// Assigns the wrapped pointer of a WeakPtr of another class 101 165 template <class O> 102 166 inline WeakPtr& operator=(const WeakPtr<O>& other) … … 106 170 } 107 171 172 /// Returns the wrapped pointer as @c T* 108 173 inline T* get() const 109 174 { … … 111 176 } 112 177 178 /// Returns the wrapped pointer as @c OrxonoxClass* 113 179 inline OrxonoxClass* getBase() const 114 180 { … … 116 182 } 117 183 184 /// Implicitly converts the WeakPtr to a pointer of type @c T* 118 185 inline operator T*() const 119 186 { … … 121 188 } 122 189 190 /// Overloaded operator, returns a pointer to the stored object. 123 191 inline T* operator->() const 124 192 { … … 127 195 } 128 196 197 /// Overloaded operator, returns a reference to the stored object. 129 198 inline T& operator*() const 130 199 { … … 133 202 } 134 203 204 /// Returns true if the wrapped pointer is NULL. 135 205 inline bool operator!() const 136 206 { … … 138 208 } 139 209 210 /// Swaps the contents of two weak pointers. 140 211 inline void swap(WeakPtr& other) 141 212 { … … 162 233 } 163 234 235 /// Resets the weak pointer (equivalent to assigning a NULL pointer). 164 236 inline void reset() 165 237 { … … 167 239 } 168 240 241 /// Registers a callback that will be executed if the stored object is destroyed. 169 242 inline void setCallback(const FunctorPtr& callback) 170 243 { … … 172 245 } 173 246 247 /// Returns the registered callback. 174 248 inline const FunctorPtr& getCallback() const 175 249 { … … 178 252 179 253 private: 254 /// Will be called by OrxonoxClass::~OrxonoxClass() if the stored object is deleted. Resets the wrapped pointer and executes the callback. 180 255 inline void objectDeleted() 181 256 { … … 186 261 } 187 262 188 T* pointer_; 189 OrxonoxClass* base_; 190 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 191 266 }; 192 267 268 /// Swaps the contents of two weak pointers. 193 269 template <class T> 194 270 void swap(WeakPtr<T>& a, WeakPtr<T>& b) … … 197 273 } 198 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>. 199 276 template <class T, class U> 200 277 WeakPtr<T> static_pointer_cast(const WeakPtr<U>& p) … … 203 280 } 204 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>. 205 283 template <class T, class U> 206 284 WeakPtr<T> const_pointer_cast(const WeakPtr<U>& p) … … 209 287 } 210 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>. 211 290 template <class T, class U> 212 291 WeakPtr<T> dynamic_pointer_cast(const WeakPtr<U>& p)
Note: See TracChangeset
for help on using the changeset viewer.