]> git.sesse.net Git - casparcg/blob - common/memory/safe_ptr.h
2.0.0: Fixed potential pure virtual function call.
[casparcg] / common / memory / safe_ptr.h
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\r
6 *    CasparCG is free software: you can redistribute it and/or modify\r
7 *    it under the terms of the GNU General Public License as published by\r
8 *    the Free Software Foundation, either version 3 of the License, or\r
9 *    (at your option) any later version.\r
10 *\r
11 *    CasparCG is distributed in the hope that it will be useful,\r
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 *    GNU General Public License for more details.\r
15 \r
16 *    You should have received a copy of the GNU General Public License\r
17 *    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 */\r
20 #pragma once\r
21 \r
22 #include <memory>\r
23 #include <type_traits>\r
24 #include <exception>\r
25 \r
26 namespace caspar {\r
27         \r
28 template<typename T>\r
29 class safe_ptr\r
30 {       \r
31         std::shared_ptr<T> impl_;\r
32         template <typename> friend class safe_ptr;\r
33 public:\r
34         typedef T element_type;\r
35         \r
36         safe_ptr() : impl_(std::make_shared<T>()){}     \r
37         \r
38         safe_ptr(const safe_ptr& other) : impl_(other.impl_){}  // noexcept\r
39         safe_ptr(safe_ptr&& other) : impl_(std::move(other.impl_)){}\r
40 \r
41         template<typename U>\r
42         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
43                 \r
44         template<typename U>    \r
45         safe_ptr(const U& impl, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)\r
46                 : impl_(std::make_shared<U>(impl)) {}\r
47         \r
48         template<typename U, typename D>                \r
49         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
50                 : impl_(new U(impl), dtor) {}\r
51 \r
52         template<typename U>    \r
53         safe_ptr(U&& impl, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)\r
54                 : impl_(std::make_shared<U>(std::forward<U>(impl))) {}\r
55 \r
56         template<typename U, typename D>        \r
57         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
58                 : impl_(new U(std::forward<U>(impl)), dtor) {}\r
59                         \r
60         template<typename U>    \r
61         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
62         {\r
63                 if(!impl_)\r
64                         throw std::invalid_argument("impl");\r
65         }\r
66         \r
67         template<typename U>    \r
68         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
69         {\r
70                 if(!impl_)\r
71                         throw std::invalid_argument("impl");\r
72         }\r
73 \r
74         template<typename U>    \r
75         explicit safe_ptr(U* impl, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(impl)\r
76         {\r
77                 if(!impl_)\r
78                         throw std::invalid_argument("impl");\r
79         }\r
80 \r
81         template<typename U, typename D>        \r
82         explicit safe_ptr(U* impl, D dtor, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(impl, dtor)\r
83         {\r
84                 if(!impl_)\r
85                         throw std::invalid_argument("impl");\r
86         }\r
87 \r
88         template<typename U>\r
89         typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, safe_ptr&>::type\r
90         operator=(const safe_ptr<U>& other)\r
91         {\r
92                 safe_ptr(other).swap(*this);\r
93                 return *this;\r
94         }\r
95 \r
96         template<typename U>\r
97         typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, safe_ptr&>::type\r
98         operator=(safe_ptr<U>&& other)\r
99         {\r
100                 safe_ptr(std::move(other)).swap(*this);\r
101                 return *this;\r
102         }\r
103 \r
104         template <typename U>\r
105         typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, safe_ptr&>::type\r
106         operator=(U&& impl)\r
107         {\r
108                 safe_ptr temp(std::forward<T>(impl));\r
109                 temp.swap(*this);\r
110                 return *this;\r
111         }\r
112 \r
113         T& operator*() const // noexcept\r
114         {\r
115                 return *impl_.get();\r
116         } \r
117 \r
118         T* operator->() const // noexcept\r
119         {\r
120                 return impl_.get();\r
121         } \r
122 \r
123         T* get() const // noexcept\r
124         {\r
125                 return impl_.get();\r
126         }  \r
127 \r
128         bool unique() const { return impl_.unique();}  // noexcept\r
129 \r
130         long use_count() const { return impl_.use_count();}  // noexcept\r
131                                 \r
132         void swap(safe_ptr& other) { impl_.swap(other.impl_); }  // noexcept\r
133         \r
134         operator const std::shared_ptr<T>&() const { return impl_;}  // noexcept\r
135 \r
136         template<class U>\r
137         bool owner_before(const safe_ptr& ptr){ return impl_.owner_before(ptr.impl_); }  // noexcept\r
138 \r
139         template<class U>\r
140         bool owner_before(const std::shared_ptr<U>& ptr){ return impl_.owner_before(ptr); }  // noexcept\r
141         \r
142         template<class D, class U> \r
143         D* get_deleter(safe_ptr<U> const& ptr) { return impl_.get_deleter(); }  // noexcept\r
144 };\r
145 \r
146 template<class T, class U>\r
147 bool operator==(const std::shared_ptr<T>& a, const safe_ptr<U>& b)  // noexcept\r
148 {\r
149         return a.get() == b.get();\r
150 }\r
151 \r
152 template<class T, class U>\r
153 bool operator==(const safe_ptr<T>& a, const std::shared_ptr<U>& b)  // noexcept\r
154 {\r
155         return a.get() == b.get();\r
156 }\r
157 \r
158 template<class T, class U>\r
159 bool operator==(const safe_ptr<T>& a, const safe_ptr<U>& b)  // noexcept\r
160 {\r
161         return a.get() == b.get();\r
162 }\r
163 \r
164 template<class T, class U>\r
165 bool operator!=(const std::shared_ptr<T>& a, const safe_ptr<U>& b)  // noexcept\r
166 {\r
167         return a.get() != b.get();\r
168 }\r
169 \r
170 template<class T, class U>\r
171 bool operator!=(const safe_ptr<T>& a, const std::shared_ptr<U>& b)  // noexcept\r
172 {\r
173         return a.get() != b.get();\r
174 }\r
175 \r
176 template<class T, class U>\r
177 bool operator!=(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept\r
178 {\r
179         return a.get() != b.get();\r
180 }\r
181 \r
182 template<class T, class U>\r
183 bool operator<(const safe_ptr<T>& a, const safe_ptr<U>& b)  // noexcept\r
184 {\r
185         return a.get() < b.get();\r
186 }\r
187 \r
188 template<class T, class U>\r
189 bool operator>(const safe_ptr<T>& a, const safe_ptr<U>& b)  // noexcept\r
190 {\r
191         return a.get() > b.get();\r
192 }\r
193 \r
194 template<class T, class U>\r
195 bool operator>=(const safe_ptr<T>& a, const safe_ptr<U>& b)  // noexcept\r
196 {\r
197         return a.get() >= b.get();\r
198 }\r
199 \r
200 template<class T, class U>\r
201 bool operator<=(const safe_ptr<T>& a, const safe_ptr<U>& b)  // noexcept\r
202 {\r
203         return a.get() <= b.get();\r
204 }\r
205 \r
206 template<class E, class T, class U>\r
207 std::basic_ostream<E, T>& operator<<(std::basic_ostream<E, T>& out,     const safe_ptr<U>& p)\r
208 {\r
209         return out << p.get();\r
210 }\r
211 \r
212 template<class T> \r
213 void swap(safe_ptr<T>& a, safe_ptr<T>& b)  // noexcept\r
214 {\r
215         a.swap(b);\r
216 }\r
217 \r
218 template<class T> \r
219 T* get_pointer(safe_ptr<T> const& p)  // noexcept\r
220 {\r
221         return p.get();\r
222 }\r
223 \r
224 template <class T, class U>\r
225 safe_ptr<T> static_pointer_cast(const safe_ptr<U>& p)  // noexcept\r
226 {\r
227         return safe_ptr<T>(std::static_pointer_cast<T>(std::shared_ptr<U>(p)));\r
228 }\r
229 \r
230 template <class T, class U>\r
231 safe_ptr<T> const_pointer_cast(const safe_ptr<U>& p)  // noexcept\r
232 {\r
233         return safe_ptr<T>(std::const_pointer_cast<T>(std::shared_ptr<U>(p)));\r
234 }\r
235 \r
236 template <class T, class U>\r
237 safe_ptr<T> dynamic_pointer_cast(const safe_ptr<U>& p)\r
238 {\r
239         auto temp = std::dynamic_pointer_cast<T>(std::shared_ptr<U>(p));\r
240         if(!temp)\r
241                 throw std::bad_cast();\r
242         return safe_ptr<T>(temp);\r
243 }\r
244 \r
245 template<typename T>\r
246 safe_ptr<T> make_safe(const std::shared_ptr<T>& ptr)\r
247 {\r
248         return safe_ptr<T>(ptr);\r
249 }\r
250 \r
251 template<typename T>\r
252 safe_ptr<T> make_safe(std::shared_ptr<T>&& ptr)\r
253 {\r
254         return safe_ptr<T>(std::move(ptr));\r
255 }\r
256 \r
257 template<typename T>\r
258 safe_ptr<T> make_safe()\r
259 {\r
260         return safe_ptr<T>();\r
261 }\r
262 \r
263 template<typename T, typename P0>\r
264 safe_ptr<T> make_safe(P0&& p0)\r
265 {\r
266         return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0)));\r
267 }\r
268 \r
269 template<typename T, typename P0, typename P1>\r
270 safe_ptr<T> make_safe(P0&& p0, P1&& p1)\r
271 {\r
272         return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1)));\r
273 }\r
274 \r
275 template<typename T, typename P0, typename P1, typename P2>\r
276 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2)\r
277 {\r
278         return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2)));\r
279 }\r
280 \r
281 template<typename T, typename P0, typename P1, typename P2, typename P3>\r
282 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3)\r
283 {\r
284         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
285 }\r
286 \r
287 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4>\r
288 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4)\r
289 {\r
290         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<P4>(p4)));\r
291 }\r
292 \r
293 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5>\r
294 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5)\r
295 {\r
296         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<P4>(p4), std::forward<P5>(p5)));\r
297 }\r
298 \r
299 }