LLVM  8.0.0svn
Optional.h
Go to the documentation of this file.
1 //===- Optional.h - Simple variant for passing optional values --*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides Optional, a template class modeled in the spirit of
11 // OCaml's 'opt' variant. The idea is to strongly type whether or not
12 // a value can be optional.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_ADT_OPTIONAL_H
17 #define LLVM_ADT_OPTIONAL_H
18 
19 #include "llvm/ADT/None.h"
20 #include "llvm/Support/AlignOf.h"
21 #include "llvm/Support/Compiler.h"
23 #include <algorithm>
24 #include <cassert>
25 #include <new>
26 #include <utility>
27 
28 namespace llvm {
29 
30 namespace optional_detail {
31 /// Storage for any type.
32 template <typename T, bool IsPodLike> struct OptionalStorage {
34  bool hasVal = false;
35 
36  OptionalStorage() = default;
37 
38  OptionalStorage(const T &y) : hasVal(true) { new (storage.buffer) T(y); }
39  OptionalStorage(const OptionalStorage &O) : hasVal(O.hasVal) {
40  if (hasVal)
41  new (storage.buffer) T(*O.getPointer());
42  }
43  OptionalStorage(T &&y) : hasVal(true) {
44  new (storage.buffer) T(std::forward<T>(y));
45  }
46  OptionalStorage(OptionalStorage &&O) : hasVal(O.hasVal) {
47  if (O.hasVal) {
48  new (storage.buffer) T(std::move(*O.getPointer()));
49  }
50  }
51 
53  if (hasVal)
54  *getPointer() = std::move(y);
55  else {
56  new (storage.buffer) T(std::move(y));
57  hasVal = true;
58  }
59  return *this;
60  }
62  if (!O.hasVal)
63  reset();
64  else {
65  *this = std::move(*O.getPointer());
66  }
67  return *this;
68  }
69 
70  // FIXME: these assignments (& the equivalent const T&/const Optional& ctors)
71  // could be made more efficient by passing by value, possibly unifying them
72  // with the rvalue versions above - but this could place a different set of
73  // requirements (notably: the existence of a default ctor) when implemented
74  // in that way. Careful SFINAE to avoid such pitfalls would be required.
76  if (hasVal)
77  *getPointer() = y;
78  else {
79  new (storage.buffer) T(y);
80  hasVal = true;
81  }
82  return *this;
83  }
85  if (!O.hasVal)
86  reset();
87  else
88  *this = *O.getPointer();
89  return *this;
90  }
91 
93 
94  void reset() {
95  if (hasVal) {
96  (*getPointer()).~T();
97  hasVal = false;
98  }
99  }
100 
102  assert(hasVal);
103  return reinterpret_cast<T *>(storage.buffer);
104  }
105  const T *getPointer() const {
106  assert(hasVal);
107  return reinterpret_cast<const T *>(storage.buffer);
108  }
109 };
110 
111 #if !defined(__GNUC__) || defined(__clang__) // GCC up to GCC7 miscompiles this.
112 /// Storage for trivially copyable types only.
113 template <typename T> struct OptionalStorage<T, true> {
115  bool hasVal = false;
116 
117  OptionalStorage() = default;
118 
119  OptionalStorage(const T &y) : hasVal(true) { new (storage.buffer) T(y); }
121  *reinterpret_cast<T *>(storage.buffer) = y;
122  hasVal = true;
123  return *this;
124  }
125 
126  void reset() { hasVal = false; }
127 };
128 #endif
129 } // namespace optional_detail
130 
131 template <typename T> class Optional {
133 
134 public:
135  using value_type = T;
136 
137  constexpr Optional() {}
138  constexpr Optional(NoneType) {}
139 
140  Optional(const T &y) : Storage(y) {}
141  Optional(const Optional &O) = default;
142 
143  Optional(T &&y) : Storage(std::forward<T>(y)) {}
144  Optional(Optional &&O) = default;
145 
147  Storage = std::move(y);
148  return *this;
149  }
150  Optional &operator=(Optional &&O) = default;
151 
152  /// Create a new object by constructing it in place with the given arguments.
153  template <typename... ArgTypes> void emplace(ArgTypes &&... Args) {
154  reset();
155  Storage.hasVal = true;
156  new (getPointer()) T(std::forward<ArgTypes>(Args)...);
157  }
158 
159  static inline Optional create(const T *y) {
160  return y ? Optional(*y) : Optional();
161  }
162 
163  Optional &operator=(const T &y) {
164  Storage = y;
165  return *this;
166  }
167  Optional &operator=(const Optional &O) = default;
168 
169  void reset() { Storage.reset(); }
170 
171  const T *getPointer() const {
172  assert(Storage.hasVal);
173  return reinterpret_cast<const T *>(Storage.storage.buffer);
174  }
176  assert(Storage.hasVal);
177  return reinterpret_cast<T *>(Storage.storage.buffer);
178  }
181 
182  explicit operator bool() const { return Storage.hasVal; }
183  bool hasValue() const { return Storage.hasVal; }
184  const T *operator->() const { return getPointer(); }
185  T *operator->() { return getPointer(); }
188 
189  template <typename U>
190  constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION {
191  return hasValue() ? getValue() : std::forward<U>(value);
192  }
193 
194 #if LLVM_HAS_RVALUE_REFERENCE_THIS
195  T &&getValue() && { return std::move(*getPointer()); }
196  T &&operator*() && { return std::move(*getPointer()); }
197 
198  template <typename U>
199  T getValueOr(U &&value) && {
200  return hasValue() ? std::move(getValue()) : std::forward<U>(value);
201  }
202 #endif
203 };
204 
205 template <typename T> struct isPodLike<Optional<T>> {
206  // An Optional<T> is pod-like if T is.
207  static const bool value = isPodLike<T>::value;
208 };
209 
210 template <typename T, typename U>
211 bool operator==(const Optional<T> &X, const Optional<U> &Y) {
212  if (X && Y)
213  return *X == *Y;
214  return X.hasValue() == Y.hasValue();
215 }
216 
217 template <typename T, typename U>
218 bool operator!=(const Optional<T> &X, const Optional<U> &Y) {
219  return !(X == Y);
220 }
221 
222 template <typename T, typename U>
223 bool operator<(const Optional<T> &X, const Optional<U> &Y) {
224  if (X && Y)
225  return *X < *Y;
226  return X.hasValue() < Y.hasValue();
227 }
228 
229 template <typename T, typename U>
230 bool operator<=(const Optional<T> &X, const Optional<U> &Y) {
231  return !(Y < X);
232 }
233 
234 template <typename T, typename U>
235 bool operator>(const Optional<T> &X, const Optional<U> &Y) {
236  return Y < X;
237 }
238 
239 template <typename T, typename U>
240 bool operator>=(const Optional<T> &X, const Optional<U> &Y) {
241  return !(X < Y);
242 }
243 
244 template<typename T>
245 bool operator==(const Optional<T> &X, NoneType) {
246  return !X;
247 }
248 
249 template<typename T>
250 bool operator==(NoneType, const Optional<T> &X) {
251  return X == None;
252 }
253 
254 template<typename T>
255 bool operator!=(const Optional<T> &X, NoneType) {
256  return !(X == None);
257 }
258 
259 template<typename T>
260 bool operator!=(NoneType, const Optional<T> &X) {
261  return X != None;
262 }
263 
264 template <typename T> bool operator<(const Optional<T> &X, NoneType) {
265  return false;
266 }
267 
268 template <typename T> bool operator<(NoneType, const Optional<T> &X) {
269  return X.hasValue();
270 }
271 
272 template <typename T> bool operator<=(const Optional<T> &X, NoneType) {
273  return !(None < X);
274 }
275 
276 template <typename T> bool operator<=(NoneType, const Optional<T> &X) {
277  return !(X < None);
278 }
279 
280 template <typename T> bool operator>(const Optional<T> &X, NoneType) {
281  return None < X;
282 }
283 
284 template <typename T> bool operator>(NoneType, const Optional<T> &X) {
285  return X < None;
286 }
287 
288 template <typename T> bool operator>=(const Optional<T> &X, NoneType) {
289  return None <= X;
290 }
291 
292 template <typename T> bool operator>=(NoneType, const Optional<T> &X) {
293  return X <= None;
294 }
295 
296 template <typename T> bool operator==(const Optional<T> &X, const T &Y) {
297  return X && *X == Y;
298 }
299 
300 template <typename T> bool operator==(const T &X, const Optional<T> &Y) {
301  return Y && X == *Y;
302 }
303 
304 template <typename T> bool operator!=(const Optional<T> &X, const T &Y) {
305  return !(X == Y);
306 }
307 
308 template <typename T> bool operator!=(const T &X, const Optional<T> &Y) {
309  return !(X == Y);
310 }
311 
312 template <typename T> bool operator<(const Optional<T> &X, const T &Y) {
313  return !X || *X < Y;
314 }
315 
316 template <typename T> bool operator<(const T &X, const Optional<T> &Y) {
317  return Y && X < *Y;
318 }
319 
320 template <typename T> bool operator<=(const Optional<T> &X, const T &Y) {
321  return !(Y < X);
322 }
323 
324 template <typename T> bool operator<=(const T &X, const Optional<T> &Y) {
325  return !(Y < X);
326 }
327 
328 template <typename T> bool operator>(const Optional<T> &X, const T &Y) {
329  return Y < X;
330 }
331 
332 template <typename T> bool operator>(const T &X, const Optional<T> &Y) {
333  return Y < X;
334 }
335 
336 template <typename T> bool operator>=(const Optional<T> &X, const T &Y) {
337  return !(X < Y);
338 }
339 
340 template <typename T> bool operator>=(const T &X, const Optional<T> &Y) {
341  return !(X < Y);
342 }
343 
344 } // end namespace llvm
345 
346 #endif // LLVM_ADT_OPTIONAL_H
#define LLVM_LVALUE_FUNCTION
Expands to &#39;&&#39; if ref-qualifiers for *this are supported.
Definition: Compiler.h:96
const NoneType None
Definition: None.h:24
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
Optional(const T &y)
Definition: Optional.h:140
const T & operator*() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:186
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
OptionalStorage & operator=(OptionalStorage &&O)
Definition: Optional.h:61
OptionalStorage(OptionalStorage &&O)
Definition: Optional.h:46
NoneType
A simple null object to allow implicit construction of Optional<T> and similar types without having t...
Definition: None.h:23
bool operator>(int64_t V1, const APSInt &V2)
Definition: APSInt.h:327
constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION
Definition: Optional.h:190
void emplace(ArgTypes &&... Args)
Create a new object by constructing it in place with the given arguments.
Definition: Optional.h:153
block Block Frequency true
OptionalStorage & operator=(T &&y)
Definition: Optional.h:52
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
Definition: BitVector.h:938
T & getValue() LLVM_LVALUE_FUNCTION
Definition: Optional.h:180
bool operator>=(int64_t V1, const APSInt &V2)
Definition: APSInt.h:325
OptionalStorage & operator=(const T &y)
Definition: Optional.h:120
Optional & operator=(const T &y)
Definition: Optional.h:163
APInt operator*(APInt a, uint64_t RHS)
Definition: APInt.h:2085
Storage for any type.
Definition: Optional.h:32
#define T
const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:179
constexpr Optional()
Definition: Optional.h:137
T * operator->()
Definition: Optional.h:185
const T * operator->() const
Definition: Optional.h:184
OptionalStorage(const OptionalStorage &O)
Definition: Optional.h:39
static Optional create(const T *y)
Definition: Optional.h:159
T & operator*() LLVM_LVALUE_FUNCTION
Definition: Optional.h:187
const T * getPointer() const
Definition: Optional.h:171
char buffer[Size]
Definition: AlignOf.h:37
constexpr Optional(NoneType)
Definition: Optional.h:138
isPodLike - This is a type trait that is used to determine whether a given type can be copied around ...
Definition: ArrayRef.h:530
OptionalStorage & operator=(const T &y)
Definition: Optional.h:75
bool operator!=(uint64_t V1, const APInt &V2)
Definition: APInt.h:1963
bool hasValue() const
Definition: Optional.h:183
OptionalStorage & operator=(const OptionalStorage &O)
Definition: Optional.h:84
Optional & operator=(T &&y)
Definition: Optional.h:146
void reset()
Definition: Optional.h:169
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
aarch64 promote const
AlignedCharArrayUnion< T > storage
Definition: Optional.h:33
Optional(T &&y)
Definition: Optional.h:143
T * getPointer()
Definition: Optional.h:175
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:1961
constexpr char Args[]
Key for Kernel::Metadata::mArgs.