OGLplus  (0.59.0) a C++ wrapper for rendering APIs

deep_copy_ptr.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_DEEP_COPY_PTR_HPP
10 #define EAGINE_DEEP_COPY_PTR_HPP
11 
12 #include <memory>
13 #include <type_traits>
14 
15 namespace eagine {
16 
17 template <typename T>
18 static inline std::unique_ptr<T>
19 make_deep_ptr_copy(const std::unique_ptr<T>& that, std::true_type) {
20  return that ? that->copy() : std::unique_ptr<T>();
21 }
22 
23 template <typename T>
24 static inline std::unique_ptr<T>
25 make_deep_ptr_copy(const std::unique_ptr<T>& that, std::false_type) {
26  return std::unique_ptr<T>(that ? new T(*that) : nullptr);
27 }
28 
29 template <typename T>
30 static inline std::unique_ptr<T>
31 make_deep_ptr_copy(const std::unique_ptr<T>& that) {
32  return make_deep_ptr_copy(that, std::is_polymorphic<T>());
33 }
34 
35 template <typename T>
36 class deep_copy_ptr : public std::unique_ptr<T> {
37 private:
38  using _base = std::unique_ptr<T>;
39  _base& _self() noexcept {
40  return *this;
41  }
42 
43 public:
44  deep_copy_ptr() noexcept = default;
45  ~deep_copy_ptr() noexcept = default;
46 
47  deep_copy_ptr(deep_copy_ptr&&) noexcept = default;
48  deep_copy_ptr& operator=(deep_copy_ptr&&) noexcept = default;
49 
50  deep_copy_ptr(std::unique_ptr<T>&& temp)
51  : _base(std::move(temp)) {}
52 
53  deep_copy_ptr(const std::unique_ptr<T>& that)
54  : _base(make_deep_ptr_copy(that)) {}
55 
56  deep_copy_ptr(const deep_copy_ptr& that)
57  : _base(make_deep_ptr_copy(that)) {}
58 
59  template <
60  typename D,
61  typename = std::enable_if_t<std::is_base_of<T, D>::value>>
62  deep_copy_ptr(deep_copy_ptr<D>&& that)
63  : _base(std::move(that)) {}
64 
65  template <
66  typename D,
67  typename = std::enable_if_t<std::is_base_of<T, D>::value>>
68  deep_copy_ptr(const deep_copy_ptr<D>& that)
69  : _base(make_deep_ptr_copy(that)) {}
70 
71  deep_copy_ptr& operator=(const deep_copy_ptr& that) {
72  _self() = make_deep_ptr_copy(that);
73  return *this;
74  }
75 };
76 
77 template <typename T, typename... P>
78 static inline deep_copy_ptr<T> make_deep_copy_ptr(P&&... p) {
79  return std::unique_ptr<T>(new T(std::forward<P>(p)...));
80 }
81 
82 } // namespace eagine
83 
84 #endif // EAGINE_DEEP_COPY_PTR_HPP
Common code is placed in this namespace.
Definition: eagine.hpp:21

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).