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"
24 #if LLVM_HAS_CXX11_TYPETRAITS
25 #include <type_traits>
29 #if LLVM_HAS_CXX11_TYPETRAITS && LLVM_HAS_RVALUE_REFERENCES
30 template<class T, class V>
31 typename std::enable_if< std::is_constructible<T, V>::value
32 , typename std::remove_reference<V>::type>::type &&
33 moveIfMoveConstructible(V &Val) {
34 return std::move(Val);
37 template<class T, class V>
38 typename std::enable_if< !std::is_constructible<T, V>::value
39 , typename std::remove_reference<V>::type>::type &
40 moveIfMoveConstructible(V &Val) {
44 template<class T, class V>
45 V &moveIfMoveConstructible(V &Val) {
50 /// \brief Stores a reference that can be changed.
52 class ReferenceStorage {
56 ReferenceStorage(T &Ref) : Storage(&Ref) {}
58 operator T &() const { return *Storage; }
59 T &get() const { return *Storage; }
62 /// \brief Represents either an error or a value T.
64 /// ErrorOr<T> is a pointer-like class that represents the result of an
65 /// operation. The result is either an error, or a value of type T. This is
66 /// designed to emulate the usage of returning a pointer where nullptr indicates
67 /// failure. However instead of just knowing that the operation failed, we also
68 /// have an error_code and optional user data that describes why it failed.
70 /// It is used like the following.
72 /// ErrorOr<Buffer> getBuffer();
73 /// void handleError(error_code ec);
75 /// auto buffer = getBuffer();
77 /// handleError(buffer);
78 /// buffer->write("adena");
82 /// An implicit conversion to bool provides a way to check if there was an
83 /// error. The unary * and -> operators provide pointer like access to the
84 /// value. Accessing the value when there is an error has undefined behavior.
86 /// When T is a reference type the behaivor is slightly different. The reference
87 /// is held in a std::reference_wrapper<std::remove_reference<T>::type>, and
88 /// there is special handling to make operator -> work as if T was not a
91 /// T cannot be a rvalue reference.
94 template <class OtherT> friend class ErrorOr;
95 static const bool isRef = is_reference<T>::value;
96 typedef ReferenceStorage<typename remove_reference<T>::type> wrap;
103 >::type storage_type;
106 typedef typename remove_reference<T>::type &reference;
107 typedef typename remove_reference<T>::type *pointer;
111 ErrorOr(E ErrorCode, typename enable_if_c<is_error_code_enum<E>::value ||
112 is_error_condition_enum<E>::value,
115 new (getErrorStorage()) error_code(make_error_code(ErrorCode));
118 ErrorOr(llvm::error_code EC) : HasError(true) {
119 new (getErrorStorage()) error_code(EC);
122 ErrorOr(T Val) : HasError(false) {
123 new (getStorage()) storage_type(moveIfMoveConstructible<storage_type>(Val));
126 ErrorOr(const ErrorOr &Other) {
127 copyConstruct(Other);
130 template <class OtherT>
131 ErrorOr(const ErrorOr<OtherT> &Other) {
132 copyConstruct(Other);
135 ErrorOr &operator =(const ErrorOr &Other) {
140 template <class OtherT>
141 ErrorOr &operator =(const ErrorOr<OtherT> &Other) {
146 #if LLVM_HAS_RVALUE_REFERENCES
147 ErrorOr(ErrorOr &&Other) {
148 moveConstruct(std::move(Other));
151 template <class OtherT>
152 ErrorOr(ErrorOr<OtherT> &&Other) {
153 moveConstruct(std::move(Other));
156 ErrorOr &operator =(ErrorOr &&Other) {
157 moveAssign(std::move(Other));
161 template <class OtherT>
162 ErrorOr &operator =(ErrorOr<OtherT> &&Other) {
163 moveAssign(std::move(Other));
170 getStorage()->~storage_type();
173 typedef void (*unspecified_bool_type)();
174 static void unspecified_bool_true() {}
176 /// \brief Return false if there is an error.
177 operator unspecified_bool_type() const {
178 return HasError ? 0 : unspecified_bool_true;
181 T &get() { return *getStorage(); }
182 const T &get() const { return const_cast<ErrorOr<T> >(this)->get(); }
184 operator llvm::error_code() const {
185 return HasError ? *getErrorStorage() : llvm::error_code::success();
188 error_code getError() const {
189 return HasError ? *getErrorStorage() : error_code::success();
192 pointer operator ->() {
193 return toPointer(getStorage());
196 reference operator *() {
197 return *getStorage();
201 template <class OtherT>
202 void copyConstruct(const ErrorOr<OtherT> &Other) {
203 if (!Other.HasError) {
204 // Get the other value.
206 new (getStorage()) storage_type(*Other.getStorage());
208 // Get other's error.
210 new (getErrorStorage()) error_code(Other);
215 static bool compareThisIfSameType(const T1 &a, const T1 &b) {
219 template <class T1, class T2>
220 static bool compareThisIfSameType(const T1 &a, const T2 &b) {
224 template <class OtherT>
225 void copyAssign(const ErrorOr<OtherT> &Other) {
226 if (compareThisIfSameType(*this, Other))
230 new (this) ErrorOr(Other);
233 #if LLVM_HAS_RVALUE_REFERENCES
234 template <class OtherT>
235 void moveConstruct(ErrorOr<OtherT> &&Other) {
236 if (!Other.HasError) {
237 // Get the other value.
239 new (getStorage()) storage_type(std::move(*Other.getStorage()));
241 // Get other's error.
243 new (getErrorStorage()) error_code(Other);
247 template <class OtherT>
248 void moveAssign(ErrorOr<OtherT> &&Other) {
249 if (compareThisIfSameType(*this, Other))
253 new (this) ErrorOr(std::move(Other));
257 pointer toPointer(pointer Val) {
261 pointer toPointer(wrap *Val) {
265 storage_type *getStorage() {
266 assert(!HasError && "Cannot get value when an error exists!");
267 return reinterpret_cast<storage_type*>(TStorage.buffer);
270 const storage_type *getStorage() const {
271 assert(!HasError && "Cannot get value when an error exists!");
272 return reinterpret_cast<const storage_type*>(TStorage.buffer);
275 error_code *getErrorStorage() {
276 assert(HasError && "Cannot get error when a value exists!");
277 return reinterpret_cast<error_code*>(ErrorStorage.buffer);
280 const error_code *getErrorStorage() const {
281 return const_cast<ErrorOr<T> *>(this)->getErrorStorage();
286 AlignedCharArrayUnion<storage_type> TStorage;
287 AlignedCharArrayUnion<error_code> ErrorStorage;
292 template<class T, class E>
293 typename enable_if_c<is_error_code_enum<E>::value ||
294 is_error_condition_enum<E>::value, bool>::type
295 operator ==(ErrorOr<T> &Err, E Code) {
296 return error_code(Err) == Code;
298 } // end namespace llvm