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"
29 AlignedCharArrayUnion<T> storage;
34 Optional(NoneType) : hasVal(false) {}
35 explicit Optional() : hasVal(false) {}
36 Optional(const T &y) : hasVal(true) {
37 new (storage.buffer) T(y);
39 Optional(const Optional &O) : hasVal(O.hasVal) {
41 new (storage.buffer) T(*O);
44 Optional(T &&y) : hasVal(true) {
45 new (storage.buffer) T(std::forward<T>(y));
47 Optional(Optional<T> &&O) : hasVal(O) {
49 new (storage.buffer) T(std::move(*O));
53 Optional &operator=(T &&y) {
55 **this = std::move(y);
57 new (storage.buffer) T(std::move(y));
62 Optional &operator=(Optional &&O) {
66 *this = std::move(*O);
72 static inline Optional create(const T* y) {
73 return y ? Optional(*y) : Optional();
76 // FIXME: these assignments (& the equivalent const T&/const Optional& ctors)
77 // could be made more efficient by passing by value, possibly unifying them
78 // with the rvalue versions above - but this could place a different set of
79 // requirements (notably: the existence of a default ctor) when implemented
80 // in that way. Careful SFINAE to avoid such pitfalls would be required.
81 Optional &operator=(const T &y) {
85 new (storage.buffer) T(y);
91 Optional &operator=(const Optional &O) {
110 const T* getPointer() const { assert(hasVal); return reinterpret_cast<const T*>(storage.buffer); }
111 T* getPointer() { assert(hasVal); return reinterpret_cast<T*>(storage.buffer); }
112 const T& getValue() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
113 T& getValue() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
115 LLVM_EXPLICIT operator bool() const { return hasVal; }
116 bool hasValue() const { return hasVal; }
117 const T* operator->() const { return getPointer(); }
118 T* operator->() { return getPointer(); }
119 const T& operator*() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
120 T& operator*() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
122 template <typename U>
123 constexpr T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION {
124 return hasValue() ? getValue() : std::forward<U>(value);
127 #if LLVM_HAS_RVALUE_REFERENCE_THIS
128 T&& getValue() && { assert(hasVal); return std::move(*getPointer()); }
129 T&& operator*() && { assert(hasVal); return std::move(*getPointer()); }
131 template <typename U>
132 T getValueOr(U &&value) && {
133 return hasValue() ? std::move(getValue()) : std::forward<U>(value);
138 template <typename T> struct isPodLike;
139 template <typename T> struct isPodLike<Optional<T> > {
140 // An Optional<T> is pod-like if T is.
141 static const bool value = isPodLike<T>::value;
144 /// \brief Poison comparison between two \c Optional objects. Clients needs to
145 /// explicitly compare the underlying values and account for empty \c Optional
148 /// This routine will never be defined. It returns \c void to help diagnose
149 /// errors at compile time.
150 template<typename T, typename U>
151 void operator==(const Optional<T> &X, const Optional<U> &Y);
153 /// \brief Poison comparison between two \c Optional objects. Clients needs to
154 /// explicitly compare the underlying values and account for empty \c Optional
157 /// This routine will never be defined. It returns \c void to help diagnose
158 /// errors at compile time.
159 template<typename T, typename U>
160 void operator!=(const Optional<T> &X, const Optional<U> &Y);
162 /// \brief Poison comparison between two \c Optional objects. Clients needs to
163 /// explicitly compare the underlying values and account for empty \c Optional
166 /// This routine will never be defined. It returns \c void to help diagnose
167 /// errors at compile time.
168 template<typename T, typename U>
169 void operator<(const Optional<T> &X, const Optional<U> &Y);
171 /// \brief Poison comparison between two \c Optional objects. Clients needs to
172 /// explicitly compare the underlying values and account for empty \c Optional
175 /// This routine will never be defined. It returns \c void to help diagnose
176 /// errors at compile time.
177 template<typename T, typename U>
178 void operator<=(const Optional<T> &X, const Optional<U> &Y);
180 /// \brief Poison comparison between two \c Optional objects. Clients needs to
181 /// explicitly compare the underlying values and account for empty \c Optional
184 /// This routine will never be defined. It returns \c void to help diagnose
185 /// errors at compile time.
186 template<typename T, typename U>
187 void operator>=(const Optional<T> &X, const Optional<U> &Y);
189 /// \brief Poison comparison between two \c Optional objects. Clients needs to
190 /// explicitly compare the underlying values and account for empty \c Optional
193 /// This routine will never be defined. It returns \c void to help diagnose
194 /// errors at compile time.
195 template<typename T, typename U>
196 void operator>(const Optional<T> &X, const Optional<U> &Y);
198 } // end llvm namespace