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;
32 Optional(NoneType) : hasVal(false) {}
33 explicit Optional() : hasVal(false) {}
34 Optional(const T &y) : hasVal(true) {
35 new (storage.buffer) T(y);
37 Optional(const Optional &O) : hasVal(O.hasVal) {
39 new (storage.buffer) T(*O);
42 Optional(T &&y) : hasVal(true) {
43 new (storage.buffer) T(std::forward<T>(y));
45 Optional(Optional<T> &&O) : hasVal(O) {
47 new (storage.buffer) T(std::move(*O));
51 Optional &operator=(T &&y) {
53 **this = std::move(y);
55 new (storage.buffer) T(std::move(y));
60 Optional &operator=(Optional &&O) {
64 *this = std::move(*O);
70 static inline Optional create(const T* y) {
71 return y ? Optional(*y) : Optional();
74 // FIXME: these assignments (& the equivalent const T&/const Optional& ctors)
75 // could be made more efficient by passing by value, possibly unifying them
76 // with the rvalue versions above - but this could place a different set of
77 // requirements (notably: the existence of a default ctor) when implemented
78 // in that way. Careful SFINAE to avoid such pitfalls would be required.
79 Optional &operator=(const T &y) {
83 new (storage.buffer) T(y);
89 Optional &operator=(const Optional &O) {
108 const T* getPointer() const { assert(hasVal); return reinterpret_cast<const T*>(storage.buffer); }
109 T* getPointer() { assert(hasVal); return reinterpret_cast<T*>(storage.buffer); }
110 const T& getValue() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
111 T& getValue() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
113 LLVM_EXPLICIT operator bool() const { return hasVal; }
114 bool hasValue() const { return hasVal; }
115 const T* operator->() const { return getPointer(); }
116 T* operator->() { return getPointer(); }
117 const T& operator*() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
118 T& operator*() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); }
120 #if LLVM_HAS_RVALUE_REFERENCE_THIS
121 T&& getValue() && { assert(hasVal); return std::move(*getPointer()); }
122 T&& operator*() && { assert(hasVal); return std::move(*getPointer()); }
126 template <typename T> struct isPodLike;
127 template <typename T> struct isPodLike<Optional<T> > {
128 // An Optional<T> is pod-like if T is.
129 static const bool value = isPodLike<T>::value;
132 /// \brief Poison comparison between two \c Optional objects. Clients needs to
133 /// explicitly compare the underlying values and account for empty \c Optional
136 /// This routine will never be defined. It returns \c void to help diagnose
137 /// errors at compile time.
138 template<typename T, typename U>
139 void operator==(const Optional<T> &X, const Optional<U> &Y);
141 /// \brief Poison comparison between two \c Optional objects. Clients needs to
142 /// explicitly compare the underlying values and account for empty \c Optional
145 /// This routine will never be defined. It returns \c void to help diagnose
146 /// errors at compile time.
147 template<typename T, typename U>
148 void operator!=(const Optional<T> &X, const Optional<U> &Y);
150 /// \brief Poison comparison between two \c Optional objects. Clients needs to
151 /// explicitly compare the underlying values and account for empty \c Optional
154 /// This routine will never be defined. It returns \c void to help diagnose
155 /// errors at compile time.
156 template<typename T, typename U>
157 void operator<(const Optional<T> &X, const Optional<U> &Y);
159 /// \brief Poison comparison between two \c Optional objects. Clients needs to
160 /// explicitly compare the underlying values and account for empty \c Optional
163 /// This routine will never be defined. It returns \c void to help diagnose
164 /// errors at compile time.
165 template<typename T, typename U>
166 void operator<=(const Optional<T> &X, const Optional<U> &Y);
168 /// \brief Poison comparison between two \c Optional objects. Clients needs to
169 /// explicitly compare the underlying values and account for empty \c Optional
172 /// This routine will never be defined. It returns \c void to help diagnose
173 /// errors at compile time.
174 template<typename T, typename U>
175 void operator>=(const Optional<T> &X, const Optional<U> &Y);
177 /// \brief Poison comparison between two \c Optional objects. Clients needs to
178 /// explicitly compare the underlying values and account for empty \c Optional
181 /// This routine will never be defined. It returns \c void to help diagnose
182 /// errors at compile time.
183 template<typename T, typename U>
184 void operator>(const Optional<T> &X, const Optional<U> &Y);
186 } // end llvm namespace