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

Committer:
IanBenzMaxim
Date:
Fri May 29 16:19:22 2020 -0500
Revision:
12:7eb41621ba22
Parent:
8:5ea891c7d1a1
Updated to version 2.2.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IanBenzMaxim 7:9cd16581b578 1 /*******************************************************************************
IanBenzMaxim 8:5ea891c7d1a1 2 * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved.
IanBenzMaxim 7:9cd16581b578 3 *
IanBenzMaxim 7:9cd16581b578 4 * Permission is hereby granted, free of charge, to any person obtaining a
IanBenzMaxim 7:9cd16581b578 5 * copy of this software and associated documentation files (the "Software"),
IanBenzMaxim 7:9cd16581b578 6 * to deal in the Software without restriction, including without limitation
IanBenzMaxim 7:9cd16581b578 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
IanBenzMaxim 7:9cd16581b578 8 * and/or sell copies of the Software, and to permit persons to whom the
IanBenzMaxim 7:9cd16581b578 9 * Software is furnished to do so, subject to the following conditions:
IanBenzMaxim 7:9cd16581b578 10 *
IanBenzMaxim 7:9cd16581b578 11 * The above copyright notice and this permission notice shall be included
IanBenzMaxim 7:9cd16581b578 12 * in all copies or substantial portions of the Software.
IanBenzMaxim 7:9cd16581b578 13 *
IanBenzMaxim 7:9cd16581b578 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
IanBenzMaxim 7:9cd16581b578 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
IanBenzMaxim 7:9cd16581b578 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IanBenzMaxim 7:9cd16581b578 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
IanBenzMaxim 7:9cd16581b578 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
IanBenzMaxim 7:9cd16581b578 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
IanBenzMaxim 7:9cd16581b578 20 * OTHER DEALINGS IN THE SOFTWARE.
IanBenzMaxim 7:9cd16581b578 21 *
IanBenzMaxim 7:9cd16581b578 22 * Except as contained in this notice, the name of Maxim Integrated
IanBenzMaxim 7:9cd16581b578 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
IanBenzMaxim 7:9cd16581b578 24 * Products, Inc. Branding Policy.
IanBenzMaxim 7:9cd16581b578 25 *
IanBenzMaxim 7:9cd16581b578 26 * The mere transfer of this software does not imply any licenses
IanBenzMaxim 7:9cd16581b578 27 * of trade secrets, proprietary technology, copyrights, patents,
IanBenzMaxim 7:9cd16581b578 28 * trademarks, maskwork rights, or any other form of intellectual
IanBenzMaxim 7:9cd16581b578 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
IanBenzMaxim 7:9cd16581b578 30 * ownership rights.
IanBenzMaxim 7:9cd16581b578 31 *******************************************************************************/
IanBenzMaxim 7:9cd16581b578 32
IanBenzMaxim 7:9cd16581b578 33 /// @file
IanBenzMaxim 7:9cd16581b578 34 /// @brief
IanBenzMaxim 7:9cd16581b578 35 /// Error handling constructs similar to std::error_code, std::error_condition,
IanBenzMaxim 7:9cd16581b578 36 /// and std::error_category.
IanBenzMaxim 7:9cd16581b578 37
IanBenzMaxim 8:5ea891c7d1a1 38 #ifndef MaximInterfaceCore_system_error_hpp
IanBenzMaxim 8:5ea891c7d1a1 39 #define MaximInterfaceCore_system_error_hpp
IanBenzMaxim 7:9cd16581b578 40
IanBenzMaxim 8:5ea891c7d1a1 41 #include <stddef.h>
IanBenzMaxim 7:9cd16581b578 42 #include <stdexcept>
IanBenzMaxim 7:9cd16581b578 43 #include <string>
IanBenzMaxim 7:9cd16581b578 44 #include "Config.hpp"
IanBenzMaxim 7:9cd16581b578 45 #include "SafeBool.hpp"
IanBenzMaxim 8:5ea891c7d1a1 46 #include "type_traits.hpp"
IanBenzMaxim 7:9cd16581b578 47 #include "Uncopyable.hpp"
IanBenzMaxim 7:9cd16581b578 48
IanBenzMaxim 7:9cd16581b578 49 namespace MaximInterfaceCore {
IanBenzMaxim 7:9cd16581b578 50
IanBenzMaxim 7:9cd16581b578 51 class error_condition;
IanBenzMaxim 7:9cd16581b578 52 class error_code;
IanBenzMaxim 7:9cd16581b578 53
IanBenzMaxim 8:5ea891c7d1a1 54 /// Error category interface.
IanBenzMaxim 7:9cd16581b578 55 class error_category : private Uncopyable {
IanBenzMaxim 7:9cd16581b578 56 public:
IanBenzMaxim 7:9cd16581b578 57 virtual ~error_category() {}
IanBenzMaxim 7:9cd16581b578 58
IanBenzMaxim 7:9cd16581b578 59 virtual const char * name() const = 0;
IanBenzMaxim 7:9cd16581b578 60
IanBenzMaxim 7:9cd16581b578 61 MaximInterfaceCore_EXPORT virtual error_condition
IanBenzMaxim 7:9cd16581b578 62 default_error_condition(int code) const;
IanBenzMaxim 7:9cd16581b578 63
IanBenzMaxim 7:9cd16581b578 64 MaximInterfaceCore_EXPORT virtual bool
IanBenzMaxim 7:9cd16581b578 65 equivalent(int code, const error_condition & condition) const;
IanBenzMaxim 7:9cd16581b578 66
IanBenzMaxim 7:9cd16581b578 67 MaximInterfaceCore_EXPORT virtual bool equivalent(const error_code & code,
IanBenzMaxim 7:9cd16581b578 68 int condition) const;
IanBenzMaxim 7:9cd16581b578 69
IanBenzMaxim 7:9cd16581b578 70 virtual std::string message(int condition) const = 0;
IanBenzMaxim 7:9cd16581b578 71 };
IanBenzMaxim 7:9cd16581b578 72
IanBenzMaxim 7:9cd16581b578 73 inline bool operator==(const error_category & lhs, const error_category & rhs) {
IanBenzMaxim 7:9cd16581b578 74 return &lhs == &rhs;
IanBenzMaxim 7:9cd16581b578 75 }
IanBenzMaxim 7:9cd16581b578 76
IanBenzMaxim 7:9cd16581b578 77 inline bool operator!=(const error_category & lhs, const error_category & rhs) {
IanBenzMaxim 7:9cd16581b578 78 return !operator==(lhs, rhs);
IanBenzMaxim 7:9cd16581b578 79 }
IanBenzMaxim 7:9cd16581b578 80
IanBenzMaxim 7:9cd16581b578 81 inline bool operator<(const error_category & lhs, const error_category & rhs) {
IanBenzMaxim 7:9cd16581b578 82 return &lhs < &rhs;
IanBenzMaxim 7:9cd16581b578 83 }
IanBenzMaxim 7:9cd16581b578 84
IanBenzMaxim 8:5ea891c7d1a1 85 /// Default error category.
IanBenzMaxim 7:9cd16581b578 86 MaximInterfaceCore_EXPORT const error_category & system_category();
IanBenzMaxim 7:9cd16581b578 87
IanBenzMaxim 8:5ea891c7d1a1 88 /// Checks if an enum type is implicitly convertible to an error_condition.
IanBenzMaxim 8:5ea891c7d1a1 89 template <typename T> struct is_error_condition_enum : false_type {};
IanBenzMaxim 8:5ea891c7d1a1 90
IanBenzMaxim 8:5ea891c7d1a1 91 /// @brief
IanBenzMaxim 8:5ea891c7d1a1 92 /// Used for classifying groups of error_code into higher-level error conditions
IanBenzMaxim 8:5ea891c7d1a1 93 /// through the error_category::equivalent methods.
IanBenzMaxim 7:9cd16581b578 94 class error_condition {
IanBenzMaxim 7:9cd16581b578 95 public:
IanBenzMaxim 7:9cd16581b578 96 error_condition() : value_(0), category_(&system_category()) {}
IanBenzMaxim 7:9cd16581b578 97
IanBenzMaxim 7:9cd16581b578 98 error_condition(int value, const error_category & category)
IanBenzMaxim 7:9cd16581b578 99 : value_(value), category_(&category) {}
IanBenzMaxim 7:9cd16581b578 100
IanBenzMaxim 8:5ea891c7d1a1 101 template <typename ErrorConditionEnum>
IanBenzMaxim 8:5ea891c7d1a1 102 error_condition(
IanBenzMaxim 8:5ea891c7d1a1 103 ErrorConditionEnum e,
IanBenzMaxim 8:5ea891c7d1a1 104 typename enable_if<
IanBenzMaxim 8:5ea891c7d1a1 105 is_error_condition_enum<ErrorConditionEnum>::value>::type * = NULL) {
IanBenzMaxim 8:5ea891c7d1a1 106 *this = make_error_condition(e);
IanBenzMaxim 8:5ea891c7d1a1 107 }
IanBenzMaxim 8:5ea891c7d1a1 108
IanBenzMaxim 8:5ea891c7d1a1 109 template <typename ErrorConditionEnum>
IanBenzMaxim 8:5ea891c7d1a1 110 typename enable_if<is_error_condition_enum<ErrorConditionEnum>::value,
IanBenzMaxim 8:5ea891c7d1a1 111 error_condition &>::type
IanBenzMaxim 8:5ea891c7d1a1 112 operator=(ErrorConditionEnum e) {
IanBenzMaxim 8:5ea891c7d1a1 113 *this = make_error_condition(e);
IanBenzMaxim 8:5ea891c7d1a1 114 return *this;
IanBenzMaxim 8:5ea891c7d1a1 115 }
IanBenzMaxim 8:5ea891c7d1a1 116
IanBenzMaxim 7:9cd16581b578 117 void assign(int value, const error_category & category) {
IanBenzMaxim 7:9cd16581b578 118 value_ = value;
IanBenzMaxim 7:9cd16581b578 119 category_ = &category;
IanBenzMaxim 7:9cd16581b578 120 }
IanBenzMaxim 7:9cd16581b578 121
IanBenzMaxim 7:9cd16581b578 122 void clear() {
IanBenzMaxim 7:9cd16581b578 123 value_ = 0;
IanBenzMaxim 7:9cd16581b578 124 category_ = &system_category();
IanBenzMaxim 7:9cd16581b578 125 }
IanBenzMaxim 7:9cd16581b578 126
IanBenzMaxim 7:9cd16581b578 127 int value() const { return value_; }
IanBenzMaxim 7:9cd16581b578 128
IanBenzMaxim 7:9cd16581b578 129 const error_category & category() const { return *category_; }
IanBenzMaxim 7:9cd16581b578 130
IanBenzMaxim 7:9cd16581b578 131 std::string message() const { return category().message(value()); }
IanBenzMaxim 7:9cd16581b578 132
IanBenzMaxim 7:9cd16581b578 133 operator SafeBool() const { return makeSafeBool(value() != 0); }
IanBenzMaxim 7:9cd16581b578 134
IanBenzMaxim 7:9cd16581b578 135 private:
IanBenzMaxim 7:9cd16581b578 136 int value_;
IanBenzMaxim 7:9cd16581b578 137 const error_category * category_;
IanBenzMaxim 7:9cd16581b578 138 };
IanBenzMaxim 7:9cd16581b578 139
IanBenzMaxim 7:9cd16581b578 140 inline bool operator==(const error_condition & lhs,
IanBenzMaxim 7:9cd16581b578 141 const error_condition & rhs) {
IanBenzMaxim 7:9cd16581b578 142 return (lhs.value() == rhs.value()) && (lhs.category() == rhs.category());
IanBenzMaxim 7:9cd16581b578 143 }
IanBenzMaxim 7:9cd16581b578 144
IanBenzMaxim 7:9cd16581b578 145 inline bool operator!=(const error_condition & lhs,
IanBenzMaxim 7:9cd16581b578 146 const error_condition & rhs) {
IanBenzMaxim 7:9cd16581b578 147 return !operator==(lhs, rhs);
IanBenzMaxim 7:9cd16581b578 148 }
IanBenzMaxim 7:9cd16581b578 149
IanBenzMaxim 7:9cd16581b578 150 inline bool operator<(const error_condition & lhs,
IanBenzMaxim 7:9cd16581b578 151 const error_condition & rhs) {
IanBenzMaxim 7:9cd16581b578 152 return (lhs.category() < rhs.category()) ||
IanBenzMaxim 7:9cd16581b578 153 ((lhs.category() == rhs.category()) && (lhs.value() < rhs.value()));
IanBenzMaxim 7:9cd16581b578 154 }
IanBenzMaxim 7:9cd16581b578 155
IanBenzMaxim 8:5ea891c7d1a1 156 /// Checks if an enum type is implicitly convertible to an error_code.
IanBenzMaxim 8:5ea891c7d1a1 157 template <typename T> struct is_error_code_enum : false_type {};
IanBenzMaxim 8:5ea891c7d1a1 158
IanBenzMaxim 8:5ea891c7d1a1 159 /// @brief Holds a raw error code produced by a subsystem.
IanBenzMaxim 8:5ea891c7d1a1 160 /// @details
IanBenzMaxim 8:5ea891c7d1a1 161 /// An error_code is composed of a pair of values: a category of errors, usually
IanBenzMaxim 8:5ea891c7d1a1 162 /// one per subsystem, and a number representing a specific error value within
IanBenzMaxim 8:5ea891c7d1a1 163 /// that category. While not required, zero is typically used as the success
IanBenzMaxim 8:5ea891c7d1a1 164 /// value so that the boolean conversion can be used as a failure test.
IanBenzMaxim 7:9cd16581b578 165 class error_code {
IanBenzMaxim 7:9cd16581b578 166 public:
IanBenzMaxim 7:9cd16581b578 167 error_code() : value_(0), category_(&system_category()) {}
IanBenzMaxim 7:9cd16581b578 168
IanBenzMaxim 7:9cd16581b578 169 error_code(int value, const error_category & category)
IanBenzMaxim 7:9cd16581b578 170 : value_(value), category_(&category) {}
IanBenzMaxim 7:9cd16581b578 171
IanBenzMaxim 8:5ea891c7d1a1 172 template <typename ErrorCodeEnum>
IanBenzMaxim 8:5ea891c7d1a1 173 error_code(
IanBenzMaxim 8:5ea891c7d1a1 174 ErrorCodeEnum e,
IanBenzMaxim 8:5ea891c7d1a1 175 typename enable_if<is_error_code_enum<ErrorCodeEnum>::value>::type * =
IanBenzMaxim 8:5ea891c7d1a1 176 NULL) {
IanBenzMaxim 8:5ea891c7d1a1 177 *this = make_error_code(e);
IanBenzMaxim 8:5ea891c7d1a1 178 }
IanBenzMaxim 8:5ea891c7d1a1 179
IanBenzMaxim 8:5ea891c7d1a1 180 template <typename ErrorCodeEnum>
IanBenzMaxim 8:5ea891c7d1a1 181 typename enable_if<is_error_code_enum<ErrorCodeEnum>::value,
IanBenzMaxim 8:5ea891c7d1a1 182 error_code &>::type
IanBenzMaxim 8:5ea891c7d1a1 183 operator=(ErrorCodeEnum e) {
IanBenzMaxim 8:5ea891c7d1a1 184 *this = make_error_code(e);
IanBenzMaxim 8:5ea891c7d1a1 185 return *this;
IanBenzMaxim 8:5ea891c7d1a1 186 }
IanBenzMaxim 8:5ea891c7d1a1 187
IanBenzMaxim 7:9cd16581b578 188 void assign(int value, const error_category & category) {
IanBenzMaxim 7:9cd16581b578 189 value_ = value;
IanBenzMaxim 7:9cd16581b578 190 category_ = &category;
IanBenzMaxim 7:9cd16581b578 191 }
IanBenzMaxim 7:9cd16581b578 192
IanBenzMaxim 7:9cd16581b578 193 void clear() {
IanBenzMaxim 7:9cd16581b578 194 value_ = 0;
IanBenzMaxim 7:9cd16581b578 195 category_ = &system_category();
IanBenzMaxim 7:9cd16581b578 196 }
IanBenzMaxim 7:9cd16581b578 197
IanBenzMaxim 7:9cd16581b578 198 int value() const { return value_; }
IanBenzMaxim 7:9cd16581b578 199
IanBenzMaxim 7:9cd16581b578 200 const error_category & category() const { return *category_; }
IanBenzMaxim 7:9cd16581b578 201
IanBenzMaxim 7:9cd16581b578 202 error_condition default_error_condition() const {
IanBenzMaxim 7:9cd16581b578 203 return category().default_error_condition(value());
IanBenzMaxim 7:9cd16581b578 204 }
IanBenzMaxim 7:9cd16581b578 205
IanBenzMaxim 7:9cd16581b578 206 std::string message() const { return category().message(value()); }
IanBenzMaxim 7:9cd16581b578 207
IanBenzMaxim 7:9cd16581b578 208 operator SafeBool() const { return makeSafeBool(value() != 0); }
IanBenzMaxim 7:9cd16581b578 209
IanBenzMaxim 7:9cd16581b578 210 private:
IanBenzMaxim 7:9cd16581b578 211 int value_;
IanBenzMaxim 7:9cd16581b578 212 const error_category * category_;
IanBenzMaxim 7:9cd16581b578 213 };
IanBenzMaxim 7:9cd16581b578 214
IanBenzMaxim 7:9cd16581b578 215 inline bool operator==(const error_code & lhs, const error_code & rhs) {
IanBenzMaxim 7:9cd16581b578 216 return (lhs.value() == rhs.value()) && (lhs.category() == rhs.category());
IanBenzMaxim 7:9cd16581b578 217 }
IanBenzMaxim 7:9cd16581b578 218
IanBenzMaxim 7:9cd16581b578 219 inline bool operator!=(const error_code & lhs, const error_code & rhs) {
IanBenzMaxim 7:9cd16581b578 220 return !operator==(lhs, rhs);
IanBenzMaxim 7:9cd16581b578 221 }
IanBenzMaxim 7:9cd16581b578 222
IanBenzMaxim 7:9cd16581b578 223 inline bool operator<(const error_code & lhs, const error_code & rhs) {
IanBenzMaxim 7:9cd16581b578 224 return (lhs.category() < rhs.category()) ||
IanBenzMaxim 7:9cd16581b578 225 ((lhs.category() == rhs.category()) && (lhs.value() < rhs.value()));
IanBenzMaxim 7:9cd16581b578 226 }
IanBenzMaxim 7:9cd16581b578 227
IanBenzMaxim 7:9cd16581b578 228 template <typename CharT, typename Traits>
IanBenzMaxim 7:9cd16581b578 229 std::basic_ostream<CharT, Traits> &
IanBenzMaxim 7:9cd16581b578 230 operator<<(std::basic_ostream<CharT, Traits> & os, const error_code & ec) {
IanBenzMaxim 7:9cd16581b578 231 os << ec.category().name() << ':' << ec.value();
IanBenzMaxim 7:9cd16581b578 232 return os;
IanBenzMaxim 7:9cd16581b578 233 }
IanBenzMaxim 7:9cd16581b578 234
IanBenzMaxim 7:9cd16581b578 235 inline bool operator==(const error_code & lhs, const error_condition & rhs) {
IanBenzMaxim 7:9cd16581b578 236 return lhs.category().equivalent(lhs.value(), rhs) ||
IanBenzMaxim 7:9cd16581b578 237 rhs.category().equivalent(lhs, rhs.value());
IanBenzMaxim 7:9cd16581b578 238 }
IanBenzMaxim 7:9cd16581b578 239
IanBenzMaxim 7:9cd16581b578 240 inline bool operator!=(const error_code & lhs, const error_condition & rhs) {
IanBenzMaxim 7:9cd16581b578 241 return !operator==(lhs, rhs);
IanBenzMaxim 7:9cd16581b578 242 }
IanBenzMaxim 7:9cd16581b578 243
IanBenzMaxim 7:9cd16581b578 244 inline bool operator==(const error_condition & lhs, const error_code & rhs) {
IanBenzMaxim 7:9cd16581b578 245 return operator==(rhs, lhs);
IanBenzMaxim 7:9cd16581b578 246 }
IanBenzMaxim 7:9cd16581b578 247
IanBenzMaxim 7:9cd16581b578 248 inline bool operator!=(const error_condition & lhs, const error_code & rhs) {
IanBenzMaxim 7:9cd16581b578 249 return !operator==(lhs, rhs);
IanBenzMaxim 7:9cd16581b578 250 }
IanBenzMaxim 7:9cd16581b578 251
IanBenzMaxim 8:5ea891c7d1a1 252 /// Wrapper for throwing error_code as an exception.
IanBenzMaxim 7:9cd16581b578 253 class system_error : public std::runtime_error {
IanBenzMaxim 7:9cd16581b578 254 public:
IanBenzMaxim 7:9cd16581b578 255 MaximInterfaceCore_EXPORT system_error(const error_code & ec);
IanBenzMaxim 7:9cd16581b578 256
IanBenzMaxim 7:9cd16581b578 257 MaximInterfaceCore_EXPORT system_error(const error_code & ec,
IanBenzMaxim 7:9cd16581b578 258 const std::string & what_arg);
IanBenzMaxim 7:9cd16581b578 259
IanBenzMaxim 7:9cd16581b578 260 MaximInterfaceCore_EXPORT system_error(const error_code & ec,
IanBenzMaxim 7:9cd16581b578 261 const char * what_arg);
IanBenzMaxim 7:9cd16581b578 262
IanBenzMaxim 7:9cd16581b578 263 MaximInterfaceCore_EXPORT system_error(int ev, const error_category & ecat);
IanBenzMaxim 7:9cd16581b578 264
IanBenzMaxim 7:9cd16581b578 265 MaximInterfaceCore_EXPORT system_error(int ev, const error_category & ecat,
IanBenzMaxim 7:9cd16581b578 266 const std::string & what_arg);
IanBenzMaxim 7:9cd16581b578 267
IanBenzMaxim 7:9cd16581b578 268 MaximInterfaceCore_EXPORT system_error(int ev, const error_category & ecat,
IanBenzMaxim 7:9cd16581b578 269 const char * what_arg);
IanBenzMaxim 7:9cd16581b578 270
IanBenzMaxim 7:9cd16581b578 271 const error_code & code() const { return code_; }
IanBenzMaxim 7:9cd16581b578 272
IanBenzMaxim 7:9cd16581b578 273 private:
IanBenzMaxim 7:9cd16581b578 274 error_code code_;
IanBenzMaxim 7:9cd16581b578 275 };
IanBenzMaxim 7:9cd16581b578 276
IanBenzMaxim 7:9cd16581b578 277 } // namespace MaximInterfaceCore
IanBenzMaxim 7:9cd16581b578 278
IanBenzMaxim 7:9cd16581b578 279 #endif