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 template<class T, class V>
30 typename std::enable_if< std::is_constructible<T, V>::value
31 , typename std::remove_reference<V>::type>::type &&
32 moveIfMoveConstructible(V &Val) {
33 return std::move(Val);
36 template<class T, class V>
37 typename std::enable_if< !std::is_constructible<T, V>::value
38 , typename std::remove_reference<V>::type>::type &
39 moveIfMoveConstructible(V &Val) {
43 /// \brief Stores a reference that can be changed.
45 class ReferenceStorage {
49 ReferenceStorage(T &Ref) : Storage(&Ref) {}
51 operator T &() const { return *Storage; }
52 T &get() const { return *Storage; }
55 /// \brief Represents either an error or a value T.
57 /// ErrorOr<T> is a pointer-like class that represents the result of an
58 /// operation. The result is either an error, or a value of type T. This is
59 /// designed to emulate the usage of returning a pointer where nullptr indicates
60 /// failure. However instead of just knowing that the operation failed, we also
61 /// have an error_code and optional user data that describes why it failed.
63 /// It is used like the following.
65 /// ErrorOr<Buffer> getBuffer();
67 /// auto buffer = getBuffer();
68 /// if (error_code ec = buffer.getError())
70 /// buffer->write("adena");
74 /// An implicit conversion to bool provides a way to check if there was an
75 /// error. The unary * and -> operators provide pointer like access to the
76 /// value. Accessing the value when there is an error has undefined behavior.
78 /// When T is a reference type the behaivor is slightly different. The reference
79 /// is held in a std::reference_wrapper<std::remove_reference<T>::type>, and
80 /// there is special handling to make operator -> work as if T was not a
83 /// T cannot be a rvalue reference.
86 template <class OtherT> friend class ErrorOr;
87 static const bool isRef = is_reference<T>::value;
88 typedef ReferenceStorage<typename remove_reference<T>::type> wrap;
98 typedef typename remove_reference<T>::type &reference;
99 typedef const typename remove_reference<T>::type &const_reference;
100 typedef typename remove_reference<T>::type *pointer;
104 ErrorOr(E ErrorCode, typename enable_if_c<is_error_code_enum<E>::value ||
105 is_error_condition_enum<E>::value,
108 new (getErrorStorage()) error_code(make_error_code(ErrorCode));
111 ErrorOr(llvm::error_code EC) : HasError(true) {
112 new (getErrorStorage()) error_code(EC);
115 ErrorOr(T Val) : HasError(false) {
116 new (getStorage()) storage_type(moveIfMoveConstructible<storage_type>(Val));
119 ErrorOr(const ErrorOr &Other) {
120 copyConstruct(Other);
123 template <class OtherT>
124 ErrorOr(const ErrorOr<OtherT> &Other) {
125 copyConstruct(Other);
128 ErrorOr &operator =(const ErrorOr &Other) {
133 template <class OtherT>
134 ErrorOr &operator =(const ErrorOr<OtherT> &Other) {
139 ErrorOr(ErrorOr &&Other) {
140 moveConstruct(std::move(Other));
143 template <class OtherT>
144 ErrorOr(ErrorOr<OtherT> &&Other) {
145 moveConstruct(std::move(Other));
148 ErrorOr &operator =(ErrorOr &&Other) {
149 moveAssign(std::move(Other));
153 template <class OtherT>
154 ErrorOr &operator =(ErrorOr<OtherT> &&Other) {
155 moveAssign(std::move(Other));
161 getStorage()->~storage_type();
164 /// \brief Return false if there is an error.
165 LLVM_EXPLICIT operator bool() const {
169 reference get() { return *getStorage(); }
170 const_reference get() const { return const_cast<ErrorOr<T> >(this)->get(); }
172 error_code getError() const {
173 return HasError ? *getErrorStorage() : error_code::success();
176 pointer operator ->() {
177 return toPointer(getStorage());
180 reference operator *() {
181 return *getStorage();
185 template <class OtherT>
186 void copyConstruct(const ErrorOr<OtherT> &Other) {
187 if (!Other.HasError) {
188 // Get the other value.
190 new (getStorage()) storage_type(*Other.getStorage());
192 // Get other's error.
194 new (getErrorStorage()) error_code(Other.getError());
199 static bool compareThisIfSameType(const T1 &a, const T1 &b) {
203 template <class T1, class T2>
204 static bool compareThisIfSameType(const T1 &a, const T2 &b) {
208 template <class OtherT>
209 void copyAssign(const ErrorOr<OtherT> &Other) {
210 if (compareThisIfSameType(*this, Other))
214 new (this) ErrorOr(Other);
217 template <class OtherT>
218 void moveConstruct(ErrorOr<OtherT> &&Other) {
219 if (!Other.HasError) {
220 // Get the other value.
222 new (getStorage()) storage_type(std::move(*Other.getStorage()));
224 // Get other's error.
226 new (getErrorStorage()) error_code(Other.getError());
230 template <class OtherT>
231 void moveAssign(ErrorOr<OtherT> &&Other) {
232 if (compareThisIfSameType(*this, Other))
236 new (this) ErrorOr(std::move(Other));
239 pointer toPointer(pointer Val) {
243 pointer toPointer(wrap *Val) {
247 storage_type *getStorage() {
248 assert(!HasError && "Cannot get value when an error exists!");
249 return reinterpret_cast<storage_type*>(TStorage.buffer);
252 const storage_type *getStorage() const {
253 assert(!HasError && "Cannot get value when an error exists!");
254 return reinterpret_cast<const storage_type*>(TStorage.buffer);
257 error_code *getErrorStorage() {
258 assert(HasError && "Cannot get error when a value exists!");
259 return reinterpret_cast<error_code*>(ErrorStorage.buffer);
262 const error_code *getErrorStorage() const {
263 return const_cast<ErrorOr<T> *>(this)->getErrorStorage();
268 AlignedCharArrayUnion<storage_type> TStorage;
269 AlignedCharArrayUnion<error_code> ErrorStorage;
274 template<class T, class E>
275 typename enable_if_c<is_error_code_enum<E>::value ||
276 is_error_condition_enum<E>::value, bool>::type
277 operator ==(ErrorOr<T> &Err, E Code) {
278 return error_code(Err) == Code;
280 } // end namespace llvm