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"
25 #if LLVM_HAS_CXX11_TYPETRAITS
26 #include <type_traits>
30 #if LLVM_HAS_CXX11_TYPETRAITS && LLVM_HAS_RVALUE_REFERENCES
31 template<class T, class V>
32 typename std::enable_if< std::is_constructible<T, V>::value
33 , typename std::remove_reference<V>::type>::type &&
34 moveIfMoveConstructible(V &Val) {
35 return std::move(Val);
38 template<class T, class V>
39 typename std::enable_if< !std::is_constructible<T, V>::value
40 , typename std::remove_reference<V>::type>::type &
41 moveIfMoveConstructible(V &Val) {
45 template<class T, class V>
46 V &moveIfMoveConstructible(V &Val) {
51 /// \brief Stores a reference that can be changed.
53 class ReferenceStorage {
57 ReferenceStorage(T &Ref) : Storage(&Ref) {}
59 operator T &() const { return *Storage; }
60 T &get() const { return *Storage; }
63 /// \brief Represents either an error or a value T.
65 /// ErrorOr<T> is a pointer-like class that represents the result of an
66 /// operation. The result is either an error, or a value of type T. This is
67 /// designed to emulate the usage of returning a pointer where nullptr indicates
68 /// failure. However instead of just knowing that the operation failed, we also
69 /// have an error_code and optional user data that describes why it failed.
71 /// It is used like the following.
73 /// ErrorOr<Buffer> getBuffer();
74 /// void handleError(error_code ec);
76 /// auto buffer = getBuffer();
78 /// handleError(buffer);
79 /// buffer->write("adena");
83 /// An implicit conversion to bool provides a way to check if there was an
84 /// error. The unary * and -> operators provide pointer like access to the
85 /// value. Accessing the value when there is an error has undefined behavior.
87 /// When T is a reference type the behaivor is slightly different. The reference
88 /// is held in a std::reference_wrapper<std::remove_reference<T>::type>, and
89 /// there is special handling to make operator -> work as if T was not a
92 /// T cannot be a rvalue reference.
95 template <class OtherT> friend class ErrorOr;
96 static const bool isRef = is_reference<T>::value;
97 typedef ReferenceStorage<typename remove_reference<T>::type> wrap;
104 >::type storage_type;
107 typedef typename remove_reference<T>::type &reference;
108 typedef typename remove_reference<T>::type *pointer;
111 ErrorOr() : IsValid(false) {}
114 ErrorOr(E ErrorCode, typename enable_if_c<is_error_code_enum<E>::value ||
115 is_error_condition_enum<E>::value,
117 : HasError(true), IsValid(true) {
118 Error = make_error_code(ErrorCode);
121 ErrorOr(llvm::error_code EC) : HasError(true), IsValid(true) {
125 ErrorOr(T Val) : HasError(false), IsValid(true) {
126 new (get()) storage_type(moveIfMoveConstructible<storage_type>(Val));
129 ErrorOr(const ErrorOr &Other) : IsValid(false) {
130 copyConstruct(Other);
133 template <class OtherT>
134 ErrorOr(const ErrorOr<OtherT> &Other) : IsValid(false) {
135 copyConstruct(Other);
138 ErrorOr &operator =(const ErrorOr &Other) {
143 template <class OtherT>
144 ErrorOr &operator =(const ErrorOr<OtherT> &Other) {
149 #if LLVM_HAS_RVALUE_REFERENCES
150 ErrorOr(ErrorOr &&Other) : IsValid(false) {
151 moveConstruct(std::move(Other));
154 template <class OtherT>
155 ErrorOr(ErrorOr<OtherT> &&Other) : IsValid(false) {
156 moveConstruct(std::move(Other));
159 ErrorOr &operator =(ErrorOr &&Other) {
160 moveAssign(std::move(Other));
164 template <class OtherT>
165 ErrorOr &operator =(ErrorOr<OtherT> &&Other) {
166 moveAssign(std::move(Other));
175 get()->~storage_type();
178 typedef void (*unspecified_bool_type)();
179 static void unspecified_bool_true() {}
181 /// \brief Return false if there is an error.
182 operator unspecified_bool_type() const {
183 assert(IsValid && "Can't do anything on a default constructed ErrorOr!");
184 return HasError ? 0 : unspecified_bool_true;
187 operator llvm::error_code() const {
188 assert(IsValid && "Can't do anything on a default constructed ErrorOr!");
189 return HasError ? Error : llvm::error_code::success();
192 pointer operator ->() {
193 return toPointer(get());
196 reference operator *() {
201 template <class OtherT>
202 void copyConstruct(const ErrorOr<OtherT> &Other) {
203 // Construct an invalid ErrorOr if other is invalid.
207 if (!Other.HasError) {
208 // Get the other value.
210 new (get()) storage_type(*Other.get());
212 // Get other's error.
219 static bool compareThisIfSameType(const T1 &a, const T1 &b) {
223 template <class T1, class T2>
224 static bool compareThisIfSameType(const T1 &a, const T2 &b) {
228 template <class OtherT>
229 void copyAssign(const ErrorOr<OtherT> &Other) {
230 if (compareThisIfSameType(*this, Other))
234 new (this) ErrorOr(Other);
237 #if LLVM_HAS_RVALUE_REFERENCES
238 template <class OtherT>
239 void moveConstruct(ErrorOr<OtherT> &&Other) {
240 // Construct an invalid ErrorOr if other is invalid.
244 if (!Other.HasError) {
245 // Get the other value.
247 new (get()) storage_type(std::move(*Other.get()));
248 // Tell other not to do any destruction.
249 Other.IsValid = false;
251 // Get other's error.
254 // Tell other not to do any destruction.
255 Other.IsValid = false;
259 template <class OtherT>
260 void moveAssign(ErrorOr<OtherT> &&Other) {
261 if (compareThisIfSameType(*this, Other))
265 new (this) ErrorOr(std::move(Other));
269 pointer toPointer(pointer Val) {
273 pointer toPointer(wrap *Val) {
277 storage_type *get() {
278 assert(IsValid && "Can't do anything on a default constructed ErrorOr!");
279 assert(!HasError && "Cannot get value when an error exists!");
280 return reinterpret_cast<storage_type*>(TStorage.buffer);
283 const storage_type *get() const {
284 assert(IsValid && "Can't do anything on a default constructed ErrorOr!");
285 assert(!HasError && "Cannot get value when an error exists!");
286 return reinterpret_cast<const storage_type*>(TStorage.buffer);
290 AlignedCharArrayUnion<storage_type> TStorage;
297 template<class T, class E>
298 typename enable_if_c<is_error_code_enum<E>::value ||
299 is_error_condition_enum<E>::value, bool>::type
300 operator ==(ErrorOr<T> &Err, E Code) {
301 return error_code(Err) == Code;
303 } // end namespace llvm