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
23#ifndef STDEXT_SHARED_OBJECT_H
24#define STDEXT_SHARED_OBJECT_H
25
26#include "types.h"
27#include <type_traits>
28#include <functional>
29#include <ostream>
30#include <atomic>
31
32namespace stdext {
33
34template<class T>
35class shared_object_ptr;
36
37class shared_object
38{
39public:
40 shared_object() : refs(0) { }
41 virtual ~shared_object() { }
42 void add_ref() { ++refs; }
43 void dec_ref() { if(--refs == 0) delete this; }
44 refcount_t ref_count() { return refs; }
45
46 template<typename T> stdext::shared_object_ptr<T> static_self_cast() { return stdext::shared_object_ptr<T>(static_cast<T*>(this)); }
47 template<typename T> stdext::shared_object_ptr<T> dynamic_self_cast() { return stdext::shared_object_ptr<T>(dynamic_cast<T*>(this)); }
48 template<typename T> stdext::shared_object_ptr<T> const_self_cast() { return stdext::shared_object_ptr<T>(const_cast<T*>(this)); }
49
50private:
51 std::atomic<refcount_t> refs;
52};
53
54template<class T>
55class shared_object_ptr
56{
57public:
58 typedef T element_type;
59
60 shared_object_ptr(): px(nullptr) { }
61 shared_object_ptr(T* p, bool add_ref = true) : px(p) {
62 static_assert(std::is_base_of<shared_object, T>::value, "classes using shared_object_ptr must be a derived of stdext::shared_object");
63 if(px != nullptr && add_ref)
64 this->add_ref();
65 }
66 shared_object_ptr(shared_object_ptr const& rhs): px(rhs.px) { if(px != nullptr) add_ref(); }
67 template<class U>
68 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(); }
69 ~shared_object_ptr() { if(px != nullptr) dec_ref(); }
70
71 void reset() { shared_object_ptr().swap(*this); }
72 void reset(T* rhs) { shared_object_ptr(rhs).swap(*this); }
73 void swap(shared_object_ptr& rhs) { std::swap(px, rhs.px); }
74 T* get() const { return px; }
75
76 refcount_t use_count() const { return ((shared_object*)px)->ref_count(); }
77 bool is_unique() const { return use_count() == 1; }
78
79 template<class U> shared_object_ptr& operator=(shared_object_ptr<U> const& rhs) { shared_object_ptr(rhs).swap(*this); return *this; }
80
81 T& operator*() const { VALIDATE(px != nullptr); return *px; }
82 T* operator->() const { VALIDATE(px != nullptr); return px; }
83
84 shared_object_ptr& operator=(shared_object_ptr const& rhs) { shared_object_ptr(rhs).swap(*this); return *this; }
85 shared_object_ptr& operator=(T* rhs) { shared_object_ptr(rhs).swap(*this); return *this; }
86
87 // implicit conversion to bool
88 typedef T* shared_object_ptr::*unspecified_bool_type;
89 operator unspecified_bool_type() const { return px == nullptr ? nullptr : &shared_object_ptr::px; }
90 bool operator!() const { return px == nullptr; }
91
92 // std::move support
93 shared_object_ptr(shared_object_ptr&& rhs): px(rhs.px) { rhs.px = nullptr; }
94 shared_object_ptr& operator=(shared_object_ptr&& rhs) { shared_object_ptr(static_cast<shared_object_ptr&&>(rhs)).swap(*this); return *this; }
95
96private:
97 void add_ref() { ((shared_object*)px)->add_ref(); }
98 void dec_ref() { ((shared_object*)px)->dec_ref(); }
99
100 T* px;
101};
102
103template<class T, class U> bool operator==(shared_object_ptr<T> const& a, shared_object_ptr<U> const& b) { return a.get() == b.get(); }
104template<class T, class U> bool operator!=(shared_object_ptr<T> const& a, shared_object_ptr<U> const& b) { return a.get() != b.get(); }
105template<class T, class U> bool operator==(shared_object_ptr<T> const& a, U* b) { return a.get() == b; }
106template<class T, class U> bool operator!=(shared_object_ptr<T> const& a, U* b) { return a.get() != b; }
107template<class T, class U> bool operator==(T * a, shared_object_ptr<U> const& b) { return a == b.get(); }
108template<class T, class U> bool operator!=(T * a, shared_object_ptr<U> const& b) { return a != b.get(); }
109template<class T> bool operator<(shared_object_ptr<T> const& a, shared_object_ptr<T> const& b) { return std::less<T*>()(a.get(), b.get()); }
110
111template<class T> T* get_pointer(shared_object_ptr<T> const& p) { return p.get(); }
112template<class T, class U> shared_object_ptr<T> static_pointer_cast(shared_object_ptr<U> const& p) { return static_cast<T*>(p.get()); }
113template<class T, class U> shared_object_ptr<T> const_pointer_cast(shared_object_ptr<U> const& p) { return const_cast<T*>(p.get()); }
114template<class T, class U> shared_object_ptr<T> dynamic_pointer_cast(shared_object_ptr<U> const& p) { return dynamic_cast<T*>(p.get()); }
115template<class T, typename... Args> stdext::shared_object_ptr<T> make_shared_object(Args... args) { return stdext::shared_object_ptr<T>(new T(args...)); }
116
117// operator<< support
118template<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; }
119
120}
121
122namespace std {
123
124// hash, for unordered_map support
125template<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()); } };
126
127// swap support
128template<class T> void swap(stdext::shared_object_ptr<T>& lhs, stdext::shared_object_ptr<T>& rhs) { lhs.swap(rhs); }
129
130}
131
132#endif
133