]>
Commit | Line | Data |
---|---|---|
223e47cc LB |
1 | //===-- Optional.h - Simple variant for passing optional values ---*- C++ -*-=// |
2 | // | |
3 | // The LLVM Compiler Infrastructure | |
4 | // | |
5 | // This file is distributed under the University of Illinois Open Source | |
6 | // License. See LICENSE.TXT for details. | |
7 | // | |
8 | //===----------------------------------------------------------------------===// | |
9 | // | |
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. | |
13 | // | |
14 | //===----------------------------------------------------------------------===// | |
15 | ||
970d7e83 LB |
16 | #ifndef LLVM_ADT_OPTIONAL_H |
17 | #define LLVM_ADT_OPTIONAL_H | |
223e47cc | 18 | |
970d7e83 | 19 | #include "llvm/ADT/None.h" |
970d7e83 | 20 | #include "llvm/Support/AlignOf.h" |
1a4d82fc | 21 | #include "llvm/Support/Compiler.h" |
223e47cc | 22 | #include <cassert> |
1a4d82fc | 23 | #include <new> |
970d7e83 | 24 | #include <utility> |
970d7e83 | 25 | |
223e47cc LB |
26 | namespace llvm { |
27 | ||
28 | template<typename T> | |
29 | class Optional { | |
970d7e83 LB |
30 | AlignedCharArrayUnion<T> storage; |
31 | bool hasVal; | |
223e47cc | 32 | public: |
1a4d82fc JJ |
33 | typedef T value_type; |
34 | ||
970d7e83 LB |
35 | Optional(NoneType) : hasVal(false) {} |
36 | explicit Optional() : hasVal(false) {} | |
37 | Optional(const T &y) : hasVal(true) { | |
38 | new (storage.buffer) T(y); | |
39 | } | |
40 | Optional(const Optional &O) : hasVal(O.hasVal) { | |
41 | if (hasVal) | |
42 | new (storage.buffer) T(*O); | |
43 | } | |
44 | ||
970d7e83 LB |
45 | Optional(T &&y) : hasVal(true) { |
46 | new (storage.buffer) T(std::forward<T>(y)); | |
47 | } | |
48 | Optional(Optional<T> &&O) : hasVal(O) { | |
49 | if (O) { | |
50 | new (storage.buffer) T(std::move(*O)); | |
51 | O.reset(); | |
52 | } | |
53 | } | |
54 | Optional &operator=(T &&y) { | |
55 | if (hasVal) | |
56 | **this = std::move(y); | |
57 | else { | |
58 | new (storage.buffer) T(std::move(y)); | |
59 | hasVal = true; | |
60 | } | |
61 | return *this; | |
62 | } | |
63 | Optional &operator=(Optional &&O) { | |
64 | if (!O) | |
65 | reset(); | |
66 | else { | |
67 | *this = std::move(*O); | |
68 | O.reset(); | |
69 | } | |
70 | return *this; | |
71 | } | |
1a4d82fc JJ |
72 | |
73 | #if LLVM_HAS_VARIADIC_TEMPLATES | |
74 | ||
75 | /// Create a new object by constructing it in place with the given arguments. | |
76 | template<typename ...ArgTypes> | |
77 | void emplace(ArgTypes &&...Args) { | |
78 | reset(); | |
79 | hasVal = true; | |
80 | new (storage.buffer) T(std::forward<ArgTypes>(Args)...); | |
81 | } | |
82 | ||
83 | #else | |
84 | ||
85 | /// Create a new object by default-constructing it in place. | |
86 | void emplace() { | |
87 | reset(); | |
88 | hasVal = true; | |
89 | new (storage.buffer) T(); | |
90 | } | |
91 | ||
92 | /// Create a new object by constructing it in place with the given arguments. | |
93 | template<typename T1> | |
94 | void emplace(T1 &&A1) { | |
95 | reset(); | |
96 | hasVal = true; | |
97 | new (storage.buffer) T(std::forward<T1>(A1)); | |
98 | } | |
99 | ||
100 | /// Create a new object by constructing it in place with the given arguments. | |
101 | template<typename T1, typename T2> | |
102 | void emplace(T1 &&A1, T2 &&A2) { | |
103 | reset(); | |
104 | hasVal = true; | |
105 | new (storage.buffer) T(std::forward<T1>(A1), std::forward<T2>(A2)); | |
106 | } | |
107 | ||
108 | /// Create a new object by constructing it in place with the given arguments. | |
109 | template<typename T1, typename T2, typename T3> | |
110 | void emplace(T1 &&A1, T2 &&A2, T3 &&A3) { | |
111 | reset(); | |
112 | hasVal = true; | |
113 | new (storage.buffer) T(std::forward<T1>(A1), std::forward<T2>(A2), | |
114 | std::forward<T3>(A3)); | |
115 | } | |
116 | ||
117 | /// Create a new object by constructing it in place with the given arguments. | |
118 | template<typename T1, typename T2, typename T3, typename T4> | |
119 | void emplace(T1 &&A1, T2 &&A2, T3 &&A3, T4 &&A4) { | |
120 | reset(); | |
121 | hasVal = true; | |
122 | new (storage.buffer) T(std::forward<T1>(A1), std::forward<T2>(A2), | |
123 | std::forward<T3>(A3), std::forward<T4>(A4)); | |
124 | } | |
125 | ||
126 | #endif // LLVM_HAS_VARIADIC_TEMPLATES | |
223e47cc LB |
127 | |
128 | static inline Optional create(const T* y) { | |
129 | return y ? Optional(*y) : Optional(); | |
130 | } | |
131 | ||
970d7e83 LB |
132 | // FIXME: these assignments (& the equivalent const T&/const Optional& ctors) |
133 | // could be made more efficient by passing by value, possibly unifying them | |
134 | // with the rvalue versions above - but this could place a different set of | |
135 | // requirements (notably: the existence of a default ctor) when implemented | |
136 | // in that way. Careful SFINAE to avoid such pitfalls would be required. | |
223e47cc | 137 | Optional &operator=(const T &y) { |
970d7e83 LB |
138 | if (hasVal) |
139 | **this = y; | |
140 | else { | |
141 | new (storage.buffer) T(y); | |
142 | hasVal = true; | |
143 | } | |
223e47cc LB |
144 | return *this; |
145 | } | |
223e47cc | 146 | |
970d7e83 LB |
147 | Optional &operator=(const Optional &O) { |
148 | if (!O) | |
149 | reset(); | |
150 | else | |
151 | *this = *O; | |
152 | return *this; | |
153 | } | |
154 | ||
155 | void reset() { | |
156 | if (hasVal) { | |
157 | (**this).~T(); | |
158 | hasVal = false; | |
159 | } | |
160 | } | |
161 | ||
162 | ~Optional() { | |
163 | reset(); | |
164 | } | |
165 | ||
166 | const T* getPointer() const { assert(hasVal); return reinterpret_cast<const T*>(storage.buffer); } | |
167 | T* getPointer() { assert(hasVal); return reinterpret_cast<T*>(storage.buffer); } | |
168 | const T& getValue() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); } | |
169 | T& getValue() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); } | |
170 | ||
171 | LLVM_EXPLICIT operator bool() const { return hasVal; } | |
223e47cc LB |
172 | bool hasValue() const { return hasVal; } |
173 | const T* operator->() const { return getPointer(); } | |
970d7e83 LB |
174 | T* operator->() { return getPointer(); } |
175 | const T& operator*() const LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); } | |
176 | T& operator*() LLVM_LVALUE_FUNCTION { assert(hasVal); return *getPointer(); } | |
177 | ||
1a4d82fc JJ |
178 | template <typename U> |
179 | LLVM_CONSTEXPR T getValueOr(U &&value) const LLVM_LVALUE_FUNCTION { | |
180 | return hasValue() ? getValue() : std::forward<U>(value); | |
181 | } | |
182 | ||
970d7e83 LB |
183 | #if LLVM_HAS_RVALUE_REFERENCE_THIS |
184 | T&& getValue() && { assert(hasVal); return std::move(*getPointer()); } | |
185 | T&& operator*() && { assert(hasVal); return std::move(*getPointer()); } | |
223e47cc | 186 | |
1a4d82fc JJ |
187 | template <typename U> |
188 | T getValueOr(U &&value) && { | |
189 | return hasValue() ? std::move(getValue()) : std::forward<U>(value); | |
223e47cc | 190 | } |
1a4d82fc | 191 | #endif |
223e47cc LB |
192 | }; |
193 | ||
970d7e83 LB |
194 | template <typename T> struct isPodLike; |
195 | template <typename T> struct isPodLike<Optional<T> > { | |
196 | // An Optional<T> is pod-like if T is. | |
197 | static const bool value = isPodLike<T>::value; | |
198 | }; | |
199 | ||
223e47cc LB |
200 | /// \brief Poison comparison between two \c Optional objects. Clients needs to |
201 | /// explicitly compare the underlying values and account for empty \c Optional | |
202 | /// objects. | |
203 | /// | |
970d7e83 | 204 | /// This routine will never be defined. It returns \c void to help diagnose |
223e47cc LB |
205 | /// errors at compile time. |
206 | template<typename T, typename U> | |
207 | void operator==(const Optional<T> &X, const Optional<U> &Y); | |
208 | ||
209 | /// \brief Poison comparison between two \c Optional objects. Clients needs to | |
210 | /// explicitly compare the underlying values and account for empty \c Optional | |
211 | /// objects. | |
212 | /// | |
970d7e83 | 213 | /// This routine will never be defined. It returns \c void to help diagnose |
223e47cc LB |
214 | /// errors at compile time. |
215 | template<typename T, typename U> | |
216 | void operator!=(const Optional<T> &X, const Optional<U> &Y); | |
217 | ||
218 | /// \brief Poison comparison between two \c Optional objects. Clients needs to | |
219 | /// explicitly compare the underlying values and account for empty \c Optional | |
220 | /// objects. | |
221 | /// | |
970d7e83 | 222 | /// This routine will never be defined. It returns \c void to help diagnose |
223e47cc LB |
223 | /// errors at compile time. |
224 | template<typename T, typename U> | |
225 | void operator<(const Optional<T> &X, const Optional<U> &Y); | |
226 | ||
227 | /// \brief Poison comparison between two \c Optional objects. Clients needs to | |
228 | /// explicitly compare the underlying values and account for empty \c Optional | |
229 | /// objects. | |
230 | /// | |
970d7e83 | 231 | /// This routine will never be defined. It returns \c void to help diagnose |
223e47cc LB |
232 | /// errors at compile time. |
233 | template<typename T, typename U> | |
234 | void operator<=(const Optional<T> &X, const Optional<U> &Y); | |
235 | ||
236 | /// \brief Poison comparison between two \c Optional objects. Clients needs to | |
237 | /// explicitly compare the underlying values and account for empty \c Optional | |
238 | /// objects. | |
239 | /// | |
970d7e83 | 240 | /// This routine will never be defined. It returns \c void to help diagnose |
223e47cc LB |
241 | /// errors at compile time. |
242 | template<typename T, typename U> | |
243 | void operator>=(const Optional<T> &X, const Optional<U> &Y); | |
244 | ||
245 | /// \brief Poison comparison between two \c Optional objects. Clients needs to | |
246 | /// explicitly compare the underlying values and account for empty \c Optional | |
247 | /// objects. | |
248 | /// | |
970d7e83 | 249 | /// This routine will never be defined. It returns \c void to help diagnose |
223e47cc LB |
250 | /// errors at compile time. |
251 | template<typename T, typename U> | |
252 | void operator>(const Optional<T> &X, const Optional<U> &Y); | |
253 | ||
254 | } // end llvm namespace | |
255 | ||
256 | #endif |