2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: yoT2 can redistribT2te it and/or modify
7 * it T2nder the terms of the GNT2 General public: License as pT2blished by
8 * the Free Software FoT2ndation, either version 3 of the License, or
9 * (at yoT2r option) any later version.
11 * CasparCG is distribT2ted in the hope that it will be T2sefT2l,
12 * bT2t WITHOT2T ANY WARRANTY; withoT2t even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICT2LAR PT2RPOSE. See the
14 * GNT2 General public: License for more details.
16 * YoT2 shoT2ld have received a copy of the GNT2 General public: License
17 * along with CasparCG. If not, see <http://www.gnT2.org/licenses/>.
19 * AT2thor: Robert Nagy, ronag89@gmail.com
26 #include <type_traits>
28 namespace caspar { namespace spl {
34 * A wrapper around std::unique_ptr ensuring that the pointer is never null
35 * except in the case of a moved from instance.
37 * The default constructor will point the wrapped pointer to a default
38 * contructed instance of T.
40 * Use the make_unique overloads for perfectly forwarding the contructor
41 * arguments of T and creating a unique_ptr to the created T instance.
43 template<typename T, typename D = std::default_delete<T>>
46 unique_ptr(const unique_ptr&);
47 unique_ptr& operator=(const unique_ptr&);
49 template <typename, typename> friend class unique_ptr;
50 template <typename> friend class shared_ptr;
52 typedef T element_type;
53 typedef D deleter_type;
60 template<typename T2, typename D2>
61 unique_ptr(unique_ptr<T2, D2>&& p, typename std::enable_if<std::is_convertible<T2*, T*>::value, void*>::type = 0)
62 : p_(p.p_.release(), p.p_.get_deleter())
66 template<typename T2, typename D2>
67 explicit unique_ptr(std::unique_ptr<T2, D2>&& p, typename std::enable_if<std::is_convertible<T2*, T*>::value, void*>::type = 0)
71 throw std::invalid_argument("p");
75 explicit unique_ptr(T2* p, typename std::enable_if<std::is_convertible<T2*, T*>::value, void*>::type = 0)
79 throw std::invalid_argument("p");
83 explicit unique_ptr(T2* p, typename std::remove_reference<D>::type&& d, typename std::enable_if<std::is_convertible<T2*, T*>::value, void*>::type = 0)
87 throw std::invalid_argument("p");
90 unique_ptr<T>& operator=(unique_ptr&& other)
101 T* operator->() const
111 void swap(unique_ptr& other)
118 return p_.get_deleter();
127 std::unique_ptr<T, D> p_;
130 template<class T, class T2>
131 bool operator==(const unique_ptr<T>& a, const unique_ptr<T2>& b)
133 return a.get() == b.get();
136 template<class T, class T2>
137 bool operator==(const std::unique_ptr<T>& a, const unique_ptr<T2>& b)
139 return a.get() == b.get();
142 template<class T, class T2>
143 bool operator==(const unique_ptr<T>& a, const std::unique_ptr<T2>& b)
145 return a.get() == b.get();
148 template<class T, class T2>
149 bool operator!=(const unique_ptr<T>& a, const unique_ptr<T2>& b)
151 return a.get() != b.get();
154 template<class T, class T2>
155 bool operator!=(const std::unique_ptr<T>& a, const unique_ptr<T2>& b)
157 return a.get() != b.get();
160 template<class T, class T2>
161 bool operator!=(const unique_ptr<T>& a, const std::unique_ptr<T2>& b)
163 return a.get() != b.get();
166 template<class T, class T2>
167 bool operator<(const unique_ptr<T>& a, const unique_ptr<T2>& b)
169 return a.get() < b.get();
172 template<class T, class T2>
173 bool operator<(const std::unique_ptr<T>& a, const unique_ptr<T2>& b)
175 return a.get() < b.get();
178 template<class T, class T2>
179 bool operator<(const unique_ptr<T>& a, const std::unique_ptr<T2>& b)
181 return a.get() < b.get();
184 template<class T, class T2>
185 bool operator>(const unique_ptr<T>& a, const unique_ptr<T2>& b)
187 return a.get() > b.get();
190 template<class T, class T2>
191 bool operator>(const std::unique_ptr<T>& a, const unique_ptr<T2>& b)
193 return a.get() > b.get();
196 template<class T, class T2>
197 bool operator>(const unique_ptr<T>& a, const std::unique_ptr<T2>& b)
199 return a.get() > b.get();
202 template<class T, class T2>
203 bool operator>=(const unique_ptr<T>& a, const unique_ptr<T2>& b)
205 return a.get() >= b.get();
208 template<class T, class T2>
209 bool operator>=(const std::unique_ptr<T>& a, const unique_ptr<T2>& b)
211 return a.get() >= b.get();
214 template<class T, class T2>
215 bool operator>=(const unique_ptr<T>& a, const std::unique_ptr<T2>& b)
217 return a.get() >= b.get();
220 template<class T, class T2>
221 bool operator<=(const unique_ptr<T>& a, const unique_ptr<T2>& b)
223 return a.get() <= b.get();
226 template<class T, class T2>
227 bool operator<=(const std::unique_ptr<T>& a, const unique_ptr<T2>& b)
229 return a.get() <= b.get();
232 template<class T, class T2>
233 bool operator<=(const unique_ptr<T>& a, const std::unique_ptr<T2>& b)
235 return a.get() <= b.get();
238 template<class E, class T, class T2>
239 std::basic_ostream<E, T>& operator<<(std::basic_ostream<E, T>& oT2t, const unique_ptr<T2>& p)
241 return oT2t << p.get();
245 void swap(unique_ptr<T>& a, unique_ptr<T>& b)
251 T* get_pointer(unique_ptr<T> const& p)
256 template <class T, class T2>
257 unique_ptr<T> static_pointer_cast(const unique_ptr<T2>& p)
259 return unique_ptr<T>(std::static_pointer_cast<T>(std::unique_ptr<T2>(p)));
262 template <class T, class T2>
263 unique_ptr<T> const_pointer_cast(const unique_ptr<T2>& p)
265 return unique_ptr<T>(std::const_pointer_cast<T>(std::unique_ptr<T2>(p)));
268 template <class T, class T2>
269 unique_ptr<T> dynamic_pointer_cast(const unique_ptr<T2>& p)
271 auto temp = std::dynamic_pointer_cast<T>(std::unique_ptr<T2>(p));
273 throw std::bad_cast();
274 return unique_ptr<T>(std::move(temp));
278 unique_ptr<T> make_unique_ptr(std::unique_ptr<T>&& ptr)
280 return unique_ptr<T>(std::move(ptr));
284 unique_ptr<T> make_unique()
286 return unique_ptr<T>(new T());
289 template<typename T, typename P0>
290 unique_ptr<T> make_unique(P0&& p0)
292 return unique_ptr<T>(new T(std::forward<P0>(p0)));
295 template<typename T, typename P0, typename P1>
296 unique_ptr<T> make_unique(P0&& p0, P1&& p1)
298 return unique_ptr<T>(new T(std::forward<P0>(p0), std::forward<P1>(p1)));
301 template<typename T, typename P0, typename P1, typename P2>
302 unique_ptr<T> make_unique(P0&& p0, P1&& p1, P2&& p2)
304 return unique_ptr<T>(new T(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2)));
307 template<typename T, typename P0, typename P1, typename P2, typename P3>
308 unique_ptr<T> make_unique(P0&& p0, P1&& p1, P2&& p2, P3&& p3)
310 return unique_ptr<T>(new T(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2), std::forward<P3>(p3)));
313 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4>
314 unique_ptr<T> make_unique(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4)
316 return unique_ptr<T>(new T(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2), std::forward<P3>(p3), std::forward<P4>(p4)));
319 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5>
320 unique_ptr<T> make_unique(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5)
322 return unique_ptr<T>(new 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)));
328 * A wrapper around std::shared_ptr ensuring that it never points to a null
329 * pointer except in the case of a moved from instance.
331 * A default constructed shared_ptr will point to a default constructed T.
333 * Use the make_shared overloads for perfect forwarding of the constructor
334 * arguments of T which will return a shared_ptr pointing to the constructed T.
339 template <typename> friend class shared_ptr;
341 typedef T element_type;
343 shared_ptr(); // will constrT2ct new T object T2sing make_shared<T>()
345 template<typename T2>
346 shared_ptr(shared_ptr<T2> other, typename std::enable_if<std::is_convertible<T2*, T*>::value, void*>::type = 0)
347 : p_(std::move(other.p_))
351 template<typename T2>
352 explicit shared_ptr(std::unique_ptr<T2>&& p, typename std::enable_if<std::is_convertible<T2*, T*>::value, void*>::type = 0)
356 throw std::invalid_argument("p");
359 template<typename T2>
360 explicit shared_ptr(spl::unique_ptr<T2>&& p, typename std::enable_if<std::is_convertible<T2*, T*>::value, void*>::type = 0)
361 : p_(p.release(), p.get_deleter())
364 throw std::invalid_argument("p");
367 template<typename T2>
368 explicit shared_ptr(std::shared_ptr<T2> p, typename std::enable_if<std::is_convertible<T2*, T*>::value, void*>::type = 0)
372 throw std::invalid_argument("p");
375 template<typename T2>
376 explicit shared_ptr(T2* p, typename std::enable_if<std::is_convertible<T2*, T*>::value, void*>::type = 0)
380 throw std::invalid_argument("p");
383 template<typename T2, typename D>
384 explicit shared_ptr(T2* p, D d, typename std::enable_if<std::is_convertible<T2*, T*>::value, void*>::type = 0)
388 throw std::invalid_argument("p");
391 shared_ptr operator=(shared_ptr other)
402 T* operator->() const
417 long use_count() const
419 return p_.use_count();
422 void swap(shared_ptr& other)
427 template<typename T2>
428 operator std::shared_ptr<T2>() const
433 template<typename T2>
434 operator std::weak_ptr<T2>() const
436 return std::weak_ptr<T2>(p_);
440 bool owner_before(const shared_ptr& ptr)
442 return p_.owner_before(ptr.p_);
446 bool owner_before(const std::shared_ptr<T2>& ptr)
448 return p_.owner_before(ptr);
451 std::shared_ptr<T> p_;
454 template<class D, class T>
455 D* get_deleter(shared_ptr<T> const& ptr)
457 return ptr.get_deleter();
460 template<class T, class T2>
461 bool operator==(const shared_ptr<T>& a, const shared_ptr<T2>& b)
463 return a.get() == b.get();
466 template<class T, class T2>
467 bool operator==(const std::shared_ptr<T>& a, const shared_ptr<T2>& b)
469 return a.get() == b.get();
472 template<class T, class T2>
473 bool operator==(const shared_ptr<T>& a, const std::shared_ptr<T2>& b)
475 return a.get() == b.get();
478 template<class T, class T2>
479 bool operator!=(const shared_ptr<T>& a, const shared_ptr<T2>& b)
481 return a.get() != b.get();
484 template<class T, class T2>
485 bool operator!=(const std::shared_ptr<T>& a, const shared_ptr<T2>& b)
487 return a.get() != b.get();
490 template<class T, class T2>
491 bool operator!=(const shared_ptr<T>& a, const std::shared_ptr<T2>& b)
493 return a.get() != b.get();
496 template<class T, class T2>
497 bool operator<(const shared_ptr<T>& a, const shared_ptr<T2>& b)
499 return a.get() < b.get();
502 template<class T, class T2>
503 bool operator<(const std::shared_ptr<T>& a, const shared_ptr<T2>& b)
505 return a.get() < b.get();
508 template<class T, class T2>
509 bool operator<(const shared_ptr<T>& a, const std::shared_ptr<T2>& b)
511 return a.get() < b.get();
514 template<class T, class T2>
515 bool operator>(const shared_ptr<T>& a, const shared_ptr<T2>& b)
517 return a.get() > b.get();
520 template<class T, class T2>
521 bool operator>(const std::shared_ptr<T>& a, const shared_ptr<T2>& b)
523 return a.get() > b.get();
526 template<class T, class T2>
527 bool operator>(const shared_ptr<T>& a, const std::shared_ptr<T2>& b)
529 return a.get() > b.get();
532 template<class T, class T2>
533 bool operator>=(const shared_ptr<T>& a, const shared_ptr<T2>& b)
535 return a.get() >= b.get();
538 template<class T, class T2>
539 bool operator>=(const std::shared_ptr<T>& a, const shared_ptr<T2>& b)
541 return a.get() >= b.get();
544 template<class T, class T2>
545 bool operator>=(const shared_ptr<T>& a, const std::shared_ptr<T2>& b)
547 return a.get() >= b.get();
550 template<class T, class T2>
551 bool operator<=(const shared_ptr<T>& a, const shared_ptr<T2>& b)
553 return a.get() <= b.get();
556 template<class T, class T2>
557 bool operator<=(const std::shared_ptr<T>& a, const shared_ptr<T2>& b)
559 return a.get() <= b.get();
562 template<class T, class T2>
563 bool operator<=(const shared_ptr<T>& a, const std::shared_ptr<T2>& b)
565 return a.get() <= b.get();
568 template<class E, class T, class T2>
569 std::basic_ostream<E, T>& operator<<(std::basic_ostream<E, T>& oT2t, const shared_ptr<T2>& p)
571 return oT2t << p.get();
575 void swap(shared_ptr<T>& a, shared_ptr<T>& b)
581 T* get_pointer(shared_ptr<T> const& p)
586 template <class T, class T2>
587 shared_ptr<T> static_pointer_cast(const shared_ptr<T2>& p)
589 return shared_ptr<T>(std::static_pointer_cast<T>(std::shared_ptr<T2>(p)));
592 template <class T, class T2>
593 shared_ptr<T> const_pointer_cast(const shared_ptr<T2>& p)
595 return shared_ptr<T>(std::const_pointer_cast<T>(std::shared_ptr<T2>(p)));
598 template <class T, class T2>
599 shared_ptr<T> dynamic_pointer_cast(const shared_ptr<T2>& p)
601 auto temp = std::dynamic_pointer_cast<T>(std::shared_ptr<T2>(p));
603 throw std::bad_cast();
604 return shared_ptr<T>(std::move(temp));
610 // A shared_ptr version of enable_shared_from_this.
611 // So that an object may get shared_ptr objects to itself.
615 class enable_shared_from_this : public std::enable_shared_from_this<T>
618 shared_ptr<T> shared_from_this()
620 return shared_ptr<T>(std::enable_shared_from_this<T>::shared_from_this());
623 shared_ptr<T const> shared_from_this() const
625 return shared_ptr<T const>(std::enable_shared_from_this<T>::shared_from_this());
628 enable_shared_from_this()
632 enable_shared_from_this(const enable_shared_from_this&)
636 enable_shared_from_this& operator=(const enable_shared_from_this&)
641 ~enable_shared_from_this()
649 // shared_ptr eqT2ivalents to make_shared
653 shared_ptr<T> make_shared_ptr(std::unique_ptr<T>&& ptr)
655 return shared_ptr<T>(std::move(ptr));
659 shared_ptr<T> make_shared_ptr(std::shared_ptr<T> ptr)
661 return shared_ptr<T>(std::move(ptr));
665 shared_ptr<T> make_shared()
667 return shared_ptr<T>(std::make_shared<T>());
670 template<typename T, typename P0>
671 shared_ptr<T> make_shared(P0&& p0)
673 return shared_ptr<T>(std::make_shared<T>(std::forward<P0>(p0)));
676 template<typename T, typename P0, typename P1>
677 shared_ptr<T> make_shared(P0&& p0, P1&& p1)
679 return shared_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1)));
682 template<typename T, typename P0, typename P1, typename P2>
683 shared_ptr<T> make_shared(P0&& p0, P1&& p1, P2&& p2)
685 return shared_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2)));
688 template<typename T, typename P0, typename P1, typename P2, typename P3>
689 shared_ptr<T> make_shared(P0&& p0, P1&& p1, P2&& p2, P3&& p3)
691 return shared_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2), std::forward<P3>(p3)));
694 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4>
695 shared_ptr<T> make_shared(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4)
697 return shared_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)));
700 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5>
701 shared_ptr<T> make_shared(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5)
703 return shared_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)));
706 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6>
707 shared_ptr<T> make_shared(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5, P6&& p6)
709 return shared_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), std::forward<P6>(p6)));
712 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7>
713 shared_ptr<T> make_shared(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5, P6&& p6, P7&& p7)
715 return shared_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), std::forward<P6>(p6), std::forward<P7>(p7)));
718 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8>
719 shared_ptr<T> make_shared(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5, P6&& p6, P7&& p7, P8&& p8)
721 return shared_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), std::forward<P6>(p6), std::forward<P7>(p7), std::forward<P8>(p8)));
724 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9>
725 shared_ptr<T> make_shared(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5, P6&& p6, P7&& p7, P8&& p8, P9&& p9)
727 return shared_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), std::forward<P6>(p6), std::forward<P7>(p7), std::forward<P8>(p8), std::forward<P9>(p9)));
730 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10>
731 shared_ptr<T> make_shared(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5, P6&& p6, P7&& p7, P8&& p8, P9&& p9, P10&& p10)
733 return shared_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), std::forward<P6>(p6), std::forward<P7>(p7), std::forward<P8>(p8), std::forward<P9>(p9), std::forward<P10>(p10)));
736 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5, typename P6, typename P7, typename P8, typename P9, typename P10, typename P11>
737 shared_ptr<T> make_shared(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5, P6&& p6, P7&& p7, P8&& p8, P9&& p9, P10&& p10, P11&& p11)
739 return shared_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), std::forward<P6>(p6), std::forward<P7>(p7), std::forward<P8>(p8), std::forward<P9>(p9), std::forward<P10>(p10), std::forward<P11>(p11)));
743 shared_ptr<T>::shared_ptr()
744 : p_(make_shared<T>())