]> git.sesse.net Git - casparcg/blob - common/memory/safe_ptr.h
e4269e69bd2166f96f984fb8287e930b77aafa7a
[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<T>& other) : impl_(other.impl_){}  // noexcept\r
39         safe_ptr(safe_ptr<T>&& 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<U*, T*>::value, safe_ptr<T>&>::type\r
90         operator=(const safe_ptr<U>& other)\r
91         {\r
92                 safe_ptr<T> temp(other);\r
93                 temp.swap(*this);\r
94                 return *this;\r
95         }\r
96 \r
97         template <typename U>\r
98         typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, safe_ptr<T>&>::type\r
99         operator=(U&& impl)\r
100         {\r
101                 safe_ptr<T> temp(std::forward<T>(impl));\r
102                 temp.swap(*this);\r
103                 return *this;\r
104         }\r
105 \r
106         T& operator*() const // noexcept\r
107         {\r
108                 return *impl_.get();\r
109         } \r
110 \r
111         T* operator->() const // noexcept\r
112         {\r
113                 return impl_.get();\r
114         } \r
115 \r
116         T* get() const // noexcept\r
117         {\r
118                 return impl_.get();\r
119         }  \r
120 \r
121         bool unique() const { return impl_.unique();}  // noexcept\r
122 \r
123         long use_count() const { return impl_.use_count();}  // noexcept\r
124                                 \r
125         void swap(safe_ptr& other) { impl_.swap(other.impl_); }  // noexcept\r
126         \r
127         operator const std::shared_ptr<T>&() const { return impl_;}  // noexcept\r
128 \r
129         template<class U>\r
130         bool owner_before(const safe_ptr<T>& ptr){ return impl_.owner_before(ptr.impl_); }  // noexcept\r
131 \r
132         template<class U>\r
133         bool owner_before(const std::shared_ptr<U>& ptr){ return impl_.owner_before(ptr); }  // noexcept\r
134         \r
135         template<class D, class U> \r
136         D* get_deleter(safe_ptr<U> const& ptr) { return impl_.get_deleter(); }  // noexcept\r
137 };\r
138 \r
139 template<class T, class U>\r
140 bool operator==(const std::shared_ptr<T>& a, const safe_ptr<U>& b)  // noexcept\r
141 {\r
142         return a.get() == b.get();\r
143 }\r
144 \r
145 template<class T, class U>\r
146 bool operator==(const safe_ptr<T>& a, const std::shared_ptr<U>& b)  // noexcept\r
147 {\r
148         return a.get() == b.get();\r
149 }\r
150 \r
151 template<class T, class U>\r
152 bool operator==(const safe_ptr<T>& a, const safe_ptr<U>& b)  // noexcept\r
153 {\r
154         return a.get() == b.get();\r
155 }\r
156 \r
157 template<class T, class U>\r
158 bool operator!=(const std::shared_ptr<T>& a, const safe_ptr<U>& b)  // noexcept\r
159 {\r
160         return a.get() != b.get();\r
161 }\r
162 \r
163 template<class T, class U>\r
164 bool operator!=(const safe_ptr<T>& a, const std::shared_ptr<U>& b)  // noexcept\r
165 {\r
166         return a.get() != b.get();\r
167 }\r
168 \r
169 template<class T, class U>\r
170 bool operator!=(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept\r
171 {\r
172         return a.get() != b.get();\r
173 }\r
174 \r
175 template<class T, class U>\r
176 bool operator<(const safe_ptr<T>& a, const safe_ptr<U>& b)  // noexcept\r
177 {\r
178         return a.get() < b.get();\r
179 }\r
180 \r
181 template<class T, class U>\r
182 bool operator>(const safe_ptr<T>& a, const safe_ptr<U>& b)  // noexcept\r
183 {\r
184         return a.get() > b.get();\r
185 }\r
186 \r
187 template<class T, class U>\r
188 bool operator>=(const safe_ptr<T>& a, const safe_ptr<U>& b)  // noexcept\r
189 {\r
190         return a.get() >= b.get();\r
191 }\r
192 \r
193 template<class T, class U>\r
194 bool operator<=(const safe_ptr<T>& a, const safe_ptr<U>& b)  // noexcept\r
195 {\r
196         return a.get() <= b.get();\r
197 }\r
198 \r
199 template<class E, class T, class U>\r
200 std::basic_ostream<E, T>& operator<<(std::basic_ostream<E, T>& out,     const safe_ptr<U>& p)\r
201 {\r
202         return out << p.get();\r
203 }\r
204 \r
205 template<class T> \r
206 void swap(safe_ptr<T>& a, safe_ptr<T>& b)  // noexcept\r
207 {\r
208         a.swap(b);\r
209 }\r
210 \r
211 template<class T> \r
212 T* get_pointer(safe_ptr<T> const& p)  // noexcept\r
213 {\r
214         return p.get();\r
215 }\r
216 \r
217 template <class T, class U>\r
218 safe_ptr<T> static_pointer_cast(const safe_ptr<U>& p)  // noexcept\r
219 {\r
220         return safe_ptr<T>(std::static_pointer_cast<T>(std::shared_ptr<U>(p)));\r
221 }\r
222 \r
223 template <class T, class U>\r
224 safe_ptr<T> const_pointer_cast(const safe_ptr<U>& p)  // noexcept\r
225 {\r
226         return safe_ptr<T>(std::const_pointer_cast<T>(std::shared_ptr<U>(p)));\r
227 }\r
228 \r
229 template <class T, class U>\r
230 safe_ptr<T> dynamic_pointer_cast(const safe_ptr<U>& p)\r
231 {\r
232         auto temp = std::dynamic_pointer_cast<T>(std::shared_ptr<U>(p));\r
233         if(!temp)\r
234                 throw std::bad_cast();\r
235         return safe_ptr<T>(temp);\r
236 }\r
237 \r
238 template<typename T>\r
239 safe_ptr<T> make_safe(const std::shared_ptr<T>& ptr)\r
240 {\r
241         return safe_ptr<T>(ptr);\r
242 }\r
243 \r
244 template<typename T>\r
245 safe_ptr<T> make_safe(std::shared_ptr<T>&& ptr)\r
246 {\r
247         return safe_ptr<T>(std::move(ptr));\r
248 }\r
249 \r
250 template<typename T>\r
251 safe_ptr<T> make_safe()\r
252 {\r
253         return safe_ptr<T>();\r
254 }\r
255 \r
256 template<typename T, typename P0>\r
257 safe_ptr<T> make_safe(P0&& p0)\r
258 {\r
259         return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0)));\r
260 }\r
261 \r
262 template<typename T, typename P0, typename P1>\r
263 safe_ptr<T> make_safe(P0&& p0, P1&& p1)\r
264 {\r
265         return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1)));\r
266 }\r
267 \r
268 template<typename T, typename P0, typename P1, typename P2>\r
269 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2)\r
270 {\r
271         return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2)));\r
272 }\r
273 \r
274 template<typename T, typename P0, typename P1, typename P2, typename P3>\r
275 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3)\r
276 {\r
277         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
278 }\r
279 \r
280 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4>\r
281 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4)\r
282 {\r
283         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
284 }\r
285 \r
286 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5>\r
287 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5)\r
288 {\r
289         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
290 }\r
291 \r
292 }