GTIRB
v2.2.0
GrammaTech Intermediate Representation for Binaries: C++ API
|
Go to the documentation of this file.
22 #ifndef GTIRB_ERROROR_H
23 #define GTIRB_ERROROR_H
28 #include <system_error>
29 #include <type_traits>
40 ErrorInfo(
const std::error_code& EC,
const std::string& S)
41 : ErrorCode(EC), Msg(
S){};
42 std::string message()
const;
45 template <
typename CharT,
typename Traits>
46 std::ostream&
operator<<(std::basic_ostream<CharT, Traits>& os,
81 template <
class T>
class ErrorOr {
82 template <
class OtherT>
friend class ErrorOr;
84 static constexpr
bool isRef = std::is_reference<T>::value;
86 using wrap = std::reference_wrapper<std::remove_reference_t<T>>;
92 using reference = std::remove_reference_t<T>&;
93 using const_reference =
const std::remove_reference_t<T>&;
94 using pointer = std::remove_reference_t<T>*;
95 using const_pointer =
const std::remove_reference_t<T>*;
99 ErrorOr(E ErrorCode,
const std::string& Msg =
"",
100 std::enable_if_t<std::is_error_code_enum<E>::value ||
101 std::is_error_condition_enum<E>::value,
107 ErrorOr(std::error_code EC,
const std::string& Msg =
"") : HasError(true) {
108 new (getErrorStorage())
ErrorInfo{EC, Msg};
115 template <
class OtherT>
117 std::enable_if_t<std::is_convertible<OtherT, T>::value>* =
nullptr)
119 new (getStorage())
storage_type(std::forward<OtherT>(Val));
124 template <
class OtherT>
126 std::enable_if_t<std::is_convertible<OtherT, T>::value>* =
nullptr) {
127 copyConstruct(Other);
130 template <
class OtherT>
133 std::enable_if_t<!std::is_convertible<OtherT, const T&>::value>* =
135 copyConstruct(Other);
140 template <
class OtherT>
142 std::enable_if_t<std::is_convertible<OtherT, T>::value>* =
nullptr) {
143 moveConstruct(std::move(Other));
148 template <
class OtherT>
151 std::enable_if_t<!std::is_convertible<OtherT, T>::value>* =
nullptr) {
152 moveConstruct(std::move(Other));
161 moveAssign(std::move(Other));
167 getStorage()->~storage_type();
171 explicit operator bool()
const {
return !HasError; }
173 reference
get() {
return *getStorage(); }
177 return HasError ? *getErrorStorage() :
ErrorInfo();
182 const_pointer
operator->()
const {
return toPointer(getStorage()); }
186 const_reference
operator*()
const {
return *getStorage(); }
189 template <
class OtherT>
void copyConstruct(
const ErrorOr<OtherT>& Other) {
190 if (!Other.HasError) {
197 new (getErrorStorage()) ErrorInfo(Other.getError());
202 static bool compareThisIfSameType(
const T1& a,
const T1& b) {
206 template <
class T1,
class T2>
207 static bool compareThisIfSameType(
const T1&,
const T2&) {
211 template <
class OtherT>
void copyAssign(
const ErrorOr<OtherT>& Other) {
212 if (compareThisIfSameType(*
this, Other))
219 template <
class OtherT>
void moveConstruct(ErrorOr<OtherT>&& Other) {
220 if (!Other.HasError) {
223 new (getStorage())
storage_type(std::move(*Other.getStorage()));
227 new (getErrorStorage()) ErrorInfo(std::move(*Other.getErrorStorage()));
231 template <
class OtherT>
void moveAssign(ErrorOr<OtherT>&& Other) {
232 if (compareThisIfSameType(*
this, Other))
236 new (
this)
ErrorOr(std::move(Other));
239 pointer toPointer(pointer Val) {
return Val; }
241 const_pointer toPointer(const_pointer Val)
const {
return Val; }
243 pointer toPointer(wrap* Val) {
return &Val->get(); }
245 const_pointer toPointer(
const wrap* Val)
const {
return &Val->get(); }
248 assert(!HasError &&
"Cannot get value when an error exists!");
253 assert(!HasError &&
"Cannot get value when an error exists!");
257 ErrorInfo* getErrorStorage() {
258 assert(HasError &&
"Cannot get error when a value exists!");
262 const ErrorInfo* getErrorStorage()
const {
263 return const_cast<ErrorOr<T>*
>(
this)->getErrorStorage();
273 template <
class T,
class E>
274 std::enable_if_t<std::is_error_code_enum<E>::value ||
275 std::is_error_condition_enum<E>::value,
278 return Err.getError().ErrorCode == Code;
282 #endif // GTIRB_ERROROR_H
const_pointer operator->() const
Definition: ErrorOr.hpp:182
ErrorOr(const ErrorOr< OtherT > &Other, std::enable_if_t< std::is_convertible< OtherT, T >::value > *=nullptr)
Definition: ErrorOr.hpp:125
char TStorage[sizeof(storage_type)]
Definition: ErrorOr.hpp:267
std::string Msg
Definition: ErrorOr.hpp:38
std::enable_if_t< std::is_error_code_enum< E >::value||std::is_error_condition_enum< E >::value, bool > operator==(const ErrorOr< T > &Err, E Code)
Definition: ErrorOr.hpp:277
#define GTIRB_EXPORT_API
This macro controls the visibility of exported symbols (i.e. classes) in shared libraries....
Definition: Export.hpp:52
const_reference get() const
Definition: ErrorOr.hpp:174
Main namespace for the GTIRB API.
Definition: Addr.hpp:28
ErrorOr(E ErrorCode, const std::string &Msg="", std::enable_if_t< std::is_error_code_enum< E >::value||std::is_error_condition_enum< E >::value, void * >=nullptr)
Definition: ErrorOr.hpp:99
ErrorOr(OtherT &&Val, std::enable_if_t< std::is_convertible< OtherT, T >::value > *=nullptr)
Definition: ErrorOr.hpp:116
ErrorOr(ErrorOr< OtherT > &&Other, std::enable_if_t<!std::is_convertible< OtherT, T >::value > *=nullptr)
Definition: ErrorOr.hpp:149
ErrorOr(ErrorOr< OtherT > &&Other, std::enable_if_t< std::is_convertible< OtherT, T >::value > *=nullptr)
Definition: ErrorOr.hpp:141
~ErrorOr()
Definition: ErrorOr.hpp:165
const_reference operator*() const
Definition: ErrorOr.hpp:186
ErrorOr & operator=(const ErrorOr &Other)
Definition: ErrorOr.hpp:155
ErrorOr(const ErrorOr &Other)
Definition: ErrorOr.hpp:122
Definition: ErrorOr.hpp:36
std::error_code ErrorCode
Definition: ErrorOr.hpp:37
ErrorOr(const ErrorInfo &EI)
Definition: ErrorOr.hpp:111
ErrorOr(const ErrorOr< OtherT > &Other, std::enable_if_t<!std::is_convertible< OtherT, const T & >::value > *=nullptr)
Definition: ErrorOr.hpp:131
ErrorOr(ErrorOr &&Other)
Definition: ErrorOr.hpp:138
ErrorInfo getError() const
Definition: ErrorOr.hpp:176
ErrorOr & operator=(ErrorOr &&Other)
Definition: ErrorOr.hpp:160
std::error_code make_error_code(gtirb::IR::load_error e)
Makes an std::error_code object from an IR::load_error object.
Definition: IR.hpp:1502
reference get()
Definition: ErrorOr.hpp:173
GTIRB_EXPORT_API std::ostream & operator<<(std::ostream &OS, const ConditionalEdge &CE)
friend class ErrorOr
Definition: ErrorOr.hpp:82
pointer operator->()
Definition: ErrorOr.hpp:180
Definition: ByteInterval.hpp:62
std::conditional_t< isRef, wrap, T > storage_type
Definition: ErrorOr.hpp:89
char ErrorStorage[sizeof(ErrorInfo)]
Definition: ErrorOr.hpp:268
ErrorOr(std::error_code EC, const std::string &Msg="")
Definition: ErrorOr.hpp:107
ErrorInfo(const std::error_code &EC, const std::string &S)
Definition: ErrorOr.hpp:40
reference operator*()
Definition: ErrorOr.hpp:184