Changeset 10555 for code/branches/core7/src/libraries/core/object
- Timestamp:
- Aug 29, 2015, 5:35:59 PM (9 years ago)
- Location:
- code/branches/core7/src/libraries/core/object
- Files:
-
- 4 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core7/src/libraries/core/object/DestroyLaterManager.cc
r10464 r10555 51 51 void DestroyLaterManager::postUpdate(const Clock& time) 52 52 { 53 // clearing the list will destroy all s martpointers and thus all retained instances (as long as there are no other smartpointers pointing to them).53 // clearing the list will destroy all strong pointers and thus all retained instances (as long as there are no other strong pointers pointing to them). 54 54 this->retainedInstances_.clear(); 55 55 } -
code/branches/core7/src/libraries/core/object/DestroyLaterManager.h
r10419 r10555 34 34 #include "util/Singleton.h" 35 35 #include "core/UpdateListener.h" 36 #include "S martPtr.h"36 #include "StrongPtr.h" 37 37 38 38 namespace orxonox … … 52 52 53 53 private: 54 std::vector<S martPtr<Destroyable> > retainedInstances_;54 std::vector<StrongPtr<Destroyable> > retainedInstances_; 55 55 56 56 static DestroyLaterManager* singletonPtr_s; -
code/branches/core7/src/libraries/core/object/Destroyable.cc
r10419 r10555 61 61 62 62 /** 63 @brief Deletes the object if no @ref orxonox::S martPtr "smartpointers" point to this object. Otherwise schedules the object to be deleted as soon as possible.64 Always call destroy() instead of using 'delete' directly, otherwise s martpointers won't work.63 @brief Deletes the object if no @ref orxonox::StrongPtr "strong pointers" point to this object. Otherwise schedules the object to be deleted as soon as possible. 64 Always call destroy() instead of using 'delete' directly, otherwise strong pointers won't work. 65 65 */ 66 66 void Destroyable::destroy() … … 81 81 void Destroyable::destroyLater() 82 82 { 83 // register in DestroyLaterManager - this ensures that a s martPtr points to this object and keeps it alive for a while83 // register in DestroyLaterManager - this ensures that a strongPtr points to this object and keeps it alive for a while 84 84 DestroyLaterManager::getInstance().retain(this); 85 85 86 // request destruction -> object will be deleted after all s martPtrs (including the one in DestroyLaterManager) were destroyed.86 // request destruction -> object will be deleted after all strongPtrs (including the one in DestroyLaterManager) were destroyed. 87 87 this->destroy(); 88 88 } -
code/branches/core7/src/libraries/core/object/Destroyable.h
r10419 r10555 30 30 @file 31 31 @ingroup Object 32 @brief Declaration of Destroyable, the base class of all objects which can be used with S martPtr and WeakPtr.32 @brief Declaration of Destroyable, the base class of all objects which can be used with StrongPtr and WeakPtr. 33 33 */ 34 34 … … 43 43 { 44 44 /** 45 @brief Classes must inherit from this class if they should be used with S martPtr or WeakPtr.45 @brief Classes must inherit from this class if they should be used with StrongPtr or WeakPtr. 46 46 */ 47 47 class _CoreExport Destroyable 48 48 { 49 49 template <class T> 50 friend class S martPtr;50 friend class StrongPtr; 51 51 52 52 friend class DestructionListener; … … 59 59 void destroyLater(); 60 60 61 /// Returns the number of @ref orxonox::S martPtr "smartpointers" that point to this object.61 /// Returns the number of @ref orxonox::StrongPtr "strong pointers" that point to this object. 62 62 inline unsigned int getReferenceCount() const 63 63 { return this->referenceCount_; } 64 64 65 65 protected: 66 /// This virtual function is called if destroy() is called and no S martPtr points to this object. Used in some cases to create a new SmartPtr to66 /// This virtual function is called if destroy() is called and no StrongPtr points to this object. Used in some cases to create a new StrongPtr to 67 67 /// prevent destruction. Don't call this function directly - use destroy() instead. 68 68 virtual void preDestroy() {} 69 69 70 70 private: 71 /// Increments the reference counter (for s martpointers).71 /// Increments the reference counter (for strong pointers). 72 72 inline void incrementReferenceCount() 73 73 { ++this->referenceCount_; } 74 /// Decrements the reference counter (for s martpointers).74 /// Decrements the reference counter (for strong pointers). 75 75 inline void decrementReferenceCount() 76 76 { … … 87 87 { this->destructionListeners_.erase(pointer); } 88 88 89 int referenceCount_; //!< Counts the references from s martpointers to this object89 int referenceCount_; //!< Counts the references from strong pointers to this object 90 90 bool requestedDestruction_; //!< Becomes true after someone called delete on this object 91 91 std::set<DestructionListener*> destructionListeners_; //!< All destruction listeners (for example weak pointers which point to this object and like to get notified if it dies) -
code/branches/core7/src/libraries/core/object/StrongPtr.h
r10553 r10555 30 30 31 31 /** 32 @defgroup SmartPtr S martPtr<T> and WeakPtr<T>32 @defgroup SmartPtr StrongPtr<T> and WeakPtr<T> 33 33 @ingroup Object 34 34 */ … … 37 37 @file 38 38 @ingroup Object SmartPtr 39 @brief Definition of S martPtr<T>, wraps a pointer to an object and keeps it alive.40 41 @anchor S martPtrExample42 43 orxonox::S martPtr is an implementation of a smart pointer - it wraps a pointer to an44 object and keeps this object alive until no S martPtr points to this object anymore.45 In contrast to orxonox::SharedPtr, S martPtr works only with classes that are derived39 @brief Definition of StrongPtr<T>, wraps a pointer to an object and keeps it alive. 40 41 @anchor StrongPtrExample 42 43 orxonox::StrongPtr is an implementation of a smart pointer - it wraps a pointer to an 44 object and keeps this object alive until no StrongPtr points to this object anymore. 45 In contrast to orxonox::SharedPtr, StrongPtr works only with classes that are derived 46 46 from orxonox::Destroyable, because it's an intrusive implementation, meaning the 47 47 reference counter is stored in the object itself. 48 48 49 It's possible to use normal pointers and s martpointers to an object simultaneously.50 You don't have to use S martPtr all the time, you can create a SmartPtr for an object49 It's possible to use normal pointers and strong pointers to an object simultaneously. 50 You don't have to use StrongPtr all the time, you can create a StrongPtr for an object 51 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 S martPtr (in52 because the reference counter is stored in the object itself and not in StrongPtr (in 53 53 contrast to SharedPtr). 54 54 55 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 s martpointers57 pointing to the object. If yes, the object will be kept alive until all s mart pointes56 rather @c object->destroy(). This function will check if there are strong pointers 57 pointing to the object. If yes, the object will be kept alive until all strong pointers 58 58 are destroyed. If no, the object is deleted instantly. 59 59 60 If all s martpointers that point to an object are destroyed, but you never called61 @c object->destroy() before, the object will not be deleted! All a S martPtr will do60 If all strong 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 StrongPtr will do 62 62 is to really just keep an object alive, but it will not delete it automatically 63 63 unless you tried to destroy it before. … … 68 68 { 69 69 public: 70 void setObject(OtherClass* object) // passes a normal pointer which will be stored in a S martPtr70 void setObject(OtherClass* object) // passes a normal pointer which will be stored in a StrongPtr 71 71 { this->object_ = object; } 72 72 73 OtherClass* getObject() const // converts the S martPtr to a normal pointer and returns it73 OtherClass* getObject() const // converts the StrongPtr to a normal pointer and returns it 74 74 { return this->object_; } 75 75 76 76 private: 77 S martPtr<OtherClass> object_; // a pointer to an instance of OtherClass is stored in a SmartPtr77 StrongPtr<OtherClass> object_; // a pointer to an instance of OtherClass is stored in a StrongPtr 78 78 }; 79 79 @endcode … … 85 85 MyClass* myclass = new MyClass(); // create an instance of MyClass 86 86 OtherClass* object = new OtherClass(); // create an instance of OtherClass 87 myclass->setObject(object); // the object is now stored in a S martPtr inside myclass88 89 object->destroy(); // we try to destroy object, but there's still a S martPtr pointing at it.90 91 # object still exists at this point (because a S martPtr points at it)92 93 delete myclass; // now we delete myclass, which also destroys the S martPtr94 95 # object doesn't exist anymore (because the S martPtr is now destroyed)87 myclass->setObject(object); // the object is now stored in a StrongPtr inside myclass 88 89 object->destroy(); // we try to destroy object, but there's still a StrongPtr pointing at it. 90 91 # object still exists at this point (because a StrongPtr points at it) 92 93 delete myclass; // now we delete myclass, which also destroys the StrongPtr 94 95 # object doesn't exist anymore (because the StrongPtr is now destroyed) 96 96 @endcode 97 97 … … 100 100 MyClass* myclass = new MyClass(); // create an instance of MyClass 101 101 OtherClass* object = new OtherClass(); // create an instance of OtherClass 102 myclass->setObject(object); // the object is now stored in a S martPtr inside myclass103 104 delete myclass; // we delete myclass, which also destroys the S martPtr102 myclass->setObject(object); // the object is now stored in a StrongPtr inside myclass 103 104 delete myclass; // we delete myclass, which also destroys the StrongPtr 105 105 106 106 # object still exists at this point (because destroy() was not called yet) … … 112 112 113 113 Note that in any case @c object->destroy() has to be called to delete the object. 114 However if a S martPtr points at it, the destruction is delayed until all SmartPtr114 However if a StrongPtr points at it, the destruction is delayed until all StrongPtr 115 115 are destroyed. 116 116 */ 117 117 118 #ifndef _S martPtr_H__119 #define _S martPtr_H__118 #ifndef _StrongPtr_H__ 119 #define _StrongPtr_H__ 120 120 121 121 #include "core/CorePrereqs.h" … … 129 129 { 130 130 /** 131 @brief A s mart pointer which wraps a pointer to an object and keeps this object alive as long as the smartpointer exists.132 133 @see See @ref S martPtrExample "this description" for more information and an example.131 @brief A strong pointer which wraps a pointer to an object and keeps this object alive as long as the strong pointer exists. 132 133 @see See @ref StrongPtrExample "this description" for more information and an example. 134 134 */ 135 135 template <class T> 136 class S martPtr136 class StrongPtr 137 137 { 138 138 public: 139 /// Constructor: Initializes the s martpointer with a null pointer.140 inline S martPtr() : pointer_(0), base_(0)141 { 142 } 143 144 /// Constructor: Initializes the s martpointer 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)145 inline S martPtr(T* pointer, bool bAddRef = true) : pointer_(pointer), base_(pointer)139 /// Constructor: Initializes the strong pointer with a null pointer. 140 inline StrongPtr() : pointer_(0), base_(0) 141 { 142 } 143 144 /// Constructor: Initializes the strong 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) 145 inline StrongPtr(T* pointer, bool bAddRef = true) : pointer_(pointer), base_(pointer) 146 146 { 147 147 if (this->base_ && bAddRef) … … 150 150 151 151 /// Copy-constructor 152 inline S martPtr(const SmartPtr& other) : pointer_(other.pointer_), base_(other.base_)152 inline StrongPtr(const StrongPtr& other) : pointer_(other.pointer_), base_(other.base_) 153 153 { 154 154 if (this->base_) … … 156 156 } 157 157 158 /// Copy-constructor for s martpointers to objects of another class.158 /// Copy-constructor for strong pointers to objects of another class. 159 159 template <class O> 160 inline S martPtr(const SmartPtr<O>& other) : pointer_(other.get()), base_(other.base_)160 inline StrongPtr(const StrongPtr<O>& other) : pointer_(other.get()), base_(other.base_) 161 161 { 162 162 if (this->base_) … … 164 164 } 165 165 166 /// Constructor: Initializes the s martpointer with the pointer that is stored in a WeakPtr.166 /// Constructor: Initializes the strong pointer with the pointer that is stored in a WeakPtr. 167 167 template <class O> 168 inline S martPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.getBase())168 inline StrongPtr(const WeakPtr<O>& other) : pointer_(other.get()), base_(other.getBase()) 169 169 { 170 170 if (this->base_) … … 173 173 174 174 /// Destructor: Decrements the reference counter. 175 inline ~S martPtr()175 inline ~StrongPtr() 176 176 { 177 177 if (this->base_) … … 180 180 181 181 /// Assigns a new pointer. 182 inline S martPtr& operator=(T* pointer)183 { 184 S martPtr(pointer).swap(*this);182 inline StrongPtr& operator=(T* pointer) 183 { 184 StrongPtr(pointer).swap(*this); 185 185 return *this; 186 186 } 187 187 188 /// Assigns the wrapped pointer of another S martPtr.189 inline S martPtr& operator=(const SmartPtr& other)190 { 191 S martPtr(other).swap(*this);188 /// Assigns the wrapped pointer of another StrongPtr. 189 inline StrongPtr& operator=(const StrongPtr& other) 190 { 191 StrongPtr(other).swap(*this); 192 192 return *this; 193 193 } 194 194 195 /// Assigns the wrapped pointer of a S martPtr of another class195 /// Assigns the wrapped pointer of a StrongPtr of another class 196 196 template <class O> 197 inline S martPtr& operator=(const SmartPtr<O>& other)198 { 199 S martPtr(other).swap(*this);197 inline StrongPtr& operator=(const StrongPtr<O>& other) 198 { 199 StrongPtr(other).swap(*this); 200 200 return *this; 201 201 } … … 203 203 /// Assigns the wrapped pointer of a WeakPtr. 204 204 template <class O> 205 inline S martPtr& operator=(const WeakPtr<O>& other)206 { 207 S martPtr(other).swap(*this);205 inline StrongPtr& operator=(const WeakPtr<O>& other) 206 { 207 StrongPtr(other).swap(*this); 208 208 return *this; 209 209 } … … 221 221 } 222 222 223 /// Implicitly converts the S martPtr to a pointer of type @c T*223 /// Implicitly converts the StrongPtr to a pointer of type @c T* 224 224 inline operator T*() const 225 225 { … … 247 247 } 248 248 249 /// Swaps the contents of two s martpointers.250 inline void swap(S martPtr& other)249 /// Swaps the contents of two strong pointers. 250 inline void swap(StrongPtr& other) 251 251 { 252 252 { … … 262 262 } 263 263 264 /// Resets the s martpointer (equivalent to assigning a NULL pointer).264 /// Resets the strong pointer (equivalent to assigning a NULL pointer). 265 265 inline void reset() 266 266 { 267 S martPtr().swap(*this);267 StrongPtr().swap(*this); 268 268 } 269 269 270 270 private: 271 271 T* pointer_; ///< The wrapped pointer to an object of type @a T 272 Destroyable* base_; ///< The wrapped pointer, casted up to Destroyable (this is needed because with just a T* pointer, S martPtr couln't be used with forward declarations)272 Destroyable* base_; ///< The wrapped pointer, casted up to Destroyable (this is needed because with just a T* pointer, StrongPtr couln't be used with forward declarations) 273 273 }; 274 274 275 /// Swaps the contents of two s martpointers.275 /// Swaps the contents of two strong pointers. 276 276 template <class T> 277 void swap(S martPtr<T>& a, SmartPtr<T>& b)277 void swap(StrongPtr<T>& a, StrongPtr<T>& b) 278 278 { 279 279 a.swap(b); 280 280 } 281 281 282 /// Uses a static_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new S martPtr<T>.282 /// Uses a static_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new StrongPtr<T>. 283 283 template <class T, class U> 284 S martPtr<T> static_pointer_cast(const SmartPtr<U>& p)284 StrongPtr<T> static_pointer_cast(const StrongPtr<U>& p) 285 285 { 286 286 return static_cast<T*>(p.get()); 287 287 } 288 288 289 /// Uses a const_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new S martPtr<T>.289 /// Uses a const_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new StrongPtr<T>. 290 290 template <class T, class U> 291 S martPtr<T> const_pointer_cast(const SmartPtr<U>& p)291 StrongPtr<T> const_pointer_cast(const StrongPtr<U>& p) 292 292 { 293 293 return const_cast<T*>(p.get()); 294 294 } 295 295 296 /// Uses a dynamic_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new S martPtr<T>.296 /// Uses a dynamic_cast to cast a pointer of type U* to a pointer of type T* and returns it in a new StrongPtr<T>. 297 297 template <class T, class U> 298 S martPtr<T> dynamic_pointer_cast(const SmartPtr<U>& p)298 StrongPtr<T> dynamic_pointer_cast(const StrongPtr<U>& p) 299 299 { 300 300 return orxonox_cast<T*>(p.get()); … … 302 302 } 303 303 304 #endif /* _S martPtr_H__ */304 #endif /* _StrongPtr_H__ */
Note: See TracChangeset
for help on using the changeset viewer.