HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
weak_or_unique_ptr.hpp
1// Copyright Take Vos 2021
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include <memory>
8
9namespace hi::inline v1 {
10
24template<typename T>
26public:
27 using value_type = T;
28 using pointer = value_type *;
29
30 ~weak_or_unique_ptr() = default;
31 constexpr weak_or_unique_ptr() noexcept = default;
33
34 weak_or_unique_ptr &operator=(std::nullptr_t) noexcept
35 {
36 _shared_ptr = {};
37 _weak_ptr = {};
38 return *this;
39 }
40
41 template<typename Y>
42 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>) weak_or_unique_ptr(weak_or_unique_ptr<Y> const &other)
43 noexcept : _shared_ptr(), _weak_ptr(other._weak_ptr) {}
44
45 template<typename Y>
46 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>) weak_or_unique_ptr &
47 operator=(weak_or_unique_ptr<Y> const &other) noexcept
48 {
49 _shared_ptr = {};
50 _weak_ptr = other._weak_ptr;
51 return *this;
52 }
53
54 template<typename Y>
55 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>) weak_or_unique_ptr(weak_or_unique_ptr<Y> &&other)
56 noexcept : _shared_ptr(other._shared_ptr), _weak_ptr(other._weak_ptr) {}
57
58 template<typename Y>
59 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>) weak_or_unique_ptr &
60 operator=(weak_or_unique_ptr<Y> &&other) noexcept
61 {
62 _shared_ptr = other._shared_ptr;
63 _weak_ptr = other._weak_ptr;
64 return *this;
65 }
66
67 template<typename Y>
68 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>) weak_or_unique_ptr(std::unique_ptr<Y> &&other)
69 noexcept : _shared_ptr(std::move(other)), _weak_ptr(_shared_ptr) {}
70
71 template<typename Y>
72 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>) weak_or_unique_ptr &
73 operator=(std::unique_ptr<Y> &&other) noexcept
74 {
75 _shared_ptr = std::move(other);
76 _weak_ptr = _shared_ptr;
77 return *this;
78 }
79
80 template<typename Y>
81 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>) weak_or_unique_ptr(std::shared_ptr<Y> const &other)
82 noexcept : _shared_ptr(), _weak_ptr(other) {}
83
84 template<typename Y>
85 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>) weak_or_unique_ptr &
86 operator=(std::shared_ptr<Y> const &other) noexcept
87 {
88 _shared_ptr = nullptr;
89 _weak_ptr = other;
90 return *this;
91 }
92
93 template<typename Y>
94 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>) weak_or_unique_ptr(std::weak_ptr<Y> const &other)
95 noexcept : _shared_ptr(), _weak_ptr(other) {}
96
97 template<typename Y>
98 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>) weak_or_unique_ptr(std::weak_ptr<Y> &&other)
99 noexcept : _shared_ptr(), _weak_ptr(std::move(other)) {}
100
101 template<typename Y>
102 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>) weak_or_unique_ptr &
103 operator=(std::weak_ptr<Y> const &other) noexcept
104 {
105 _shared_ptr = nullptr;
106 _weak_ptr = other;
107 return *this;
108 }
109
110 template<typename Y>
111 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>) weak_or_unique_ptr &
112 operator=(std::weak_ptr<Y> &&other) noexcept
113 {
114 _shared_ptr = nullptr;
115 _weak_ptr = std::move(other);
116 return *this;
117 }
118
119 void reset() noexcept
120 {
121 _shared_ptr = nullptr;
122 _weak_ptr = nullptr;
123 }
124
125 [[nodiscard]] bool expired() const noexcept
126 {
127 return _weak_ptr.expired();
128 }
129
130 [[nodiscard]] std::shared_ptr<value_type> lock() const noexcept
131 {
132 return _weak_ptr.lock();
133 }
134
135private:
136 std::shared_ptr<T> _shared_ptr;
137 std::weak_ptr<T> _weak_ptr;
138
139 template<typename>
140 friend class weak_or_unique_ptr;
141};
142
143} // namespace hi::inline v1
Class that hold either a weak_ptr or a unique_ptr This class is to hold a weak_ptr,...
Definition weak_or_unique_ptr.hpp:25
Definition concepts.hpp:35
T move(T... args)