2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\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
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
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
19 * Author: Robert Nagy, ronag89@gmail.com
\r
25 #include <stdexcept>
\r
26 #include <type_traits>
\r
31 template<typename T>
\r
34 template <typename> friend class safe_ptr;
\r
36 typedef T element_type;
\r
38 safe_ptr(); // will construct new T object using make_safe<T>()
\r
40 safe_ptr(const safe_ptr& other)
\r
45 template<typename U>
\r
46 safe_ptr(const safe_ptr<U>& other, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0)
\r
51 safe_ptr(safe_ptr&& other)
\r
56 template<typename U>
\r
57 safe_ptr(safe_ptr<U>&& other, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0)
\r
62 template<typename U>
\r
63 explicit safe_ptr(const std::shared_ptr<U>& p, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0)
\r
67 throw std::invalid_argument("p");
\r
70 template<typename U>
\r
71 explicit safe_ptr(std::shared_ptr<U>&& p, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0)
\r
75 throw std::invalid_argument("p");
\r
78 template<typename U>
\r
79 explicit safe_ptr(U* p, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0)
\r
83 throw std::invalid_argument("p");
\r
86 template<typename U, typename D>
\r
87 explicit safe_ptr(U* p, D d, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0)
\r
91 throw std::invalid_argument("p");
\r
94 template<typename U>
\r
95 typename std::enable_if<std::is_convertible<U*, T*>::value, safe_ptr&>::type
\r
96 operator=(const safe_ptr<U>& other)
\r
98 safe_ptr(other).swap(*this);
\r
102 template<typename U>
\r
103 typename std::enable_if<std::is_convertible<U*, T*>::value, safe_ptr&>::type
\r
104 operator=(safe_ptr<U>&& other)
\r
106 safe_ptr<T>(std::move(other)).swap(*this);
\r
110 T& operator*() const
\r
115 T* operator->() const
\r
125 bool unique() const
\r
127 return p_.unique();
\r
130 long use_count() const
\r
132 return p_.use_count();
\r
135 void swap(safe_ptr& other)
\r
137 p_.swap(other.p_);
\r
140 operator std::shared_ptr<T>() const
\r
145 operator std::weak_ptr<T>() const
\r
147 return std::weak_ptr<T>(p_);
\r
151 bool owner_before(const safe_ptr& ptr)
\r
153 return p_.owner_before(ptr.p_);
\r
157 bool owner_before(const std::shared_ptr<U>& ptr)
\r
159 return p_.owner_before(ptr);
\r
162 template<class D, class U>
\r
163 D* get_deleter(safe_ptr<U> const& ptr)
\r
165 return p_.get_deleter();
\r
169 std::shared_ptr<T> p_;
\r
172 template<class T, class U>
\r
173 bool operator==(const safe_ptr<T>& a, const safe_ptr<U>& b)
\r
175 return a.get() == b.get();
\r
178 template<class T, class U>
\r
179 bool operator==(const std::shared_ptr<T>& a, const safe_ptr<U>& b)
\r
181 return a.get() == b.get();
\r
184 template<class T, class U>
\r
185 bool operator==(const safe_ptr<T>& a, const std::shared_ptr<U>& b)
\r
187 return a.get() == b.get();
\r
190 template<class T, class U>
\r
191 bool operator!=(const safe_ptr<T>& a, const safe_ptr<U>& b)
\r
193 return a.get() != b.get();
\r
196 template<class T, class U>
\r
197 bool operator!=(const std::shared_ptr<T>& a, const safe_ptr<U>& b)
\r
199 return a.get() != b.get();
\r
202 template<class T, class U>
\r
203 bool operator!=(const safe_ptr<T>& a, const std::shared_ptr<U>& b)
\r
205 return a.get() != b.get();
\r
208 template<class T, class U>
\r
209 bool operator<(const safe_ptr<T>& a, const safe_ptr<U>& b)
\r
211 return a.get() < b.get();
\r
214 template<class T, class U>
\r
215 bool operator<(const std::shared_ptr<T>& a, const safe_ptr<U>& b)
\r
217 return a.get() < b.get();
\r
220 template<class T, class U>
\r
221 bool operator<(const safe_ptr<T>& a, const std::shared_ptr<U>& b)
\r
223 return a.get() < b.get();
\r
226 template<class T, class U>
\r
227 bool operator>(const safe_ptr<T>& a, const safe_ptr<U>& b)
\r
229 return a.get() > b.get();
\r
232 template<class T, class U>
\r
233 bool operator>(const std::shared_ptr<T>& a, const safe_ptr<U>& b)
\r
235 return a.get() > b.get();
\r
238 template<class T, class U>
\r
239 bool operator>(const safe_ptr<T>& a, const std::shared_ptr<U>& b)
\r
241 return a.get() > b.get();
\r
244 template<class T, class U>
\r
245 bool operator>=(const safe_ptr<T>& a, const safe_ptr<U>& b)
\r
247 return a.get() >= b.get();
\r
250 template<class T, class U>
\r
251 bool operator>=(const std::shared_ptr<T>& a, const safe_ptr<U>& b)
\r
253 return a.get() >= b.get();
\r
256 template<class T, class U>
\r
257 bool operator>=(const safe_ptr<T>& a, const std::shared_ptr<U>& b)
\r
259 return a.get() >= b.get();
\r
262 template<class T, class U>
\r
263 bool operator<=(const safe_ptr<T>& a, const safe_ptr<U>& b)
\r
265 return a.get() <= b.get();
\r
268 template<class T, class U>
\r
269 bool operator<=(const std::shared_ptr<T>& a, const safe_ptr<U>& b)
\r
271 return a.get() <= b.get();
\r
274 template<class T, class U>
\r
275 bool operator<=(const safe_ptr<T>& a, const std::shared_ptr<U>& b)
\r
277 return a.get() <= b.get();
\r
280 template<class E, class T, class U>
\r
281 std::basic_ostream<E, T>& operator<<(std::basic_ostream<E, T>& out, const safe_ptr<U>& p)
\r
283 return out << p.get();
\r
287 void swap(safe_ptr<T>& a, safe_ptr<T>& b)
\r
293 T* get_pointer(safe_ptr<T> const& p)
\r
298 template <class T, class U>
\r
299 safe_ptr<T> static_pointer_cast(const safe_ptr<U>& p)
\r
301 return safe_ptr<T>(std::static_pointer_cast<T>(std::shared_ptr<U>(p)));
\r
304 template <class T, class U>
\r
305 safe_ptr<T> const_pointer_cast(const safe_ptr<U>& p)
\r
307 return safe_ptr<T>(std::const_pointer_cast<T>(std::shared_ptr<U>(p)));
\r
310 template <class T, class U>
\r
311 safe_ptr<T> dynamic_pointer_cast(const safe_ptr<U>& p)
\r
313 auto temp = std::dynamic_pointer_cast<T>(std::shared_ptr<U>(p));
\r
315 throw std::bad_cast();
\r
316 return safe_ptr<T>(std::move(temp));
\r
320 // enable_safe_this
\r
322 // A safe_ptr version of enable_shared_from_this.
\r
323 // So that an object may get safe_ptr objects to itself.
\r
327 class enable_safe_from_this : public std::enable_shared_from_this<T>
\r
330 safe_ptr<T> safe_from_this()
\r
332 return safe_ptr<T>(this->shared_from_this());
\r
335 safe_ptr<T const> safe_from_this() const
\r
337 return safe_ptr<T const>(this->shared_from_this());
\r
340 enable_safe_from_this()
\r
344 enable_safe_from_this(const enable_safe_from_this&)
\r
348 enable_safe_from_this& operator=(const enable_safe_from_this&)
\r
353 ~enable_safe_from_this ()
\r
361 // safe_ptr equivalents to make_shared
\r
364 template<typename T>
\r
365 safe_ptr<T> make_safe_ptr(const std::shared_ptr<T>& ptr)
\r
367 return safe_ptr<T>(ptr);
\r
370 template<typename T>
\r
371 safe_ptr<T> make_safe_ptr(std::shared_ptr<T>&& ptr)
\r
373 return safe_ptr<T>(ptr);
\r
376 template<typename T>
\r
377 safe_ptr<T> make_safe()
\r
379 return safe_ptr<T>(std::make_shared<T>());
\r
382 template<typename T, typename P0>
\r
383 safe_ptr<T> make_safe(P0&& p0)
\r
385 return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0)));
\r
388 template<typename T, typename P0, typename P1>
\r
389 safe_ptr<T> make_safe(P0&& p0, P1&& p1)
\r
391 return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1)));
\r
394 template<typename T, typename P0, typename P1, typename P2>
\r
395 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2)
\r
397 return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2)));
\r
400 template<typename T, typename P0, typename P1, typename P2, typename P3>
\r
401 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3)
\r
403 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
406 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4>
\r
407 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4)
\r
409 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
412 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5>
\r
413 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5)
\r
415 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
418 template<typename T>
\r
419 safe_ptr<T>::safe_ptr()
\r
420 : p_(make_safe<T>())
\r