forked from stlab/stlab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy_on_write.hpp
More file actions
216 lines (156 loc) · 6.58 KB
/
copy_on_write.hpp
File metadata and controls
216 lines (156 loc) · 6.58 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
Copyright 2013 Adobe
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
/**************************************************************************************************/
#ifndef STLAB_COPY_ON_WRITE_HPP
#define STLAB_COPY_ON_WRITE_HPP
/**************************************************************************************************/
#include <atomic>
#include <cassert>
#include <cstddef>
#include <utility>
/**************************************************************************************************/
namespace stlab {
/**************************************************************************************************/
template <typename T> // T models Regular
class copy_on_write {
struct model {
std::atomic<std::size_t> _count{1};
model() noexcept(std::is_nothrow_constructible_v<T>) = default;
template <class... Args>
explicit model(Args&&... args) noexcept(std::is_nothrow_constructible_v<T, Args&&...>) : _value(std::forward<Args>(args)...) {}
T _value;
};
model* _self;
template <class U>
using disable_copy = std::enable_if_t<!std::is_same<std::decay_t<U>, copy_on_write>::value>*;
template <typename U>
using disable_copy_assign =
std::enable_if_t<!std::is_same<std::decay_t<U>, copy_on_write>::value, copy_on_write&>;
public:
/* [[deprecated]] */ using value_type = T;
using element_type = T;
copy_on_write() noexcept(std::is_nothrow_constructible_v<T>) {
static model default_s;
_self = &default_s;
// coverity[useless_call]
++_self->_count;
}
template <class U>
copy_on_write(U&& x, disable_copy<U> = nullptr) : _self(new model(std::forward<U>(x))) {}
template <class U, class V, class... Args>
copy_on_write(U&& x, V&& y, Args&&... args)
: _self(new model(std::forward<U>(x), std::forward<V>(y), std::forward<Args>(args)...)) {}
copy_on_write(const copy_on_write& x) noexcept : _self(x._self) {
assert(_self && "FATAL (sparent) : using a moved copy_on_write object");
// coverity[useless_call]
++_self->_count;
}
copy_on_write(copy_on_write&& x) noexcept : _self(x._self) {
assert(_self && "WARNING (sparent) : using a moved copy_on_write object");
x._self = nullptr;
}
~copy_on_write() {
if (_self && (--_self->_count == 0)) delete _self;
}
auto operator=(const copy_on_write& x) noexcept -> copy_on_write& {
return *this = copy_on_write(x);
}
auto operator=(copy_on_write&& x) noexcept -> copy_on_write& {
auto tmp = std::move(x);
swap(*this, tmp);
return *this;
}
template <class U>
auto operator=(U&& x) -> disable_copy_assign<U> {
if (_self && unique()) {
_self->_value = std::forward<U>(x);
return *this;
}
return *this = copy_on_write(std::forward<U>(x));
}
auto write() -> element_type& {
if (!unique()) *this = copy_on_write(read());
return _self->_value;
}
auto read() const noexcept -> const element_type& {
assert(_self && "FATAL (sparent) : using a moved copy_on_write object");
return _self->_value;
}
operator const element_type&() const noexcept { return read(); }
auto operator*() const noexcept -> const element_type& { return read(); }
auto operator-> () const noexcept -> const element_type* { return &read(); }
bool unique() const noexcept {
assert(_self && "FATAL (sparent) : using a moved copy_on_write object");
return _self->_count == 1;
}
[[deprecated]] bool unique_instance() const noexcept { return unique(); }
bool identity(const copy_on_write& x) const noexcept {
assert((_self && x._self) && "FATAL (sparent) : using a moved copy_on_write object");
return _self == x._self;
}
friend inline void swap(copy_on_write& x, copy_on_write& y) noexcept {
std::swap(x._self, y._self);
}
friend inline bool operator<(const copy_on_write& x, const copy_on_write& y) noexcept {
return !x.identity(y) && (*x < *y);
}
friend inline bool operator<(const copy_on_write& x, const element_type& y) noexcept {
return *x < y;
}
friend inline bool operator<(const element_type& x, const copy_on_write& y) noexcept {
return x < *y;
}
friend inline bool operator>(const copy_on_write& x, const copy_on_write& y) noexcept {
return y < x;
}
friend inline bool operator>(const copy_on_write& x, const element_type& y) noexcept {
return y < x;
}
friend inline bool operator>(const element_type& x, const copy_on_write& y) noexcept {
return y < x;
}
friend inline bool operator<=(const copy_on_write& x, const copy_on_write& y) noexcept {
return !(y < x);
}
friend inline bool operator<=(const copy_on_write& x, const element_type& y) noexcept {
return !(y < x);
}
friend inline bool operator<=(const element_type& x, const copy_on_write& y) noexcept {
return !(y < x);
}
friend inline bool operator>=(const copy_on_write& x, const copy_on_write& y) noexcept {
return !(x < y);
}
friend inline bool operator>=(const copy_on_write& x, const element_type& y) noexcept {
return !(x < y);
}
friend inline bool operator>=(const element_type& x, const copy_on_write& y) noexcept {
return !(x < y);
}
friend inline bool operator==(const copy_on_write& x, const copy_on_write& y) noexcept {
return x.identity(y) || (*x == *y);
}
friend inline bool operator==(const copy_on_write& x, const element_type& y) noexcept {
return *x == y;
}
friend inline bool operator==(const element_type& x, const copy_on_write& y) noexcept {
return x == *y;
}
friend inline bool operator!=(const copy_on_write& x, const copy_on_write& y) noexcept {
return !(x == y);
}
friend inline bool operator!=(const copy_on_write& x, const element_type& y) noexcept {
return !(x == y);
}
friend inline bool operator!=(const element_type& x, const copy_on_write& y) noexcept {
return !(x == y);
}
};
/**************************************************************************************************/
} // namespace stlab
/**************************************************************************************************/
#endif
/**************************************************************************************************/