diff options
Diffstat (limited to 'ue5/shared_ptr.hpp')
| -rw-r--r-- | ue5/shared_ptr.hpp | 282 |
1 files changed, 281 insertions, 1 deletions
diff --git a/ue5/shared_ptr.hpp b/ue5/shared_ptr.hpp index 37989e3..7f502bd 100644 --- a/ue5/shared_ptr.hpp +++ b/ue5/shared_ptr.hpp | |||
| @@ -1,3 +1,10 @@ | |||
| 1 | /** | ||
| 2 | * @module shared_ptr | ||
| 3 | * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348) | ||
| 4 | * @brief Class template shared_ptr | ||
| 5 | * @date 13.06.2009 | ||
| 6 | */ | ||
| 7 | |||
| 1 | #ifndef SHARED_PTR_H | 8 | #ifndef SHARED_PTR_H |
| 2 | #define SHARED_PTR_H | 9 | #define SHARED_PTR_H |
| 3 | 10 | ||
| @@ -8,29 +15,85 @@ | |||
| 8 | 15 | ||
| 9 | namespace Ti | 16 | namespace Ti |
| 10 | { | 17 | { |
| 18 | /** | ||
| 19 | * @class shared_ptr | ||
| 20 | * | ||
| 21 | * The templates stores a pointer, usually obtained via new. | ||
| 22 | * shared_ptr implements semantics of shared ownership; the last remaining | ||
| 23 | * owner of the pointer is responsible for destroying the object, or | ||
| 24 | * otherwise releasing the resources associated with the stored pointer. | ||
| 25 | * A shared_ptr object is empty if it does not own a pointer. | ||
| 26 | */ | ||
| 11 | template <typename T> | 27 | template <typename T> |
| 12 | class shared_ptr | 28 | class shared_ptr |
| 13 | { | 29 | { |
| 14 | private: | 30 | private: |
| 31 | /** stored pointer */ | ||
| 15 | T* m_ptr; | 32 | T* m_ptr; |
| 33 | /** reference counter */ | ||
| 16 | unsigned long* m_count; | 34 | unsigned long* m_count; |
| 17 | 35 | ||
| 18 | public: | 36 | public: |
| 37 | /** | ||
| 38 | * @method shared_ptr | ||
| 39 | * @brief Default ctor | ||
| 40 | * @param - | ||
| 41 | * @return - | ||
| 42 | * @globalvars none | ||
| 43 | * @exception none | ||
| 44 | * @pre none | ||
| 45 | * @post get() == m_count == NULL | ||
| 46 | */ | ||
| 19 | shared_ptr() | 47 | shared_ptr() |
| 20 | : m_ptr(NULL), m_count(NULL) | 48 | : m_ptr(NULL), m_count(NULL) |
| 21 | {} | 49 | {} |
| 22 | 50 | ||
| 51 | /** | ||
| 52 | * @method get | ||
| 53 | * @brief Returns pointer of enclosed object | ||
| 54 | * @param - | ||
| 55 | * @return returns the stored pointer, null pointer if *this is empty. | ||
| 56 | * @globalvars none | ||
| 57 | * @exception none | ||
| 58 | * @pre none | ||
| 59 | * @post none | ||
| 60 | */ | ||
| 23 | T* get() const | 61 | T* get() const |
| 24 | { | 62 | { |
| 25 | return m_ptr; | 63 | return m_ptr; |
| 26 | } | 64 | } |
| 27 | 65 | ||
| 66 | /** | ||
| 67 | * @method shared_ptr | ||
| 68 | * @brief Copy ctor. If r is empty, constructs an empty shared_ptr | ||
| 69 | * object - otherwise, constructs a shared_ptr object that | ||
| 70 | * shares ownership with r. | ||
| 71 | * @param other instance to copy from | ||
| 72 | * @return - | ||
| 73 | * @globalvars none | ||
| 74 | * @exception none | ||
| 75 | * @pre none | ||
| 76 | * @post get() == other.get() && m_count == other.m_count | ||
| 77 | */ | ||
| 28 | shared_ptr(const shared_ptr<T>& other) | 78 | shared_ptr(const shared_ptr<T>& other) |
| 29 | : m_ptr(other.m_ptr), m_count(other.m_count) | 79 | : m_ptr(other.m_ptr), m_count(other.m_count) |
| 30 | { | 80 | { |
| 31 | add_ref(); | 81 | add_ref(); |
| 32 | } | 82 | } |
| 33 | 83 | ||
| 84 | /** | ||
| 85 | * @method shared_ptr | ||
| 86 | * @brief Copy ctor. If other is empty, constructs an empty shared_ptr | ||
| 87 | * object - otherwise, constructs a shared_ptr object that | ||
| 88 | * shares ownership with other. | ||
| 89 | * @param other instance to copy from | ||
| 90 | * @return - | ||
| 91 | * @globalvars none | ||
| 92 | * @exception none | ||
| 93 | * @pre The constructor shall not participate in the overload | ||
| 94 | * resolution unless O* is implicitly convertible to T*. | ||
| 95 | * @post get() == other.get() && m_count == other.m_count | ||
| 96 | */ | ||
| 34 | template <typename O> | 97 | template <typename O> |
| 35 | shared_ptr(const shared_ptr<O>& other) | 98 | shared_ptr(const shared_ptr<O>& other) |
| 36 | : m_ptr(other.m_ptr), m_count(other.m_count) | 99 | : m_ptr(other.m_ptr), m_count(other.m_count) |
| @@ -38,7 +101,18 @@ namespace Ti | |||
| 38 | add_ref(); | 101 | add_ref(); |
| 39 | } | 102 | } |
| 40 | 103 | ||
| 41 | /* used to construct shared_ptr with a sub-object of O */ | 104 | /** |
| 105 | * @method shared_ptr | ||
| 106 | * @brief Copy ctor. Constructs a shared_ptr instance that stores ptr | ||
| 107 | * and shares ownership with other. | ||
| 108 | * @param other instance to copy from | ||
| 109 | * @param ptr ptr to store | ||
| 110 | * @return - | ||
| 111 | * @globalvars none | ||
| 112 | * @exception none | ||
| 113 | * @pre none | ||
| 114 | * @post get() == p && m_count == other.m_count | ||
| 115 | */ | ||
| 42 | template <typename O> | 116 | template <typename O> |
| 43 | shared_ptr(const shared_ptr<O>& other, T* ptr) | 117 | shared_ptr(const shared_ptr<O>& other, T* ptr) |
| 44 | : m_ptr(ptr), m_count(other.m_count) | 118 | : m_ptr(ptr), m_count(other.m_count) |
| @@ -46,11 +120,33 @@ namespace Ti | |||
| 46 | add_ref(); | 120 | add_ref(); |
| 47 | } | 121 | } |
| 48 | 122 | ||
| 123 | /** | ||
| 124 | * @method shared_ptr | ||
| 125 | * @brief Copy ctor. Constructs a shared_ptr object that owns the pointer p. | ||
| 126 | * @param p pointer to store | ||
| 127 | * @return - | ||
| 128 | * @globalvars none | ||
| 129 | * @exception bad_alloc if memory couldn't be optained | ||
| 130 | * @pre p shall be convertible to T*. Y shall be a complete type. The | ||
| 131 | * expression delete p shall be well formed, shall have well | ||
| 132 | * defined behavior, and shall not throw exceptions. | ||
| 133 | * @post get() == p && m_count == 1 | ||
| 134 | */ | ||
| 49 | template <typename O> | 135 | template <typename O> |
| 50 | explicit shared_ptr(O* p) | 136 | explicit shared_ptr(O* p) |
| 51 | : m_ptr(p), m_count(new unsigned long(1)) | 137 | : m_ptr(p), m_count(new unsigned long(1)) |
| 52 | {} | 138 | {} |
| 53 | 139 | ||
| 140 | /** | ||
| 141 | * @method operator= | ||
| 142 | * @brief operator = | ||
| 143 | * @param other object to compare with | ||
| 144 | * @return always *this | ||
| 145 | * @globalvars none | ||
| 146 | * @exception none | ||
| 147 | * @pre none | ||
| 148 | * @post none | ||
| 149 | */ | ||
| 54 | shared_ptr& operator=(const shared_ptr<T>& other) | 150 | shared_ptr& operator=(const shared_ptr<T>& other) |
| 55 | { | 151 | { |
| 56 | if (*this == other) | 152 | if (*this == other) |
| @@ -62,6 +158,16 @@ namespace Ti | |||
| 62 | return *this; | 158 | return *this; |
| 63 | } | 159 | } |
| 64 | 160 | ||
| 161 | /** | ||
| 162 | * @method operator= | ||
| 163 | * @brief operator = | ||
| 164 | * @param other object to compare with | ||
| 165 | * @return always *this | ||
| 166 | * @globalvars none | ||
| 167 | * @exception none | ||
| 168 | * @pre none | ||
| 169 | * @post none | ||
| 170 | */ | ||
| 65 | template <typename O> | 171 | template <typename O> |
| 66 | shared_ptr& operator=(const shared_ptr<O>& other) | 172 | shared_ptr& operator=(const shared_ptr<O>& other) |
| 67 | { | 173 | { |
| @@ -74,39 +180,110 @@ namespace Ti | |||
| 74 | return *this; | 180 | return *this; |
| 75 | } | 181 | } |
| 76 | 182 | ||
| 183 | /** | ||
| 184 | * @method ~shared_ptr | ||
| 185 | * @brief Dtor | ||
| 186 | * @param - | ||
| 187 | * @return - | ||
| 188 | * @globalvars none | ||
| 189 | * @exception none | ||
| 190 | * @pre none | ||
| 191 | * @post none | ||
| 192 | */ | ||
| 77 | ~shared_ptr() | 193 | ~shared_ptr() |
| 78 | { | 194 | { |
| 79 | release(); | 195 | release(); |
| 80 | } | 196 | } |
| 81 | 197 | ||
| 198 | /** | ||
| 199 | * @method operator* | ||
| 200 | * @brief operator * | ||
| 201 | * @param - | ||
| 202 | * @return returns *get() | ||
| 203 | * @globalvars none | ||
| 204 | * @exception none | ||
| 205 | * @pre get() != NULL | ||
| 206 | * @post none | ||
| 207 | */ | ||
| 82 | T& operator*() const | 208 | T& operator*() const |
| 83 | { | 209 | { |
| 84 | return *m_ptr; | 210 | return *m_ptr; |
| 85 | } | 211 | } |
| 86 | 212 | ||
| 213 | /** | ||
| 214 | * @method operator-> | ||
| 215 | * @brief operator -> | ||
| 216 | * @param - | ||
| 217 | * @return returns get() | ||
| 218 | * @globalvars none | ||
| 219 | * @exception none | ||
| 220 | * @pre get() != NULL | ||
| 221 | * @post none | ||
| 222 | */ | ||
| 87 | T* operator->() const | 223 | T* operator->() const |
| 88 | { | 224 | { |
| 89 | return m_ptr; | 225 | return m_ptr; |
| 90 | } | 226 | } |
| 91 | 227 | ||
| 228 | /** | ||
| 229 | * @method swap | ||
| 230 | * @brief Exchanges the contents of *this and other. | ||
| 231 | * @param other instance to swap with | ||
| 232 | * @return - | ||
| 233 | * @globalvars none | ||
| 234 | * @exception none | ||
| 235 | * @pre none | ||
| 236 | * @post none | ||
| 237 | */ | ||
| 92 | void swap(shared_ptr<T>& other) | 238 | void swap(shared_ptr<T>& other) |
| 93 | { | 239 | { |
| 94 | std::swap(m_ptr, other.m_ptr); | 240 | std::swap(m_ptr, other.m_ptr); |
| 95 | std::swap(m_count, other.m_count); | 241 | std::swap(m_count, other.m_count); |
| 96 | } | 242 | } |
| 97 | 243 | ||
| 244 | /** | ||
| 245 | * @method reset | ||
| 246 | * @brief Equivalent to shared_ptr().swap(*this). | ||
| 247 | * @param - | ||
| 248 | * @return - | ||
| 249 | * @globalvars none | ||
| 250 | * @exception none | ||
| 251 | * @pre none | ||
| 252 | * @post none | ||
| 253 | */ | ||
| 98 | inline void reset() | 254 | inline void reset() |
| 99 | { | 255 | { |
| 100 | shared_ptr().swap(*this); | 256 | shared_ptr().swap(*this); |
| 101 | } | 257 | } |
| 102 | 258 | ||
| 103 | private: | 259 | private: |
| 260 | /** | ||
| 261 | * @method add_ref | ||
| 262 | * @brief Increase reference counter | ||
| 263 | * @param - | ||
| 264 | * @return - | ||
| 265 | * @globalvars none | ||
| 266 | * @exception none | ||
| 267 | * @pre m_count != NULL | ||
| 268 | * @post none | ||
| 269 | */ | ||
| 104 | void add_ref() | 270 | void add_ref() |
| 105 | { | 271 | { |
| 106 | if (m_count != NULL) | 272 | if (m_count != NULL) |
| 107 | ++(*m_count); | 273 | ++(*m_count); |
| 108 | } | 274 | } |
| 109 | 275 | ||
| 276 | /** | ||
| 277 | * @method release | ||
| 278 | * @brief Decrease reference counter | ||
| 279 | * if m_count == 0 free all resources | ||
| 280 | * @param - | ||
| 281 | * @return - | ||
| 282 | * @globalvars none | ||
| 283 | * @exception none | ||
| 284 | * @pre m_ptr != NULL && m_count != NULL | ||
| 285 | * @post none | ||
| 286 | */ | ||
| 110 | void release() | 287 | void release() |
| 111 | { | 288 | { |
| 112 | if (m_ptr != NULL) | 289 | if (m_ptr != NULL) |
| @@ -147,48 +324,139 @@ namespace Ti | |||
| 147 | friend shared_ptr<R> shared_dynamic_cast(const shared_ptr<F>& from); | 324 | friend shared_ptr<R> shared_dynamic_cast(const shared_ptr<F>& from); |
| 148 | }; | 325 | }; |
| 149 | 326 | ||
| 327 | /** | ||
| 328 | * @method make_shared | ||
| 329 | * @brief Creates an instance of shared_ptr containing a pointer to the | ||
| 330 | * also created instance of T(args...). | ||
| 331 | * @param args arguments for ctor of T. | ||
| 332 | * @return A shared_ptr instance that stores and owns the address of the | ||
| 333 | * newly constructed object of type T. | ||
| 334 | * @globalvars none | ||
| 335 | * @exception bad_alloc | ||
| 336 | * @pre none | ||
| 337 | * @post get() != 0 && use_count() == 1 | ||
| 338 | */ | ||
| 150 | template <typename T, typename ... Args> | 339 | template <typename T, typename ... Args> |
| 151 | shared_ptr<T> make_shared(Args ... args) | 340 | shared_ptr<T> make_shared(Args ... args) |
| 152 | { | 341 | { |
| 153 | return shared_ptr<T>(new T(args ...)); | 342 | return shared_ptr<T>(new T(args ...)); |
| 154 | } | 343 | } |
| 155 | 344 | ||
| 345 | /** | ||
| 346 | * @method operator== | ||
| 347 | * @brief operator == | ||
| 348 | * @param a object1 to compare | ||
| 349 | * @param b object2 to compare | ||
| 350 | * @return true if both objects contain the same pointer, false otherwise. | ||
| 351 | * @globalvars none | ||
| 352 | * @exception none | ||
| 353 | * @pre none | ||
| 354 | * @post none | ||
| 355 | */ | ||
| 156 | template<typename U1, typename U2> | 356 | template<typename U1, typename U2> |
| 157 | bool operator==(const shared_ptr<U1>& a, const shared_ptr<U2>& b) | 357 | bool operator==(const shared_ptr<U1>& a, const shared_ptr<U2>& b) |
| 158 | { | 358 | { |
| 159 | return a.get() == b.get(); | 359 | return a.get() == b.get(); |
| 160 | } | 360 | } |
| 161 | 361 | ||
| 362 | /** | ||
| 363 | * @method operator== | ||
| 364 | * @brief operator == | ||
| 365 | * @param a shared_ptr to compare | ||
| 366 | * @param b pointer to compare | ||
| 367 | * @return true if a contains pointer b, false otherwise. | ||
| 368 | * @globalvars none | ||
| 369 | * @exception none | ||
| 370 | * @pre none | ||
| 371 | * @post none | ||
| 372 | */ | ||
| 162 | template<typename U1, typename U2> | 373 | template<typename U1, typename U2> |
| 163 | bool operator==(const shared_ptr<U1>& a, const U2* b) | 374 | bool operator==(const shared_ptr<U1>& a, const U2* b) |
| 164 | { | 375 | { |
| 165 | return a.get() == b; | 376 | return a.get() == b; |
| 166 | } | 377 | } |
| 167 | 378 | ||
| 379 | /** | ||
| 380 | * @method operator== | ||
| 381 | * @brief operator == | ||
| 382 | * @param a pointer to compare | ||
| 383 | * @param b shared_ptr to compare | ||
| 384 | * @return true if b contains pointer a, false otherwise. | ||
| 385 | * @globalvars none | ||
| 386 | * @exception none | ||
| 387 | * @pre none | ||
| 388 | * @post none | ||
| 389 | */ | ||
| 168 | template<typename U1, typename U2> | 390 | template<typename U1, typename U2> |
| 169 | bool operator==(const U1* a, const shared_ptr<U2>& b) | 391 | bool operator==(const U1* a, const shared_ptr<U2>& b) |
| 170 | { | 392 | { |
| 171 | return a == b.get(); | 393 | return a == b.get(); |
| 172 | } | 394 | } |
| 173 | 395 | ||
| 396 | /** | ||
| 397 | * @method operator!= | ||
| 398 | * @brief operator != | ||
| 399 | * @param a shared_ptr to compare | ||
| 400 | * @param b pointer to compare | ||
| 401 | * @return true if a contains different pointer than b, false otherwise. | ||
| 402 | * @globalvars none | ||
| 403 | * @exception none | ||
| 404 | * @pre none | ||
| 405 | * @post none | ||
| 406 | */ | ||
| 174 | template<typename U1, typename U2> | 407 | template<typename U1, typename U2> |
| 175 | bool operator!=(const shared_ptr<U1>& a, const U2* b) | 408 | bool operator!=(const shared_ptr<U1>& a, const U2* b) |
| 176 | { | 409 | { |
| 177 | return a.get() != b; | 410 | return a.get() != b; |
| 178 | } | 411 | } |
| 179 | 412 | ||
| 413 | /** | ||
| 414 | * @method operator!= | ||
| 415 | * @brief operator != | ||
| 416 | * @param a pointer to compare | ||
| 417 | * @param b shared_ptr to compare | ||
| 418 | * @return true if b contains different pointer than a, false otherwise. | ||
| 419 | * @globalvars none | ||
| 420 | * @exception none | ||
| 421 | * @pre none | ||
| 422 | * @post none | ||
| 423 | */ | ||
| 180 | template<typename U1, typename U2> | 424 | template<typename U1, typename U2> |
| 181 | bool operator!=(const U1* a, const shared_ptr<U2>& b) | 425 | bool operator!=(const U1* a, const shared_ptr<U2>& b) |
| 182 | { | 426 | { |
| 183 | return a != b.get(); | 427 | return a != b.get(); |
| 184 | } | 428 | } |
| 185 | 429 | ||
| 430 | /** | ||
| 431 | * @method operator!= | ||
| 432 | * @brief operator != | ||
| 433 | * @param a object1 to compare | ||
| 434 | * @param b object2 to compare | ||
| 435 | * @return true if both objects contain different pointers, false otherwise. | ||
| 436 | * @globalvars none | ||
| 437 | * @exception none | ||
| 438 | * @pre none | ||
| 439 | * @post none | ||
| 440 | */ | ||
| 186 | template<typename U1, typename U2> | 441 | template<typename U1, typename U2> |
| 187 | bool operator!=(const shared_ptr<U1>& a, const shared_ptr<U2>& b) | 442 | bool operator!=(const shared_ptr<U1>& a, const shared_ptr<U2>& b) |
| 188 | { | 443 | { |
| 189 | return a.get() != b.get(); | 444 | return a.get() != b.get(); |
| 190 | } | 445 | } |
| 191 | 446 | ||
| 447 | /** | ||
| 448 | * @method shared_dynamic_cast | ||
| 449 | * @brief Converts pointer from F to T by using dynamic_cast. | ||
| 450 | * @param from object to cast | ||
| 451 | * @return When dynamic_cast<T*>(from.get()) returns a nonzero value, | ||
| 452 | * a shared_ptr<T> object that stores a copy of it and shares | ||
| 453 | * ownership with from - otherwise an empty shared_ptr<T> object. | ||
| 454 | * @globalvars none | ||
| 455 | * @exception none | ||
| 456 | * @pre The expression shared_dynamic_cast<T*>(from.get()) shall be well | ||
| 457 | * formed and shall have well defined behavior. | ||
| 458 | * @post w.get() == dynamic_cast<T*>(from.get()), where w is the return value. | ||
| 459 | */ | ||
| 192 | template <typename T, typename F> | 460 | template <typename T, typename F> |
| 193 | shared_ptr<T> shared_dynamic_cast(const shared_ptr<F>& from) | 461 | shared_ptr<T> shared_dynamic_cast(const shared_ptr<F>& from) |
| 194 | { | 462 | { |
| @@ -203,6 +471,18 @@ namespace Ti | |||
| 203 | namespace std | 471 | namespace std |
| 204 | { | 472 | { |
| 205 | using namespace Ti; | 473 | using namespace Ti; |
| 474 | |||
| 475 | /** | ||
| 476 | * @method swap | ||
| 477 | * @brief Exchanges the contents of t1 and t2. | ||
| 478 | * @param t1 instance#1 of shared_ptr | ||
| 479 | * @param t2 instance#2 of shared_ptr | ||
| 480 | * @return - | ||
| 481 | * @globalvars none | ||
| 482 | * @exception none | ||
| 483 | * @pre none | ||
| 484 | * @post none | ||
| 485 | */ | ||
| 206 | template <typename T> | 486 | template <typename T> |
| 207 | inline void swap(shared_ptr<T>& t1, shared_ptr<T>& t2) | 487 | inline void swap(shared_ptr<T>& t1, shared_ptr<T>& t2) |
| 208 | { | 488 | { |
