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 tt {
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 *>)
43 weak_or_unique_ptr(weak_or_unique_ptr<Y> const &other) noexcept : _shared_ptr(), _weak_ptr(other._weak_ptr)
44 {
45 }
46
47 template<typename Y>
48 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>)
49 weak_or_unique_ptr &operator=(weak_or_unique_ptr<Y> const &other) noexcept
50 {
51 _shared_ptr = {};
52 _weak_ptr = other._weak_ptr;
53 return *this;
54 }
55
56 template<typename Y>
57 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>)
58 weak_or_unique_ptr(weak_or_unique_ptr<Y> &&other) noexcept : _shared_ptr(other._shared_ptr), _weak_ptr(other._weak_ptr)
59 {
60 }
61
62 template<typename Y>
63 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>)
64 weak_or_unique_ptr &operator=(weak_or_unique_ptr<Y> &&other) noexcept
65 {
66 _shared_ptr = other._shared_ptr;
67 _weak_ptr = other._weak_ptr;
68 return *this;
69 }
70
71 template<typename Y>
72 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>)
73 weak_or_unique_ptr(std::unique_ptr<Y> &&other) noexcept : _shared_ptr(std::move(other)), _weak_ptr(_shared_ptr)
74 {
75 }
76
77 template<typename Y>
78 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>)
79 weak_or_unique_ptr &operator=(std::unique_ptr<Y> &&other) noexcept
80 {
81 _shared_ptr = std::move(other);
82 _weak_ptr = _shared_ptr;
83 return *this;
84 }
85
86 template<typename Y>
87 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>)
88 weak_or_unique_ptr(std::shared_ptr<Y> const &other) noexcept :
89 _shared_ptr(), _weak_ptr(other)
90 {
91 }
92
93 template<typename Y>
94 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>)
95 weak_or_unique_ptr &operator=(std::shared_ptr<Y> const &other) noexcept
96 {
97 _shared_ptr = nullptr;
98 _weak_ptr = other;
99 return *this;
100 }
101
102 template<typename Y>
103 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>)
104 weak_or_unique_ptr(std::weak_ptr<Y> const &other) noexcept : _shared_ptr(), _weak_ptr(other)
105 {
106 }
107
108 template<typename Y>
109 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>)
110 weak_or_unique_ptr(std::weak_ptr<Y> &&other) noexcept : _shared_ptr(), _weak_ptr(std::move(other))
111 {
112 }
113
114 template<typename Y>
115 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>)
116 weak_or_unique_ptr &operator=(std::weak_ptr<Y> const &other) noexcept
117 {
118 _shared_ptr = nullptr;
119 _weak_ptr = other;
120 return *this;
121 }
122
123 template<typename Y>
124 requires(std::is_convertible_v<std::remove_cvref_t<Y> *, value_type *>)
125 weak_or_unique_ptr &operator=(std::weak_ptr<Y> &&other) noexcept
126 {
127 _shared_ptr = nullptr;
128 _weak_ptr = std::move(other);
129 return *this;
130 }
131
132 void reset() noexcept
133 {
134 _shared_ptr = nullptr;
135 _weak_ptr = nullptr;
136 }
137
138 [[nodiscard]] bool expired() const noexcept
139 {
140 return _weak_ptr.expired();
141 }
142
143 [[nodiscard]] std::shared_ptr<value_type> lock() const noexcept
144 {
145 return _weak_ptr.lock();
146 }
147
148private:
149 std::shared_ptr<T> _shared_ptr;
150 std::weak_ptr<T> _weak_ptr;
151
152 template<typename>
153 friend class weak_or_unique_ptr;
154};
155
156} // namespace tt
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:18
T move(T... args)