Skip to content

Instantly share code, notes, and snippets.

@emaxerrno
Created March 18, 2015 20:48
Show Gist options
  • Select an option

  • Save emaxerrno/341ca5c49350890aaa73 to your computer and use it in GitHub Desktop.

Select an option

Save emaxerrno/341ca5c49350890aaa73 to your computer and use it in GitHub Desktop.
error.hpp
#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