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<T>& other) : impl_(other.impl_){} // noexcept
\r
39 safe_ptr(safe_ptr<T>&& 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<U*, T*>::value, safe_ptr<T>&>::type
\r
90 operator=(const safe_ptr<U>& other)
\r
92 safe_ptr<T> temp(other);
\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
101 safe_ptr<T> temp(std::forward<T>(impl));
\r
106 T& operator*() const // noexcept
\r
108 return *impl_.get();
\r
111 T* operator->() const // noexcept
\r
113 return impl_.get();
\r
116 T* get() const // noexcept
\r
118 return impl_.get();
\r
121 bool unique() const { return impl_.unique();} // noexcept
\r
123 long use_count() const { return impl_.use_count();} // noexcept
\r
125 void swap(safe_ptr& other) { impl_.swap(other.impl_); } // noexcept
\r
127 operator const std::shared_ptr<T>&() const { return impl_;} // noexcept
\r
130 bool owner_before(const safe_ptr<T>& ptr){ return impl_.owner_before(ptr.impl_); } // noexcept
\r
133 bool owner_before(const std::shared_ptr<U>& ptr){ return impl_.owner_before(ptr); } // noexcept
\r
135 template<class D, class U>
\r
136 D* get_deleter(safe_ptr<U> const& ptr) { return impl_.get_deleter(); } // noexcept
\r
139 template<class T, class U>
\r
140 bool operator==(const std::shared_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
142 return a.get() == b.get();
\r
145 template<class T, class U>
\r
146 bool operator==(const safe_ptr<T>& a, const std::shared_ptr<U>& b) // noexcept
\r
148 return a.get() == b.get();
\r
151 template<class T, class U>
\r
152 bool operator==(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
154 return a.get() == b.get();
\r
157 template<class T, class U>
\r
158 bool operator!=(const std::shared_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
160 return a.get() != b.get();
\r
163 template<class T, class U>
\r
164 bool operator!=(const safe_ptr<T>& a, const std::shared_ptr<U>& b) // noexcept
\r
166 return a.get() != b.get();
\r
169 template<class T, class U>
\r
170 bool operator!=(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
172 return a.get() != b.get();
\r
175 template<class T, class U>
\r
176 bool operator<(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
178 return a.get() < b.get();
\r
181 template<class T, class U>
\r
182 bool operator>(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
184 return a.get() > b.get();
\r
187 template<class T, class U>
\r
188 bool operator>=(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
190 return a.get() >= b.get();
\r
193 template<class T, class U>
\r
194 bool operator<=(const safe_ptr<T>& a, const safe_ptr<U>& b) // noexcept
\r
196 return a.get() <= b.get();
\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
202 return out << p.get();
\r
206 void swap(safe_ptr<T>& a, safe_ptr<T>& b) // noexcept
\r
212 T* get_pointer(safe_ptr<T> const& p) // noexcept
\r
217 template <class T, class U>
\r
218 safe_ptr<T> static_pointer_cast(const safe_ptr<U>& p) // noexcept
\r
220 return safe_ptr<T>(std::static_pointer_cast<T>(std::shared_ptr<U>(p)));
\r
223 template <class T, class U>
\r
224 safe_ptr<T> const_pointer_cast(const safe_ptr<U>& p) // noexcept
\r
226 return safe_ptr<T>(std::const_pointer_cast<T>(std::shared_ptr<U>(p)));
\r
229 template <class T, class U>
\r
230 safe_ptr<T> dynamic_pointer_cast(const safe_ptr<U>& p)
\r
232 auto temp = std::dynamic_pointer_cast<T>(std::shared_ptr<U>(p));
\r
234 throw std::bad_cast();
\r
235 return safe_ptr<T>(temp);
\r
238 template<typename T>
\r
239 safe_ptr<T> make_safe(const std::shared_ptr<T>& ptr)
\r
241 return safe_ptr<T>(ptr);
\r
244 template<typename T>
\r
245 safe_ptr<T> make_safe(std::shared_ptr<T>&& ptr)
\r
247 return safe_ptr<T>(std::move(ptr));
\r
250 template<typename T>
\r
251 safe_ptr<T> make_safe()
\r
253 return safe_ptr<T>();
\r
256 template<typename T, typename P0>
\r
257 safe_ptr<T> make_safe(P0&& p0)
\r
259 return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0)));
\r
262 template<typename T, typename P0, typename P1>
\r
263 safe_ptr<T> make_safe(P0&& p0, P1&& p1)
\r
265 return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1)));
\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
271 return safe_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2)));
\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
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
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
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
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
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