1 //===- llvm/Support/ErrorOr.h - Error Smart Pointer -----------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 /// Provides ErrorOr<T> smart pointer.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_SUPPORT_ERROR_OR_H
17 #define LLVM_SUPPORT_ERROR_OR_H
19 #include "llvm/ADT/PointerIntPair.h"
20 #include "llvm/Support/AlignOf.h"
21 #include "llvm/Support/system_error.h"
22 #include "llvm/Support/type_traits.h"
25 #if LLVM_HAS_CXX11_TYPETRAITS
26 #include <type_traits>
30 #if LLVM_HAS_CXX11_TYPETRAITS && LLVM_HAS_RVALUE_REFERENCES
31 template<class T, class V>
32 typename std::enable_if< std::is_constructible<T, V>::value
33 , typename std::remove_reference<V>::type>::type &&
34 moveIfMoveConstructible(V &Val) {
35 return std::move(Val);
38 template<class T, class V>
39 typename std::enable_if< !std::is_constructible<T, V>::value
40 , typename std::remove_reference<V>::type>::type &
41 moveIfMoveConstructible(V &Val) {
45 template<class T, class V>
46 V &moveIfMoveConstructible(V &Val) {
51 /// \brief Stores a reference that can be changed.
53 class ReferenceStorage {
57 ReferenceStorage(T &Ref) : Storage(&Ref) {}
59 operator T &() const { return *Storage; }
60 T &get() const { return *Storage; }
63 /// \brief Represents either an error or a value T.
65 /// ErrorOr<T> is a pointer-like class that represents the result of an
66 /// operation. The result is either an error, or a value of type T. This is
67 /// designed to emulate the usage of returning a pointer where nullptr indicates
68 /// failure. However instead of just knowing that the operation failed, we also
69 /// have an error_code and optional user data that describes why it failed.
71 /// It is used like the following.
73 /// ErrorOr<Buffer> getBuffer();
74 /// void handleError(error_code ec);
76 /// auto buffer = getBuffer();
78 /// handleError(buffer);
79 /// buffer->write("adena");
83 /// An implicit conversion to bool provides a way to check if there was an
84 /// error. The unary * and -> operators provide pointer like access to the
85 /// value. Accessing the value when there is an error has undefined behavior.
87 /// When T is a reference type the behaivor is slightly different. The reference
88 /// is held in a std::reference_wrapper<std::remove_reference<T>::type>, and
89 /// there is special handling to make operator -> work as if T was not a
92 /// T cannot be a rvalue reference.
95 template <class OtherT> friend class ErrorOr;
96 static const bool isRef = is_reference<T>::value;
97 typedef ReferenceStorage<typename remove_reference<T>::type> wrap;
104 >::type storage_type;
107 typedef typename remove_reference<T>::type &reference;
108 typedef typename remove_reference<T>::type *pointer;
112 ErrorOr(E ErrorCode, typename enable_if_c<is_error_code_enum<E>::value ||
113 is_error_condition_enum<E>::value,
116 new (getError()) error_code(make_error_code(ErrorCode));
119 ErrorOr(llvm::error_code EC) : HasError(true) {
120 new (getError()) error_code(EC);
123 ErrorOr(T Val) : HasError(false) {
124 new (get()) storage_type(moveIfMoveConstructible<storage_type>(Val));
127 ErrorOr(const ErrorOr &Other) {
128 copyConstruct(Other);
131 template <class OtherT>
132 ErrorOr(const ErrorOr<OtherT> &Other) {
133 copyConstruct(Other);
136 ErrorOr &operator =(const ErrorOr &Other) {
141 template <class OtherT>
142 ErrorOr &operator =(const ErrorOr<OtherT> &Other) {
147 #if LLVM_HAS_RVALUE_REFERENCES
148 ErrorOr(ErrorOr &&Other) {
149 moveConstruct(std::move(Other));
152 template <class OtherT>
153 ErrorOr(ErrorOr<OtherT> &&Other) {
154 moveConstruct(std::move(Other));
157 ErrorOr &operator =(ErrorOr &&Other) {
158 moveAssign(std::move(Other));
162 template <class OtherT>
163 ErrorOr &operator =(ErrorOr<OtherT> &&Other) {
164 moveAssign(std::move(Other));
171 get()->~storage_type();
174 typedef void (*unspecified_bool_type)();
175 static void unspecified_bool_true() {}
177 /// \brief Return false if there is an error.
178 operator unspecified_bool_type() const {
179 return HasError ? 0 : unspecified_bool_true;
182 operator llvm::error_code() const {
183 return HasError ? *getError() : llvm::error_code::success();
186 pointer operator ->() {
187 return toPointer(get());
190 reference operator *() {
195 template <class OtherT>
196 void copyConstruct(const ErrorOr<OtherT> &Other) {
197 if (!Other.HasError) {
198 // Get the other value.
200 new (get()) storage_type(*Other.get());
202 // Get other's error.
204 new (getError()) error_code(Other);
209 static bool compareThisIfSameType(const T1 &a, const T1 &b) {
213 template <class T1, class T2>
214 static bool compareThisIfSameType(const T1 &a, const T2 &b) {
218 template <class OtherT>
219 void copyAssign(const ErrorOr<OtherT> &Other) {
220 if (compareThisIfSameType(*this, Other))
224 new (this) ErrorOr(Other);
227 #if LLVM_HAS_RVALUE_REFERENCES
228 template <class OtherT>
229 void moveConstruct(ErrorOr<OtherT> &&Other) {
230 if (!Other.HasError) {
231 // Get the other value.
233 new (get()) storage_type(std::move(*Other.get()));
235 // Get other's error.
237 new (getError()) error_code(Other);
241 template <class OtherT>
242 void moveAssign(ErrorOr<OtherT> &&Other) {
243 if (compareThisIfSameType(*this, Other))
247 new (this) ErrorOr(std::move(Other));
251 pointer toPointer(pointer Val) {
255 pointer toPointer(wrap *Val) {
259 storage_type *get() {
260 assert(!HasError && "Cannot get value when an error exists!");
261 return reinterpret_cast<storage_type*>(TStorage.buffer);
264 const storage_type *get() const {
265 assert(!HasError && "Cannot get value when an error exists!");
266 return reinterpret_cast<const storage_type*>(TStorage.buffer);
269 error_code *getError() {
270 assert(HasError && "Cannot get error when a value exists!");
271 return reinterpret_cast<error_code*>(ErrorStorage.buffer);
274 const error_code *getError() const {
275 return const_cast<ErrorOr<T> *>(this)->getError();
280 AlignedCharArrayUnion<storage_type> TStorage;
281 AlignedCharArrayUnion<error_code> ErrorStorage;
286 template<class T, class E>
287 typename enable_if_c<is_error_code_enum<E>::value ||
288 is_error_condition_enum<E>::value, bool>::type
289 operator ==(ErrorOr<T> &Err, E Code) {
290 return error_code(Err) == Code;
292 } // end namespace llvm