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 #include <type_traits>
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 = is_reference<T>::value;
86 typedef ReferenceStorage<typename remove_reference<T>::type> wrap;
96 typedef typename remove_reference<T>::type &reference;
97 typedef const typename remove_reference<T>::type &const_reference;
98 typedef typename remove_reference<T>::type *pointer;
102 ErrorOr(E ErrorCode, typename enable_if_c<is_error_code_enum<E>::value ||
103 is_error_condition_enum<E>::value,
106 new (getErrorStorage()) error_code(make_error_code(ErrorCode));
109 ErrorOr(llvm::error_code EC) : HasError(true) {
110 new (getErrorStorage()) error_code(EC);
113 ErrorOr(T Val) : HasError(false) {
114 new (getStorage()) storage_type(moveIfMoveConstructible<storage_type>(Val));
117 ErrorOr(const ErrorOr &Other) {
118 copyConstruct(Other);
121 template <class OtherT>
122 ErrorOr(const ErrorOr<OtherT> &Other) {
123 copyConstruct(Other);
126 ErrorOr &operator =(const ErrorOr &Other) {
131 template <class OtherT>
132 ErrorOr &operator =(const ErrorOr<OtherT> &Other) {
137 ErrorOr(ErrorOr &&Other) {
138 moveConstruct(std::move(Other));
141 template <class OtherT>
142 ErrorOr(ErrorOr<OtherT> &&Other) {
143 moveConstruct(std::move(Other));
146 ErrorOr &operator =(ErrorOr &&Other) {
147 moveAssign(std::move(Other));
151 template <class OtherT>
152 ErrorOr &operator =(ErrorOr<OtherT> &&Other) {
153 moveAssign(std::move(Other));
159 getStorage()->~storage_type();
162 /// \brief Return false if there is an error.
163 LLVM_EXPLICIT operator bool() const {
167 reference get() { return *getStorage(); }
168 const_reference get() const { return const_cast<ErrorOr<T> >(this)->get(); }
170 error_code getError() const {
171 return HasError ? *getErrorStorage() : error_code::success();
174 pointer operator ->() {
175 return toPointer(getStorage());
178 reference operator *() {
179 return *getStorage();
183 template <class OtherT>
184 void copyConstruct(const ErrorOr<OtherT> &Other) {
185 if (!Other.HasError) {
186 // Get the other value.
188 new (getStorage()) storage_type(*Other.getStorage());
190 // Get other's error.
192 new (getErrorStorage()) error_code(Other.getError());
197 static bool compareThisIfSameType(const T1 &a, const T1 &b) {
201 template <class T1, class T2>
202 static bool compareThisIfSameType(const T1 &a, const T2 &b) {
206 template <class OtherT>
207 void copyAssign(const ErrorOr<OtherT> &Other) {
208 if (compareThisIfSameType(*this, Other))
212 new (this) ErrorOr(Other);
215 template <class OtherT>
216 void moveConstruct(ErrorOr<OtherT> &&Other) {
217 if (!Other.HasError) {
218 // Get the other value.
220 new (getStorage()) storage_type(std::move(*Other.getStorage()));
222 // Get other's error.
224 new (getErrorStorage()) error_code(Other.getError());
228 template <class OtherT>
229 void moveAssign(ErrorOr<OtherT> &&Other) {
230 if (compareThisIfSameType(*this, Other))
234 new (this) ErrorOr(std::move(Other));
237 pointer toPointer(pointer Val) {
241 pointer toPointer(wrap *Val) {
245 storage_type *getStorage() {
246 assert(!HasError && "Cannot get value when an error exists!");
247 return reinterpret_cast<storage_type*>(TStorage.buffer);
250 const storage_type *getStorage() const {
251 assert(!HasError && "Cannot get value when an error exists!");
252 return reinterpret_cast<const storage_type*>(TStorage.buffer);
255 error_code *getErrorStorage() {
256 assert(HasError && "Cannot get error when a value exists!");
257 return reinterpret_cast<error_code*>(ErrorStorage.buffer);
260 const error_code *getErrorStorage() const {
261 return const_cast<ErrorOr<T> *>(this)->getErrorStorage();
266 AlignedCharArrayUnion<storage_type> TStorage;
267 AlignedCharArrayUnion<error_code> ErrorStorage;
272 template<class T, class E>
273 typename enable_if_c<is_error_code_enum<E>::value ||
274 is_error_condition_enum<E>::value, bool>::type
275 operator ==(ErrorOr<T> &Err, E Code) {
276 return error_code(Err) == Code;
278 } // end namespace llvm