2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\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
23 #include <type_traits>
\r
24 #include <exception>
\r
26 #include "../utility/assert.h"
\r
30 template<typename T>
\r
33 std::shared_ptr<T> impl_;
\r
34 template <typename> friend class safe_ptr;
\r
36 typedef T element_type;
\r
38 safe_ptr() : impl_(std::make_shared<T>()){}
\r
40 safe_ptr(const safe_ptr<T>& other) : impl_(other.impl_){} // noexcept
\r
41 safe_ptr(safe_ptr<T>&& other) : impl_(std::move(other.impl_)){}
\r
43 template<typename U>
\r
44 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 template<typename U>
\r
47 safe_ptr(const U& impl, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)
\r
48 : impl_(std::make_shared<U>(impl)) {}
\r
50 template<typename U, typename D>
\r
51 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
52 : impl_(new U(impl), dtor) {}
\r
54 template<typename U>
\r
55 safe_ptr(U&& impl, typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, void>::type* = 0)
\r
56 : impl_(std::make_shared<U>(std::forward<U>(impl))) {}
\r
58 template<typename U, typename D>
\r
59 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
60 : impl_(new U(std::forward<U>(impl)), dtor) {}
\r
62 template<typename U>
\r
63 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
66 throw std::invalid_argument("impl");
\r
69 template<typename U>
\r
70 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
73 throw std::invalid_argument("impl");
\r
76 template<typename U>
\r
77 explicit safe_ptr(U* impl, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(impl)
\r
80 throw std::invalid_argument("impl");
\r
83 template<typename U, typename D>
\r
84 explicit safe_ptr(U* impl, D dtor, typename std::enable_if<std::is_convertible<U*, T*>::value, void*>::type = 0) : impl_(impl, dtor)
\r
87 throw std::invalid_argument("impl");
\r
90 template<typename U>
\r
91 typename std::enable_if<std::is_convertible<U*, T*>::value, safe_ptr<T>&>::type
\r
92 operator=(const safe_ptr<U>& other)
\r
94 safe_ptr<T> temp(other);
\r
99 template <typename U>
\r
100 typename std::enable_if<std::is_convertible<typename std::add_pointer<U>::type, T*>::value, safe_ptr<T>&>::type
\r
101 operator=(U&& impl)
\r
103 safe_ptr<T> temp(std::forward<T>(impl));
\r
108 T& operator*() const // noexcept
\r
110 CASPAR_ASSERT(impl_);
\r
111 return *impl_.get();
\r
114 T* operator->() const // noexcept
\r
116 CASPAR_ASSERT(impl_);
\r
117 return impl_.get();
\r
120 T* get() const // noexcept
\r
122 CASPAR_ASSERT(impl_);
\r
123 return impl_.get();
\r
126 bool unique() const { return impl_.unique();} // noexcept
\r
128 long use_count() const { return impl_.use_count();} // noexcept
\r
130 void swap(safe_ptr& other) { impl_.swap(other.impl_); } // noexcept
\r
132 operator const std::shared_ptr<T>&() const { return impl_;} // noexcept
\r
135 bool owner_before(const safe_ptr<T>& ptr){ return impl_.owner_before(ptr.impl_); } // noexcept
\r
138 bool owner_before(const std::shared_ptr<U>& ptr){ return impl_.owner_before(ptr); } // noexcept
\r
140 template<class D, class U>
\r
141 D* get_deleter(safe_ptr<U> const& ptr) { return impl_.get_deleter(); } // noexcept
\r
144 template<class T, class U>
\r
145 bool operator==(const std::shared_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
147 return a.get() == b.get();
\r
150 template<class T, class U>
\r
151 bool operator==(const safe_ptr<T>& a, const std::shared_ptr<U>& b) // noexcept
\r
153 return a.get() == b.get();
\r
156 template<class T, class U>
\r
157 bool operator==(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
159 return a.get() == b.get();
\r
162 template<class T, class U>
\r
163 bool operator!=(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
165 return a.get() != b.get();
\r
168 template<class T, class U>
\r
169 bool operator<(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
171 return a.get() < b.get();
\r
174 template<class T, class U>
\r
175 bool operator>(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
177 return a.get() > b.get();
\r
180 template<class T, class U>
\r
181 bool operator>=(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
183 return a.get() >= b.get();
\r
186 template<class T, class U>
\r
187 bool operator<=(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
189 return a.get() <= b.get();
\r
192 template<class E, class T, class U>
\r
193 std::basic_ostream<E, T>& operator<<(std::basic_ostream<E, T>& out, const safe_ptr<U>& p)
\r
195 return out << p.get();
\r
199 void swap(safe_ptr<T>& a, safe_ptr<T>& b) // noexcept
\r
205 T* get_pointer(safe_ptr<T> const& p) // noexcept
\r
210 template <class T, class U>
\r
211 safe_ptr<T> static_pointer_cast(const safe_ptr<U>& p) // noexcept
\r
213 return safe_ptr<T>(std::static_pointer_cast<T>(std::shared_ptr<U>(p)));
\r
216 template <class T, class U>
\r
217 safe_ptr<T> const_pointer_cast(const safe_ptr<U>& p) // noexcept
\r
219 return safe_ptr<T>(std::const_pointer_cast<T>(std::shared_ptr<U>(p)));
\r
222 template <class T, class U>
\r
223 safe_ptr<T> dynamic_pointer_cast(const safe_ptr<U>& p)
\r
225 auto temp = std::dynamic_pointer_cast<T>(std::shared_ptr<U>(p));
\r
227 throw std::bad_cast();
\r
228 return safe_ptr<T>(temp);
\r
231 template<typename T>
\r
232 safe_ptr<T> make_safe(const std::shared_ptr<T>& ptr)
\r
234 return safe_ptr<T>(ptr);
\r
237 template<typename T>
\r
238 safe_ptr<T> make_safe(std::shared_ptr<T>&& ptr)
\r
240 return safe_ptr<T>(std::move(ptr));
\r
243 template<typename T>
\r
244 safe_ptr<T> make_safe()
\r
246 return safe_ptr<T>();
\r
249 template<typename T, typename P0>
\r
250 safe_ptr<T> make_safe(P0&& p0)
\r
252 return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0)));
\r
255 template<typename T, typename P0, typename P1>
\r
256 safe_ptr<T> make_safe(P0&& p0, P1&& p1)
\r
258 return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1)));
\r
261 template<typename T, typename P0, typename P1, typename P2>
\r
262 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2)
\r
264 return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2)));
\r
267 template<typename T, typename P0, typename P1, typename P2, typename P3>
\r
268 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3)
\r
270 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
273 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4>
\r
274 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4)
\r
276 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
279 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5>
\r
280 safe_ptr<T> make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5)
\r
282 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