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"
23 #include <type_traits>
26 template<class T, class V>
27 typename std::enable_if< std::is_constructible<T, V>::value
28 , typename std::remove_reference<V>::type>::type &&
29 moveIfMoveConstructible(V &Val) {
30 return std::move(Val);
33 template<class T, class V>
34 typename std::enable_if< !std::is_constructible<T, V>::value
35 , typename std::remove_reference<V>::type>::type &
36 moveIfMoveConstructible(V &Val) {
40 /// \brief Stores a reference that can be changed.
42 class ReferenceStorage {
46 ReferenceStorage(T &Ref) : Storage(&Ref) {}
48 operator T &() const { return *Storage; }
49 T &get() const { return *Storage; }
52 /// \brief Represents either an error or a value T.
54 /// ErrorOr<T> is a pointer-like class that represents the result of an
55 /// operation. The result is either an error, or a value of type T. This is
56 /// designed to emulate the usage of returning a pointer where nullptr indicates
57 /// failure. However instead of just knowing that the operation failed, we also
58 /// have an error_code and optional user data that describes why it failed.
60 /// It is used like the following.
62 /// ErrorOr<Buffer> getBuffer();
64 /// auto buffer = getBuffer();
65 /// if (error_code ec = buffer.getError())
67 /// buffer->write("adena");
71 /// An implicit conversion to bool provides a way to check if there was an
72 /// error. The unary * and -> operators provide pointer like access to the
73 /// value. Accessing the value when there is an error has undefined behavior.
75 /// When T is a reference type the behaivor is slightly different. The reference
76 /// is held in a std::reference_wrapper<std::remove_reference<T>::type>, and
77 /// there is special handling to make operator -> work as if T was not a
80 /// T cannot be a rvalue reference.
83 template <class OtherT> friend class ErrorOr;
84 static const bool isRef = std::is_reference<T>::value;
85 typedef ReferenceStorage<typename std::remove_reference<T>::type> wrap;
88 typedef typename std::conditional<isRef, wrap, T>::type storage_type;
91 typedef typename std::remove_reference<T>::type &reference;
92 typedef const typename std::remove_reference<T>::type &const_reference;
93 typedef typename std::remove_reference<T>::type *pointer;
97 ErrorOr(E ErrorCode, typename std::enable_if<is_error_code_enum<E>::value ||
98 is_error_condition_enum<E>::value,
101 new (getErrorStorage()) error_code(make_error_code(ErrorCode));
104 ErrorOr(llvm::error_code EC) : HasError(true) {
105 new (getErrorStorage()) error_code(EC);
108 ErrorOr(T Val) : HasError(false) {
109 new (getStorage()) storage_type(moveIfMoveConstructible<storage_type>(Val));
112 ErrorOr(const ErrorOr &Other) {
113 copyConstruct(Other);
116 template <class OtherT>
117 ErrorOr(const ErrorOr<OtherT> &Other) {
118 copyConstruct(Other);
121 ErrorOr &operator =(const ErrorOr &Other) {
126 template <class OtherT>
127 ErrorOr &operator =(const ErrorOr<OtherT> &Other) {
132 ErrorOr(ErrorOr &&Other) {
133 moveConstruct(std::move(Other));
136 template <class OtherT>
137 ErrorOr(ErrorOr<OtherT> &&Other) {
138 moveConstruct(std::move(Other));
141 ErrorOr &operator =(ErrorOr &&Other) {
142 moveAssign(std::move(Other));
146 template <class OtherT>
147 ErrorOr &operator =(ErrorOr<OtherT> &&Other) {
148 moveAssign(std::move(Other));
154 getStorage()->~storage_type();
157 /// \brief Return false if there is an error.
158 LLVM_EXPLICIT operator bool() const {
162 reference get() { return *getStorage(); }
163 const_reference get() const { return const_cast<ErrorOr<T> >(this)->get(); }
165 error_code getError() const {
166 return HasError ? *getErrorStorage() : error_code();
169 pointer operator ->() {
170 return toPointer(getStorage());
173 reference operator *() {
174 return *getStorage();
178 template <class OtherT>
179 void copyConstruct(const ErrorOr<OtherT> &Other) {
180 if (!Other.HasError) {
181 // Get the other value.
183 new (getStorage()) storage_type(*Other.getStorage());
185 // Get other's error.
187 new (getErrorStorage()) error_code(Other.getError());
192 static bool compareThisIfSameType(const T1 &a, const T1 &b) {
196 template <class T1, class T2>
197 static bool compareThisIfSameType(const T1 &a, const T2 &b) {
201 template <class OtherT>
202 void copyAssign(const ErrorOr<OtherT> &Other) {
203 if (compareThisIfSameType(*this, Other))
207 new (this) ErrorOr(Other);
210 template <class OtherT>
211 void moveConstruct(ErrorOr<OtherT> &&Other) {
212 if (!Other.HasError) {
213 // Get the other value.
215 new (getStorage()) storage_type(std::move(*Other.getStorage()));
217 // Get other's error.
219 new (getErrorStorage()) error_code(Other.getError());
223 template <class OtherT>
224 void moveAssign(ErrorOr<OtherT> &&Other) {
225 if (compareThisIfSameType(*this, Other))
229 new (this) ErrorOr(std::move(Other));
232 pointer toPointer(pointer Val) {
236 pointer toPointer(wrap *Val) {
240 storage_type *getStorage() {
241 assert(!HasError && "Cannot get value when an error exists!");
242 return reinterpret_cast<storage_type*>(TStorage.buffer);
245 const storage_type *getStorage() const {
246 assert(!HasError && "Cannot get value when an error exists!");
247 return reinterpret_cast<const storage_type*>(TStorage.buffer);
250 error_code *getErrorStorage() {
251 assert(HasError && "Cannot get error when a value exists!");
252 return reinterpret_cast<error_code*>(ErrorStorage.buffer);
255 const error_code *getErrorStorage() const {
256 return const_cast<ErrorOr<T> *>(this)->getErrorStorage();
261 AlignedCharArrayUnion<storage_type> TStorage;
262 AlignedCharArrayUnion<error_code> ErrorStorage;
267 template<class T, class E>
268 typename std::enable_if<is_error_code_enum<E>::value ||
269 is_error_condition_enum<E>::value, bool>::type
270 operator ==(ErrorOr<T> &Err, E Code) {
271 return error_code(Err) == Code;
273 } // end namespace llvm