stlab-copy-on-write 1.0.6
Copy-on-write wrapper for any type
Loading...
Searching...
No Matches
copy_on_write.hpp
Go to the documentation of this file.
1/*
2 Copyright 2013 Adobe
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5*/
6/**************************************************************************************************/
7
16
17#ifndef STLAB_COPY_ON_WRITE_HPP
18#define STLAB_COPY_ON_WRITE_HPP
19
20/**************************************************************************************************/
21
26
84
97
98/**************************************************************************************************/
99
100#include <atomic>
101#include <cassert>
102#include <cstddef>
103#include <type_traits>
104#include <utility>
105
106/**************************************************************************************************/
107
111namespace stlab {
112
113/**************************************************************************************************/
114
123template <typename T> // T models Regular
125 struct model {
126 std::atomic<std::size_t> _count{1};
127
128 model() noexcept(std::is_nothrow_constructible_v<T>) = default;
129
130 template <class... Args>
131 explicit model(Args&&... args) noexcept(std::is_nothrow_constructible_v<T, Args&&...>) :
132 _value(std::forward<Args>(args)...) {}
133
134 T _value;
135 };
136
137 model* _self;
138
139 template <class U>
140 using disable_copy = std::enable_if_t<!std::is_same_v<std::decay_t<U>, copy_on_write>>*;
141
142 template <typename U>
143 using disable_copy_assign =
144 std::enable_if_t<!std::is_same_v<std::decay_t<U>, copy_on_write>, copy_on_write&>;
145
146 auto default_model() noexcept(std::is_nothrow_constructible_v<T>) -> model* {
147 static model default_s;
148 return &default_s;
149 }
150
151public:
157 /* [[deprecated]] */ using value_type = T;
158
162 using element_type = T;
164
170 copy_on_write() noexcept(std::is_nothrow_constructible_v<T>) {
171 _self = default_model();
172
173 // coverity[useless_call]
174 _self->_count.fetch_add(1, std::memory_order_relaxed);
175 }
176
180 template <class U>
181 copy_on_write(U&& x, disable_copy<U> = nullptr) : _self(new model(std::forward<U>(x))) {}
182
187 template <class U, class V, class... Args>
188 copy_on_write(U&& x, V&& y, Args&&... args) :
189 _self(new model(std::forward<U>(x), std::forward<V>(y), std::forward<Args>(args)...)) {}
190
194 copy_on_write(const copy_on_write& x) noexcept : _self(x._self) {
195 assert(_self && "FATAL (sparent) : using a moved copy_on_write object");
196
197 // coverity[useless_call]
198 _self->_count.fetch_add(1, std::memory_order_relaxed);
199 }
200
204 copy_on_write(copy_on_write&& x) noexcept : _self{std::exchange(x._self, nullptr)} {
205 assert(_self && "WARNING (sparent) : using a moved copy_on_write object");
206 }
207
212 assert(!_self || ((_self->_count > 0) && "FATAL (sparent) : double delete"));
213 if (_self && (_self->_count.fetch_sub(1, std::memory_order_release) == 1)) {
214 std::atomic_thread_fence(std::memory_order_acquire);
215 if constexpr (std::is_default_constructible_v<element_type>) {
216 assert(_self != default_model());
217 }
218 delete _self;
219 }
220 }
221
225 auto operator=(const copy_on_write& x) noexcept -> copy_on_write& {
226 // self-assignment is not allowed to disable cert-oop54-cpp warning (and is likely a bug)
227 assert(this != &x && "self-assignment is not allowed");
228 return *this = copy_on_write(x);
229 }
230
234 auto operator=(copy_on_write&& x) noexcept -> copy_on_write& {
235 auto tmp{std::move(x)};
236 swap(*this, tmp);
237 return *this;
238 }
239
244 template <class U>
245 auto operator=(U&& x) -> disable_copy_assign<U> {
246 if (_self && unique()) {
247 _self->_value = std::forward<U>(x);
248 return *this;
249 }
250
251 return *this = copy_on_write(std::forward<U>(x));
252 }
253
256
263 auto write() -> element_type& {
264 if (!unique()) *this = copy_on_write(read());
265
266 return _self->_value;
267 }
268
282 template <class Transform, class Inplace>
283 auto write(Transform transform, Inplace inplace) -> element_type& {
284 static_assert(std::is_invocable_r_v<T, Transform, const T&>,
285 "Transform must be invocable with const T&");
286 static_assert(std::is_invocable_r_v<void, Inplace, T&>,
287 "Inplace must be invocable with T&");
288
289 if (!unique()) {
290 *this = copy_on_write(transform(read()));
291 } else {
292 inplace(_self->_value);
293 }
294
295 return _self->_value;
296 }
297
301 [[nodiscard]] auto read() const noexcept -> const element_type& {
302 assert(_self && "FATAL (sparent) : using a moved copy_on_write object");
303
304 return _self->_value;
305 }
306
310 operator const element_type&() const noexcept { return read(); }
311
315 auto operator*() const noexcept -> const element_type& { return read(); }
316
320 auto operator->() const noexcept -> const element_type* { return &read(); }
321
327 [[nodiscard]] auto unique() const noexcept -> bool {
328 assert(_self && "FATAL (sparent) : using a moved copy_on_write object");
329
330 return _self->_count.load(std::memory_order_acquire) == 1;
331 }
332
337 [[deprecated]] [[nodiscard]] auto unique_instance() const noexcept -> bool { return unique(); }
338
342 [[nodiscard]] auto identity(const copy_on_write& x) const noexcept -> bool {
343 assert((_self && x._self) && "FATAL (sparent) : using a moved copy_on_write object");
344
345 return _self == x._self;
346 }
347
350
357 friend inline void swap(copy_on_write& x, copy_on_write& y) noexcept {
358 std::swap(x._self, y._self);
359 }
360
365 friend inline auto operator<(const copy_on_write& x, const copy_on_write& y) noexcept -> bool {
366 return !x.identity(y) && (*x < *y);
367 }
368
369 friend inline auto operator<(const copy_on_write& x, const element_type& y) noexcept -> bool {
370 return *x < y;
371 }
372
373 friend inline auto operator<(const element_type& x, const copy_on_write& y) noexcept -> bool {
374 return x < *y;
375 }
376
377 friend inline auto operator>(const copy_on_write& x, const copy_on_write& y) noexcept -> bool {
378 return y < x;
379 }
380
381 friend inline auto operator>(const copy_on_write& x, const element_type& y) noexcept -> bool {
382 return y < x;
383 }
384
385 friend inline auto operator>(const element_type& x, const copy_on_write& y) noexcept -> bool {
386 return y < x;
387 }
388
389 friend inline auto operator<=(const copy_on_write& x, const copy_on_write& y) noexcept -> bool {
390 return !(y < x);
391 }
392
393 friend inline auto operator<=(const copy_on_write& x, const element_type& y) noexcept -> bool {
394 return !(y < x);
395 }
396
397 friend inline auto operator<=(const element_type& x, const copy_on_write& y) noexcept -> bool {
398 return !(y < x);
399 }
400
401 friend inline auto operator>=(const copy_on_write& x, const copy_on_write& y) noexcept -> bool {
402 return !(x < y);
403 }
404
405 friend inline auto operator>=(const copy_on_write& x, const element_type& y) noexcept -> bool {
406 return !(x < y);
407 }
408
409 friend inline auto operator>=(const element_type& x, const copy_on_write& y) noexcept -> bool {
410 return !(x < y);
411 }
412
413 friend inline auto operator==(const copy_on_write& x, const copy_on_write& y) noexcept -> bool {
414 return x.identity(y) || (*x == *y);
415 }
416
417 friend inline auto operator==(const copy_on_write& x, const element_type& y) noexcept -> bool {
418 return *x == y;
419 }
420
421 friend inline auto operator==(const element_type& x, const copy_on_write& y) noexcept -> bool {
422 return x == *y;
423 }
424
425 friend inline auto operator!=(const copy_on_write& x, const copy_on_write& y) noexcept -> bool {
426 return !(x == y);
427 }
428
429 friend inline auto operator!=(const copy_on_write& x, const element_type& y) noexcept -> bool {
430 return !(x == y);
431 }
432
433 friend inline auto operator!=(const element_type& x, const copy_on_write& y) noexcept -> bool {
434 return !(x == y);
435 }
436
438};
439/**************************************************************************************************/
440
441} // namespace stlab
442
443/**************************************************************************************************/
444
445#endif
446
447/**************************************************************************************************/
copy_on_write(U &&x, V &&y, Args &&... args)
Constructs a new instance by forwarding multiple arguments to the wrapped value constructor.
Definition copy_on_write.hpp:188
auto operator=(copy_on_write &&x) noexcept -> copy_on_write &
Move assignment operator that takes ownership of the source object's data.
Definition copy_on_write.hpp:234
copy_on_write(U &&x, disable_copy< U >=nullptr)
Constructs a new instance by forwarding arguments to the wrapped value constructor.
Definition copy_on_write.hpp:181
auto operator=(const copy_on_write &x) noexcept -> copy_on_write &
Copy assignment operator that shares the underlying data with the source object.
Definition copy_on_write.hpp:225
~copy_on_write()
Destructor.
Definition copy_on_write.hpp:211
copy_on_write() noexcept(std::is_nothrow_constructible_v< T >)
Default constructs the wrapped value.
Definition copy_on_write.hpp:170
auto operator=(U &&x) -> disable_copy_assign< U >
Assigns a new value to the wrapped object, optimizing for in-place assignment when unique.
Definition copy_on_write.hpp:245
copy_on_write(const copy_on_write &x) noexcept
Copy constructor that shares the underlying data with the source object.
Definition copy_on_write.hpp:194
copy_on_write(copy_on_write &&x) noexcept
Move constructor that takes ownership of the source object's data.
Definition copy_on_write.hpp:204
T element_type
The type of value stored.
Definition copy_on_write.hpp:162
T value_type
Definition copy_on_write.hpp:157
friend void swap(copy_on_write &x, copy_on_write &y) noexcept
Efficiently swaps the contents of two copy_on_write objects.
Definition copy_on_write.hpp:357
friend auto operator<(const element_type &x, const copy_on_write &y) noexcept -> bool
Comparisons can be done with the underlying value or the copy_on_write object.
Definition copy_on_write.hpp:373
friend auto operator!=(const element_type &x, const copy_on_write &y) noexcept -> bool
Comparisons can be done with the underlying value or the copy_on_write object.
Definition copy_on_write.hpp:433
friend auto operator<(const copy_on_write &x, const copy_on_write &y) noexcept -> bool
Comparisons can be done with the underlying value or the copy_on_write object.
Definition copy_on_write.hpp:365
friend auto operator>=(const copy_on_write &x, const copy_on_write &y) noexcept -> bool
Comparisons can be done with the underlying value or the copy_on_write object.
Definition copy_on_write.hpp:401
friend auto operator<=(const element_type &x, const copy_on_write &y) noexcept -> bool
Comparisons can be done with the underlying value or the copy_on_write object.
Definition copy_on_write.hpp:397
friend auto operator!=(const copy_on_write &x, const element_type &y) noexcept -> bool
Comparisons can be done with the underlying value or the copy_on_write object.
Definition copy_on_write.hpp:429
friend auto operator>=(const element_type &x, const copy_on_write &y) noexcept -> bool
Comparisons can be done with the underlying value or the copy_on_write object.
Definition copy_on_write.hpp:409
friend auto operator==(const copy_on_write &x, const copy_on_write &y) noexcept -> bool
Comparisons can be done with the underlying value or the copy_on_write object.
Definition copy_on_write.hpp:413
friend auto operator<(const copy_on_write &x, const element_type &y) noexcept -> bool
Comparisons can be done with the underlying value or the copy_on_write object.
Definition copy_on_write.hpp:369
friend auto operator>=(const copy_on_write &x, const element_type &y) noexcept -> bool
Comparisons can be done with the underlying value or the copy_on_write object.
Definition copy_on_write.hpp:405
friend auto operator<=(const copy_on_write &x, const element_type &y) noexcept -> bool
Comparisons can be done with the underlying value or the copy_on_write object.
Definition copy_on_write.hpp:393
friend auto operator>(const copy_on_write &x, const copy_on_write &y) noexcept -> bool
Comparisons can be done with the underlying value or the copy_on_write object.
Definition copy_on_write.hpp:377
friend auto operator>(const element_type &x, const copy_on_write &y) noexcept -> bool
Comparisons can be done with the underlying value or the copy_on_write object.
Definition copy_on_write.hpp:385
friend auto operator<=(const copy_on_write &x, const copy_on_write &y) noexcept -> bool
Comparisons can be done with the underlying value or the copy_on_write object.
Definition copy_on_write.hpp:389
friend auto operator!=(const copy_on_write &x, const copy_on_write &y) noexcept -> bool
Comparisons can be done with the underlying value or the copy_on_write object.
Definition copy_on_write.hpp:425
friend auto operator==(const copy_on_write &x, const element_type &y) noexcept -> bool
Comparisons can be done with the underlying value or the copy_on_write object.
Definition copy_on_write.hpp:417
friend auto operator==(const element_type &x, const copy_on_write &y) noexcept -> bool
Comparisons can be done with the underlying value or the copy_on_write object.
Definition copy_on_write.hpp:421
friend auto operator>(const copy_on_write &x, const element_type &y) noexcept -> bool
Comparisons can be done with the underlying value or the copy_on_write object.
Definition copy_on_write.hpp:381
auto write(Transform transform, Inplace inplace) -> element_type &
If the object is not unique, the transform is applied to the underlying value to copy it and a refere...
Definition copy_on_write.hpp:283
auto operator*() const noexcept -> const element_type &
Dereference operator that returns a const reference to the underlying value.
Definition copy_on_write.hpp:315
auto identity(const copy_on_write &x) const noexcept -> bool
Returns true if this object and the given object share the same underlying data.
Definition copy_on_write.hpp:342
auto read() const noexcept -> const element_type &
Returns a const reference to the underlying value for read-only access.
Definition copy_on_write.hpp:301
auto unique_instance() const noexcept -> bool
Definition copy_on_write.hpp:337
auto operator->() const noexcept -> const element_type *
Arrow operator that returns a const pointer to the underlying value.
Definition copy_on_write.hpp:320
auto unique() const noexcept -> bool
Returns true if this is the only reference to the underlying object.
Definition copy_on_write.hpp:327
auto write() -> element_type &
Obtains a non-const reference to the underlying value.
Definition copy_on_write.hpp:263
Definition copy_on_write.hpp:111