Skip to content

Commit 699c228

Browse files
author
Serge Guelton
committed
Revert r353927
llvm-svn: 353940
1 parent 4152a09 commit 699c228

File tree

2 files changed

+31
-84
lines changed

2 files changed

+31
-84
lines changed

llvm/include/llvm/ADT/Optional.h

Lines changed: 31 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -29,77 +29,36 @@ namespace llvm {
2929
class raw_ostream;
3030

3131
namespace optional_detail {
32-
3332
/// Storage for any type.
34-
//
35-
template <class T> struct OptionalTrivialStorage {
33+
template <typename T, bool = is_trivially_copyable<T>::value> struct OptionalStorage {
3634
AlignedCharArrayUnion<T> storage;
3735
bool hasVal = false;
38-
OptionalTrivialStorage() = default;
39-
OptionalTrivialStorage(OptionalTrivialStorage const &) = default;
40-
OptionalTrivialStorage(OptionalTrivialStorage &&) = default;
41-
OptionalTrivialStorage &operator=(OptionalTrivialStorage const &) = default;
42-
OptionalTrivialStorage &operator=(OptionalTrivialStorage &&) = default;
43-
~OptionalTrivialStorage() = default;
44-
45-
OptionalTrivialStorage(const T &y) : hasVal(true) {
46-
new (storage.buffer) T(y);
47-
}
48-
OptionalTrivialStorage(T &&y) : hasVal(true) {
49-
new (storage.buffer) T(std::move(y));
50-
}
51-
52-
OptionalTrivialStorage &operator=(const T &y) {
53-
new (storage.buffer) T(y);
54-
hasVal = true;
55-
return *this;
56-
}
57-
OptionalTrivialStorage &operator=(T &&y) {
58-
new (storage.buffer) T(std::move(y));
59-
hasVal = true;
60-
return *this;
61-
}
62-
63-
T *getPointer() {
64-
assert(hasVal);
65-
return reinterpret_cast<T *>(storage.buffer);
66-
}
67-
const T *getPointer() const {
68-
assert(hasVal);
69-
return reinterpret_cast<const T *>(storage.buffer);
70-
}
71-
void reset() { hasVal = false; }
72-
};
73-
74-
template <typename T> struct OptionalStorage : OptionalTrivialStorage<T> {
7536

7637
OptionalStorage() = default;
7738

78-
OptionalStorage(const T &y) : OptionalTrivialStorage<T>(y) {}
79-
OptionalStorage(T &&y) : OptionalTrivialStorage<T>(std::move(y)) {}
80-
81-
OptionalStorage(const OptionalStorage &O) : OptionalTrivialStorage<T>() {
82-
this->hasVal = O.hasVal;
83-
if (this->hasVal)
84-
new (this->storage.buffer) T(*O.getPointer());
39+
OptionalStorage(const T &y) : hasVal(true) { new (storage.buffer) T(y); }
40+
OptionalStorage(const OptionalStorage &O) : hasVal(O.hasVal) {
41+
if (hasVal)
42+
new (storage.buffer) T(*O.getPointer());
8543
}
86-
87-
OptionalStorage(OptionalStorage &&O) : OptionalTrivialStorage<T>() {
88-
this->hasVal = O.hasVal;
44+
OptionalStorage(T &&y) : hasVal(true) {
45+
new (storage.buffer) T(std::forward<T>(y));
46+
}
47+
OptionalStorage(OptionalStorage &&O) : hasVal(O.hasVal) {
8948
if (O.hasVal) {
90-
new (this->storage.buffer) T(std::move(*O.getPointer()));
49+
new (storage.buffer) T(std::move(*O.getPointer()));
9150
}
9251
}
9352

9453
OptionalStorage &operator=(T &&y) {
95-
if (this->hasVal)
96-
*this->getPointer() = std::move(y);
54+
if (hasVal)
55+
*getPointer() = std::move(y);
9756
else {
98-
OptionalTrivialStorage<T>::operator=(std::move(y));
57+
new (storage.buffer) T(std::move(y));
58+
hasVal = true;
9959
}
10060
return *this;
10161
}
102-
10362
OptionalStorage &operator=(OptionalStorage &&O) {
10463
if (!O.hasVal)
10564
reset();
@@ -115,10 +74,11 @@ template <typename T> struct OptionalStorage : OptionalTrivialStorage<T> {
11574
// requirements (notably: the existence of a default ctor) when implemented
11675
// in that way. Careful SFINAE to avoid such pitfalls would be required.
11776
OptionalStorage &operator=(const T &y) {
118-
if (this->hasVal)
119-
*this->getPointer() = y;
77+
if (hasVal)
78+
*getPointer() = y;
12079
else {
121-
OptionalTrivialStorage<T>::operator=(y);
80+
new (storage.buffer) T(y);
81+
hasVal = true;
12282
}
12383
return *this;
12484
}
@@ -133,19 +93,26 @@ template <typename T> struct OptionalStorage : OptionalTrivialStorage<T> {
13393
~OptionalStorage() { reset(); }
13494

13595
void reset() {
136-
if (this->hasVal) {
137-
(*this->getPointer()).~T();
138-
OptionalTrivialStorage<T>::reset();
96+
if (hasVal) {
97+
(*getPointer()).~T();
98+
hasVal = false;
13999
}
140100
}
101+
102+
T *getPointer() {
103+
assert(hasVal);
104+
return reinterpret_cast<T *>(storage.buffer);
105+
}
106+
const T *getPointer() const {
107+
assert(hasVal);
108+
return reinterpret_cast<const T *>(storage.buffer);
109+
}
141110
};
142111

143112
} // namespace optional_detail
144113

145114
template <typename T> class Optional {
146-
typename std::conditional<is_trivially_copyable<T>::value,
147-
optional_detail::OptionalTrivialStorage<T>,
148-
optional_detail::OptionalStorage<T>>::type Storage;
115+
optional_detail::OptionalStorage<T> Storage;
149116

150117
public:
151118
using value_type = T;

llvm/unittests/ADT/OptionalTest.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ using namespace llvm;
1818

1919
namespace {
2020

21-
static_assert(llvm::is_trivially_copyable<Optional<int>>::value,
22-
"trivially copyable");
23-
24-
static_assert(llvm::is_trivially_copyable<Optional<std::array<int, 3>>>::value,
25-
"trivially copyable");
26-
2721
struct NonDefaultConstructible {
2822
static unsigned CopyConstructions;
2923
static unsigned Destructions;
@@ -51,10 +45,6 @@ unsigned NonDefaultConstructible::CopyConstructions = 0;
5145
unsigned NonDefaultConstructible::Destructions = 0;
5246
unsigned NonDefaultConstructible::CopyAssignments = 0;
5347

54-
static_assert(
55-
!llvm::is_trivially_copyable<Optional<NonDefaultConstructible>>::value,
56-
"not trivially copyable");
57-
5848
// Test fixture
5949
class OptionalTest : public testing::Test {
6050
};
@@ -213,10 +203,6 @@ struct MultiArgConstructor {
213203
};
214204
unsigned MultiArgConstructor::Destructions = 0;
215205

216-
static_assert(
217-
!llvm::is_trivially_copyable<Optional<MultiArgConstructor>>::value,
218-
"not trivially copyable");
219-
220206
TEST_F(OptionalTest, Emplace) {
221207
MultiArgConstructor::ResetCounts();
222208
Optional<MultiArgConstructor> A;
@@ -264,9 +250,6 @@ unsigned MoveOnly::MoveConstructions = 0;
264250
unsigned MoveOnly::Destructions = 0;
265251
unsigned MoveOnly::MoveAssignments = 0;
266252

267-
static_assert(!llvm::is_trivially_copyable<Optional<MoveOnly>>::value,
268-
"not trivially copyable");
269-
270253
TEST_F(OptionalTest, MoveOnlyNull) {
271254
MoveOnly::ResetCounts();
272255
Optional<MoveOnly> O;
@@ -368,9 +351,6 @@ struct Immovable {
368351
unsigned Immovable::Constructions = 0;
369352
unsigned Immovable::Destructions = 0;
370353

371-
static_assert(!llvm::is_trivially_copyable<Optional<Immovable>>::value,
372-
"not trivially copyable");
373-
374354
TEST_F(OptionalTest, ImmovableEmplace) {
375355
Optional<Immovable> A;
376356
Immovable::ResetCounts();

0 commit comments

Comments
 (0)