]> git.sesse.net Git - casparcg/blob - common/memory/safe_ptr.h
2.0.0.2: Simplified conversion between shared_ptr and safe_ptr through a make_safe...
[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 #include <tbb/spin_mutex.h>\r
27 \r
28 namespace caspar {\r
29         \r
30 template<typename T>\r
31 class safe_ptr\r
32 {       \r
33         std::shared_ptr<T> impl_;\r
34         tbb::spin_mutex mutex_;\r
35         template <typename> friend class safe_ptr;\r
36 public:\r
37         typedef T element_type;\r
38         \r
39         safe_ptr() : impl_(std::make_shared<T>()){}     \r
40         \r
41         safe_ptr(const safe_ptr<T>& other) : impl_(other.impl_){}  // noexcept\r
42         safe_ptr(safe_ptr<T>&& other) : impl_(std::move(other.impl_)){}\r
43 \r
44         template<typename U>\r
45         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
46                 \r
47         template<typename U>    \r
48         safe_ptr(const U& impl, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)\r
49                 : impl_(std::make_shared<U>(impl)) {}\r
50         \r
51         template<typename U, typename D>                \r
52         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
53                 : impl_(new U(impl), dtor) {}\r
54 \r
55         template<typename U>    \r
56         safe_ptr(U&& impl, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)\r
57                 : impl_(std::make_shared<U>(std::forward<U>(impl))) {}\r
58 \r
59         template<typename U, typename D>        \r
60         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
61                 : impl_(new U(std::forward<U>(impl)), dtor) {}\r
62                         \r
63         template<typename U>    \r
64         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
65         {\r
66                 if(!impl_)\r
67                         throw std::invalid_argument("impl");\r
68         }\r
69         \r
70         template<typename U>    \r
71         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
72         {\r
73                 if(!impl_)\r
74                         throw std::invalid_argument("impl");\r
75         }\r
76 \r
77         template<typename U>    \r
78         explicit safe_ptr(U* impl, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(impl)\r
79         {\r
80                 if(!impl_)\r
81                         throw std::invalid_argument("impl");\r
82         }\r
83 \r
84         template<typename U, typename D>        \r
85         explicit safe_ptr(U* impl, D dtor, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(impl, dtor)\r
86         {\r
87                 if(!impl_)\r
88                         throw std::invalid_argument("impl");\r
89         }\r
90 \r
91         template<typename U>\r
92         typename std::enable_if<std::is_convertible<U*, T*>::value, safe_ptr<T>&>::type\r
93         operator=(const safe_ptr<U>& other)\r
94         {\r
95                 safe_ptr<T> temp(other);\r
96                 temp.swap(*this);\r
97                 return *this;\r
98         }\r
99 \r
100         template <typename U>\r
101         typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, safe_ptr<T>&>::type\r
102         operator=(U&& impl)\r
103         {\r
104                 safe_ptr<T> temp(std::forward<T>(impl));\r
105                 temp.swap(*this);\r
106                 return *this;\r
107         }\r
108 \r
109         T& operator*() const { return *impl_.get();}  // noexcept\r
110 \r
111         T* operator->() const { return impl_.get();}  // noexcept\r
112 \r
113         T* get() const { return impl_.get();}  // noexcept\r
114 \r
115         bool unique() const { return impl_.unique();}  // noexcept\r
116 \r
117         long use_count() const { return impl_.use_count();}  // noexcept\r
118                                 \r
119         void swap(safe_ptr& other) { impl_.swap(other.impl_); }  // noexcept\r
120         \r
121         operator const std::shared_ptr<T>&() const { return impl_;}  // noexcept\r
122 \r
123         template<class U>\r
124         bool owner_before(const safe_ptr<T>& ptr){ return impl_.owner_before(ptr.impl_); }  // noexcept\r
125 \r
126         template<class U>\r
127         bool owner_before(const std::shared_ptr<U>& ptr){ return impl_.owner_before(ptr); }  // noexcept\r
128         \r
129         template<class D, class U> \r
130         D* get_deleter(safe_ptr<U> const& ptr) { return impl_.get_deleter(); }  // noexcept\r
131 };\r
132 \r
133 template<class T, class U>\r
134 bool operator==(const safe_ptr<T>& a, const safe_ptr<U>& b)  // noexcept\r
135 {\r
136         return a.get() == b.get();\r
137 }\r
138 \r
139 template<class T, class U>\r
140 bool operator!=(const safe_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 safe_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 safe_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 safe_ptr<U>& b)  // noexcept\r
165 {\r
166         return a.get() <= b.get();\r
167 }\r
168 \r
169 template<class E, class T, class U>\r
170 std::basic_ostream<E, T>& operator<<(std::basic_ostream<E, T>& out,     const safe_ptr<U>& p)\r
171 {\r
172         return out << p.get();\r
173 }\r
174 \r
175 template<class T> \r
176 void swap(safe_ptr<T>& a, safe_ptr<T>& b)  // noexcept\r
177 {\r
178         a.swap(b);\r
179 }\r
180 \r
181 template<class T> \r
182 T* get_pointer(safe_ptr<T> const& p)  // noexcept\r
183 {\r
184         return p.get();\r
185 }\r
186 \r
187 template <class T, class U>\r
188 safe_ptr<T> static_pointer_cast(const safe_ptr<U>& p)  // noexcept\r
189 {\r
190         return safe_ptr<T>(std::static_pointer_cast<T>(std::shared_ptr<U>(p)));\r
191 }\r
192 \r
193 template <class T, class U>\r
194 safe_ptr<T> const_pointer_cast(const safe_ptr<U>& p)  // noexcept\r
195 {\r
196         return safe_ptr<T>(std::const_pointer_cast<T>(std::shared_ptr<U>(p)));\r
197 }\r
198 \r
199 template <class T, class U>\r
200 safe_ptr<T> dynamic_pointer_cast(const safe_ptr<U>& p)\r
201 {\r
202         auto temp = std::dynamic_pointer_cast<T>(std::shared_ptr<U>(p));\r
203         if(!temp)\r
204                 throw std::bad_cast();\r
205         return safe_ptr<T>(temp);\r
206 }\r
207 \r
208 template<typename T>\r
209 safe_ptr<T> make_safe(const std::shared_ptr<T>& ptr)\r
210 {\r
211         return safe_ptr<T>(ptr);\r
212 }\r
213 \r
214 template<typename T>\r
215 safe_ptr<T> make_safe(std::shared_ptr<T>&& ptr)\r
216 {\r
217         return safe_ptr<T>(std::move(ptr));\r
218 }\r
219 \r
220 template<typename T>\r
221 safe_ptr<T> make_safe()\r
222 {\r
223         return safe_ptr<T>();\r
224 }\r
225 \r
226 template<typename T, typename P0>\r
227 safe_ptr<T> make_safe(P0&& p0)\r
228 {\r
229         return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0)));\r
230 }\r
231 \r
232 template<typename T, typename P0, typename P1>\r
233 safe_ptr<T> make_safe(P0&& p0, P1&& p1)\r
234 {\r
235         return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1)));\r
236 }\r
237 \r
238 template<typename T, typename P0, typename P1, typename P2>\r
239 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2)\r
240 {\r
241         return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2)));\r
242 }\r
243 \r
244 template<typename T, typename P0, typename P1, typename P2, typename P3>\r
245 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3)\r
246 {\r
247         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
248 }\r
249 \r
250 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4>\r
251 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&&)\r
252 {\r
253         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
254 }\r
255 \r
256 }