16 #ifndef WPIUTIL_WPI_ERROROR_H
17 #define WPIUTIL_WPI_ERROROR_H
19 #include "wpi/AlignOf.h"
21 #include <system_error>
22 #include <type_traits>
35 operator T &()
const {
return *Storage; }
36 T &
get()
const {
return *Storage; }
70 template <
class OtherT>
friend class ErrorOr;
72 static const bool isRef = std::is_reference<T>::value;
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>
101 typename std::enable_if<std::is_convertible<OtherT, T>::value>::type
104 new (getStorage()) storage_type(std::forward<OtherT>(Val));
108 copyConstruct(Other);
111 template <
class OtherT>
114 typename std::enable_if<std::is_convertible<OtherT, T>::value>::type * =
116 copyConstruct(Other);
119 template <
class OtherT>
122 typename std::enable_if<
123 !std::is_convertible<OtherT, const T &>::value>::type * =
nullptr) {
124 copyConstruct(Other);
128 moveConstruct(std::move(Other));
131 template <
class OtherT>
134 typename std::enable_if<std::is_convertible<OtherT, T>::value>::type * =
136 moveConstruct(std::move(Other));
141 template <
class OtherT>
144 typename std::enable_if<!std::is_convertible<OtherT, T>::value>::type * =
146 moveConstruct(std::move(Other));
155 moveAssign(std::move(Other));
161 getStorage()->~storage_type();
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) {
194 new (getStorage()) storage_type(*Other.getStorage());
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) {
226 new (getStorage()) storage_type(std::move(*Other.getStorage()));
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;
291 #endif // LLVM_SUPPORT_ERROROR_H
Stores a reference that can be changed.
Definition: ErrorOr.h:29
Definition: optional.h:885
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
Represents either an error or a value T.
Definition: ErrorOr.h:69