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/ADT/PointerIntPair.h"
20 #include "llvm/Support/AlignOf.h"
22 #include <system_error>
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 /// Implicit conversion to bool returns true if there is a usable value. The
72 /// unary * and -> operators provide pointer like access to the value. Accessing
73 /// 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;
94 typedef const typename std::remove_reference<T>::type *const_pointer;
99 typename std::enable_if<std::is_error_code_enum<E>::value ||
100 std::is_error_condition_enum<E>::value,
103 new (getErrorStorage()) std::error_code(make_error_code(ErrorCode));
106 ErrorOr(std::error_code EC) : HasError(true) {
107 new (getErrorStorage()) std::error_code(EC);
110 ErrorOr(T Val) : HasError(false) {
111 new (getStorage()) storage_type(moveIfMoveConstructible<storage_type>(Val));
114 ErrorOr(const ErrorOr &Other) {
115 copyConstruct(Other);
118 template <class OtherT>
120 const ErrorOr<OtherT> &Other,
121 typename std::enable_if<std::is_convertible<OtherT, T>::value>::type * =
123 copyConstruct(Other);
126 template <class OtherT>
128 const ErrorOr<OtherT> &Other,
129 typename std::enable_if<
130 !std::is_convertible<OtherT, const T &>::value>::type * = nullptr) {
131 copyConstruct(Other);
134 ErrorOr(ErrorOr &&Other) {
135 moveConstruct(std::move(Other));
138 template <class OtherT>
140 ErrorOr<OtherT> &&Other,
141 typename std::enable_if<std::is_convertible<OtherT, T>::value>::type * =
143 moveConstruct(std::move(Other));
146 // This might eventually need SFINAE but it's more complex than is_convertible
147 // & I'm too lazy to write it right now.
148 template <class OtherT>
150 ErrorOr<OtherT> &&Other,
151 typename std::enable_if<!std::is_convertible<OtherT, T>::value>::type * =
153 moveConstruct(std::move(Other));
156 ErrorOr &operator=(const ErrorOr &Other) {
161 ErrorOr &operator=(ErrorOr &&Other) {
162 moveAssign(std::move(Other));
168 getStorage()->~storage_type();
171 /// \brief Return false if there is an error.
172 explicit operator bool() const {
176 reference get() { return *getStorage(); }
177 const_reference get() const { return const_cast<ErrorOr<T> *>(this)->get(); }
179 std::error_code getError() const {
180 return HasError ? *getErrorStorage() : std::error_code();
183 pointer operator ->() {
184 return toPointer(getStorage());
187 const_pointer operator ->() const {
188 return toPointer(getStorage());
191 reference operator *() {
192 return *getStorage();
195 const_reference operator *() const {
196 return *getStorage();
200 template <class OtherT>
201 void copyConstruct(const ErrorOr<OtherT> &Other) {
202 if (!Other.HasError) {
203 // Get the other value.
205 new (getStorage()) storage_type(*Other.getStorage());
207 // Get other's error.
209 new (getErrorStorage()) std::error_code(Other.getError());
214 static bool compareThisIfSameType(const T1 &a, const T1 &b) {
218 template <class T1, class T2>
219 static bool compareThisIfSameType(const T1 &a, const T2 &b) {
223 template <class OtherT>
224 void copyAssign(const ErrorOr<OtherT> &Other) {
225 if (compareThisIfSameType(*this, Other))
229 new (this) ErrorOr(Other);
232 template <class OtherT>
233 void moveConstruct(ErrorOr<OtherT> &&Other) {
234 if (!Other.HasError) {
235 // Get the other value.
237 new (getStorage()) storage_type(std::move(*Other.getStorage()));
239 // Get other's error.
241 new (getErrorStorage()) std::error_code(Other.getError());
245 template <class OtherT>
246 void moveAssign(ErrorOr<OtherT> &&Other) {
247 if (compareThisIfSameType(*this, Other))
251 new (this) ErrorOr(std::move(Other));
254 pointer toPointer(pointer Val) {
258 const_pointer toPointer(const_pointer Val) const {
262 pointer toPointer(wrap *Val) {
266 const_pointer toPointer(const wrap *Val) const {
271 storage_type *getStorage() {
272 assert(!HasError && "Cannot get value when an error exists!");
273 return reinterpret_cast<storage_type*>(TStorage.buffer);
276 const storage_type *getStorage() const {
277 assert(!HasError && "Cannot get value when an error exists!");
278 return reinterpret_cast<const storage_type*>(TStorage.buffer);
281 std::error_code *getErrorStorage() {
282 assert(HasError && "Cannot get error when a value exists!");
283 return reinterpret_cast<std::error_code *>(ErrorStorage.buffer);
286 const std::error_code *getErrorStorage() const {
287 return const_cast<ErrorOr<T> *>(this)->getErrorStorage();
292 AlignedCharArrayUnion<storage_type> TStorage;
293 AlignedCharArrayUnion<std::error_code> ErrorStorage;
298 template <class T, class E>
299 typename std::enable_if<std::is_error_code_enum<E>::value ||
300 std::is_error_condition_enum<E>::value,
302 operator==(const ErrorOr<T> &Err, E Code) {
303 return Err.getError() == Code;
305 } // end namespace llvm