]> git.sesse.net Git - casparcg/blob - common/memory/safe_ptr.h
-
[casparcg] / common / memory / safe_ptr.h
1 #pragma once\r
2 \r
3 #include <memory>\r
4 #include <type_traits>\r
5 #include <exception>\r
6 \r
7 #include <tbb/spin_mutex.h>\r
8 \r
9 namespace caspar {\r
10         \r
11 template<typename T>\r
12 class safe_ptr\r
13 {       \r
14         std::shared_ptr<T> impl_;\r
15         tbb::spin_mutex mutex_;\r
16         template <typename> friend class safe_ptr;\r
17 public:\r
18         typedef T element_type;\r
19         \r
20         safe_ptr() : impl_(std::make_shared<T>()){}     \r
21         \r
22         safe_ptr(const safe_ptr<T>& other) : impl_(other.impl_){}  // noexcept\r
23         safe_ptr(safe_ptr<T>&& other) : impl_(std::move(other.impl_)){}\r
24 \r
25         template<typename U>\r
26         safe_ptr(const safe_ptr<U>& other, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(other.impl_){}  // noexcept\r
27                 \r
28         template<typename U>    \r
29         safe_ptr(const U& impl, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)\r
30                 : impl_(std::make_shared<U>(impl)) {}\r
31         \r
32         template<typename U, typename D>                \r
33         safe_ptr(const U& impl, D dtor, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)\r
34                 : impl_(new U(impl), dtor) {}\r
35 \r
36         template<typename U>    \r
37         safe_ptr(U&& impl, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)\r
38                 : impl_(std::make_shared<U>(std::forward<U>(impl))) {}\r
39 \r
40         template<typename U, typename D>        \r
41         safe_ptr(U&& impl, D dtor, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)\r
42                 : impl_(new U(std::forward<U>(impl)), dtor) {}\r
43                         \r
44         template<typename U>    \r
45         explicit safe_ptr(const std::shared_ptr<U>& impl, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(impl)\r
46         {\r
47                 if(!impl_)\r
48                         throw std::invalid_argument("impl");\r
49         }\r
50         \r
51         template<typename U>    \r
52         explicit safe_ptr(std::shared_ptr<U>&& impl, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(std::move(impl))\r
53         {\r
54                 if(!impl_)\r
55                         throw std::invalid_argument("impl");\r
56         }\r
57 \r
58         template<typename U>    \r
59         explicit safe_ptr(U* impl, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(impl)\r
60         {\r
61                 if(!impl_)\r
62                         throw std::invalid_argument("impl");\r
63         }\r
64 \r
65         template<typename U, typename D>        \r
66         explicit safe_ptr(U* impl, D dtor, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(impl, dtor)\r
67         {\r
68                 if(!impl_)\r
69                         throw std::invalid_argument("impl");\r
70         }\r
71 \r
72         template<typename U>\r
73         typename std::enable_if<std::is_convertible<U*, T*>::value, safe_ptr<T>&>::type\r
74         operator=(const safe_ptr<U>& other)\r
75         {\r
76                 safe_ptr<T> temp(other);\r
77                 temp.swap(*this);\r
78                 return *this;\r
79         }\r
80 \r
81         template <typename U>\r
82         typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, safe_ptr<T>&>::type\r
83         operator=(U&& impl)\r
84         {\r
85                 safe_ptr<T> temp(std::forward<T>(impl));\r
86                 temp.swap(*this);\r
87                 return *this;\r
88         }\r
89 \r
90         T& operator*() const { return *impl_.get();}  // noexcept\r
91 \r
92         T* operator->() const { return impl_.get();}  // noexcept\r
93 \r
94         T* get() const { return impl_.get();}  // noexcept\r
95 \r
96         bool unique() const { return impl_.unique();}  // noexcept\r
97 \r
98         long use_count() const { return impl_.use_count();}  // noexcept\r
99                                 \r
100         void swap(safe_ptr& other) { impl_.swap(other.impl_); }  // noexcept\r
101         \r
102         operator const std::shared_ptr<T>&() const { return impl_;}  // noexcept\r
103 \r
104         template<class U>\r
105         bool owner_before(const safe_ptr<T>& ptr){ return impl_.owner_before(ptr.impl_); }  // noexcept\r
106 \r
107         template<class U>\r
108         bool owner_before(const std::shared_ptr<U>& ptr){ return impl_.owner_before(ptr); }  // noexcept\r
109         \r
110         template<class D, class U> \r
111         D* get_deleter(safe_ptr<U> const& ptr) { return impl_.get_deleter(); }  // noexcept\r
112 };\r
113 \r
114 template<class T, class U>\r
115 bool operator==(const safe_ptr<T>& a, const safe_ptr<U>& b)  // noexcept\r
116 {\r
117         return a.get() == b.get();\r
118 }\r
119 \r
120 template<class T, class U>\r
121 bool operator!=(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept\r
122 {\r
123         return a.get() != b.get();\r
124 }\r
125 \r
126 template<class T, class U>\r
127 bool operator<(const safe_ptr<T>& a, const safe_ptr<U>& b)  // noexcept\r
128 {\r
129         return a.get() < b.get();\r
130 }\r
131 \r
132 template<class T, class U>\r
133 bool operator>(const safe_ptr<T>& a, const safe_ptr<U>& b)  // noexcept\r
134 {\r
135         return a.get() > b.get();\r
136 }\r
137 \r
138 template<class T, class U>\r
139 bool operator>=(const safe_ptr<T>& a, const safe_ptr<U>& b)  // noexcept\r
140 {\r
141         return a.get() >= b.get();\r
142 }\r
143 \r
144 template<class T, class U>\r
145 bool operator<=(const safe_ptr<T>& a, const safe_ptr<U>& b)  // noexcept\r
146 {\r
147         return a.get() <= b.get();\r
148 }\r
149 \r
150 template<class E, class T, class U>\r
151 std::basic_ostream<E, T>& operator<<(std::basic_ostream<E, T>& out,     const safe_ptr<U>& p)\r
152 {\r
153         return out << p.get();\r
154 }\r
155 \r
156 template<class T> \r
157 void swap(safe_ptr<T>& a, safe_ptr<T>& b)  // noexcept\r
158 {\r
159         a.swap(b);\r
160 }\r
161 \r
162 template<class T> \r
163 T* get_pointer(safe_ptr<T> const& p)  // noexcept\r
164 {\r
165         return p.get();\r
166 }\r
167 \r
168 template <class T, class U>\r
169 safe_ptr<T> static_pointer_cast(const safe_ptr<U>& p)  // noexcept\r
170 {\r
171         return safe_ptr<T>(std::static_pointer_cast<T>(std::shared_ptr<U>(p)));\r
172 }\r
173 \r
174 template <class T, class U>\r
175 safe_ptr<T> const_pointer_cast(const safe_ptr<U>& p)  // noexcept\r
176 {\r
177         return safe_ptr<T>(std::const_pointer_cast<T>(std::shared_ptr<U>(p)));\r
178 }\r
179 \r
180 template <class T, class U>\r
181 safe_ptr<T> dynamic_pointer_cast(const safe_ptr<U>& p)\r
182 {\r
183         auto temp = std::dynamic_pointer_cast<T>(std::shared_ptr<U>(p));\r
184         if(!temp)\r
185                 throw std::bad_cast();\r
186         return safe_ptr<T>(temp);\r
187 }\r
188 \r
189 template<typename T>\r
190 safe_ptr<T> make_safe()\r
191 {\r
192         return safe_ptr<T>();\r
193 }\r
194 \r
195 template<typename T, typename P0>\r
196 safe_ptr<T> make_safe(P0&& p0)\r
197 {\r
198         return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0)));\r
199 }\r
200 \r
201 template<typename T, typename P0, typename P1>\r
202 safe_ptr<T> make_safe(P0&& p0, P1&& p1)\r
203 {\r
204         return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1)));\r
205 }\r
206 \r
207 template<typename T, typename P0, typename P1, typename P2>\r
208 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2)\r
209 {\r
210         return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2)));\r
211 }\r
212 \r
213 template<typename T, typename P0, typename P1, typename P2, typename P3>\r
214 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3)\r
215 {\r
216         return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2), std::forward<P3>(p3)));\r
217 }\r
218 \r
219 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4>\r
220 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&&)\r
221 {\r
222         return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2), std::forward<P3>(p3), std::forward<P3>(p4)));\r
223 }\r
224 \r
225 }