Changeset 7196 for code/branches/consolecommands3/src/libraries
- Timestamp:
- Aug 21, 2010, 4:54:29 PM (14 years ago)
- Location:
- code/branches/consolecommands3/src/libraries/core
- Files:
-
- 2 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/consolecommands3/src/libraries/core/Executor.cc
r7192 r7196 44 44 Executor::Executor(Functor* functor, const std::string& name) 45 45 { 46 this->references_ = 0;47 46 this->functor_ = functor; 48 47 this->name_ = name; -
code/branches/consolecommands3/src/libraries/core/Executor.h
r7192 r7196 36 36 #include "util/MultiType.h" 37 37 #include "Functor.h" 38 #include "ExecutorPtr.h" 38 39 39 40 namespace orxonox … … 112 113 113 114 private: 114 inline void incrementReferenceCount()115 { ++this->references_; }116 inline void decrementReferenceCount()117 { --this->references_; if (this->references_ == 0) delete this; }118 119 int references_;120 115 static int instances_s; 121 116 }; … … 198 193 }; 199 194 200 201 202 typedef SharedPtr<Executor> ExecutorPtr;203 204 typedef SharedChildPtr<ExecutorStatic, Executor> ExecutorStaticPtr;205 206 template <class T>207 class ExecutorMemberPtr : public SharedChildPtr<ExecutorMember<T>, Executor>208 {209 public:210 inline ExecutorMemberPtr() : SharedChildPtr<ExecutorMember<T>, Executor>() {}211 inline ExecutorMemberPtr(ExecutorMember<T>* pointer) : SharedChildPtr<ExecutorMember<T>, Executor>(pointer) {}212 // inline ExecutorMemberPtr(const ExecutorMemberPtr& other) : SharedChildPtr<ExecutorMember<T>, Executor>(other) {}213 };214 215 216 217 195 inline Executor* createExecutor(Functor* functor, const std::string& name = "") 218 196 { -
code/branches/consolecommands3/src/libraries/core/Functor.h
r7192 r7196 38 38 #include "util/Debug.h" 39 39 #include "util/MultiType.h" 40 #include " SharedPtr.h"40 #include "FunctorPtr.h" 41 41 42 42 namespace orxonox … … 104 104 105 105 public: 106 Functor() : references_(0){ ++instances_s; COUT(0) << "functor ++: " << instances_s << std::endl; }106 Functor() { ++instances_s; COUT(0) << "functor ++: " << instances_s << std::endl; } 107 107 virtual ~Functor() { --instances_s; COUT(0) << "functor --: " << instances_s << std::endl; } 108 108 … … 124 124 125 125 private: 126 inline void incrementReferenceCount()127 { ++this->references_; }128 inline void decrementReferenceCount()129 { --this->references_; if (this->references_ == 0) delete this; }130 131 int references_;132 126 static int instances_s; 133 127 }; … … 212 206 T* object_; 213 207 const T* constObject_; 214 };215 216 217 218 typedef SharedPtr<Functor> FunctorPtr;219 220 typedef SharedChildPtr<FunctorStatic, Functor> FunctorStaticPtr;221 222 template <class T>223 class FunctorMemberPtr : public SharedChildPtr<FunctorMember<T>, Functor>224 {225 public:226 inline FunctorMemberPtr() : SharedChildPtr<FunctorMember<T>, Functor>() {}227 inline FunctorMemberPtr(FunctorMember<T>* pointer) : SharedChildPtr<FunctorMember<T>, Functor>(pointer) {}228 // inline FunctorMemberPtr(const FunctorMemberPtr& other) : SharedChildPtr<FunctorMember<T>, Functor>(other) {}229 208 }; 230 209 -
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.