Changeset 7264
- Timestamp:
- Aug 29, 2010, 9:37:38 PM (14 years ago)
- Location:
- code/branches/consolecommands3/src/libraries/util
- Files:
-
- 3 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/consolecommands3/src/libraries/util/CMakeLists.txt
r7207 r7264 32 32 OutputHandler.cc 33 33 ScopedSingletonManager.cc 34 SharedPtr.cc 34 35 SignalHandler.cc 35 36 Sleep.cc 37 SmallObjectAllocator.cc 36 38 SubString.cc 37 39 COMPILATION_END -
code/branches/consolecommands3/src/libraries/util/SharedPtr.h
r7212 r7264 31 31 32 32 #include "UtilPrereqs.h" 33 33 34 #include <algorithm> 34 35 #include <cassert> 35 36 37 #include "SmallObjectAllocator.h" 38 36 39 namespace orxonox 37 40 { 38 class Shared PtrDestroyer41 class SharedCounter 39 42 { 40 43 public: 44 SharedCounter() : count_(1) {} 41 45 virtual void destroy() = 0; 46 47 int count_; 42 48 }; 43 49 44 50 template <class T> 45 class Shared PtrDestroyerImpl : public SharedPtrDestroyer51 class SharedCounterImpl : public SharedCounter 46 52 { 47 53 public: 48 Shared PtrDestroyerImpl(T* pointer) : pointer_(pointer) {}54 SharedCounterImpl(T* pointer) : pointer_(pointer) {} 49 55 50 56 void destroy() … … 57 63 }; 58 64 65 _UtilExport SmallObjectAllocator& createSharedCounterPool(); 66 67 FORCEINLINE SmallObjectAllocator& getSharedCounterPool() 68 { 69 static SmallObjectAllocator& instance = createSharedCounterPool(); 70 return instance; 71 } 72 59 73 template <class T> 60 74 class SharedPtr … … 64 78 65 79 public: 66 inline SharedPtr() : pointer_(0), counter_(0) , destroyer_(0)80 inline SharedPtr() : pointer_(0), counter_(0) 67 81 { 68 82 } 69 83 70 inline SharedPtr(T* pointer) : pointer_(pointer), counter_(0) , destroyer_(0)84 inline SharedPtr(T* pointer) : pointer_(pointer), counter_(0) 71 85 { 72 86 if (this->pointer_) 73 87 { 74 this->counter_ = new int(1);75 this-> destroyer_ = new SharedPtrDestroyerImpl<T>(this->pointer_);88 void* chunk = getSharedCounterPool().alloc(); 89 this->counter_ = new (chunk) SharedCounterImpl<T>(this->pointer_); 76 90 } 77 91 } 78 92 79 inline SharedPtr(const SharedPtr& other) : pointer_(other.pointer_), counter_(other.counter_) , destroyer_(other.destroyer_)93 inline SharedPtr(const SharedPtr& other) : pointer_(other.pointer_), counter_(other.counter_) 80 94 { 81 95 if (this->pointer_) 82 ++ (*this->counter_);96 ++this->counter_->count_; 83 97 } 84 98 85 99 template <class O> 86 inline SharedPtr(const SharedPtr<O>& other) : pointer_(other.pointer_), counter_(other.counter_) , destroyer_(other.destroyer_)100 inline SharedPtr(const SharedPtr<O>& other) : pointer_(other.pointer_), counter_(other.counter_) 87 101 { 88 102 if (this->pointer_) 89 ++ (*this->counter_);103 ++this->counter_->count_; 90 104 } 91 105 … … 94 108 if (this->pointer_) 95 109 { 96 -- (*this->counter_);110 --this->counter_->count_; 97 111 98 if ( *this->counter_ == 0)112 if (this->counter_->count_ == 0) 99 113 { 100 this->destroyer_->destroy(); 101 delete this->destroyer_; 102 delete this->counter_; 114 this->counter_->destroy(); 115 getSharedCounterPool().free(this->counter_); 103 116 } 104 117 } … … 122 135 { 123 136 O* temp = static_cast<O*>(this->pointer_); // temp value for prettier compiler error in case of an invalid static_cast 124 return SharedPtr<O>(temp, this->counter_ , this->destroyer_);137 return SharedPtr<O>(temp, this->counter_); 125 138 } 126 139 … … 151 164 std::swap(this->pointer_, other.pointer_); 152 165 std::swap(this->counter_, other.counter_); 153 std::swap(this->destroyer_, other.destroyer_);154 166 } 155 167 156 168 private: 157 inline SharedPtr(T* pointer, int* counter, SharedPtrDestroyer* destroyer) : pointer_(pointer), counter_(counter), destroyer_(destroyer)169 inline SharedPtr(T* pointer, SharedCounter* counter) : pointer_(pointer), counter_(counter) 158 170 { 159 171 if (this->pointer_) 160 ++ (*this->counter_);172 ++this->counter_->count_; 161 173 } 162 174 163 175 T* pointer_; 164 int* counter_; 165 SharedPtrDestroyer* destroyer_; 176 SharedCounter* counter_; 166 177 }; 167 178
Note: See TracChangeset
for help on using the changeset viewer.