ErrorOr.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #ifndef LLVM_SUPPORT_ERROROR_H
  16. #define LLVM_SUPPORT_ERROROR_H
  17. #include "llvm/ADT/PointerIntPair.h"
  18. #include "llvm/Support/AlignOf.h"
  19. #include <cassert>
  20. #include <system_error>
  21. #include <type_traits>
  22. namespace llvm {
  23. template<class T, class V>
  24. typename std::enable_if< std::is_constructible<T, V>::value
  25. , typename std::remove_reference<V>::type>::type &&
  26. moveIfMoveConstructible(V &Val) {
  27. return std::move(Val);
  28. }
  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 Val;
  34. }
  35. /// \brief Stores a reference that can be changed.
  36. template <typename T>
  37. class ReferenceStorage {
  38. T *Storage;
  39. public:
  40. ReferenceStorage(T &Ref) : Storage(&Ref) {}
  41. operator T &() const { return *Storage; }
  42. T &get() const { return *Storage; }
  43. };
  44. /// \brief Represents either an error or a value T.
  45. ///
  46. /// ErrorOr<T> is a pointer-like class that represents the result of an
  47. /// operation. The result is either an error, or a value of type T. This is
  48. /// designed to emulate the usage of returning a pointer where nullptr indicates
  49. /// failure. However instead of just knowing that the operation failed, we also
  50. /// have an error_code and optional user data that describes why it failed.
  51. ///
  52. /// It is used like the following.
  53. /// \code
  54. /// ErrorOr<Buffer> getBuffer();
  55. ///
  56. /// auto buffer = getBuffer();
  57. /// if (error_code ec = buffer.getError())
  58. /// return ec;
  59. /// buffer->write("adena");
  60. /// \endcode
  61. ///
  62. ///
  63. /// Implicit conversion to bool returns true if there is a usable value. The
  64. /// unary * and -> operators provide pointer like access to the value. Accessing
  65. /// the value when there is an error has undefined behavior.
  66. ///
  67. /// When T is a reference type the behaivor is slightly different. The reference
  68. /// is held in a std::reference_wrapper<std::remove_reference<T>::type>, and
  69. /// there is special handling to make operator -> work as if T was not a
  70. /// reference.
  71. ///
  72. /// T cannot be a rvalue reference.
  73. template<class T>
  74. class ErrorOr {
  75. template <class OtherT> friend class ErrorOr;
  76. static const bool isRef = std::is_reference<T>::value;
  77. typedef ReferenceStorage<typename std::remove_reference<T>::type> wrap;
  78. public:
  79. typedef typename std::conditional<isRef, wrap, T>::type storage_type;
  80. private:
  81. typedef typename std::remove_reference<T>::type &reference;
  82. typedef const typename std::remove_reference<T>::type &const_reference;
  83. typedef typename std::remove_reference<T>::type *pointer;
  84. public:
  85. template <class E>
  86. ErrorOr(E ErrorCode,
  87. typename std::enable_if<std::is_error_code_enum<E>::value ||
  88. std::is_error_condition_enum<E>::value,
  89. void *>::type = 0)
  90. : HasError(true) {
  91. new (getErrorStorage()) std::error_code(make_error_code(ErrorCode));
  92. }
  93. ErrorOr(std::error_code EC) : HasError(true) {
  94. new (getErrorStorage()) std::error_code(EC);
  95. }
  96. ErrorOr(T Val) : HasError(false) {
  97. new (getStorage()) storage_type(moveIfMoveConstructible<storage_type>(Val));
  98. }
  99. ErrorOr(const ErrorOr &Other) {
  100. copyConstruct(Other);
  101. }
  102. template <class OtherT>
  103. ErrorOr(
  104. const ErrorOr<OtherT> &Other,
  105. typename std::enable_if<std::is_convertible<OtherT, T>::value>::type * =
  106. nullptr) {
  107. copyConstruct(Other);
  108. }
  109. template <class OtherT>
  110. explicit ErrorOr(
  111. const ErrorOr<OtherT> &Other,
  112. typename std::enable_if<
  113. !std::is_convertible<OtherT, const T &>::value>::type * = nullptr) {
  114. copyConstruct(Other);
  115. }
  116. ErrorOr(ErrorOr &&Other) {
  117. moveConstruct(std::move(Other));
  118. }
  119. template <class OtherT>
  120. ErrorOr(
  121. ErrorOr<OtherT> &&Other,
  122. typename std::enable_if<std::is_convertible<OtherT, T>::value>::type * =
  123. nullptr) {
  124. moveConstruct(std::move(Other));
  125. }
  126. // This might eventually need SFINAE but it's more complex than is_convertible
  127. // & I'm too lazy to write it right now.
  128. template <class OtherT>
  129. explicit ErrorOr(
  130. ErrorOr<OtherT> &&Other,
  131. typename std::enable_if<!std::is_convertible<OtherT, T>::value>::type * =
  132. nullptr) {
  133. moveConstruct(std::move(Other));
  134. }
  135. ErrorOr &operator=(const ErrorOr &Other) {
  136. copyAssign(Other);
  137. return *this;
  138. }
  139. ErrorOr &operator=(ErrorOr &&Other) {
  140. moveAssign(std::move(Other));
  141. return *this;
  142. }
  143. ~ErrorOr() {
  144. if (!HasError)
  145. getStorage()->~storage_type();
  146. }
  147. /// \brief Return false if there is an error.
  148. explicit operator bool() const {
  149. return !HasError;
  150. }
  151. reference get() { return *getStorage(); }
  152. const_reference get() const { return const_cast<ErrorOr<T> *>(this)->get(); }
  153. std::error_code getError() const {
  154. return HasError ? *getErrorStorage() : std::error_code();
  155. }
  156. pointer operator ->() {
  157. return toPointer(getStorage());
  158. }
  159. reference operator *() {
  160. return *getStorage();
  161. }
  162. private:
  163. template <class OtherT>
  164. void copyConstruct(const ErrorOr<OtherT> &Other) {
  165. if (!Other.HasError) {
  166. // Get the other value.
  167. HasError = false;
  168. new (getStorage()) storage_type(*Other.getStorage());
  169. } else {
  170. // Get other's error.
  171. HasError = true;
  172. new (getErrorStorage()) std::error_code(Other.getError());
  173. }
  174. }
  175. template <class T1>
  176. static bool compareThisIfSameType(const T1 &a, const T1 &b) {
  177. return &a == &b;
  178. }
  179. template <class T1, class T2>
  180. static bool compareThisIfSameType(const T1 &a, const T2 &b) {
  181. return false;
  182. }
  183. template <class OtherT>
  184. void copyAssign(const ErrorOr<OtherT> &Other) {
  185. if (compareThisIfSameType(*this, Other))
  186. return;
  187. this->~ErrorOr();
  188. new (this) ErrorOr(Other);
  189. }
  190. template <class OtherT>
  191. void moveConstruct(ErrorOr<OtherT> &&Other) {
  192. if (!Other.HasError) {
  193. // Get the other value.
  194. HasError = false;
  195. new (getStorage()) storage_type(std::move(*Other.getStorage()));
  196. } else {
  197. // Get other's error.
  198. HasError = true;
  199. new (getErrorStorage()) std::error_code(Other.getError());
  200. }
  201. }
  202. template <class OtherT>
  203. void moveAssign(ErrorOr<OtherT> &&Other) {
  204. if (compareThisIfSameType(*this, Other))
  205. return;
  206. this->~ErrorOr();
  207. new (this) ErrorOr(std::move(Other));
  208. }
  209. pointer toPointer(pointer Val) {
  210. return Val;
  211. }
  212. pointer toPointer(wrap *Val) {
  213. return &Val->get();
  214. }
  215. storage_type *getStorage() {
  216. assert(!HasError && "Cannot get value when an error exists!");
  217. return reinterpret_cast<storage_type*>(TStorage.buffer);
  218. }
  219. const storage_type *getStorage() const {
  220. assert(!HasError && "Cannot get value when an error exists!");
  221. return reinterpret_cast<const storage_type*>(TStorage.buffer);
  222. }
  223. std::error_code *getErrorStorage() {
  224. assert(HasError && "Cannot get error when a value exists!");
  225. return reinterpret_cast<std::error_code *>(ErrorStorage.buffer);
  226. }
  227. const std::error_code *getErrorStorage() const {
  228. return const_cast<ErrorOr<T> *>(this)->getErrorStorage();
  229. }
  230. union {
  231. AlignedCharArrayUnion<storage_type> TStorage;
  232. AlignedCharArrayUnion<std::error_code> ErrorStorage;
  233. };
  234. bool HasError : 1;
  235. };
  236. template <class T, class E>
  237. typename std::enable_if<std::is_error_code_enum<E>::value ||
  238. std::is_error_condition_enum<E>::value,
  239. bool>::type
  240. operator==(const ErrorOr<T> &Err, E Code) {
  241. return Err.getError() == Code;
  242. }
  243. } // end namespace llvm
  244. #endif