Remove system_error.h.
[oota-llvm.git] / include / llvm / Support / ErrorOr.h
1 //===- llvm/Support/ErrorOr.h - Error Smart Pointer -----------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 ///
12 /// Provides ErrorOr<T> smart pointer.
13 ///
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_SUPPORT_ERROR_OR_H
17 #define LLVM_SUPPORT_ERROR_OR_H
18
19 #include "llvm/ADT/PointerIntPair.h"
20 #include "llvm/Support/AlignOf.h"
21 #include <cassert>
22 #include <system_error>
23 #include <type_traits>
24
25 namespace llvm {
26 using std::error_code;
27 template<class T, class V>
28 typename std::enable_if< std::is_constructible<T, V>::value
29                        , typename std::remove_reference<V>::type>::type &&
30  moveIfMoveConstructible(V &Val) {
31   return std::move(Val);
32 }
33
34 template<class T, class V>
35 typename std::enable_if< !std::is_constructible<T, V>::value
36                        , typename std::remove_reference<V>::type>::type &
37 moveIfMoveConstructible(V &Val) {
38   return Val;
39 }
40
41 /// \brief Stores a reference that can be changed.
42 template <typename T>
43 class ReferenceStorage {
44   T *Storage;
45
46 public:
47   ReferenceStorage(T &Ref) : Storage(&Ref) {}
48
49   operator T &() const { return *Storage; }
50   T &get() const { return *Storage; }
51 };
52
53 /// \brief Represents either an error or a value T.
54 ///
55 /// ErrorOr<T> is a pointer-like class that represents the result of an
56 /// operation. The result is either an error, or a value of type T. This is
57 /// designed to emulate the usage of returning a pointer where nullptr indicates
58 /// failure. However instead of just knowing that the operation failed, we also
59 /// have an error_code and optional user data that describes why it failed.
60 ///
61 /// It is used like the following.
62 /// \code
63 ///   ErrorOr<Buffer> getBuffer();
64 ///
65 ///   auto buffer = getBuffer();
66 ///   if (error_code ec = buffer.getError())
67 ///     return ec;
68 ///   buffer->write("adena");
69 /// \endcode
70 ///
71 ///
72 /// An implicit conversion to bool provides a way to check if there was an
73 /// error. The unary * and -> operators provide pointer like access to the
74 /// value. Accessing the value when there is an error has undefined behavior.
75 ///
76 /// When T is a reference type the behaivor is slightly different. The reference
77 /// is held in a std::reference_wrapper<std::remove_reference<T>::type>, and
78 /// there is special handling to make operator -> work as if T was not a
79 /// reference.
80 ///
81 /// T cannot be a rvalue reference.
82 template<class T>
83 class ErrorOr {
84   template <class OtherT> friend class ErrorOr;
85   static const bool isRef = std::is_reference<T>::value;
86   typedef ReferenceStorage<typename std::remove_reference<T>::type> wrap;
87
88 public:
89   typedef typename std::conditional<isRef, wrap, T>::type storage_type;
90
91 private:
92   typedef typename std::remove_reference<T>::type &reference;
93   typedef const typename std::remove_reference<T>::type &const_reference;
94   typedef typename std::remove_reference<T>::type *pointer;
95
96 public:
97   template <class E>
98   ErrorOr(E ErrorCode,
99           typename std::enable_if<std::is_error_code_enum<E>::value ||
100                                       std::is_error_condition_enum<E>::value,
101                                   void *>::type = 0)
102       : HasError(true) {
103     using std::make_error_code;
104     new (getErrorStorage()) error_code(make_error_code(ErrorCode));
105   }
106
107   ErrorOr(std::error_code EC) : HasError(true) {
108     new (getErrorStorage()) error_code(EC);
109   }
110
111   ErrorOr(T Val) : HasError(false) {
112     new (getStorage()) storage_type(moveIfMoveConstructible<storage_type>(Val));
113   }
114
115   ErrorOr(const ErrorOr &Other) {
116     copyConstruct(Other);
117   }
118
119   template <class OtherT>
120   ErrorOr(const ErrorOr<OtherT> &Other) {
121     copyConstruct(Other);
122   }
123
124   ErrorOr &operator =(const ErrorOr &Other) {
125     copyAssign(Other);
126     return *this;
127   }
128
129   template <class OtherT>
130   ErrorOr &operator =(const ErrorOr<OtherT> &Other) {
131     copyAssign(Other);
132     return *this;
133   }
134
135   ErrorOr(ErrorOr &&Other) {
136     moveConstruct(std::move(Other));
137   }
138
139   template <class OtherT>
140   ErrorOr(ErrorOr<OtherT> &&Other) {
141     moveConstruct(std::move(Other));
142   }
143
144   ErrorOr &operator =(ErrorOr &&Other) {
145     moveAssign(std::move(Other));
146     return *this;
147   }
148
149   template <class OtherT>
150   ErrorOr &operator =(ErrorOr<OtherT> &&Other) {
151     moveAssign(std::move(Other));
152     return *this;
153   }
154
155   ~ErrorOr() {
156     if (!HasError)
157       getStorage()->~storage_type();
158   }
159
160   /// \brief Return false if there is an error.
161   LLVM_EXPLICIT operator bool() const {
162     return !HasError;
163   }
164
165   reference get() { return *getStorage(); }
166   const_reference get() const { return const_cast<ErrorOr<T> >(this)->get(); }
167
168   error_code getError() const {
169     return HasError ? *getErrorStorage() : error_code();
170   }
171
172   pointer operator ->() {
173     return toPointer(getStorage());
174   }
175
176   reference operator *() {
177     return *getStorage();
178   }
179
180 private:
181   template <class OtherT>
182   void copyConstruct(const ErrorOr<OtherT> &Other) {
183     if (!Other.HasError) {
184       // Get the other value.
185       HasError = false;
186       new (getStorage()) storage_type(*Other.getStorage());
187     } else {
188       // Get other's error.
189       HasError = true;
190       new (getErrorStorage()) error_code(Other.getError());
191     }
192   }
193
194   template <class T1>
195   static bool compareThisIfSameType(const T1 &a, const T1 &b) {
196     return &a == &b;
197   }
198
199   template <class T1, class T2>
200   static bool compareThisIfSameType(const T1 &a, const T2 &b) {
201     return false;
202   }
203
204   template <class OtherT>
205   void copyAssign(const ErrorOr<OtherT> &Other) {
206     if (compareThisIfSameType(*this, Other))
207       return;
208
209     this->~ErrorOr();
210     new (this) ErrorOr(Other);
211   }
212
213   template <class OtherT>
214   void moveConstruct(ErrorOr<OtherT> &&Other) {
215     if (!Other.HasError) {
216       // Get the other value.
217       HasError = false;
218       new (getStorage()) storage_type(std::move(*Other.getStorage()));
219     } else {
220       // Get other's error.
221       HasError = true;
222       new (getErrorStorage()) error_code(Other.getError());
223     }
224   }
225
226   template <class OtherT>
227   void moveAssign(ErrorOr<OtherT> &&Other) {
228     if (compareThisIfSameType(*this, Other))
229       return;
230
231     this->~ErrorOr();
232     new (this) ErrorOr(std::move(Other));
233   }
234
235   pointer toPointer(pointer Val) {
236     return Val;
237   }
238
239   pointer toPointer(wrap *Val) {
240     return &Val->get();
241   }
242
243   storage_type *getStorage() {
244     assert(!HasError && "Cannot get value when an error exists!");
245     return reinterpret_cast<storage_type*>(TStorage.buffer);
246   }
247
248   const storage_type *getStorage() const {
249     assert(!HasError && "Cannot get value when an error exists!");
250     return reinterpret_cast<const storage_type*>(TStorage.buffer);
251   }
252
253   error_code *getErrorStorage() {
254     assert(HasError && "Cannot get error when a value exists!");
255     return reinterpret_cast<error_code*>(ErrorStorage.buffer);
256   }
257
258   const error_code *getErrorStorage() const {
259     return const_cast<ErrorOr<T> *>(this)->getErrorStorage();
260   }
261
262
263   union {
264     AlignedCharArrayUnion<storage_type> TStorage;
265     AlignedCharArrayUnion<error_code> ErrorStorage;
266   };
267   bool HasError : 1;
268 };
269
270 template <class T, class E>
271 typename std::enable_if<std::is_error_code_enum<E>::value ||
272                             std::is_error_condition_enum<E>::value,
273                         bool>::type
274 operator==(ErrorOr<T> &Err, E Code) {
275   return error_code(Err) == Code;
276 }
277 } // end namespace llvm
278
279 #endif