|
25 | 25 | #define SWIFT_BASIC_OPTIONAL_H
|
26 | 26 |
|
27 | 27 | #include "llvm/ADT/Optional.h"
|
28 |
| -#include <type_traits> |
29 |
| -#include <utility> |
30 |
| -#include <cassert> |
31 | 28 |
|
32 | 29 | namespace swift {
|
33 |
| - /// An enum whose purpose is to make it easier to initialize an |
34 |
| - /// empty optional. |
35 |
| - static const enum class Nothing_t { Nothing } Nothing = Nothing_t::Nothing; |
| 30 | + static auto Nothing = llvm::None; |
| 31 | + using Nothing_t = decltype(Nothing); |
36 | 32 |
|
37 |
| - static_assert(!std::is_convertible<Nothing_t, bool>::value, |
38 |
| - "Nothing must not be implicitly convertible to bool"); |
39 |
| - |
40 |
| - template<typename T> |
41 |
| - class Optional { |
42 |
| - // Place Value in an anonymous union to suppress implicit value semantics. |
43 |
| - union { |
44 |
| - T Value; |
45 |
| - }; |
46 |
| - unsigned HasValue : 1; |
47 |
| - |
48 |
| - public: |
49 |
| - typedef T value_type; |
50 |
| - |
51 |
| - /// \brief Construct an empty instance. |
52 |
| - Optional() : HasValue(false) { } |
53 |
| - |
54 |
| - /// \brief Construct an empty instance. |
55 |
| - Optional(Nothing_t _) : HasValue(false) { } |
56 |
| - |
57 |
| - /// \brief Construct an instance containing a value of type \c T |
58 |
| - /// constructed with the given arguments. |
59 |
| - /// |
60 |
| - /// \param Args The arguments with which the \c T object will be |
61 |
| - /// direct-initialized. |
62 |
| - template<typename ...ArgTypes> |
63 |
| - Optional(ArgTypes &&...Args) |
64 |
| - : Value(std::forward<ArgTypes>(Args)...), HasValue(true) |
65 |
| - { |
66 |
| - } |
67 |
| - |
68 |
| - Optional(const llvm::Optional<T> &other) : HasValue(false) { |
69 |
| - if (other) |
70 |
| - emplace(*other); |
71 |
| - } |
72 |
| - |
73 |
| - Optional(llvm::Optional<T> &&other) : HasValue(false) { |
74 |
| - if (other) |
75 |
| - emplace(std::move(*other)); |
76 |
| - } |
77 |
| - |
78 |
| - Optional(Optional &Other) : HasValue(Other.HasValue) { |
79 |
| - if (HasValue) |
80 |
| - ::new ((void*)&Value) T(Other.Value); |
81 |
| - } |
82 |
| - |
83 |
| - Optional(const Optional &Other) : HasValue(Other.HasValue) { |
84 |
| - if (HasValue) |
85 |
| - ::new ((void*)&Value) T(Other.Value); |
86 |
| - } |
87 |
| - |
88 |
| - Optional(Optional &&Other) : HasValue(Other.HasValue) { |
89 |
| - if (HasValue) { |
90 |
| - ::new ((void*)&Value) T(std::move(Other.Value)); |
91 |
| - Other.HasValue = false; |
92 |
| - } |
93 |
| - } |
94 |
| - |
95 |
| - Optional &operator=(const Optional &Other) { |
96 |
| - if (HasValue && Other.HasValue) { |
97 |
| - Value = Other.Value; |
98 |
| - return *this; |
99 |
| - } |
100 |
| - |
101 |
| - if (HasValue) { |
102 |
| - reset(); |
103 |
| - return *this; |
104 |
| - } |
105 |
| - |
106 |
| - if (Other.HasValue) { |
107 |
| - HasValue = true; |
108 |
| - ::new ((void*)&Value) T(Other.Value); |
109 |
| - } |
110 |
| - |
111 |
| - return *this; |
112 |
| - } |
113 |
| - |
114 |
| - Optional &operator=(Optional &&Other) { |
115 |
| - if (HasValue && Other.HasValue) { |
116 |
| - Value = std::move(Other.Value); |
117 |
| - Other.reset(); |
118 |
| - return *this; |
119 |
| - } |
120 |
| - |
121 |
| - if (HasValue) { |
122 |
| - reset(); |
123 |
| - return *this; |
124 |
| - } |
125 |
| - |
126 |
| - if (Other.HasValue) { |
127 |
| - HasValue = true; |
128 |
| - ::new ((void*)&Value) T(std::move(Other.Value)); |
129 |
| - Other.reset(); |
130 |
| - } |
131 |
| - |
132 |
| - return *this; |
133 |
| - } |
134 |
| - |
135 |
| - ~Optional() { reset(); } |
136 |
| - |
137 |
| - // Create a new object by constructing it in place with the given arguments. |
138 |
| - template<typename ...ArgTypes> |
139 |
| - void emplace(ArgTypes &&...Args) { |
140 |
| - reset(); |
141 |
| - HasValue = true; |
142 |
| - ::new ((void*)&Value) T(std::forward<ArgTypes>(Args)...); |
143 |
| - } |
144 |
| - |
145 |
| - void reset() { |
146 |
| - if (!HasValue) |
147 |
| - return; |
148 |
| - |
149 |
| - Value.~T(); |
150 |
| - HasValue = false; |
151 |
| - } |
152 |
| - |
153 |
| - T &getValue() & { assert(HasValue); return Value; } |
154 |
| - const T &getValue() const & { assert(HasValue); return Value; } |
155 |
| - |
156 |
| - T getValue() && { |
157 |
| - assert(HasValue); |
158 |
| - T result = std::move(Value); |
159 |
| - reset(); |
160 |
| - return result; |
161 |
| - } |
162 |
| - |
163 |
| - template <typename U> |
164 |
| - constexpr T getValueOr(U &&value) const & { |
165 |
| - return hasValue() ? getValue() : value; |
166 |
| - } |
167 |
| - |
168 |
| - template <typename U> |
169 |
| - T getValueOr(U &&value) && { |
170 |
| - return hasValue() ? getValue() : value; |
171 |
| - } |
172 |
| - |
173 |
| - bool hasValue() const { return HasValue; } |
174 |
| - explicit operator bool() const { return HasValue; } |
175 |
| - |
176 |
| - const T* operator->() const { assert(HasValue); return &Value; } |
177 |
| - T* operator->() { assert(HasValue); return &Value; } |
178 |
| - const T& operator*() const & { assert(HasValue); return Value; } |
179 |
| - T& operator*() & { assert(HasValue); return Value; } |
180 |
| - |
181 |
| - T operator*() && { |
182 |
| - assert(HasValue); |
183 |
| - T result = std::move(Value); |
184 |
| - reset(); |
185 |
| - return result; |
186 |
| - } |
187 |
| - |
188 |
| - /// Return the value inside the optional if populated; otherwise, execute |
189 |
| - /// the given block, store its result inside the optional, and return it. |
190 |
| - template<typename NullaryFunctor> |
191 |
| - T const &cache(NullaryFunctor &&f) { |
192 |
| - if (hasValue()) |
193 |
| - return getValue(); |
194 |
| - emplace(f()); |
195 |
| - return getValue(); |
196 |
| - } |
197 |
| - }; |
| 33 | + template <typename T> |
| 34 | + using Optional = llvm::Optional<T>; |
198 | 35 | }
|
199 | 36 |
|
200 | 37 | #endif
|
0 commit comments