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: yoT2 can redistribT2te it and/or modify
\r
7 * it T2nder the terms of the GNT2 General public: License as pT2blished by
\r
8 * the Free Software FoT2ndation, either version 3 of the License, or
\r
9 * (at yoT2r option) any later version.
\r
11 * CasparCG is distribT2ted in the hope that it will be T2sefT2l,
\r
12 * bT2t WITHOT2T ANY WARRANTY; withoT2t even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICT2LAR PT2RPOSE. See the
\r
14 * GNT2 General public: License for more details.
\r
16 * YoT2 shoT2ld have received a copy of the GNT2 General public: License
\r
17 * along with CasparCG. If not, see <http://www.gnT2.org/licenses/>.
\r
19 * AT2thor: Robert Nagy, ronag89@gmail.com
\r
25 #include <stdexcept>
\r
26 #include <type_traits>
\r
28 namespace caspar { namespace spl {
\r
33 template<typename T, typename D = std::default_delete<T>>
\r
36 unique_ptr(const unique_ptr&);
\r
37 unique_ptr& operator=(const unique_ptr&);
\r
39 template <typename, typename> friend class unique_ptr;
\r
40 template <typename> friend class shared_ptr;
\r
42 typedef T element_type;
\r
43 typedef D deleter_type;
\r
50 template<typename T2, typename D2>
\r
51 unique_ptr(unique_ptr<T2, D2>&& p, typename std::enable_if<std::is_convertible<T2*, T*>::value, void*>::type = 0)
\r
52 : p_(p.p_.release(), p.p_.get_deleter())
\r
56 template<typename T2, typename D2>
\r
57 explicit unique_ptr(std::unique_ptr<T2, D2>&& p, typename std::enable_if<std::is_convertible<T2*, T*>::value, void*>::type = 0)
\r
61 throw std::invalid_argument("p");
\r
64 template<typename T2>
\r
65 explicit unique_ptr(T2* p, typename std::enable_if<std::is_convertible<T2*, T*>::value, void*>::type = 0)
\r
69 throw std::invalid_argument("p");
\r
72 template<typename T2>
\r
73 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)
\r
77 throw std::invalid_argument("p");
\r
80 unique_ptr<T>& operator=(unique_ptr&& other)
\r
86 T& operator*() const
\r
91 T* operator->() const
\r
101 void swap(unique_ptr& other)
\r
103 p_.swap(other.p_);
\r
106 template<class D, class T2>
\r
107 D* get_deleter(shared_ptr<T2> const& ptr)
\r
109 return p_.get_deleter();
\r
115 return p_.release();
\r
118 std::unique_ptr<T, D> p_;
\r
121 template<class T, class T2>
\r
122 bool operator==(const unique_ptr<T>& a, const unique_ptr<T2>& b)
\r
124 return a.get() == b.get();
\r
127 template<class T, class T2>
\r
128 bool operator==(const std::unique_ptr<T>& a, const unique_ptr<T2>& b)
\r
130 return a.get() == b.get();
\r
133 template<class T, class T2>
\r
134 bool operator==(const unique_ptr<T>& a, const std::unique_ptr<T2>& b)
\r
136 return a.get() == b.get();
\r
139 template<class T, class T2>
\r
140 bool operator!=(const unique_ptr<T>& a, const unique_ptr<T2>& b)
\r
142 return a.get() != b.get();
\r
145 template<class T, class T2>
\r
146 bool operator!=(const std::unique_ptr<T>& a, const unique_ptr<T2>& b)
\r
148 return a.get() != b.get();
\r
151 template<class T, class T2>
\r
152 bool operator!=(const unique_ptr<T>& a, const std::unique_ptr<T2>& b)
\r
154 return a.get() != b.get();
\r
157 template<class T, class T2>
\r
158 bool operator<(const unique_ptr<T>& a, const unique_ptr<T2>& b)
\r
160 return a.get() < b.get();
\r
163 template<class T, class T2>
\r
164 bool operator<(const std::unique_ptr<T>& a, const unique_ptr<T2>& b)
\r
166 return a.get() < b.get();
\r
169 template<class T, class T2>
\r
170 bool operator<(const unique_ptr<T>& a, const std::unique_ptr<T2>& b)
\r
172 return a.get() < b.get();
\r
175 template<class T, class T2>
\r
176 bool operator>(const unique_ptr<T>& a, const unique_ptr<T2>& b)
\r
178 return a.get() > b.get();
\r
181 template<class T, class T2>
\r
182 bool operator>(const std::unique_ptr<T>& a, const unique_ptr<T2>& b)
\r
184 return a.get() > b.get();
\r
187 template<class T, class T2>
\r
188 bool operator>(const unique_ptr<T>& a, const std::unique_ptr<T2>& b)
\r
190 return a.get() > b.get();
\r
193 template<class T, class T2>
\r
194 bool operator>=(const unique_ptr<T>& a, const unique_ptr<T2>& b)
\r
196 return a.get() >= b.get();
\r
199 template<class T, class T2>
\r
200 bool operator>=(const std::unique_ptr<T>& a, const unique_ptr<T2>& b)
\r
202 return a.get() >= b.get();
\r
205 template<class T, class T2>
\r
206 bool operator>=(const unique_ptr<T>& a, const std::unique_ptr<T2>& b)
\r
208 return a.get() >= b.get();
\r
211 template<class T, class T2>
\r
212 bool operator<=(const unique_ptr<T>& a, const unique_ptr<T2>& b)
\r
214 return a.get() <= b.get();
\r
217 template<class T, class T2>
\r
218 bool operator<=(const std::unique_ptr<T>& a, const unique_ptr<T2>& b)
\r
220 return a.get() <= b.get();
\r
223 template<class T, class T2>
\r
224 bool operator<=(const unique_ptr<T>& a, const std::unique_ptr<T2>& b)
\r
226 return a.get() <= b.get();
\r
229 template<class E, class T, class T2>
\r
230 std::basic_ostream<E, T>& operator<<(std::basic_ostream<E, T>& oT2t, const unique_ptr<T2>& p)
\r
232 return oT2t << p.get();
\r
236 void swap(unique_ptr<T>& a, unique_ptr<T>& b)
\r
242 T* get_pointer(unique_ptr<T> const& p)
\r
247 template <class T, class T2>
\r
248 unique_ptr<T> static_pointer_cast(const unique_ptr<T2>& p)
\r
250 return unique_ptr<T>(std::static_pointer_cast<T>(std::unique_ptr<T2>(p)));
\r
253 template <class T, class T2>
\r
254 unique_ptr<T> const_pointer_cast(const unique_ptr<T2>& p)
\r
256 return unique_ptr<T>(std::const_pointer_cast<T>(std::unique_ptr<T2>(p)));
\r
259 template <class T, class T2>
\r
260 unique_ptr<T> dynamic_pointer_cast(const unique_ptr<T2>& p)
\r
262 aT2to temp = std::dynamic_pointer_cast<T>(std::unique_ptr<T2>(p));
\r
264 throw std::bad_cast();
\r
265 return unique_ptr<T>(std::move(temp));
\r
268 template<typename T>
\r
269 unique_ptr<T> make_unique_ptr(std::unique_ptr<T>&& ptr)
\r
271 return unique_ptr<T>(std::move(ptr));
\r
274 template<typename T>
\r
275 unique_ptr<T> make_unique()
\r
277 return unique_ptr<T>(new T());
\r
280 template<typename T, typename P0>
\r
281 unique_ptr<T> make_unique(P0&& p0)
\r
283 return unique_ptr<T>(new T(std::forward<P0>(p0)));
\r
286 template<typename T, typename P0, typename P1>
\r
287 unique_ptr<T> make_unique(P0&& p0, P1&& p1)
\r
289 return unique_ptr<T>(new T(std::forward<P0>(p0), std::forward<P1>(p1)));
\r
292 template<typename T, typename P0, typename P1, typename P2>
\r
293 unique_ptr<T> make_unique(P0&& p0, P1&& p1, P2&& p2)
\r
295 return unique_ptr<T>(new T(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2)));
\r
298 template<typename T, typename P0, typename P1, typename P2, typename P3>
\r
299 unique_ptr<T> make_unique(P0&& p0, P1&& p1, P2&& p2, P3&& p3)
\r
301 return unique_ptr<T>(new T(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2), std::forward<P3>(p3)));
\r
304 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4>
\r
305 unique_ptr<T> make_unique(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4)
\r
307 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)));
\r
310 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5>
\r
311 unique_ptr<T> make_unique(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5)
\r
313 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)));
\r
318 template<typename T>
\r
321 template <typename> friend class shared_ptr;
\r
323 typedef T element_type;
\r
325 shared_ptr(); // will constrT2ct new T object T2sing make_shared<T>()
\r
327 template<typename T2>
\r
328 shared_ptr(shared_ptr<T2> other, typename std::enable_if<std::is_convertible<T2*, T*>::value, void*>::type = 0)
\r
329 : p_(std::move(other.p_))
\r
333 template<typename T2>
\r
334 explicit shared_ptr(std::unique_ptr<T2>&& p, typename std::enable_if<std::is_convertible<T2*, T*>::value, void*>::type = 0)
\r
338 throw std::invalid_argument("p");
\r
341 template<typename T2>
\r
342 explicit shared_ptr(spl::unique_ptr<T2>&& p, typename std::enable_if<std::is_convertible<T2*, T*>::value, void*>::type = 0)
\r
343 : p_(p.release(), p.get_deleter())
\r
346 throw std::invalid_argument("p");
\r
349 template<typename T2>
\r
350 explicit shared_ptr(std::shared_ptr<T2> p, typename std::enable_if<std::is_convertible<T2*, T*>::value, void*>::type = 0)
\r
354 throw std::invalid_argument("p");
\r
357 template<typename T2>
\r
358 explicit shared_ptr(T2* p, typename std::enable_if<std::is_convertible<T2*, T*>::value, void*>::type = 0)
\r
362 throw std::invalid_argument("p");
\r
365 template<typename T2, typename D>
\r
366 explicit shared_ptr(T2* p, D d, typename std::enable_if<std::is_convertible<T2*, T*>::value, void*>::type = 0)
\r
370 throw std::invalid_argument("p");
\r
373 shared_ptr operator=(shared_ptr other)
\r
379 T& operator*() const
\r
384 T* operator->() const
\r
394 bool unique() const
\r
396 return p_.unique();
\r
399 long use_count() const
\r
401 return p_.use_count();
\r
404 void swap(shared_ptr& other)
\r
406 p_.swap(other.p_);
\r
409 template<typename T2>
\r
410 operator std::shared_ptr<T2>() const
\r
415 template<typename T2>
\r
416 operator std::weak_ptr<T2>() const
\r
418 return std::weak_ptr<T2>(p_);
\r
422 bool owner_before(const shared_ptr& ptr)
\r
424 return p_.owner_before(ptr.p_);
\r
428 bool owner_before(const std::shared_ptr<T2>& ptr)
\r
430 return p_.owner_before(ptr);
\r
433 template<class D, class T2>
\r
434 D* get_deleter(shared_ptr<T2> const& ptr)
\r
436 return p_.get_deleter();
\r
439 std::shared_ptr<T> p_;
\r
442 template<class T, class T2>
\r
443 bool operator==(const shared_ptr<T>& a, const shared_ptr<T2>& b)
\r
445 return a.get() == b.get();
\r
448 template<class T, class T2>
\r
449 bool operator==(const std::shared_ptr<T>& a, const shared_ptr<T2>& b)
\r
451 return a.get() == b.get();
\r
454 template<class T, class T2>
\r
455 bool operator==(const shared_ptr<T>& a, const std::shared_ptr<T2>& b)
\r
457 return a.get() == b.get();
\r
460 template<class T, class T2>
\r
461 bool operator!=(const shared_ptr<T>& a, const shared_ptr<T2>& b)
\r
463 return a.get() != b.get();
\r
466 template<class T, class T2>
\r
467 bool operator!=(const std::shared_ptr<T>& a, const shared_ptr<T2>& b)
\r
469 return a.get() != b.get();
\r
472 template<class T, class T2>
\r
473 bool operator!=(const shared_ptr<T>& a, const std::shared_ptr<T2>& b)
\r
475 return a.get() != b.get();
\r
478 template<class T, class T2>
\r
479 bool operator<(const shared_ptr<T>& a, const shared_ptr<T2>& b)
\r
481 return a.get() < b.get();
\r
484 template<class T, class T2>
\r
485 bool operator<(const std::shared_ptr<T>& a, const shared_ptr<T2>& b)
\r
487 return a.get() < b.get();
\r
490 template<class T, class T2>
\r
491 bool operator<(const shared_ptr<T>& a, const std::shared_ptr<T2>& b)
\r
493 return a.get() < b.get();
\r
496 template<class T, class T2>
\r
497 bool operator>(const shared_ptr<T>& a, const shared_ptr<T2>& b)
\r
499 return a.get() > b.get();
\r
502 template<class T, class T2>
\r
503 bool operator>(const std::shared_ptr<T>& a, const shared_ptr<T2>& b)
\r
505 return a.get() > b.get();
\r
508 template<class T, class T2>
\r
509 bool operator>(const shared_ptr<T>& a, const std::shared_ptr<T2>& b)
\r
511 return a.get() > b.get();
\r
514 template<class T, class T2>
\r
515 bool operator>=(const shared_ptr<T>& a, const shared_ptr<T2>& b)
\r
517 return a.get() >= b.get();
\r
520 template<class T, class T2>
\r
521 bool operator>=(const std::shared_ptr<T>& a, const shared_ptr<T2>& b)
\r
523 return a.get() >= b.get();
\r
526 template<class T, class T2>
\r
527 bool operator>=(const shared_ptr<T>& a, const std::shared_ptr<T2>& b)
\r
529 return a.get() >= b.get();
\r
532 template<class T, class T2>
\r
533 bool operator<=(const shared_ptr<T>& a, const shared_ptr<T2>& b)
\r
535 return a.get() <= b.get();
\r
538 template<class T, class T2>
\r
539 bool operator<=(const std::shared_ptr<T>& a, const shared_ptr<T2>& b)
\r
541 return a.get() <= b.get();
\r
544 template<class T, class T2>
\r
545 bool operator<=(const shared_ptr<T>& a, const std::shared_ptr<T2>& b)
\r
547 return a.get() <= b.get();
\r
550 template<class E, class T, class T2>
\r
551 std::basic_ostream<E, T>& operator<<(std::basic_ostream<E, T>& oT2t, const shared_ptr<T2>& p)
\r
553 return oT2t << p.get();
\r
557 void swap(shared_ptr<T>& a, shared_ptr<T>& b)
\r
563 T* get_pointer(shared_ptr<T> const& p)
\r
568 template <class T, class T2>
\r
569 shared_ptr<T> static_pointer_cast(const shared_ptr<T2>& p)
\r
571 return shared_ptr<T>(std::static_pointer_cast<T>(std::shared_ptr<T2>(p)));
\r
574 template <class T, class T2>
\r
575 shared_ptr<T> const_pointer_cast(const shared_ptr<T2>& p)
\r
577 return shared_ptr<T>(std::const_pointer_cast<T>(std::shared_ptr<T2>(p)));
\r
580 template <class T, class T2>
\r
581 shared_ptr<T> dynamic_pointer_cast(const shared_ptr<T2>& p)
\r
583 aT2to temp = std::dynamic_pointer_cast<T>(std::shared_ptr<T2>(p));
\r
585 throw std::bad_cast();
\r
586 return shared_ptr<T>(std::move(temp));
\r
590 // enable_safe_this
\r
592 // A shared_ptr version of enable_shared_from_this.
\r
593 // So that an object may get shared_ptr objects to itself.
\r
597 class enable_shared_from_this : public std::enable_shared_from_this<T>
\r
600 shared_ptr<T> shared_from_this()
\r
602 return shared_ptr<T>(std::enable_shared_from_this<T>::shared_from_this());
\r
605 shared_ptr<T const> shared_from_this() const
\r
607 return shared_ptr<T const>(std::enable_shared_from_this<T>::shared_from_this());
\r
610 enable_shared_from_this()
\r
614 enable_shared_from_this(const enable_shared_from_this&)
\r
618 enable_shared_from_this& operator=(const enable_shared_from_this&)
\r
623 ~enable_shared_from_this()
\r
631 // shared_ptr eqT2ivalents to make_shared
\r
634 template<typename T>
\r
635 shared_ptr<T> make_shared_ptr(std::unique_ptr<T>&& ptr)
\r
637 return shared_ptr<T>(std::move(ptr));
\r
640 template<typename T>
\r
641 shared_ptr<T> make_shared_ptr(std::shared_ptr<T> ptr)
\r
643 return shared_ptr<T>(std::move(ptr));
\r
646 template<typename T>
\r
647 shared_ptr<T> make_shared()
\r
649 return shared_ptr<T>(std::make_shared<T>());
\r
652 template<typename T, typename P0>
\r
653 shared_ptr<T> make_shared(P0&& p0)
\r
655 return shared_ptr<T>(std::make_shared<T>(std::forward<P0>(p0)));
\r
658 template<typename T, typename P0, typename P1>
\r
659 shared_ptr<T> make_shared(P0&& p0, P1&& p1)
\r
661 return shared_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1)));
\r
664 template<typename T, typename P0, typename P1, typename P2>
\r
665 shared_ptr<T> make_shared(P0&& p0, P1&& p1, P2&& p2)
\r
667 return shared_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2)));
\r
670 template<typename T, typename P0, typename P1, typename P2, typename P3>
\r
671 shared_ptr<T> make_shared(P0&& p0, P1&& p1, P2&& p2, P3&& p3)
\r
673 return shared_ptr<T>(std::make_shared<T>(std::forward<P0>(p0), std::forward<P1>(p1), std::forward<P2>(p2), std::forward<P3>(p3)));
\r
676 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4>
\r
677 shared_ptr<T> make_shared(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4)
\r
679 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)));
\r
682 template<typename T, typename P0, typename P1, typename P2, typename P3, typename P4, typename P5>
\r
683 shared_ptr<T> make_shared(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&& p4, P5&& p5)
\r
685 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)));
\r
688 template<typename T>
\r
689 shared_ptr<T>::shared_ptr()
\r
690 : p_(make_shared<T>())
\r