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"
30 AlignedCharArrayUnion<T> storage;
35 Optional(NoneType) : hasVal(false) {}
36 explicit Optional() : hasVal(false) {}
37 Optional(const T &y) : hasVal(true) {
38 new (storage.buffer) T(y);
40 Optional(const Optional &O) : hasVal(O.hasVal) {
42 new (storage.buffer) T(*O);
45 Optional(T &&y) : hasVal(true) {
46 new (storage.buffer) T(std::forward<T>(y));
48 Optional(Optional<T> &&O) : hasVal(O) {
50 new (storage.buffer) T(std::move(*O));
54 Optional &operator=(T &&y) {
56 **this = std::move(y);
58 new (storage.buffer) T(std::move(y));
63 Optional &operator=(Optional &&O) {
67 *this = std::move(*O);
73 /// Create a new object by constructing it in place with the given arguments.
74 template<typename ...ArgTypes>
75 void emplace(ArgTypes &&...Args) {
78 new (storage.buffer) T(std::forward<ArgTypes>(Args)...);
81 static inline Optional create(const T* y) {
82 return y ? Optional(*y) : Optional();
85 // FIXME: these assignments (& the equivalent const T&/const Optional& ctors)
86 // could be made more efficient by passing by value, possibly unifying them
87 // with the rvalue versions above - but this could place a different set of
88 // requirements (notably: the existence of a default ctor) when implemented
89 // in that way. Careful SFINAE to avoid such pitfalls would be required.
90 Optional &operator=(const T &y) {
94 new (storage.buffer) T(y);
100 Optional &operator=(const Optional &O) {
119 const T* getPointer() const { assert(hasVal); return reinterpret_cast<const T*>(storage.buffer); }
120 T* getPointer() { assert(hasVal); return reinterpret_cast<T*>(storage.buffer); }
121 const T& getValue() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
122 T& getValue() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
124 explicit operator bool() const { return hasVal; }
125 bool hasValue() const { return hasVal; }
126 const T* operator->() const { return getPointer(); }
127 T* operator->() { return getPointer(); }
128 const T& operator*() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
129 T& operator*() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
131 template <typename U>
132 constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION {
133 return hasValue() ? getValue() : std::forward<U>(value);
136 #if LLVM_HAS_RVALUE_REFERENCE_THIS
137 T&& getValue() && { assert(hasVal); return std::move(*getPointer()); }
138 T&& operator*() && { assert(hasVal); return std::move(*getPointer()); }
140 template <typename U>
141 T getValueOr(U &&value) && {
142 return hasValue() ? std::move(getValue()) : std::forward<U>(value);
147 template <typename T> struct isPodLike;
148 template <typename T> struct isPodLike<Optional<T> > {
149 // An Optional<T> is pod-like if T is.
150 static const bool value = isPodLike<T>::value;
153 template <typename T, typename U>
154 bool operator==(const Optional<T> &X, const Optional<U> &Y) {
157 return X.hasValue() == Y.hasValue();
160 template <typename T, typename U>
161 bool operator!=(const Optional<T> &X, const Optional<U> &Y) {
165 template <typename T, typename U>
166 bool operator<(const Optional<T> &X, const Optional<U> &Y) {
169 return X.hasValue() < Y.hasValue();
172 template <typename T, typename U>
173 bool operator<=(const Optional<T> &X, const Optional<U> &Y) {
177 template <typename T, typename U>
178 bool operator>(const Optional<T> &X, const Optional<U> &Y) {
182 template <typename T, typename U>
183 bool operator>=(const Optional<T> &X, const Optional<U> &Y) {
188 bool operator==(const Optional<T> &X, NoneType) {
193 bool operator==(NoneType, const Optional<T> &X) {
198 bool operator!=(const Optional<T> &X, NoneType) {
203 bool operator!=(NoneType, const Optional<T> &X) {
207 template <typename T> bool operator<(const Optional<T> &X, NoneType) {
211 template <typename T> bool operator<(NoneType, const Optional<T> &X) {
215 template <typename T> bool operator<=(const Optional<T> &X, NoneType) {
219 template <typename T> bool operator<=(NoneType, const Optional<T> &X) {
223 template <typename T> bool operator>(const Optional<T> &X, NoneType) {
227 template <typename T> bool operator>(NoneType, const Optional<T> &X) {
231 template <typename T> bool operator>=(const Optional<T> &X, NoneType) {
235 template <typename T> bool operator>=(NoneType, const Optional<T> &X) {
239 template <typename T> bool operator==(const Optional<T> &X, const T &Y) {
243 template <typename T> bool operator==(const T &X, const Optional<T> &Y) {
247 template <typename T> bool operator!=(const Optional<T> &X, const T &Y) {
251 template <typename T> bool operator!=(const T &X, const Optional<T> &Y) {
255 template <typename T> bool operator<(const Optional<T> &X, const T &Y) {
259 template <typename T> bool operator<(const T &X, const Optional<T> &Y) {
263 template <typename T> bool operator<=(const Optional<T> &X, const T &Y) {
267 template <typename T> bool operator<=(const T &X, const Optional<T> &Y) {
271 template <typename T> bool operator>(const Optional<T> &X, const T &Y) {
275 template <typename T> bool operator>(const T &X, const Optional<T> &Y) {
279 template <typename T> bool operator>=(const Optional<T> &X, const T &Y) {
283 template <typename T> bool operator>=(const T &X, const Optional<T> &Y) {
287 } // end llvm namespace