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"
32 AlignedCharArrayUnion<T> storage;
39 explicit Optional() {}
41 Optional(const T &y) : hasVal(true) {
42 new (storage.buffer) T(y);
45 Optional(const Optional &O) : hasVal(O.hasVal) {
47 new (storage.buffer) T(*O);
50 Optional(T &&y) : hasVal(true) {
51 new (storage.buffer) T(std::forward<T>(y));
54 Optional(Optional<T> &&O) : hasVal(O) {
56 new (storage.buffer) T(std::move(*O));
65 Optional &operator=(T &&y) {
67 **this = std::move(y);
69 new (storage.buffer) T(std::move(y));
75 Optional &operator=(Optional &&O) {
79 *this = std::move(*O);
85 /// Create a new object by constructing it in place with the given arguments.
86 template<typename ...ArgTypes>
87 void emplace(ArgTypes &&...Args) {
90 new (storage.buffer) T(std::forward<ArgTypes>(Args)...);
93 static inline Optional create(const T* y) {
94 return y ? Optional(*y) : Optional();
97 // FIXME: these assignments (& the equivalent const T&/const Optional& ctors)
98 // could be made more efficient by passing by value, possibly unifying them
99 // with the rvalue versions above - but this could place a different set of
100 // requirements (notably: the existence of a default ctor) when implemented
101 // in that way. Careful SFINAE to avoid such pitfalls would be required.
102 Optional &operator=(const T &y) {
106 new (storage.buffer) T(y);
112 Optional &operator=(const Optional &O) {
127 const T* getPointer() const { assert(hasVal); return reinterpret_cast<const T*>(storage.buffer); }
128 T* getPointer() { assert(hasVal); return reinterpret_cast<T*>(storage.buffer); }
129 const T& getValue() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
130 T& getValue() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
132 explicit operator bool() const { return hasVal; }
133 bool hasValue() const { return hasVal; }
134 const T* operator->() const { return getPointer(); }
135 T* operator->() { return getPointer(); }
136 const T& operator*() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
137 T& operator*() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
139 template <typename U>
140 constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION {
141 return hasValue() ? getValue() : std::forward<U>(value);
144 #if LLVM_HAS_RVALUE_REFERENCE_THIS
145 T&& getValue() && { assert(hasVal); return std::move(*getPointer()); }
146 T&& operator*() && { assert(hasVal); return std::move(*getPointer()); }
148 template <typename U>
149 T getValueOr(U &&value) && {
150 return hasValue() ? std::move(getValue()) : std::forward<U>(value);
155 template <typename T> struct isPodLike<Optional<T>> {
156 // An Optional<T> is pod-like if T is.
157 static const bool value = isPodLike<T>::value;
160 template <typename T, typename U>
161 bool operator==(const Optional<T> &X, const Optional<U> &Y) {
164 return X.hasValue() == Y.hasValue();
167 template <typename T, typename U>
168 bool operator!=(const Optional<T> &X, const Optional<U> &Y) {
172 template <typename T, typename U>
173 bool operator<(const Optional<T> &X, const Optional<U> &Y) {
176 return X.hasValue() < Y.hasValue();
179 template <typename T, typename U>
180 bool operator<=(const Optional<T> &X, const Optional<U> &Y) {
184 template <typename T, typename U>
185 bool operator>(const Optional<T> &X, const Optional<U> &Y) {
189 template <typename T, typename U>
190 bool operator>=(const Optional<T> &X, const Optional<U> &Y) {
195 bool operator==(const Optional<T> &X, NoneType) {
200 bool operator==(NoneType, const Optional<T> &X) {
205 bool operator!=(const Optional<T> &X, NoneType) {
210 bool operator!=(NoneType, const Optional<T> &X) {
214 template <typename T> bool operator<(const Optional<T> &X, NoneType) {
218 template <typename T> bool operator<(NoneType, const Optional<T> &X) {
222 template <typename T> bool operator<=(const Optional<T> &X, NoneType) {
226 template <typename T> 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, const T &Y) {
250 template <typename T> bool operator==(const T &X, const Optional<T> &Y) {
254 template <typename T> bool operator!=(const Optional<T> &X, const T &Y) {
258 template <typename T> bool operator!=(const T &X, const Optional<T> &Y) {
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 } // end namespace llvm
296 #endif // LLVM_ADT_OPTIONAL_H