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