Created
March 18, 2015 20:48
-
-
Save emaxerrno/341ca5c49350890aaa73 to your computer and use it in GitHub Desktop.
error.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include <boost/optional.hpp> | |
| namespace bolt { | |
| // TODO(agallego) - AbstractError_t should really inherit from | |
| // an template<left,right>either_t; type - refactor later | |
| template <typename T> struct AbstractError { | |
| using type = T; | |
| boost::optional<T> error = boost::none; | |
| int code = 0; | |
| // boolean operator used in if(), while(), etc. | |
| bool operator!() const noexcept { return !error.is_initialized(); } | |
| }; | |
| template <typename T> | |
| inline bool operator==(const AbstractError<T> &x, const AbstractError<T> &y) { | |
| return x.error == y.error && x.code == y.code; | |
| } | |
| template <typename T> | |
| inline bool operator!=(const AbstractError<T> &x, const AbstractError<T> &y) { | |
| return !(x == y); | |
| } | |
| template <typename T> | |
| inline bool operator<(const AbstractError<T> &x, const AbstractError<T> &y) { | |
| return x < y; | |
| } | |
| template <typename T> | |
| inline bool operator<=(const AbstractError<T> &x, const AbstractError<T> &y) { | |
| return x <= y; | |
| } | |
| template <typename T> | |
| inline bool operator>(const AbstractError<T> &x, const AbstractError<T> &y) { | |
| return x > y; | |
| } | |
| template <typename T> | |
| inline bool operator>=(const AbstractError<T> &x, const AbstractError<T> &y) { | |
| return x >= y; | |
| } | |
| // handy methods | |
| template <typename T> | |
| inline bool operator==(const AbstractError<T> &x, const T &y) { | |
| return x.error == y; | |
| } | |
| template <typename T> | |
| inline bool operator!=(const AbstractError<T> &x, const T &y) { | |
| return !(x == y); | |
| } | |
| template <typename T> | |
| inline bool operator==(const T &x, const AbstractError<T> &y) { | |
| return y && x == y.error; | |
| } | |
| template <typename T> | |
| inline bool operator!=(const T &x, const AbstractError<T> &y) { | |
| return y && !(x == y); | |
| } | |
| struct Error : public AbstractError<std::string> {}; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment