- Timestamp:
- Aug 21, 2010, 4:54:29 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/consolecommands3/src/libraries/core/SharedPtr.h
r7192 r7196 31 31 32 32 #include "CorePrereqs.h" 33 #include <algorithm> 33 34 34 35 namespace orxonox 35 36 { 37 class SharedPtrDestroyer 38 { 39 public: 40 virtual void destroy() = 0; 41 }; 42 43 template <class T> 44 class SharedPtrDestroyerImpl : public SharedPtrDestroyer 45 { 46 public: 47 SharedPtrDestroyerImpl(T* pointer) : pointer_(pointer) {} 48 49 void destroy() 50 { 51 // COUT(0) << "delete " << this->pointer_ << std::endl; 52 delete this->pointer_; 53 } 54 55 private: 56 T* pointer_; 57 }; 58 36 59 template <class T> 37 60 class SharedPtr 38 61 { 62 template <class O> 63 friend class SharedPtr; 64 39 65 public: 40 inline SharedPtr() : pointer_(0) 66 inline SharedPtr() : pointer_(0), counter_(0), destroyer_(0) 41 67 { 42 68 // COUT(0) << "SharedPtr (1): " << this->pointer_ << std::endl; 43 69 } 44 70 45 inline SharedPtr(T* pointer) : pointer_(pointer) 71 inline SharedPtr(T* pointer) : pointer_(pointer), counter_(0), destroyer_(0) 46 72 { 47 73 // COUT(0) << "SharedPtr (2): " << this->pointer_ << std::endl; 48 74 if (this->pointer_) 49 this->pointer_->incrementReferenceCount(); 75 { 76 this->counter_ = new int(1); 77 this->destroyer_ = new SharedPtrDestroyerImpl<T>(this->pointer_); 78 } 50 79 } 51 80 52 inline SharedPtr(const SharedPtr& other) : pointer_(other.pointer_) 81 inline SharedPtr(const SharedPtr& other) : pointer_(other.pointer_), counter_(other.counter_), destroyer_(other.destroyer_) 53 82 { 54 83 // COUT(0) << "SharedPtr (3): " << this->pointer_ << std::endl; 55 84 if (this->pointer_) 56 this->pointer_->incrementReferenceCount(); 85 ++(*this->counter_); 86 } 87 88 template <class O> 89 inline SharedPtr(const SharedPtr<O>& other) : pointer_(other.pointer_), counter_(other.counter_), destroyer_(other.destroyer_) 90 { 91 // COUT(0) << "SharedPtr (4): " << this->pointer_ << std::endl; 92 if (this->pointer_) 93 ++(*this->counter_); 57 94 } 58 95 … … 61 98 // COUT(0) << "~SharedPtr: " << this->pointer_ << std::endl; 62 99 if (this->pointer_) 63 this->pointer_->decrementReferenceCount(); 100 { 101 --(*this->counter_); 102 103 if (*this->counter_ == 0) 104 { 105 this->destroyer_->destroy(); 106 delete this->destroyer_; 107 delete this->counter_; 108 } 109 } 64 110 } 65 111 66 112 inline const SharedPtr& operator=(const SharedPtr& other) 67 113 { 68 // COUT(0) << "SharedPtr= " << std::endl;114 // COUT(0) << "SharedPtr= (1)" << std::endl; 69 115 SharedPtr(other).swap(*this); 70 116 return *this; 71 117 } 72 118 73 inline void cast(const SharedPtr& other) 119 template <class O> 120 inline const SharedPtr& operator=(const SharedPtr<O>& other) 74 121 { 75 // COUT(0) << "SharedPtr cast" << std::endl;122 // COUT(0) << "SharedPtr= (2)" << std::endl; 76 123 SharedPtr(other).swap(*this); 124 return *this; 125 } 126 127 template <class O> 128 inline SharedPtr<O> cast() const 129 { 130 O* temp = static_cast<O*>(this->pointer_); // temp value for prettier compiler error in case of an invalid static_cast 131 return SharedPtr<O>(temp, this->counter_, this->destroyer_); 77 132 } 78 133 … … 96 151 inline void swap(SharedPtr& other) 97 152 { 98 T* temp = this->pointer_;99 this->pointer_ = other.pointer_;100 other.pointer_ = temp;153 std::swap(this->pointer_, other.pointer_); 154 std::swap(this->counter_, other.counter_); 155 std::swap(this->destroyer_, other.destroyer_); 101 156 } 102 157 103 158 private: 159 inline SharedPtr(T* pointer, int* counter, SharedPtrDestroyer* destroyer) : pointer_(pointer), counter_(counter), destroyer_(destroyer) 160 { 161 // COUT(0) << "SharedPtr (5): " << this->pointer_ << std::endl; 162 if (this->pointer_) 163 ++(*this->counter_); 164 } 165 104 166 T* pointer_; 167 int* counter_; 168 SharedPtrDestroyer* destroyer_; 105 169 }; 106 170 /* 107 171 template <class T, class Parent> 108 172 class SharedChildPtr : public SharedPtr<Parent> … … 116 180 inline T& operator*() const { return *static_cast<T*>(SharedPtr<Parent>::operator->()); } 117 181 }; 182 */ 118 183 } 119 184
Note: See TracChangeset
for help on using the changeset viewer.