-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathref_ptr.hpp
More file actions
196 lines (182 loc) · 6.92 KB
/
ref_ptr.hpp
File metadata and controls
196 lines (182 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// itlib-ref_ptr v1.01
//
// A ref-counting smart pointer with a stable use_count
//
// SPDX-License-Identifier: MIT
// MIT License:
// Copyright(c) 2026 Borislav Stanimirov
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files(the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and / or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions :
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//
// VERSION HISTORY
//
// 1.01 (2026-02-03) * nullptr_t constructor and assignment
// * _as_shared_ptr_unsafe return ref to avoid copy
// 1.00 (2026-01-31) Initial release
//
//
// DOCUMENTATION
//
// Simply include this file wherever you need.
//
// ref_ptr is a smart, ref-couting pointer im most ways equivalent to
// std::shared_ptr, but without weak_ptr support.
//
// It is currently implemented through shared_ptr, but can be changed to an
// optimized custom implementation in the future.
// Since there is no weak_ptr, use_count() == 1 is reliable and can be used to
// determine if the state is unique.
//
// An important use case for this is copy-on-write implementations.
// ref_ptr<const T> would be a detached read-only view of a state.//
// Since unique() is reliable, a safe "promotion" via a const_cast is possible
// (We're not doing this until we have a good motivational use case)
//
// Note that ref_ptr is just a facility and not a complete CoW implementation.
// While ref_ptr<const T> will likely "spill" out of an implementation as the
// read-only view of the state, ref_ptr<mutable T> will typically be used only
// internally within the implementation where copy guards and other logic will
// be implemented as needed
//
// NO VOID SUPPORT (YET)
// Note that ref_ptr<void> is not supported at the moment.
// It's not a huge additional effort, but there is no immediate use case for it
// either. The main use case is CoW and complete type erasure in this context
// is simply not very useful.
//
// API:
// * everything from std::shared_ptr including...
// * bool unique() const noexcept; - reliable uniqueness check (which used to
// be in std::shared_ptr but was deprecated and removed)
// * _as_shared_ptr_unsafe() - to get a shared_ptr as a last resort or to
// interop with existing APIs
// * external factory functions:
// * make_ref_ptr<T>(Args&&... args) - corresponds to std::make_shared<T>
// * make_ref_ptr_from(T&& obj) - creates a ref_ptr by copying/moving a value
//
// Future Ideas:
// * void support
// * static/const/dynamic casts
// * strong_ref_ptr - a non-nullable ref_ptr variant
//
//
// TESTS
//
// You can find unit tests in the official repo:
// https://github.com/iboB/itlib/blob/master/test/
//
#pragma once
#include <memory>
#include <cstddef>
#include <type_traits>
namespace itlib {
template <typename T>
class ref_ptr : private std::shared_ptr<T> {
template <typename U>
friend class ref_ptr;
using super = std::shared_ptr<T>;
// only used when creating a new ref_ptr
explicit ref_ptr(std::shared_ptr<T> ptr)
: super(std::move(ptr))
{}
public:
ref_ptr() = default;
ref_ptr(const ref_ptr&) = default;
ref_ptr& operator=(const ref_ptr&) = default;
ref_ptr(ref_ptr&&) noexcept = default;
ref_ptr& operator=(ref_ptr&&) noexcept = default;
ref_ptr(std::nullptr_t)
: super(nullptr)
{}
ref_ptr& operator=(std::nullptr_t) {
super::operator=(nullptr);
return *this;
}
template <typename U>
ref_ptr(const ref_ptr<U>& ptr) noexcept
: super(ptr)
{}
template <typename U>
ref_ptr& operator=(const ref_ptr<U>& ptr) noexcept {
super::operator=(ptr);
return *this;
}
template <typename U>
ref_ptr(const ref_ptr<U>&& ptr) noexcept
: super(std::move(ptr))
{}
template <typename U>
ref_ptr& operator=(const ref_ptr<U>&& ptr) noexcept {
super::operator=(std::move(ptr));
return *this;
}
using super::get;
using super::operator->;
using super::operator*;
using super::operator bool;
using super::use_count;
using super::reset;
// we could just do an operator <=>, but let's support pre-c++20
template <typename U>
bool operator==(const ref_ptr<U>& other) const noexcept { return get() == other.get(); }
template <typename U>
bool operator!=(const ref_ptr<U>& other) const noexcept { return get() != other.get(); }
template <typename U>
bool operator<(const ref_ptr<U>& other) const noexcept { return get() < other.get(); }
template <typename U>
bool operator<=(const ref_ptr<U>& other) const noexcept { return get() <= other.get(); }
template <typename U>
bool operator>(const ref_ptr<U>& other) const noexcept { return get() > other.get(); }
template <typename U>
bool operator>=(const ref_ptr<U>& other) const noexcept { return get() >= other.get(); }
bool unique() const noexcept {
return use_count() == 1;
}
template <typename... Args>
static ref_ptr make(Args&&... args) {
return ref_ptr(std::make_shared<T>(std::forward<Args>(args)...));
}
// only use as a last resort
// when ref_ptr needs to be provided to an existing API relying on shared_ptr
// be sure that the leaked shared_ptr will not be used to create weak_ptr-s
const std::shared_ptr<T>& _as_shared_ptr_unsafe() const& noexcept {
return *this;
}
std::shared_ptr<T> _as_shared_ptr_unsafe() && noexcept {
return std::move(*this);
}
static ref_ptr<T> _from_shared_ptr_unsafe(std::shared_ptr<T> ptr) noexcept {
return ref_ptr<T>(std::move(ptr));
}
};
template <typename T, typename... Args>
ref_ptr<T> make_ref_ptr(Args&&... args) {
return ref_ptr<T>::make(std::forward<Args>(args)...);
}
template <typename T>
auto make_ref_ptr_from(T&& obj) -> ref_ptr<typename std::remove_reference<T>::type>{
return ref_ptr<typename std::remove_reference<T>::type>::make(std::forward<T>(obj));
}
template <typename T>
ref_ptr<T> _ref_ptr_from_shared_ptr_unsafe(std::shared_ptr<T> ptr) noexcept {
return ref_ptr<T>::_from_shared_ptr_unsafe(std::move(ptr));
}
} // namespace itlib