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
28 template<typename T>
\r
31 std::shared_ptr<T> impl_;
\r
32 template <typename> friend class safe_ptr;
\r
34 typedef T element_type;
\r
36 safe_ptr() : impl_(std::make_shared<T>()){}
\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
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
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
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
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
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
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
64 throw std::invalid_argument("impl");
\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
71 throw std::invalid_argument("impl");
\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
78 throw std::invalid_argument("impl");
\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
85 throw std::invalid_argument("impl");
\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
92 safe_ptr(other).swap(*this);
\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
100 safe_ptr(std::move(other)).swap(*this);
\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
108 safe_ptr temp(std::forward<T>(impl));
\r
113 T& operator*() const // noexcept
\r
115 return *impl_.get();
\r
118 T* operator->() const // noexcept
\r
120 return impl_.get();
\r
123 T* get() const // noexcept
\r
125 return impl_.get();
\r
128 bool unique() const { return impl_.unique();} // noexcept
\r
130 long use_count() const { return impl_.use_count();} // noexcept
\r
132 void swap(safe_ptr& other) { impl_.swap(other.impl_); } // noexcept
\r
134 operator const std::shared_ptr<T>&() const { return impl_;} // noexcept
\r
137 bool owner_before(const safe_ptr& ptr){ return impl_.owner_before(ptr.impl_); } // noexcept
\r
140 bool owner_before(const std::shared_ptr<U>& ptr){ return impl_.owner_before(ptr); } // noexcept
\r
142 template<class D, class U>
\r
143 D* get_deleter(safe_ptr<U> const& ptr) { return impl_.get_deleter(); } // noexcept
\r
146 template<class T, class U>
\r
147 bool operator==(const std::shared_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
149 return a.get() == b.get();
\r
152 template<class T, class U>
\r
153 bool operator==(const safe_ptr<T>& a, const std::shared_ptr<U>& b) // noexcept
\r
155 return a.get() == b.get();
\r
158 template<class T, class U>
\r
159 bool operator==(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
161 return a.get() == b.get();
\r
164 template<class T, class U>
\r
165 bool operator!=(const std::shared_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
167 return a.get() != b.get();
\r
170 template<class T, class U>
\r
171 bool operator!=(const safe_ptr<T>& a, const std::shared_ptr<U>& b) // noexcept
\r
173 return a.get() != b.get();
\r
176 template<class T, class U>
\r
177 bool operator!=(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
179 return a.get() != b.get();
\r
182 template<class T, class U>
\r
183 bool operator<(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
185 return a.get() < b.get();
\r
188 template<class T, class U>
\r
189 bool operator>(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
191 return a.get() > b.get();
\r
194 template<class T, class U>
\r
195 bool operator>=(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
197 return a.get() >= b.get();
\r
200 template<class T, class U>
\r
201 bool operator<=(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
203 return a.get() <= b.get();
\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
209 return out << p.get();
\r
213 void swap(safe_ptr<T>& a, safe_ptr<T>& b) // noexcept
\r
219 T* get_pointer(safe_ptr<T> const& p) // noexcept
\r
224 template <class T, class U>
\r
225 safe_ptr<T> static_pointer_cast(const safe_ptr<U>& p) // noexcept
\r
227 return safe_ptr<T>(std::static_pointer_cast<T>(std::shared_ptr<U>(p)));
\r
230 template <class T, class U>
\r
231 safe_ptr<T> const_pointer_cast(const safe_ptr<U>& p) // noexcept
\r
233 return safe_ptr<T>(std::const_pointer_cast<T>(std::shared_ptr<U>(p)));
\r
236 template <class T, class U>
\r
237 safe_ptr<T> dynamic_pointer_cast(const safe_ptr<U>& p)
\r
239 auto temp = std::dynamic_pointer_cast<T>(std::shared_ptr<U>(p));
\r
241 throw std::bad_cast();
\r
242 return safe_ptr<T>(temp);
\r
245 template<typename T>
\r
246 safe_ptr<T> make_safe(const std::shared_ptr<T>& ptr)
\r
248 return safe_ptr<T>(ptr);
\r
251 template<typename T>
\r
252 safe_ptr<T> make_safe(std::shared_ptr<T>&& ptr)
\r
254 return safe_ptr<T>(std::move(ptr));
\r
257 template<typename T>
\r
258 safe_ptr<T> make_safe()
\r
260 return safe_ptr<T>();
\r
263 template<typename T, typename P0>
\r
264 safe_ptr<T> make_safe(P0&& p0)
\r
266 return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0)));
\r
269 template<typename T, typename P0, typename P1>
\r
270 safe_ptr<T> make_safe(P0&& p0, P1&& p1)
\r
272 return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1)));
\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
278 return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2)));
\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
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
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
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
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
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