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/Compiler.h"
21 #include "llvm/Support/AlignOf.h"
24 #if LLVM_HAS_RVALUE_REFERENCES
32 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 #if LLVM_HAS_RVALUE_REFERENCES
46 Optional(T &&y) : hasVal(true) {
47 new (storage.buffer) T(std::forward<T>(y));
49 Optional(Optional<T> &&O) : hasVal(O) {
51 new (storage.buffer) T(std::move(*O));
55 Optional &operator=(T &&y) {
57 **this = std::move(y);
59 new (storage.buffer) T(std::move(y));
64 Optional &operator=(Optional &&O) {
68 *this = std::move(*O);
75 static inline Optional create(const T* y) {
76 return y ? Optional(*y) : Optional();
79 // FIXME: these assignments (& the equivalent const T&/const Optional& ctors)
80 // could be made more efficient by passing by value, possibly unifying them
81 // with the rvalue versions above - but this could place a different set of
82 // requirements (notably: the existence of a default ctor) when implemented
83 // in that way. Careful SFINAE to avoid such pitfalls would be required.
84 Optional &operator=(const T &y) {
88 new (storage.buffer) T(y);
94 Optional &operator=(const Optional &O) {
113 const T* getPointer() const { assert(hasVal); return reinterpret_cast<const T*>(storage.buffer); }
114 T* getPointer() { assert(hasVal); return reinterpret_cast<T*>(storage.buffer); }
115 const T& getValue() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
116 T& getValue() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
118 LLVM_EXPLICIT operator bool() const { return hasVal; }
119 bool hasValue() const { return hasVal; }
120 const T* operator->() const { return getPointer(); }
121 T* operator->() { return getPointer(); }
122 const T& operator*() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
123 T& operator*() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
125 #if LLVM_HAS_RVALUE_REFERENCE_THIS
126 T&& getValue() && { assert(hasVal); return std::move(*getPointer()); }
127 T&& operator*() && { assert(hasVal); return std::move(*getPointer()); }
131 template<typename T> struct simplify_type;
133 template <typename T>
134 struct simplify_type<const Optional<T> > {
135 typedef const T* SimpleType;
136 static SimpleType getSimplifiedValue(const Optional<T> &Val) {
137 return Val.getPointer();
141 template <typename T>
142 struct simplify_type<Optional<T> >
143 : public simplify_type<const Optional<T> > {};
145 template <typename T> struct isPodLike;
146 template <typename T> struct isPodLike<Optional<T> > {
147 // An Optional<T> is pod-like if T is.
148 static const bool value = isPodLike<T>::value;
151 /// \brief Poison comparison between two \c Optional objects. Clients needs to
152 /// explicitly compare the underlying values and account for empty \c Optional
155 /// This routine will never be defined. It returns \c void to help diagnose
156 /// errors at compile time.
157 template<typename T, typename U>
158 void operator==(const Optional<T> &X, const Optional<U> &Y);
160 /// \brief Poison comparison between two \c Optional objects. Clients needs to
161 /// explicitly compare the underlying values and account for empty \c Optional
164 /// This routine will never be defined. It returns \c void to help diagnose
165 /// errors at compile time.
166 template<typename T, typename U>
167 void operator!=(const Optional<T> &X, const Optional<U> &Y);
169 /// \brief Poison comparison between two \c Optional objects. Clients needs to
170 /// explicitly compare the underlying values and account for empty \c Optional
173 /// This routine will never be defined. It returns \c void to help diagnose
174 /// errors at compile time.
175 template<typename T, typename U>
176 void operator<(const Optional<T> &X, const Optional<U> &Y);
178 /// \brief Poison comparison between two \c Optional objects. Clients needs to
179 /// explicitly compare the underlying values and account for empty \c Optional
182 /// This routine will never be defined. It returns \c void to help diagnose
183 /// errors at compile time.
184 template<typename T, typename U>
185 void operator<=(const Optional<T> &X, const Optional<U> &Y);
187 /// \brief Poison comparison between two \c Optional objects. Clients needs to
188 /// explicitly compare the underlying values and account for empty \c Optional
191 /// This routine will never be defined. It returns \c void to help diagnose
192 /// errors at compile time.
193 template<typename T, typename U>
194 void operator>=(const Optional<T> &X, const Optional<U> &Y);
196 /// \brief Poison comparison between two \c Optional objects. Clients needs to
197 /// explicitly compare the underlying values and account for empty \c Optional
200 /// This routine will never be defined. It returns \c void to help diagnose
201 /// errors at compile time.
202 template<typename T, typename U>
203 void operator>(const Optional<T> &X, const Optional<U> &Y);
205 } // end llvm namespace