1 //===- llvm/Support/ErrorOr.h - Error Smart Pointer -------------*- C++ -*-===//
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_ERROROR_H
17 #define LLVM_SUPPORT_ERROROR_H
19 #include "llvm/Support/AlignOf.h"
21 #include <system_error>
22 #include <type_traits>
27 /// \brief Stores a reference that can be changed.
29 class ReferenceStorage {
33 ReferenceStorage(T &Ref) : Storage(&Ref) {}
35 operator T &() const { return *Storage; }
36 T &get() const { return *Storage; }
39 /// \brief Represents either an error or a value T.
41 /// ErrorOr<T> is a pointer-like class that represents the result of an
42 /// operation. The result is either an error, or a value of type T. This is
43 /// designed to emulate the usage of returning a pointer where nullptr indicates
44 /// failure. However instead of just knowing that the operation failed, we also
45 /// have an error_code and optional user data that describes why it failed.
47 /// It is used like the following.
49 /// ErrorOr<Buffer> getBuffer();
51 /// auto buffer = getBuffer();
52 /// if (error_code ec = buffer.getError())
54 /// buffer->write("adena");
58 /// Implicit conversion to bool returns true if there is a usable value. The
59 /// unary * and -> operators provide pointer like access to the value. Accessing
60 /// the value when there is an error has undefined behavior.
62 /// When T is a reference type the behavior is slightly different. The reference
63 /// is held in a std::reference_wrapper<std::remove_reference<T>::type>, and
64 /// there is special handling to make operator -> work as if T was not a
67 /// T cannot be a rvalue reference.
70 template <class OtherT> friend class ErrorOr;
72 static const bool isRef = std::is_reference<T>::value;
74 using wrap = ReferenceStorage<typename std::remove_reference<T>::type>;
77 using storage_type = typename std::conditional<isRef, wrap, T>::type;
80 using reference = typename std::remove_reference<T>::type &;
81 using const_reference = const typename std::remove_reference<T>::type &;
82 using pointer = typename std::remove_reference<T>::type *;
83 using const_pointer = const typename std::remove_reference<T>::type *;
88 typename std::enable_if<std::is_error_code_enum<E>::value ||
89 std::is_error_condition_enum<E>::value,
90 void *>::type = nullptr)
92 new (getErrorStorage()) std::error_code(make_error_code(ErrorCode));
95 ErrorOr(std::error_code EC) : HasError(true) {
96 new (getErrorStorage()) std::error_code(EC);
99 template <class OtherT>
100 ErrorOr(OtherT &&Val,
101 typename std::enable_if<std::is_convertible<OtherT, T>::value>::type
104 new (getStorage()) storage_type(std::forward<OtherT>(Val));
107 ErrorOr(const ErrorOr &Other) {
108 copyConstruct(Other);
111 template <class OtherT>
113 const ErrorOr<OtherT> &Other,
114 typename std::enable_if<std::is_convertible<OtherT, T>::value>::type * =
116 copyConstruct(Other);
119 template <class OtherT>
121 const ErrorOr<OtherT> &Other,
122 typename std::enable_if<
123 !std::is_convertible<OtherT, const T &>::value>::type * = nullptr) {
124 copyConstruct(Other);
127 ErrorOr(ErrorOr &&Other) {
128 moveConstruct(std::move(Other));
131 template <class OtherT>
133 ErrorOr<OtherT> &&Other,
134 typename std::enable_if<std::is_convertible<OtherT, T>::value>::type * =
136 moveConstruct(std::move(Other));
139 // This might eventually need SFINAE but it's more complex than is_convertible
140 // & I'm too lazy to write it right now.
141 template <class OtherT>
143 ErrorOr<OtherT> &&Other,
144 typename std::enable_if<!std::is_convertible<OtherT, T>::value>::type * =
146 moveConstruct(std::move(Other));
149 ErrorOr &operator=(const ErrorOr &Other) {
154 ErrorOr &operator=(ErrorOr &&Other) {
155 moveAssign(std::move(Other));
161 getStorage()->~storage_type();
164 /// \brief Return false if there is an error.
165 explicit operator bool() const {
169 reference get() { return *getStorage(); }
170 const_reference get() const { return const_cast<ErrorOr<T> *>(this)->get(); }
172 std::error_code getError() const {
173 return HasError ? *getErrorStorage() : std::error_code();
176 pointer operator ->() {
177 return toPointer(getStorage());
180 const_pointer operator->() const { return toPointer(getStorage()); }
182 reference operator *() {
183 return *getStorage();
186 const_reference operator*() const { return *getStorage(); }
189 template <class OtherT>
190 void copyConstruct(const ErrorOr<OtherT> &Other) {
191 if (!Other.HasError) {
192 // Get the other value.
194 new (getStorage()) storage_type(*Other.getStorage());
196 // Get other's error.
198 new (getErrorStorage()) std::error_code(Other.getError());
203 static bool compareThisIfSameType(const T1 &a, const T1 &b) {
207 template <class T1, class T2>
208 static bool compareThisIfSameType(const T1 &a, const T2 &b) {
212 template <class OtherT>
213 void copyAssign(const ErrorOr<OtherT> &Other) {
214 if (compareThisIfSameType(*this, Other))
218 new (this) ErrorOr(Other);
221 template <class OtherT>
222 void moveConstruct(ErrorOr<OtherT> &&Other) {
223 if (!Other.HasError) {
224 // Get the other value.
226 new (getStorage()) storage_type(std::move(*Other.getStorage()));
228 // Get other's error.
230 new (getErrorStorage()) std::error_code(Other.getError());
234 template <class OtherT>
235 void moveAssign(ErrorOr<OtherT> &&Other) {
236 if (compareThisIfSameType(*this, Other))
240 new (this) ErrorOr(std::move(Other));
243 pointer toPointer(pointer Val) {
247 const_pointer toPointer(const_pointer Val) const { return Val; }
249 pointer toPointer(wrap *Val) {
253 const_pointer toPointer(const wrap *Val) const { return &Val->get(); }
255 storage_type *getStorage() {
256 assert(!HasError && "Cannot get value when an error exists!");
257 return reinterpret_cast<storage_type*>(TStorage.buffer);
260 const storage_type *getStorage() const {
261 assert(!HasError && "Cannot get value when an error exists!");
262 return reinterpret_cast<const storage_type*>(TStorage.buffer);
265 std::error_code *getErrorStorage() {
266 assert(HasError && "Cannot get error when a value exists!");
267 return reinterpret_cast<std::error_code *>(ErrorStorage.buffer);
270 const std::error_code *getErrorStorage() const {
271 return const_cast<ErrorOr<T> *>(this)->getErrorStorage();
275 AlignedCharArrayUnion<storage_type> TStorage;
276 AlignedCharArrayUnion<std::error_code> ErrorStorage;
281 template <class T, class E>
282 typename std::enable_if<std::is_error_code_enum<E>::value ||
283 std::is_error_condition_enum<E>::value,
285 operator==(const ErrorOr<T> &Err, E Code) {
286 return Err.getError() == Code;
289 } // end namespace llvm
291 #endif // LLVM_SUPPORT_ERROROR_H