Device interface library for multiple platforms including Mbed.
Dependents: DeepCover Embedded Security in IoT MaximInterface MAXREFDES155#
Maxim Interface is a library framework focused on providing flexible and expressive hardware interfaces. Both communication interfaces such as I2C and 1-Wire and device interfaces such as DS18B20 are supported. Modern C++ concepts are used extensively while keeping compatibility with C++98/C++03 and requiring no external dependencies. The embedded-friendly design does not depend on exceptions or RTTI.
The full version of the project is hosted on GitLab: https://gitlab.com/iabenz/MaximInterface
Diff: Utilities/system_error.hpp
- Revision:
- 5:a8c83a2e6fa4
- Parent:
- 3:f818ea5172ed
- Child:
- 6:471901a04573
--- a/Utilities/system_error.hpp Fri Jan 19 10:25:02 2018 -0600 +++ b/Utilities/system_error.hpp Wed Jan 23 13:11:04 2019 -0600 @@ -37,8 +37,10 @@ #ifndef MaximInterface_system_error #define MaximInterface_system_error +#include <stdexcept> #include <string> -#include <MaximInterface/Utilities/Export.h> +#include "Export.h" +#include "SafeBool.hpp" #include "Uncopyable.hpp" namespace MaximInterface { @@ -51,20 +53,27 @@ virtual ~error_category() {} virtual const char * name() const = 0; + MaximInterface_EXPORT virtual error_condition default_error_condition(int code) const; + MaximInterface_EXPORT virtual bool equivalent(int code, const error_condition & condition) const; + MaximInterface_EXPORT virtual bool equivalent(const error_code & code, int condition) const; + virtual std::string message(int condition) const = 0; }; + inline bool operator==(const error_category & lhs, const error_category & rhs) { return &lhs == &rhs; } + inline bool operator!=(const error_category & lhs, const error_category & rhs) { return !operator==(lhs, rhs); } + inline bool operator<(const error_category & lhs, const error_category & rhs) { return &lhs < &rhs; } @@ -74,6 +83,7 @@ class error_condition { public: error_condition() : value_(0), category_(&system_category()) {} + error_condition(int value, const error_category & category) : value_(value), category_(&category) {} @@ -81,27 +91,35 @@ value_ = value; category_ = &category; } + void clear() { value_ = 0; category_ = &system_category(); } + int value() const { return value_; } + const error_category & category() const { return *category_; } + std::string message() const { return category().message(value()); } - operator bool() const { return value() != 0; } + + operator SafeBool() const { return makeSafeBool(value() != 0); } private: int value_; const error_category * category_; }; + inline bool operator==(const error_condition & lhs, const error_condition & rhs) { return (lhs.value() == rhs.value()) && (lhs.category() == rhs.category()); } + inline bool operator!=(const error_condition & lhs, const error_condition & rhs) { return !operator==(lhs, rhs); } + inline bool operator<(const error_condition & lhs, const error_condition & rhs) { return (lhs.category() < rhs.category()) || @@ -111,6 +129,7 @@ class error_code { public: error_code() : value_(0), category_(&system_category()) {} + error_code(int value, const error_category & category) : value_(value), category_(&category) {} @@ -118,28 +137,37 @@ value_ = value; category_ = &category; } + void clear() { value_ = 0; category_ = &system_category(); } + int value() const { return value_; } + const error_category & category() const { return *category_; } + error_condition default_error_condition() const { return category().default_error_condition(value()); } + std::string message() const { return category().message(value()); } - operator bool() const { return value() != 0; } + + operator SafeBool() const { return makeSafeBool(value() != 0); } private: int value_; const error_category * category_; }; + inline bool operator==(const error_code & lhs, const error_code & rhs) { return (lhs.value() == rhs.value()) && (lhs.category() == rhs.category()); } + inline bool operator!=(const error_code & lhs, const error_code & rhs) { return !operator==(lhs, rhs); } + inline bool operator<(const error_code & lhs, const error_code & rhs) { return (lhs.category() < rhs.category()) || ((lhs.category() == rhs.category()) && (lhs.value() < rhs.value())); @@ -156,16 +184,43 @@ return lhs.category().equivalent(lhs.value(), rhs) || rhs.category().equivalent(lhs, rhs.value()); } + inline bool operator!=(const error_code & lhs, const error_condition & rhs) { return !operator==(lhs, rhs); } + inline bool operator==(const error_condition & lhs, const error_code & rhs) { return operator==(rhs, lhs); } + inline bool operator!=(const error_condition & lhs, const error_code & rhs) { return !operator==(lhs, rhs); } +class system_error : public std::runtime_error { +public: + MaximInterface_EXPORT system_error(const error_code & ec); + + MaximInterface_EXPORT system_error(const error_code & ec, + const std::string & what_arg); + + MaximInterface_EXPORT system_error(const error_code & ec, + const char * what_arg); + + MaximInterface_EXPORT system_error(int ev, const error_category & ecat); + + MaximInterface_EXPORT system_error(int ev, const error_category & ecat, + const std::string & what_arg); + + MaximInterface_EXPORT system_error(int ev, const error_category & ecat, + const char * what_arg); + + const error_code & code() const { return code_; } + +private: + error_code code_; +}; + } // namespace MaximInterface #endif