No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

shared_object.h 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2010-2017 OTClient <https://github.com/edubart/otclient>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #ifndef STDEXT_SHARED_OBJECT_H
  23. #define STDEXT_SHARED_OBJECT_H
  24. #include "types.h"
  25. #include <type_traits>
  26. #include <functional>
  27. #include <cassert>
  28. #include <ostream>
  29. #ifdef THREAD_SAFE
  30. #include <atomic>
  31. #endif
  32. namespace stdext {
  33. template<class T>
  34. class shared_object_ptr;
  35. class shared_object
  36. {
  37. public:
  38. shared_object() : refs(0) { }
  39. virtual ~shared_object() { }
  40. void add_ref() { ++refs; }
  41. void dec_ref() { if(--refs == 0) delete this; }
  42. refcount_t ref_count() { return refs; }
  43. template<typename T> stdext::shared_object_ptr<T> static_self_cast() { return stdext::shared_object_ptr<T>(static_cast<T*>(this)); }
  44. template<typename T> stdext::shared_object_ptr<T> dynamic_self_cast() { return stdext::shared_object_ptr<T>(dynamic_cast<T*>(this)); }
  45. template<typename T> stdext::shared_object_ptr<T> const_self_cast() { return stdext::shared_object_ptr<T>(const_cast<T*>(this)); }
  46. private:
  47. #ifdef THREAD_SAFE
  48. std::atomic<refcount_t> refs;
  49. #else
  50. refcount_t refs;
  51. #endif
  52. };
  53. template<class T>
  54. class shared_object_ptr
  55. {
  56. public:
  57. typedef T element_type;
  58. shared_object_ptr(): px(nullptr) { }
  59. shared_object_ptr(T* p, bool add_ref = true) : px(p) {
  60. static_assert(std::is_base_of<shared_object, T>::value, "classes using shared_object_ptr must be a derived of stdext::shared_object");
  61. if(px != nullptr && add_ref)
  62. this->add_ref();
  63. }
  64. shared_object_ptr(shared_object_ptr const& rhs): px(rhs.px) { if(px != nullptr) add_ref(); }
  65. template<class U>
  66. shared_object_ptr(shared_object_ptr<U> const& rhs, typename std::enable_if<std::is_convertible<U*,T*>::value, U*>::type = nullptr) : px(rhs.get()) { if(px != nullptr) add_ref(); }
  67. ~shared_object_ptr() { if(px != nullptr) dec_ref(); }
  68. void reset() { shared_object_ptr().swap(*this); }
  69. void reset(T* rhs) { shared_object_ptr(rhs).swap(*this); }
  70. void swap(shared_object_ptr& rhs) { std::swap(px, rhs.px); }
  71. T* get() const { return px; }
  72. refcount_t use_count() const { return ((shared_object*)px)->ref_count(); }
  73. bool is_unique() const { return use_count() == 1; }
  74. template<class U> shared_object_ptr& operator=(shared_object_ptr<U> const& rhs) { shared_object_ptr(rhs).swap(*this); return *this; }
  75. T& operator*() const { assert(px != nullptr); return *px; }
  76. T* operator->() const { assert(px != nullptr); return px; }
  77. shared_object_ptr& operator=(shared_object_ptr const& rhs) { shared_object_ptr(rhs).swap(*this); return *this; }
  78. shared_object_ptr& operator=(T* rhs) { shared_object_ptr(rhs).swap(*this); return *this; }
  79. // implicit conversion to bool
  80. typedef T* shared_object_ptr::*unspecified_bool_type;
  81. operator unspecified_bool_type() const { return px == nullptr ? nullptr : &shared_object_ptr::px; }
  82. bool operator!() const { return px == nullptr; }
  83. // std::move support
  84. shared_object_ptr(shared_object_ptr&& rhs): px(rhs.px) { rhs.px = nullptr; }
  85. shared_object_ptr& operator=(shared_object_ptr&& rhs) { shared_object_ptr(static_cast<shared_object_ptr&&>(rhs)).swap(*this); return *this; }
  86. private:
  87. void add_ref() { ((shared_object*)px)->add_ref(); }
  88. void dec_ref() { ((shared_object*)px)->dec_ref(); }
  89. T* px;
  90. };
  91. template<class T, class U> bool operator==(shared_object_ptr<T> const& a, shared_object_ptr<U> const& b) { return a.get() == b.get(); }
  92. template<class T, class U> bool operator!=(shared_object_ptr<T> const& a, shared_object_ptr<U> const& b) { return a.get() != b.get(); }
  93. template<class T, class U> bool operator==(shared_object_ptr<T> const& a, U* b) { return a.get() == b; }
  94. template<class T, class U> bool operator!=(shared_object_ptr<T> const& a, U* b) { return a.get() != b; }
  95. template<class T, class U> bool operator==(T * a, shared_object_ptr<U> const& b) { return a == b.get(); }
  96. template<class T, class U> bool operator!=(T * a, shared_object_ptr<U> const& b) { return a != b.get(); }
  97. template<class T> bool operator<(shared_object_ptr<T> const& a, shared_object_ptr<T> const& b) { return std::less<T*>()(a.get(), b.get()); }
  98. template<class T> T* get_pointer(shared_object_ptr<T> const& p) { return p.get(); }
  99. template<class T, class U> shared_object_ptr<T> static_pointer_cast(shared_object_ptr<U> const& p) { return static_cast<T*>(p.get()); }
  100. template<class T, class U> shared_object_ptr<T> const_pointer_cast(shared_object_ptr<U> const& p) { return const_cast<T*>(p.get()); }
  101. template<class T, class U> shared_object_ptr<T> dynamic_pointer_cast(shared_object_ptr<U> const& p) { return dynamic_cast<T*>(p.get()); }
  102. template<class T, typename... Args> stdext::shared_object_ptr<T> make_shared_object(Args... args) { return stdext::shared_object_ptr<T>(new T(args...)); }
  103. // operator<< support
  104. template<class E, class T, class Y> std::basic_ostream<E, T>& operator<<(std::basic_ostream<E, T>& os, shared_object_ptr<Y> const& p) { os << p.get(); return os; }
  105. }
  106. namespace std {
  107. // hash, for unordered_map support
  108. template<typename T> struct hash<stdext::shared_object_ptr<T>> { size_t operator()(const stdext::shared_object_ptr<T>& p) const { return std::hash<T*>()(p.get()); } };
  109. // swap support
  110. template<class T> void swap(stdext::shared_object_ptr<T>& lhs, stdext::shared_object_ptr<T>& rhs) { lhs.swap(rhs); }
  111. }
  112. #endif