1 //===- Optional.h - Simple variant for passing optional values --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ADT_OPTIONAL_H
17 #define LLVM_ADT_OPTIONAL_H
19 #include "llvm/ADT/None.h"
20 #include "llvm/Support/AlignOf.h"
21 #include "llvm/Support/Compiler.h"
22 #include "llvm/Support/type_traits.h"
30 template <typename T> class Optional {
31 AlignedCharArrayUnion<T> storage;
38 explicit Optional() {}
40 Optional(const T &y) : hasVal(true) { new (storage.buffer) T(y); }
42 Optional(const Optional &O) : hasVal(O.hasVal) {
44 new (storage.buffer) T(*O);
47 Optional(T &&y) : hasVal(true) { new (storage.buffer) T(std::forward<T>(y)); }
49 Optional(Optional<T> &&O) : hasVal(O) {
51 new (storage.buffer) T(std::move(*O));
56 ~Optional() { reset(); }
58 Optional &operator=(T &&y) {
60 **this = std::move(y);
62 new (storage.buffer) T(std::move(y));
68 Optional &operator=(Optional &&O) {
72 *this = std::move(*O);
78 /// Create a new object by constructing it in place with the given arguments.
79 template <typename... ArgTypes> void emplace(ArgTypes &&... Args) {
82 new (storage.buffer) T(std::forward<ArgTypes>(Args)...);
85 static inline Optional create(const T *y) {
86 return y ? Optional(*y) : Optional();
89 // FIXME: these assignments (& the equivalent const T&/const Optional& ctors)
90 // could be made more efficient by passing by value, possibly unifying them
91 // with the rvalue versions above - but this could place a different set of
92 // requirements (notably: the existence of a default ctor) when implemented
93 // in that way. Careful SFINAE to avoid such pitfalls would be required.
94 Optional &operator=(const T &y) {
98 new (storage.buffer) T(y);
104 Optional &operator=(const Optional &O) {
119 const T *getPointer() const {
121 return reinterpret_cast<const T *>(storage.buffer);
125 return reinterpret_cast<T *>(storage.buffer);
127 const T &getValue() const LLVM_LVALUE_FUNCTION {
129 return *getPointer();
131 T &getValue() LLVM_LVALUE_FUNCTION {
133 return *getPointer();
136 explicit operator bool() const { return hasVal; }
137 bool hasValue() const { return hasVal; }
138 const T *operator->() const { return getPointer(); }
139 T *operator->() { return getPointer(); }
140 const T &operator*() const LLVM_LVALUE_FUNCTION {
142 return *getPointer();
144 T &operator*() LLVM_LVALUE_FUNCTION {
146 return *getPointer();
149 template <typename U>
150 constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION {
151 return hasValue() ? getValue() : std::forward<U>(value);
154 #if LLVM_HAS_RVALUE_REFERENCE_THIS
157 return std::move(*getPointer());
161 return std::move(*getPointer());
164 template <typename U>
165 T getValueOr(U &&value) && {
166 return hasValue() ? std::move(getValue()) : std::forward<U>(value);
171 template <typename T> struct isPodLike<Optional<T>> {
172 // An Optional<T> is pod-like if T is.
173 static const bool value = isPodLike<T>::value;
176 template <typename T, typename U>
177 bool operator==(const Optional<T> &X, const Optional<U> &Y) {
180 return X.hasValue() == Y.hasValue();
183 template <typename T, typename U>
184 bool operator!=(const Optional<T> &X, const Optional<U> &Y) {
188 template <typename T, typename U>
189 bool operator<(const Optional<T> &X, const Optional<U> &Y) {
192 return X.hasValue() < Y.hasValue();
195 template <typename T, typename U>
196 bool operator<=(const Optional<T> &X, const Optional<U> &Y) {
200 template <typename T, typename U>
201 bool operator>(const Optional<T> &X, const Optional<U> &Y) {
205 template <typename T, typename U>
206 bool operator>=(const Optional<T> &X, const Optional<U> &Y) {
211 bool operator==(const Optional<T> &X, NoneType) {
216 bool operator==(NoneType, const Optional<T> &X) {
221 bool operator!=(const Optional<T> &X, NoneType) {
226 bool operator!=(NoneType, const Optional<T> &X) {
230 template <typename T> bool operator<(const Optional<T> &X, NoneType) {
234 template <typename T> bool operator<(NoneType, const Optional<T> &X) {
238 template <typename T> bool operator<=(const Optional<T> &X, NoneType) {
242 template <typename T> bool operator<=(NoneType, const Optional<T> &X) {
246 template <typename T> bool operator>(const Optional<T> &X, NoneType) {
250 template <typename T> bool operator>(NoneType, const Optional<T> &X) {
254 template <typename T> bool operator>=(const Optional<T> &X, NoneType) {
258 template <typename T> bool operator>=(NoneType, const Optional<T> &X) {
262 template <typename T> bool operator==(const Optional<T> &X, const T &Y) {
266 template <typename T> bool operator==(const T &X, const Optional<T> &Y) {
270 template <typename T> bool operator!=(const Optional<T> &X, const T &Y) {
274 template <typename T> bool operator!=(const T &X, const Optional<T> &Y) {
278 template <typename T> bool operator<(const Optional<T> &X, const T &Y) {
282 template <typename T> bool operator<(const T &X, const Optional<T> &Y) {
286 template <typename T> bool operator<=(const Optional<T> &X, const T &Y) {
290 template <typename T> bool operator<=(const T &X, const Optional<T> &Y) {
294 template <typename T> bool operator>(const Optional<T> &X, const T &Y) {
298 template <typename T> bool operator>(const T &X, const Optional<T> &Y) {
302 template <typename T> bool operator>=(const Optional<T> &X, const T &Y) {
306 template <typename T> bool operator>=(const T &X, const Optional<T> &Y) {
310 } // end namespace llvm
312 #endif // LLVM_ADT_OPTIONAL_H