9 #ifndef EAGINE_DEEP_COPY_PTR_HPP
10 #define EAGINE_DEEP_COPY_PTR_HPP
13 #include <type_traits>
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>();
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);
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>());
36 class deep_copy_ptr :
public std::unique_ptr<T> {
38 using _base = std::unique_ptr<T>;
39 _base& _self() noexcept {
44 deep_copy_ptr() noexcept = default;
45 ~deep_copy_ptr() noexcept = default;
47 deep_copy_ptr(deep_copy_ptr&&) noexcept = default;
48 deep_copy_ptr& operator=(deep_copy_ptr&&) noexcept = default;
50 deep_copy_ptr(std::unique_ptr<T>&& temp)
51 : _base(std::move(temp)) {}
53 deep_copy_ptr(
const std::unique_ptr<T>& that)
54 : _base(make_deep_ptr_copy(that)) {}
56 deep_copy_ptr(
const deep_copy_ptr& that)
57 : _base(make_deep_ptr_copy(that)) {}
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)) {}
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)) {}
71 deep_copy_ptr& operator=(
const deep_copy_ptr& that) {
72 _self() = make_deep_ptr_copy(that);
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)...));
84 #endif // EAGINE_DEEP_COPY_PTR_HPP