17#ifndef STLAB_COPY_ON_WRITE_HPP
18#define STLAB_COPY_ON_WRITE_HPP
103 std::atomic<std::size_t> _count{1};
105 model()
noexcept(std::is_nothrow_constructible_v<T>) =
default;
107 template <
class... Args>
108 explicit model(Args&&... args)
noexcept(std::is_nothrow_constructible_v<T, Args&&...>)
109 : _value(std::forward<Args>(args)...) {}
117 using disable_copy = std::enable_if_t<!std::is_same_v<std::decay_t<U>,
copy_on_write>>*;
119 template <
typename U>
120 using disable_copy_assign =
123 auto default_model()
noexcept(std::is_nothrow_constructible_v<T>) -> model* {
124 static model default_s;
143 _self = default_model();
146 _self->_count.fetch_add(1, std::memory_order_relaxed);
153 copy_on_write(U&& x, disable_copy<U> =
nullptr) : _self(new model(std::forward<U>(x))) {}
158 template <
class U,
class V,
class... Args>
160 : _self(new model(std::forward<U>(x), std::forward<V>(y), std::forward<Args>(args)...)) {}
166 assert(_self &&
"FATAL (sparent) : using a moved copy_on_write object");
169 _self->_count.fetch_add(1, std::memory_order_relaxed);
176 assert(_self &&
"WARNING (sparent) : using a moved copy_on_write object");
180 assert(!_self || ((_self->_count > 0) &&
"FATAL (sparent) : double delete"));
181 if (_self && (_self->_count.fetch_sub(1, std::memory_order_release) == 1)) {
182 std::atomic_thread_fence(std::memory_order_acquire);
183 if constexpr (std::is_default_constructible_v<element_type>) {
184 assert(_self != default_model());
195 assert(
this != &x &&
"self-assignment is not allowed");
203 auto tmp{std::move(x)};
214 _self->_value = std::forward<U>(x);
230 return _self->_value;
245 template <
class Transform,
class Inplace>
247 static_assert(std::is_invocable_r_v<T, Transform, const T&>,
248 "Transform must be invocable with const T&");
249 static_assert(std::is_invocable_r_v<void, Inplace, T&>,
250 "Inplace must be invocable with T&");
255 inplace(_self->_value);
258 return _self->_value;
265 assert(_self &&
"FATAL (sparent) : using a moved copy_on_write object");
267 return _self->_value;
290 [[nodiscard]]
auto unique() const noexcept ->
bool {
291 assert(_self &&
"FATAL (sparent) : using a moved copy_on_write object");
293 return _self->_count.load(std::memory_order_acquire) == 1;
306 assert((_self && x._self) &&
"FATAL (sparent) : using a moved copy_on_write object");
308 return _self == x._self;
315 std::swap(x._self, y._self);
323 return !x.identity(y) && (*x < *y);
371 return x.identity(y) || (*x == *y);
friend void swap(copy_on_write &x, copy_on_write &y) noexcept
friend auto operator<(const element_type &x, const copy_on_write &y) noexcept -> bool
copy_on_write(U &&x, V &&y, Args &&... args)
auto operator=(copy_on_write &&x) noexcept -> copy_on_write &
auto write(Transform transform, Inplace inplace) -> element_type &
friend auto operator!=(const element_type &x, const copy_on_write &y) noexcept -> bool
copy_on_write(U &&x, disable_copy< U >=nullptr)
friend auto operator<(const copy_on_write &x, const copy_on_write &y) noexcept -> bool
auto operator=(const copy_on_write &x) noexcept -> copy_on_write &
friend auto operator>=(const copy_on_write &x, const copy_on_write &y) noexcept -> bool
auto operator*() const noexcept -> const element_type &
auto identity(const copy_on_write &x) const noexcept -> bool
friend auto operator<=(const element_type &x, const copy_on_write &y) noexcept -> bool
auto read() const noexcept -> const element_type &
friend auto operator!=(const copy_on_write &x, const element_type &y) noexcept -> bool
friend auto operator>=(const element_type &x, const copy_on_write &y) noexcept -> bool
friend auto operator==(const copy_on_write &x, const copy_on_write &y) noexcept -> bool
friend auto operator<(const copy_on_write &x, const element_type &y) noexcept -> bool
auto unique_instance() const noexcept -> bool
friend auto operator>=(const copy_on_write &x, const element_type &y) noexcept -> bool
auto operator->() const noexcept -> const element_type *
friend auto operator<=(const copy_on_write &x, const element_type &y) noexcept -> bool
copy_on_write() noexcept(std::is_nothrow_constructible_v< T >)
auto unique() const noexcept -> bool
auto write() -> element_type &
friend auto operator>(const copy_on_write &x, const copy_on_write &y) noexcept -> bool
friend auto operator>(const element_type &x, const copy_on_write &y) noexcept -> bool
friend auto operator<=(const copy_on_write &x, const copy_on_write &y) noexcept -> bool
auto operator=(U &&x) -> disable_copy_assign< U >
friend auto operator!=(const copy_on_write &x, const copy_on_write &y) noexcept -> bool
copy_on_write(const copy_on_write &x) noexcept
friend auto operator==(const copy_on_write &x, const element_type &y) noexcept -> bool
friend auto operator==(const element_type &x, const copy_on_write &y) noexcept -> bool
friend auto operator>(const copy_on_write &x, const element_type &y) noexcept -> bool
copy_on_write(copy_on_write &&x) noexcept