-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_on_write.h
199 lines (174 loc) · 7.12 KB
/
copy_on_write.h
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
// Copyright 2020-2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _COPY_ON_WRITE_H
#define _COPY_ON_WRITE_H
#include <cstddef>
#include <type_traits>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/utility/utility.h"
#include "ref.h"
namespace refptr {
// Manages an instance of `T` on the heap. Copying `CopyOnWriteNoDef<T>` is
// similarly cheap as copying a `shared_ptr`. Actual copying of `T` is deferred
// until a mutable reference is requested by `AsMutable`.
//
// `T` must satisfy the following properties:
//
// 1. Must be copy-constructible.
// 2. Allow multi-threaded access to `const T&` (assuming no thread accesses it
// mutably).
// 3. Copies of `T` must be semantically indistinguishable from each other.
// In particular programs should not depend on pointer (in)equality of
// instances.
//
// `CopyOnWriteNoDef<T>` then satisfies the same properties, allowing easy
// nesting of data structures.
//
// Instances should be always passed by value, not by reference.
template <typename T>
class ABSL_ATTRIBUTE_TRIVIAL_ABI CopyOnWriteNoDef {
public:
using element_type = T;
template <typename... Arg>
explicit CopyOnWriteNoDef(absl::in_place_t, Arg&&... args)
: ref_(New<T>(std::forward<Arg>(args)...).Share()) {}
explicit CopyOnWriteNoDef(std::nullptr_t) : ref_(nullptr) {}
CopyOnWriteNoDef(const CopyOnWriteNoDef&) = default;
CopyOnWriteNoDef(CopyOnWriteNoDef&&) = default;
CopyOnWriteNoDef& operator=(const CopyOnWriteNoDef&) = default;
CopyOnWriteNoDef& operator=(CopyOnWriteNoDef&&) = default;
bool operator==(std::nullptr_t) const { return ref_ == nullptr; }
bool operator!=(std::nullptr_t) const { return ref_ != nullptr; }
const T& operator*() const { return *ref_; }
const T* operator->() const { return &this->operator*(); }
// See `CopyOnWrite<T>::AsMutable` below.
T& AsMutable() {
static_assert(std::is_copy_constructible<T>::value,
"T must be copy-constructible");
return Adopt(
absl::visit(ExtractOrCopy(), std::move(ref_).AttemptToClaim()));
}
// Modifies a copy of this instance with `mutator`, which receives `T&` as
// its only argument. Returns the modified copy.
template <typename F>
ABSL_MUST_USE_RESULT CopyOnWriteNoDef With(F&& mutator) const& {
return CopyOnWriteNoDef(*this).With(std::forward<F>(mutator));
}
// Consumes `*this` to modify it, making a copy of the pointed-to value if
// necessary, and returns a pointer to the result.
template <typename F>
ABSL_MUST_USE_RESULT CopyOnWriteNoDef With(F&& mutator) && {
std::forward<F>(mutator)(AsMutable());
return std::move(*this);
}
protected:
struct ExtractOrCopy {
Ref<T> operator()(Ref<T> owned) { return owned; }
Ref<T> operator()(const Ref<const T>& copy) { return New<T>(*copy); }
};
explicit CopyOnWriteNoDef(Ref<const T> ref) : ref_(std::move(ref)) {}
T& Adopt(Ref<T> owned) {
T& value = *owned;
ref_ = std::move(owned).Share();
return value;
}
Ref<const T> ref_;
};
// Manages an instance of `T` on the heap. Copying `CopyOnWrite<T>` is
// similarly cheap as copying a `shared_ptr`. Actual copying of `T` is deferred
// until a mutable reference is requested by `AsMutable`.
//
// Default creation of instances of `T` is deferred similarly:
// Default-constructed `CopyOnWrite<T>` object references a shared, global,
// read-only instance of `T` until it is modified for the first time (this
// state is tracked by the `LazyDefault()` method.).
// This allows easy, direct nesting of data structures as in:
//
// struct Foo {
// CopyOnWrite<Foo> nested;
// };
//
// Unlike `CopyOnWriteNoDef`, `CopyOnWrite` can never be null, and therefore
// lacks the corresponding constructor and comparison operators.
//
// `T` must satisfy the following properties:
//
// 1. All requirements of `CopyOnWriteNoDef` above.
// 2. `T` must be default-constructible.
// 3. Two default-constructed instances of `T` must be semantically
// indistinguishable from each other.
//
// `CopyOnWrite<T>` then satisfies the same properties, allowing easy nesting
// of data structures.
//
// Instances should be always passed by value, not by reference.
template <typename T>
class ABSL_ATTRIBUTE_TRIVIAL_ABI CopyOnWrite : protected CopyOnWriteNoDef<T> {
public:
using element_type = T;
// Construct a lazily initialized instance that returns a shared,
// default-constructed `const T&` instance until modified.
CopyOnWrite() : CopyOnWriteNoDef<T>(nullptr) {}
template <typename... Arg>
explicit CopyOnWrite(absl::in_place_t, Arg&&... args)
: CopyOnWriteNoDef<T>(absl::in_place, std::forward<Arg>(args)...) {}
CopyOnWrite(const CopyOnWrite&) = default;
CopyOnWrite(CopyOnWrite&&) = default;
CopyOnWrite& operator=(const CopyOnWrite&) = default;
CopyOnWrite& operator=(CopyOnWrite&&) = default;
const T& operator*() const {
return LazyDefault() ? SharedDefault() : CopyOnWriteNoDef<T>::operator*();
}
const T* operator->() const { return &this->operator*(); }
// Return `true` iff this instance was default-constructed and unmodified
// yet, in which case `operator*` returns a shared instance of `const& T`.
// Once `AsMutable()` is called, this always returns `false`.
bool LazyDefault() const { return CopyOnWriteNoDef<T>::ref_ == nullptr; }
// If this instance is the sole owner of `T`, returns it.
// Otherwise makes a new copy on the heap, points this object to it, and
// returns the copy.
//
// Important: The returned reference is valid only until this pointer is
// copied or destroyed. Therefore callers should not store the returned
// reference and rather call `AsMutable()` repeatedly as needed.
// The `With` functions below provide a safer alternative.
T& AsMutable() {
if (LazyDefault()) {
return CopyOnWriteNoDef<T>::Adopt(New<T>());
}
return CopyOnWriteNoDef<T>::AsMutable();
}
// Modifies a copy of this instance with `mutator`, which receives `T&` as
// its only argument. Returns the modified copy, while `*this` remains
// unchanged.
template <typename F>
ABSL_MUST_USE_RESULT CopyOnWrite With(F&& mutator) const& {
return CopyOnWrite(*this).With(std::forward<F>(mutator));
}
// Consumes `*this` to modify it, making a copy of the pointed-to value if
// necessary, and returns a pointer with the modified result.
template <typename F>
ABSL_MUST_USE_RESULT CopyOnWrite With(F&& mutator) && {
std::forward<F>(mutator)(CopyOnWriteNoDef<T>::AsMutable());
return std::move(*this);
}
protected:
static const T& SharedDefault() {
static const T instance;
return instance;
}
};
} // namespace refptr
#endif // _COPY_ON_WRITE_H