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"
22 #include <system_error>
23 #include <type_traits>
26 using std::error_code;
27 template<class T, class V>
28 typename std::enable_if< std::is_constructible<T, V>::value
29 , typename std::remove_reference<V>::type>::type &&
30 moveIfMoveConstructible(V &Val) {
31 return std::move(Val);
34 template<class T, class V>
35 typename std::enable_if< !std::is_constructible<T, V>::value
36 , typename std::remove_reference<V>::type>::type &
37 moveIfMoveConstructible(V &Val) {
41 /// \brief Stores a reference that can be changed.
43 class ReferenceStorage {
47 ReferenceStorage(T &Ref) : Storage(&Ref) {}
49 operator T &() const { return *Storage; }
50 T &get() const { return *Storage; }
53 /// \brief Represents either an error or a value T.
55 /// ErrorOr<T> is a pointer-like class that represents the result of an
56 /// operation. The result is either an error, or a value of type T. This is
57 /// designed to emulate the usage of returning a pointer where nullptr indicates
58 /// failure. However instead of just knowing that the operation failed, we also
59 /// have an error_code and optional user data that describes why it failed.
61 /// It is used like the following.
63 /// ErrorOr<Buffer> getBuffer();
65 /// auto buffer = getBuffer();
66 /// if (error_code ec = buffer.getError())
68 /// buffer->write("adena");
72 /// An implicit conversion to bool provides a way to check if there was an
73 /// error. The unary * and -> operators provide pointer like access to the
74 /// value. Accessing the value when there is an error has undefined behavior.
76 /// When T is a reference type the behaivor is slightly different. The reference
77 /// is held in a std::reference_wrapper<std::remove_reference<T>::type>, and
78 /// there is special handling to make operator -> work as if T was not a
81 /// T cannot be a rvalue reference.
84 template <class OtherT> friend class ErrorOr;
85 static const bool isRef = std::is_reference<T>::value;
86 typedef ReferenceStorage<typename std::remove_reference<T>::type> wrap;
89 typedef typename std::conditional<isRef, wrap, T>::type storage_type;
92 typedef typename std::remove_reference<T>::type &reference;
93 typedef const typename std::remove_reference<T>::type &const_reference;
94 typedef typename std::remove_reference<T>::type *pointer;
99 typename std::enable_if<std::is_error_code_enum<E>::value ||
100 std::is_error_condition_enum<E>::value,
103 using std::make_error_code;
104 new (getErrorStorage()) error_code(make_error_code(ErrorCode));
107 ErrorOr(std::error_code EC) : HasError(true) {
108 new (getErrorStorage()) error_code(EC);
111 ErrorOr(T Val) : HasError(false) {
112 new (getStorage()) storage_type(moveIfMoveConstructible<storage_type>(Val));
115 ErrorOr(const ErrorOr &Other) {
116 copyConstruct(Other);
119 template <class OtherT>
120 ErrorOr(const ErrorOr<OtherT> &Other) {
121 copyConstruct(Other);
124 ErrorOr &operator =(const ErrorOr &Other) {
129 template <class OtherT>
130 ErrorOr &operator =(const ErrorOr<OtherT> &Other) {
135 ErrorOr(ErrorOr &&Other) {
136 moveConstruct(std::move(Other));
139 template <class OtherT>
140 ErrorOr(ErrorOr<OtherT> &&Other) {
141 moveConstruct(std::move(Other));
144 ErrorOr &operator =(ErrorOr &&Other) {
145 moveAssign(std::move(Other));
149 template <class OtherT>
150 ErrorOr &operator =(ErrorOr<OtherT> &&Other) {
151 moveAssign(std::move(Other));
157 getStorage()->~storage_type();
160 /// \brief Return false if there is an error.
161 LLVM_EXPLICIT operator bool() const {
165 reference get() { return *getStorage(); }
166 const_reference get() const { return const_cast<ErrorOr<T> >(this)->get(); }
168 error_code getError() const {
169 return HasError ? *getErrorStorage() : error_code();
172 pointer operator ->() {
173 return toPointer(getStorage());
176 reference operator *() {
177 return *getStorage();
181 template <class OtherT>
182 void copyConstruct(const ErrorOr<OtherT> &Other) {
183 if (!Other.HasError) {
184 // Get the other value.
186 new (getStorage()) storage_type(*Other.getStorage());
188 // Get other's error.
190 new (getErrorStorage()) error_code(Other.getError());
195 static bool compareThisIfSameType(const T1 &a, const T1 &b) {
199 template <class T1, class T2>
200 static bool compareThisIfSameType(const T1 &a, const T2 &b) {
204 template <class OtherT>
205 void copyAssign(const ErrorOr<OtherT> &Other) {
206 if (compareThisIfSameType(*this, Other))
210 new (this) ErrorOr(Other);
213 template <class OtherT>
214 void moveConstruct(ErrorOr<OtherT> &&Other) {
215 if (!Other.HasError) {
216 // Get the other value.
218 new (getStorage()) storage_type(std::move(*Other.getStorage()));
220 // Get other's error.
222 new (getErrorStorage()) error_code(Other.getError());
226 template <class OtherT>
227 void moveAssign(ErrorOr<OtherT> &&Other) {
228 if (compareThisIfSameType(*this, Other))
232 new (this) ErrorOr(std::move(Other));
235 pointer toPointer(pointer Val) {
239 pointer toPointer(wrap *Val) {
243 storage_type *getStorage() {
244 assert(!HasError && "Cannot get value when an error exists!");
245 return reinterpret_cast<storage_type*>(TStorage.buffer);
248 const storage_type *getStorage() const {
249 assert(!HasError && "Cannot get value when an error exists!");
250 return reinterpret_cast<const storage_type*>(TStorage.buffer);
253 error_code *getErrorStorage() {
254 assert(HasError && "Cannot get error when a value exists!");
255 return reinterpret_cast<error_code*>(ErrorStorage.buffer);
258 const error_code *getErrorStorage() const {
259 return const_cast<ErrorOr<T> *>(this)->getErrorStorage();
264 AlignedCharArrayUnion<storage_type> TStorage;
265 AlignedCharArrayUnion<error_code> ErrorStorage;
270 template <class T, class E>
271 typename std::enable_if<std::is_error_code_enum<E>::value ||
272 std::is_error_condition_enum<E>::value,
274 operator==(ErrorOr<T> &Err, E Code) {
275 return error_code(Err) == Code;
277 } // end namespace llvm