-
Notifications
You must be signed in to change notification settings - Fork 0
/
0001-Implement-conditional-copy-move-ctors-assign-operato.patch
226 lines (211 loc) · 7.28 KB
/
0001-Implement-conditional-copy-move-ctors-assign-operato.patch
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
217
218
219
220
221
222
223
224
225
From 8db6e289b42fbd754f1d84903c34dd5468b35605 Mon Sep 17 00:00:00 2001
From: Hidehiko Abe <[email protected]>
Date: Fri, 26 Jan 2018 18:01:11 +0000
Subject: [PATCH] Implement conditional copy/move ctors/assign-operators.
BUG=784732
TEST=Ran trybot.
Change-Id: Iec5f9eaa7482d4e23f5bf2eea4b34c9cd867f89d
Reviewed-on: https://chromium-review.googlesource.com/856021
Commit-Queue: Hidehiko Abe <[email protected]>
Reviewed-by: danakj <[email protected]>
Cr-Commit-Position: refs/heads/master@{#532004}
---
base/optional.h | 63 +++++++++++++++++++++++++++++++++++--
base/optional_unittest.cc | 80 +++++++++++++++++++++++++++++++++++++++++++----
2 files changed, 134 insertions(+), 9 deletions(-)
diff --git a/base/optional.h b/base/optional.h
index 5a50eb455be6..bb25118ca199 100644
--- a/base/optional.h
+++ b/base/optional.h
@@ -266,6 +266,58 @@ class OptionalBase {
OptionalStorage<T> storage_;
};
+// The following {Copy,Move}{Constructible,Assignable} structs are helpers to
+// implement constructor/assign-operator overloading. Specifically, if T is
+// is not movable but copyable, Optional<T>'s move constructor should not
+// participate in overload resolution. This inheritance trick implements that.
+template <bool is_copy_constructible>
+struct CopyConstructible {};
+
+template <>
+struct CopyConstructible<false> {
+ constexpr CopyConstructible() = default;
+ constexpr CopyConstructible(const CopyConstructible&) = delete;
+ constexpr CopyConstructible(CopyConstructible&&) = default;
+ CopyConstructible& operator=(const CopyConstructible&) = default;
+ CopyConstructible& operator=(CopyConstructible&&) = default;
+};
+
+template <bool is_move_constructible>
+struct MoveConstructible {};
+
+template <>
+struct MoveConstructible<false> {
+ constexpr MoveConstructible() = default;
+ constexpr MoveConstructible(const MoveConstructible&) = default;
+ constexpr MoveConstructible(MoveConstructible&&) = delete;
+ MoveConstructible& operator=(const MoveConstructible&) = default;
+ MoveConstructible& operator=(MoveConstructible&&) = default;
+};
+
+template <bool is_copy_assignable>
+struct CopyAssignable {};
+
+template <>
+struct CopyAssignable<false> {
+ constexpr CopyAssignable() = default;
+ constexpr CopyAssignable(const CopyAssignable&) = default;
+ constexpr CopyAssignable(CopyAssignable&&) = default;
+ CopyAssignable& operator=(const CopyAssignable&) = delete;
+ CopyAssignable& operator=(CopyAssignable&&) = default;
+};
+
+template <bool is_move_assignable>
+struct MoveAssignable {};
+
+template <>
+struct MoveAssignable<false> {
+ constexpr MoveAssignable() = default;
+ constexpr MoveAssignable(const MoveAssignable&) = default;
+ constexpr MoveAssignable(MoveAssignable&&) = default;
+ MoveAssignable& operator=(const MoveAssignable&) = default;
+ MoveAssignable& operator=(MoveAssignable&&) = delete;
+};
+
} // namespace internal
// base::Optional is a Chromium version of the C++17 optional class:
@@ -280,12 +332,18 @@ class OptionalBase {
// - No exceptions are thrown, because they are banned from Chromium.
// - All the non-members are in the 'base' namespace instead of 'std'.
template <typename T>
-class Optional : public internal::OptionalBase<T> {
+class Optional
+ : public internal::OptionalBase<T>,
+ public internal::CopyConstructible<std::is_copy_constructible<T>::value>,
+ public internal::MoveConstructible<std::is_move_constructible<T>::value>,
+ public internal::CopyAssignable<std::is_copy_constructible<T>::value &&
+ std::is_copy_assignable<T>::value>,
+ public internal::MoveAssignable<std::is_move_constructible<T>::value &&
+ std::is_move_assignable<T>::value> {
public:
using value_type = T;
// Defer default/copy/move constructor implementation to OptionalBase.
- // TODO(hidehiko): Implement conditional enabling.
constexpr Optional() = default;
constexpr Optional(const Optional& other) = default;
constexpr Optional(Optional&& other) = default;
@@ -316,7 +374,6 @@ class Optional : public internal::OptionalBase<T> {
~Optional() = default;
// Defer copy-/move- assign operator implementation to OptionalBase.
- // TOOD(hidehiko): Implement conditional enabling.
Optional& operator=(const Optional& other) = default;
Optional& operator=(Optional&& other) = default;
diff --git a/base/optional_unittest.cc b/base/optional_unittest.cc
index 7cc05ef2987d..09f3106bfa7f 100644
--- a/base/optional_unittest.cc
+++ b/base/optional_unittest.cc
@@ -115,11 +115,29 @@ class DeletedDefaultConstructor {
int foo_;
};
-class DeletedCopyConstructor {
+class DeletedCopy {
public:
- explicit DeletedCopyConstructor(int foo) : foo_(foo) {}
- DeletedCopyConstructor(const DeletedCopyConstructor&) = delete;
- DeletedCopyConstructor(DeletedCopyConstructor&&) = default;
+ explicit DeletedCopy(int foo) : foo_(foo) {}
+ DeletedCopy(const DeletedCopy&) = delete;
+ DeletedCopy(DeletedCopy&&) = default;
+
+ DeletedCopy& operator=(const DeletedCopy&) = delete;
+ DeletedCopy& operator=(DeletedCopy&&) = default;
+
+ int foo() const { return foo_; }
+
+ private:
+ int foo_;
+};
+
+class DeletedMove {
+ public:
+ explicit DeletedMove(int foo) : foo_(foo) {}
+ DeletedMove(const DeletedMove&) = default;
+ DeletedMove(DeletedMove&&) = delete;
+
+ DeletedMove& operator=(const DeletedMove&) = default;
+ DeletedMove& operator=(DeletedMove&&) = delete;
int foo() const { return foo_; }
@@ -279,8 +297,18 @@ TEST(OptionalTest, MoveConstructor) {
// Even if copy constructor is deleted, move constructor needs to work.
// Note that it couldn't be constexpr.
{
- Optional<DeletedCopyConstructor> first(in_place, 42);
- Optional<DeletedCopyConstructor> second(std::move(first));
+ Optional<DeletedCopy> first(in_place, 42);
+ Optional<DeletedCopy> second(std::move(first));
+
+ EXPECT_TRUE(second.has_value());
+ EXPECT_EQ(42, second->foo());
+
+ EXPECT_TRUE(first.has_value());
+ }
+
+ {
+ Optional<DeletedMove> first(in_place, 42);
+ Optional<DeletedMove> second(std::move(first));
EXPECT_TRUE(second.has_value());
EXPECT_EQ(42, second->foo());
@@ -465,6 +493,26 @@ TEST(OptionalTest, AssignObject) {
EXPECT_TRUE(a.value() == TestObject(3, 0.1));
EXPECT_TRUE(a == b);
}
+
+ {
+ Optional<DeletedMove> a(in_place, 42);
+ Optional<DeletedMove> b;
+ b = a;
+
+ EXPECT_TRUE(!!a);
+ EXPECT_TRUE(!!b);
+ EXPECT_EQ(a->foo(), b->foo());
+ }
+
+ {
+ Optional<DeletedMove> a(in_place, 42);
+ Optional<DeletedMove> b(in_place, 1);
+ b = a;
+
+ EXPECT_TRUE(!!a);
+ EXPECT_TRUE(!!b);
+ EXPECT_EQ(a->foo(), b->foo());
+ }
}
TEST(OptionalTest, AssignObject_rvalue) {
@@ -513,6 +561,26 @@ TEST(OptionalTest, AssignObject_rvalue) {
EXPECT_EQ(TestObject::State::MOVE_ASSIGNED, a->state());
EXPECT_EQ(TestObject::State::MOVED_FROM, b->state());
}
+
+ {
+ Optional<DeletedMove> a(in_place, 42);
+ Optional<DeletedMove> b;
+ b = std::move(a);
+
+ EXPECT_TRUE(!!a);
+ EXPECT_TRUE(!!b);
+ EXPECT_EQ(42, b->foo());
+ }
+
+ {
+ Optional<DeletedMove> a(in_place, 42);
+ Optional<DeletedMove> b(in_place, 1);
+ b = std::move(a);
+
+ EXPECT_TRUE(!!a);
+ EXPECT_TRUE(!!b);
+ EXPECT_EQ(42, b->foo());
+ }
}
TEST(OptionalTest, AssignNull) {
--
2.14.3