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:
Mon Sep 16 11:13:37 2019 -0500
Revision:
8:5ea891c7d1a1
Updated to version 2.0.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IanBenzMaxim 8:5ea891c7d1a1 1 /*******************************************************************************
IanBenzMaxim 8:5ea891c7d1a1 2 * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved.
IanBenzMaxim 8:5ea891c7d1a1 3 *
IanBenzMaxim 8:5ea891c7d1a1 4 * Permission is hereby granted, free of charge, to any person obtaining a
IanBenzMaxim 8:5ea891c7d1a1 5 * copy of this software and associated documentation files (the "Software"),
IanBenzMaxim 8:5ea891c7d1a1 6 * to deal in the Software without restriction, including without limitation
IanBenzMaxim 8:5ea891c7d1a1 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
IanBenzMaxim 8:5ea891c7d1a1 8 * and/or sell copies of the Software, and to permit persons to whom the
IanBenzMaxim 8:5ea891c7d1a1 9 * Software is furnished to do so, subject to the following conditions:
IanBenzMaxim 8:5ea891c7d1a1 10 *
IanBenzMaxim 8:5ea891c7d1a1 11 * The above copyright notice and this permission notice shall be included
IanBenzMaxim 8:5ea891c7d1a1 12 * in all copies or substantial portions of the Software.
IanBenzMaxim 8:5ea891c7d1a1 13 *
IanBenzMaxim 8:5ea891c7d1a1 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
IanBenzMaxim 8:5ea891c7d1a1 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
IanBenzMaxim 8:5ea891c7d1a1 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IanBenzMaxim 8:5ea891c7d1a1 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
IanBenzMaxim 8:5ea891c7d1a1 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
IanBenzMaxim 8:5ea891c7d1a1 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
IanBenzMaxim 8:5ea891c7d1a1 20 * OTHER DEALINGS IN THE SOFTWARE.
IanBenzMaxim 8:5ea891c7d1a1 21 *
IanBenzMaxim 8:5ea891c7d1a1 22 * Except as contained in this notice, the name of Maxim Integrated
IanBenzMaxim 8:5ea891c7d1a1 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
IanBenzMaxim 8:5ea891c7d1a1 24 * Products, Inc. Branding Policy.
IanBenzMaxim 8:5ea891c7d1a1 25 *
IanBenzMaxim 8:5ea891c7d1a1 26 * The mere transfer of this software does not imply any licenses
IanBenzMaxim 8:5ea891c7d1a1 27 * of trade secrets, proprietary technology, copyrights, patents,
IanBenzMaxim 8:5ea891c7d1a1 28 * trademarks, maskwork rights, or any other form of intellectual
IanBenzMaxim 8:5ea891c7d1a1 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
IanBenzMaxim 8:5ea891c7d1a1 30 * ownership rights.
IanBenzMaxim 8:5ea891c7d1a1 31 *******************************************************************************/
IanBenzMaxim 8:5ea891c7d1a1 32
IanBenzMaxim 8:5ea891c7d1a1 33 #ifndef MaximInterfaceCore_Result_hpp
IanBenzMaxim 8:5ea891c7d1a1 34 #define MaximInterfaceCore_Result_hpp
IanBenzMaxim 8:5ea891c7d1a1 35
IanBenzMaxim 8:5ea891c7d1a1 36 #include "None.hpp"
IanBenzMaxim 8:5ea891c7d1a1 37 #include "Optional.hpp"
IanBenzMaxim 8:5ea891c7d1a1 38 #include "SafeBool.hpp"
IanBenzMaxim 8:5ea891c7d1a1 39 #include "system_error.hpp"
IanBenzMaxim 8:5ea891c7d1a1 40
IanBenzMaxim 8:5ea891c7d1a1 41 // Include for std::swap.
IanBenzMaxim 8:5ea891c7d1a1 42 #include <algorithm>
IanBenzMaxim 8:5ea891c7d1a1 43 #include <utility>
IanBenzMaxim 8:5ea891c7d1a1 44
IanBenzMaxim 8:5ea891c7d1a1 45 namespace MaximInterfaceCore {
IanBenzMaxim 8:5ea891c7d1a1 46
IanBenzMaxim 8:5ea891c7d1a1 47 /// @brief Expected value container that holds either a value or an error.
IanBenzMaxim 8:5ea891c7d1a1 48 /// @details
IanBenzMaxim 8:5ea891c7d1a1 49 /// Result can be used to multiplex errors and values returned from functions
IanBenzMaxim 8:5ea891c7d1a1 50 /// that may fail as an alternative to exceptions.
IanBenzMaxim 8:5ea891c7d1a1 51 /// @tparam T
IanBenzMaxim 8:5ea891c7d1a1 52 /// Must be void or meet the requirements for DefaultConstructible,
IanBenzMaxim 8:5ea891c7d1a1 53 /// CopyConstructible, and CopyAssignable.
IanBenzMaxim 8:5ea891c7d1a1 54 template <typename T> class Result {
IanBenzMaxim 8:5ea891c7d1a1 55 public:
IanBenzMaxim 8:5ea891c7d1a1 56 typedef T value_type;
IanBenzMaxim 8:5ea891c7d1a1 57
IanBenzMaxim 8:5ea891c7d1a1 58 Result() : value_(), error_(), hasValue_(false) {}
IanBenzMaxim 8:5ea891c7d1a1 59
IanBenzMaxim 8:5ea891c7d1a1 60 Result(const T & value) : value_(value), error_(), hasValue_(true) {}
IanBenzMaxim 8:5ea891c7d1a1 61
IanBenzMaxim 8:5ea891c7d1a1 62 Result(const error_code & error)
IanBenzMaxim 8:5ea891c7d1a1 63 : value_(), error_(error), hasValue_(false) {}
IanBenzMaxim 8:5ea891c7d1a1 64
IanBenzMaxim 8:5ea891c7d1a1 65 template <typename ErrorCodeEnum>
IanBenzMaxim 8:5ea891c7d1a1 66 Result(ErrorCodeEnum error,
IanBenzMaxim 8:5ea891c7d1a1 67 typename enable_if<is_error_code_enum<ErrorCodeEnum>::value>::type * =
IanBenzMaxim 8:5ea891c7d1a1 68 NULL)
IanBenzMaxim 8:5ea891c7d1a1 69 : value_(), error_(error), hasValue_(false) {}
IanBenzMaxim 8:5ea891c7d1a1 70
IanBenzMaxim 8:5ea891c7d1a1 71 template <typename U>
IanBenzMaxim 8:5ea891c7d1a1 72 explicit Result(const Result<U> & other)
IanBenzMaxim 8:5ea891c7d1a1 73 : value_(other.value()), error_(other.error()),
IanBenzMaxim 8:5ea891c7d1a1 74 hasValue_(other.hasValue()) {}
IanBenzMaxim 8:5ea891c7d1a1 75
IanBenzMaxim 8:5ea891c7d1a1 76 Result & operator=(const T & value) {
IanBenzMaxim 8:5ea891c7d1a1 77 value_ = value;
IanBenzMaxim 8:5ea891c7d1a1 78 error_.clear();
IanBenzMaxim 8:5ea891c7d1a1 79 hasValue_ = true;
IanBenzMaxim 8:5ea891c7d1a1 80 return *this;
IanBenzMaxim 8:5ea891c7d1a1 81 }
IanBenzMaxim 8:5ea891c7d1a1 82
IanBenzMaxim 8:5ea891c7d1a1 83 Result & operator=(const error_code & error) {
IanBenzMaxim 8:5ea891c7d1a1 84 error_ = error;
IanBenzMaxim 8:5ea891c7d1a1 85 clearValue();
IanBenzMaxim 8:5ea891c7d1a1 86 return *this;
IanBenzMaxim 8:5ea891c7d1a1 87 }
IanBenzMaxim 8:5ea891c7d1a1 88
IanBenzMaxim 8:5ea891c7d1a1 89 template <typename ErrorCodeEnum>
IanBenzMaxim 8:5ea891c7d1a1 90 typename enable_if<is_error_code_enum<ErrorCodeEnum>::value, Result &>::type
IanBenzMaxim 8:5ea891c7d1a1 91 operator=(ErrorCodeEnum error) {
IanBenzMaxim 8:5ea891c7d1a1 92 error_ = error;
IanBenzMaxim 8:5ea891c7d1a1 93 clearValue();
IanBenzMaxim 8:5ea891c7d1a1 94 return *this;
IanBenzMaxim 8:5ea891c7d1a1 95 }
IanBenzMaxim 8:5ea891c7d1a1 96
IanBenzMaxim 8:5ea891c7d1a1 97 template <typename U> Result & operator=(const Result<U> & other) {
IanBenzMaxim 8:5ea891c7d1a1 98 assign(other);
IanBenzMaxim 8:5ea891c7d1a1 99 return *this;
IanBenzMaxim 8:5ea891c7d1a1 100 }
IanBenzMaxim 8:5ea891c7d1a1 101
IanBenzMaxim 8:5ea891c7d1a1 102 Result & operator=(const Result & other) {
IanBenzMaxim 8:5ea891c7d1a1 103 assign(other);
IanBenzMaxim 8:5ea891c7d1a1 104 return *this;
IanBenzMaxim 8:5ea891c7d1a1 105 }
IanBenzMaxim 8:5ea891c7d1a1 106
IanBenzMaxim 8:5ea891c7d1a1 107 bool hasValue() const { return hasValue_; }
IanBenzMaxim 8:5ea891c7d1a1 108
IanBenzMaxim 8:5ea891c7d1a1 109 operator SafeBool() const { return makeSafeBool(hasValue()); }
IanBenzMaxim 8:5ea891c7d1a1 110
IanBenzMaxim 8:5ea891c7d1a1 111 const T & value() const { return value_; }
IanBenzMaxim 8:5ea891c7d1a1 112
IanBenzMaxim 8:5ea891c7d1a1 113 T & value() {
IanBenzMaxim 8:5ea891c7d1a1 114 return const_cast<T &>(static_cast<const Result &>(*this).value());
IanBenzMaxim 8:5ea891c7d1a1 115 }
IanBenzMaxim 8:5ea891c7d1a1 116
IanBenzMaxim 8:5ea891c7d1a1 117 const error_code & error() const { return error_; }
IanBenzMaxim 8:5ea891c7d1a1 118
IanBenzMaxim 8:5ea891c7d1a1 119 error_code & error() {
IanBenzMaxim 8:5ea891c7d1a1 120 return const_cast<error_code &>(static_cast<const Result &>(*this).error());
IanBenzMaxim 8:5ea891c7d1a1 121 }
IanBenzMaxim 8:5ea891c7d1a1 122
IanBenzMaxim 8:5ea891c7d1a1 123 void swap(Result & other) {
IanBenzMaxim 8:5ea891c7d1a1 124 using std::swap;
IanBenzMaxim 8:5ea891c7d1a1 125 if (hasValue_ || other.hasValue_) {
IanBenzMaxim 8:5ea891c7d1a1 126 swap(value_, other.value_);
IanBenzMaxim 8:5ea891c7d1a1 127 swap(hasValue_, other.hasValue_);
IanBenzMaxim 8:5ea891c7d1a1 128 }
IanBenzMaxim 8:5ea891c7d1a1 129 swap(error_, other.error_);
IanBenzMaxim 8:5ea891c7d1a1 130 }
IanBenzMaxim 8:5ea891c7d1a1 131
IanBenzMaxim 8:5ea891c7d1a1 132 private:
IanBenzMaxim 8:5ea891c7d1a1 133 void clearValue() {
IanBenzMaxim 8:5ea891c7d1a1 134 if (hasValue_) {
IanBenzMaxim 8:5ea891c7d1a1 135 hasValue_ = false;
IanBenzMaxim 8:5ea891c7d1a1 136 value_ = T();
IanBenzMaxim 8:5ea891c7d1a1 137 }
IanBenzMaxim 8:5ea891c7d1a1 138 }
IanBenzMaxim 8:5ea891c7d1a1 139
IanBenzMaxim 8:5ea891c7d1a1 140 template <typename U> void assign(const Result<U> & other) {
IanBenzMaxim 8:5ea891c7d1a1 141 if (hasValue_ || other.hasValue()) {
IanBenzMaxim 8:5ea891c7d1a1 142 value_ = other.value();
IanBenzMaxim 8:5ea891c7d1a1 143 hasValue_ = other.hasValue();
IanBenzMaxim 8:5ea891c7d1a1 144 }
IanBenzMaxim 8:5ea891c7d1a1 145 error_ = other.error();
IanBenzMaxim 8:5ea891c7d1a1 146 }
IanBenzMaxim 8:5ea891c7d1a1 147
IanBenzMaxim 8:5ea891c7d1a1 148 T value_;
IanBenzMaxim 8:5ea891c7d1a1 149 error_code error_;
IanBenzMaxim 8:5ea891c7d1a1 150 bool hasValue_;
IanBenzMaxim 8:5ea891c7d1a1 151 };
IanBenzMaxim 8:5ea891c7d1a1 152
IanBenzMaxim 8:5ea891c7d1a1 153 template <typename T> Result<T> makeResult(const T & value) { return value; }
IanBenzMaxim 8:5ea891c7d1a1 154
IanBenzMaxim 8:5ea891c7d1a1 155 template <typename T> void swap(Result<T> & lhs, Result<T> & rhs) {
IanBenzMaxim 8:5ea891c7d1a1 156 lhs.swap(rhs);
IanBenzMaxim 8:5ea891c7d1a1 157 }
IanBenzMaxim 8:5ea891c7d1a1 158
IanBenzMaxim 8:5ea891c7d1a1 159 template <typename T, typename U>
IanBenzMaxim 8:5ea891c7d1a1 160 bool operator==(const Result<T> & lhs, const Result<U> & rhs) {
IanBenzMaxim 8:5ea891c7d1a1 161 if (lhs.hasValue() != rhs.hasValue()) {
IanBenzMaxim 8:5ea891c7d1a1 162 return false;
IanBenzMaxim 8:5ea891c7d1a1 163 }
IanBenzMaxim 8:5ea891c7d1a1 164 if (!lhs.hasValue()) {
IanBenzMaxim 8:5ea891c7d1a1 165 return lhs.error() == rhs.error();
IanBenzMaxim 8:5ea891c7d1a1 166 }
IanBenzMaxim 8:5ea891c7d1a1 167 return lhs.value() == rhs.value();
IanBenzMaxim 8:5ea891c7d1a1 168 }
IanBenzMaxim 8:5ea891c7d1a1 169
IanBenzMaxim 8:5ea891c7d1a1 170 template <typename T, typename U>
IanBenzMaxim 8:5ea891c7d1a1 171 bool operator!=(const Result<T> & lhs, const Result<U> & rhs) {
IanBenzMaxim 8:5ea891c7d1a1 172 if (lhs.hasValue() != rhs.hasValue()) {
IanBenzMaxim 8:5ea891c7d1a1 173 return true;
IanBenzMaxim 8:5ea891c7d1a1 174 }
IanBenzMaxim 8:5ea891c7d1a1 175 if (!lhs.hasValue()) {
IanBenzMaxim 8:5ea891c7d1a1 176 return lhs.error() != rhs.error();
IanBenzMaxim 8:5ea891c7d1a1 177 }
IanBenzMaxim 8:5ea891c7d1a1 178 return lhs.value() != rhs.value();
IanBenzMaxim 8:5ea891c7d1a1 179 }
IanBenzMaxim 8:5ea891c7d1a1 180
IanBenzMaxim 8:5ea891c7d1a1 181 template <typename T, typename U>
IanBenzMaxim 8:5ea891c7d1a1 182 typename enable_if<!is_error_code_enum<U>::value, bool>::type
IanBenzMaxim 8:5ea891c7d1a1 183 operator==(const Result<T> & exp, const U & value) {
IanBenzMaxim 8:5ea891c7d1a1 184 return exp.hasValue() ? exp.value() == value : false;
IanBenzMaxim 8:5ea891c7d1a1 185 }
IanBenzMaxim 8:5ea891c7d1a1 186
IanBenzMaxim 8:5ea891c7d1a1 187 template <typename T, typename U>
IanBenzMaxim 8:5ea891c7d1a1 188 typename enable_if<!is_error_code_enum<T>::value, bool>::type
IanBenzMaxim 8:5ea891c7d1a1 189 operator==(const T & value, const Result<U> & exp) {
IanBenzMaxim 8:5ea891c7d1a1 190 return operator==(exp, value);
IanBenzMaxim 8:5ea891c7d1a1 191 }
IanBenzMaxim 8:5ea891c7d1a1 192
IanBenzMaxim 8:5ea891c7d1a1 193 template <typename T, typename U>
IanBenzMaxim 8:5ea891c7d1a1 194 typename enable_if<!is_error_code_enum<U>::value, bool>::type
IanBenzMaxim 8:5ea891c7d1a1 195 operator!=(const Result<T> & exp, const U & value) {
IanBenzMaxim 8:5ea891c7d1a1 196 return exp.hasValue() ? exp.value() != value : true;
IanBenzMaxim 8:5ea891c7d1a1 197 }
IanBenzMaxim 8:5ea891c7d1a1 198
IanBenzMaxim 8:5ea891c7d1a1 199 template <typename T, typename U>
IanBenzMaxim 8:5ea891c7d1a1 200 typename enable_if<!is_error_code_enum<T>::value, bool>::type
IanBenzMaxim 8:5ea891c7d1a1 201 operator!=(const T & value, const Result<U> & exp) {
IanBenzMaxim 8:5ea891c7d1a1 202 return operator!=(exp, value);
IanBenzMaxim 8:5ea891c7d1a1 203 }
IanBenzMaxim 8:5ea891c7d1a1 204
IanBenzMaxim 8:5ea891c7d1a1 205 template <typename T>
IanBenzMaxim 8:5ea891c7d1a1 206 bool operator==(const Result<T> & exp, const error_code & error) {
IanBenzMaxim 8:5ea891c7d1a1 207 return exp.hasValue() ? false : exp.error() == error;
IanBenzMaxim 8:5ea891c7d1a1 208 }
IanBenzMaxim 8:5ea891c7d1a1 209
IanBenzMaxim 8:5ea891c7d1a1 210 template <typename T>
IanBenzMaxim 8:5ea891c7d1a1 211 bool operator==(const error_code & error, const Result<T> & exp) {
IanBenzMaxim 8:5ea891c7d1a1 212 return operator==(exp, error);
IanBenzMaxim 8:5ea891c7d1a1 213 }
IanBenzMaxim 8:5ea891c7d1a1 214
IanBenzMaxim 8:5ea891c7d1a1 215 template <typename T>
IanBenzMaxim 8:5ea891c7d1a1 216 bool operator!=(const Result<T> & exp, const error_code & error) {
IanBenzMaxim 8:5ea891c7d1a1 217 return exp.hasValue() ? true : exp.error() != error;
IanBenzMaxim 8:5ea891c7d1a1 218 }
IanBenzMaxim 8:5ea891c7d1a1 219
IanBenzMaxim 8:5ea891c7d1a1 220 template <typename T>
IanBenzMaxim 8:5ea891c7d1a1 221 bool operator!=(const error_code & error, const Result<T> & exp) {
IanBenzMaxim 8:5ea891c7d1a1 222 return operator!=(exp, error);
IanBenzMaxim 8:5ea891c7d1a1 223 }
IanBenzMaxim 8:5ea891c7d1a1 224
IanBenzMaxim 8:5ea891c7d1a1 225 // Specialization for void.
IanBenzMaxim 8:5ea891c7d1a1 226 template <> class Result<void> {
IanBenzMaxim 8:5ea891c7d1a1 227 public:
IanBenzMaxim 8:5ea891c7d1a1 228 typedef void value_type;
IanBenzMaxim 8:5ea891c7d1a1 229
IanBenzMaxim 8:5ea891c7d1a1 230 Result() : error_(), hasValue_(false) {}
IanBenzMaxim 8:5ea891c7d1a1 231
IanBenzMaxim 8:5ea891c7d1a1 232 Result(None) : error_(), hasValue_(true) {}
IanBenzMaxim 8:5ea891c7d1a1 233
IanBenzMaxim 8:5ea891c7d1a1 234 Result(const error_code & error) : error_(error), hasValue_(false) {}
IanBenzMaxim 8:5ea891c7d1a1 235
IanBenzMaxim 8:5ea891c7d1a1 236 template <typename ErrorCodeEnum>
IanBenzMaxim 8:5ea891c7d1a1 237 Result(ErrorCodeEnum error,
IanBenzMaxim 8:5ea891c7d1a1 238 typename enable_if<is_error_code_enum<ErrorCodeEnum>::value>::type * =
IanBenzMaxim 8:5ea891c7d1a1 239 NULL)
IanBenzMaxim 8:5ea891c7d1a1 240 : error_(error), hasValue_(false) {}
IanBenzMaxim 8:5ea891c7d1a1 241
IanBenzMaxim 8:5ea891c7d1a1 242 bool hasValue() const { return hasValue_; }
IanBenzMaxim 8:5ea891c7d1a1 243
IanBenzMaxim 8:5ea891c7d1a1 244 operator SafeBool() const { return makeSafeBool(hasValue()); }
IanBenzMaxim 8:5ea891c7d1a1 245
IanBenzMaxim 8:5ea891c7d1a1 246 void value() const {}
IanBenzMaxim 8:5ea891c7d1a1 247
IanBenzMaxim 8:5ea891c7d1a1 248 const error_code & error() const { return error_; }
IanBenzMaxim 8:5ea891c7d1a1 249
IanBenzMaxim 8:5ea891c7d1a1 250 error_code & error() {
IanBenzMaxim 8:5ea891c7d1a1 251 return const_cast<error_code &>(static_cast<const Result &>(*this).error());
IanBenzMaxim 8:5ea891c7d1a1 252 }
IanBenzMaxim 8:5ea891c7d1a1 253
IanBenzMaxim 8:5ea891c7d1a1 254 void swap(Result & other) {
IanBenzMaxim 8:5ea891c7d1a1 255 using std::swap;
IanBenzMaxim 8:5ea891c7d1a1 256 swap(hasValue_, other.hasValue_);
IanBenzMaxim 8:5ea891c7d1a1 257 swap(error_, other.error_);
IanBenzMaxim 8:5ea891c7d1a1 258 }
IanBenzMaxim 8:5ea891c7d1a1 259
IanBenzMaxim 8:5ea891c7d1a1 260 private:
IanBenzMaxim 8:5ea891c7d1a1 261 error_code error_;
IanBenzMaxim 8:5ea891c7d1a1 262 bool hasValue_;
IanBenzMaxim 8:5ea891c7d1a1 263 };
IanBenzMaxim 8:5ea891c7d1a1 264
IanBenzMaxim 8:5ea891c7d1a1 265 inline Result<void> makeResult(None) { return none; }
IanBenzMaxim 8:5ea891c7d1a1 266
IanBenzMaxim 8:5ea891c7d1a1 267 template <>
IanBenzMaxim 8:5ea891c7d1a1 268 inline bool operator==(const Result<void> & lhs, const Result<void> & rhs) {
IanBenzMaxim 8:5ea891c7d1a1 269 if (lhs.hasValue() != rhs.hasValue()) {
IanBenzMaxim 8:5ea891c7d1a1 270 return false;
IanBenzMaxim 8:5ea891c7d1a1 271 }
IanBenzMaxim 8:5ea891c7d1a1 272 if (!lhs.hasValue()) {
IanBenzMaxim 8:5ea891c7d1a1 273 return lhs.error() == rhs.error();
IanBenzMaxim 8:5ea891c7d1a1 274 }
IanBenzMaxim 8:5ea891c7d1a1 275 return true;
IanBenzMaxim 8:5ea891c7d1a1 276 }
IanBenzMaxim 8:5ea891c7d1a1 277
IanBenzMaxim 8:5ea891c7d1a1 278 template <>
IanBenzMaxim 8:5ea891c7d1a1 279 inline bool operator!=(const Result<void> & lhs, const Result<void> & rhs) {
IanBenzMaxim 8:5ea891c7d1a1 280 if (lhs.hasValue() != rhs.hasValue()) {
IanBenzMaxim 8:5ea891c7d1a1 281 return true;
IanBenzMaxim 8:5ea891c7d1a1 282 }
IanBenzMaxim 8:5ea891c7d1a1 283 if (!lhs.hasValue()) {
IanBenzMaxim 8:5ea891c7d1a1 284 return lhs.error() != rhs.error();
IanBenzMaxim 8:5ea891c7d1a1 285 }
IanBenzMaxim 8:5ea891c7d1a1 286 return false;
IanBenzMaxim 8:5ea891c7d1a1 287 }
IanBenzMaxim 8:5ea891c7d1a1 288
IanBenzMaxim 8:5ea891c7d1a1 289 template <> inline bool operator==(const Result<void> & exp, const None &) {
IanBenzMaxim 8:5ea891c7d1a1 290 return exp.hasValue();
IanBenzMaxim 8:5ea891c7d1a1 291 }
IanBenzMaxim 8:5ea891c7d1a1 292
IanBenzMaxim 8:5ea891c7d1a1 293 template <> inline bool operator==(const None &, const Result<void> & exp) {
IanBenzMaxim 8:5ea891c7d1a1 294 return operator==(exp, none);
IanBenzMaxim 8:5ea891c7d1a1 295 }
IanBenzMaxim 8:5ea891c7d1a1 296
IanBenzMaxim 8:5ea891c7d1a1 297 template <> inline bool operator!=(const Result<void> & exp, const None &) {
IanBenzMaxim 8:5ea891c7d1a1 298 return !operator==(exp, none);
IanBenzMaxim 8:5ea891c7d1a1 299 }
IanBenzMaxim 8:5ea891c7d1a1 300
IanBenzMaxim 8:5ea891c7d1a1 301 template <> inline bool operator!=(const None &, const Result<void> & exp) {
IanBenzMaxim 8:5ea891c7d1a1 302 return operator!=(exp, none);
IanBenzMaxim 8:5ea891c7d1a1 303 }
IanBenzMaxim 8:5ea891c7d1a1 304
IanBenzMaxim 8:5ea891c7d1a1 305 namespace detail {
IanBenzMaxim 8:5ea891c7d1a1 306
IanBenzMaxim 8:5ea891c7d1a1 307 template <typename T> Optional<error_code> TRY_helper(const Result<T> & expr) {
IanBenzMaxim 8:5ea891c7d1a1 308 Optional<error_code> error;
IanBenzMaxim 8:5ea891c7d1a1 309 if (!expr) {
IanBenzMaxim 8:5ea891c7d1a1 310 error = expr.error();
IanBenzMaxim 8:5ea891c7d1a1 311 }
IanBenzMaxim 8:5ea891c7d1a1 312 return error;
IanBenzMaxim 8:5ea891c7d1a1 313 }
IanBenzMaxim 8:5ea891c7d1a1 314
IanBenzMaxim 8:5ea891c7d1a1 315 template <typename T, typename U>
IanBenzMaxim 8:5ea891c7d1a1 316 Optional<error_code> TRY_VALUE_helper(T & var, const Result<U> & expr) {
IanBenzMaxim 8:5ea891c7d1a1 317 Optional<error_code> error;
IanBenzMaxim 8:5ea891c7d1a1 318 if (expr) {
IanBenzMaxim 8:5ea891c7d1a1 319 var = expr.value();
IanBenzMaxim 8:5ea891c7d1a1 320 } else {
IanBenzMaxim 8:5ea891c7d1a1 321 error = expr.error();
IanBenzMaxim 8:5ea891c7d1a1 322 }
IanBenzMaxim 8:5ea891c7d1a1 323 return error;
IanBenzMaxim 8:5ea891c7d1a1 324 }
IanBenzMaxim 8:5ea891c7d1a1 325
IanBenzMaxim 8:5ea891c7d1a1 326 } // namespace detail
IanBenzMaxim 8:5ea891c7d1a1 327 } // namespace MaximInterfaceCore
IanBenzMaxim 8:5ea891c7d1a1 328
IanBenzMaxim 8:5ea891c7d1a1 329 // clang-format off
IanBenzMaxim 8:5ea891c7d1a1 330
IanBenzMaxim 8:5ea891c7d1a1 331 /// @brief
IanBenzMaxim 8:5ea891c7d1a1 332 /// Evaluates an expression returning a Result type by continuing execution if
IanBenzMaxim 8:5ea891c7d1a1 333 /// successful or returning the error to the calling function if unsuccessful.
IanBenzMaxim 8:5ea891c7d1a1 334 #define MaximInterfaceCore_TRY(expr) \
IanBenzMaxim 8:5ea891c7d1a1 335 if (const ::MaximInterfaceCore::Optional< ::MaximInterfaceCore::error_code> \
IanBenzMaxim 8:5ea891c7d1a1 336 MaximInterfaceCore_TRY_error = \
IanBenzMaxim 8:5ea891c7d1a1 337 ::MaximInterfaceCore::detail::TRY_helper(expr)) \
IanBenzMaxim 8:5ea891c7d1a1 338 return MaximInterfaceCore_TRY_error.value()
IanBenzMaxim 8:5ea891c7d1a1 339
IanBenzMaxim 8:5ea891c7d1a1 340 /// @brief
IanBenzMaxim 8:5ea891c7d1a1 341 /// Evaluates an expression returning a non-void Result type by assigning the
IanBenzMaxim 8:5ea891c7d1a1 342 /// value to the specified variable if successful or returning the error to the
IanBenzMaxim 8:5ea891c7d1a1 343 /// calling function if unsuccessful.
IanBenzMaxim 8:5ea891c7d1a1 344 #define MaximInterfaceCore_TRY_VALUE(var, expr) \
IanBenzMaxim 8:5ea891c7d1a1 345 if (const ::MaximInterfaceCore::Optional< ::MaximInterfaceCore::error_code> \
IanBenzMaxim 8:5ea891c7d1a1 346 MaximInterfaceCore_TRY_VALUE_error = \
IanBenzMaxim 8:5ea891c7d1a1 347 ::MaximInterfaceCore::detail::TRY_VALUE_helper(var, expr)) \
IanBenzMaxim 8:5ea891c7d1a1 348 return MaximInterfaceCore_TRY_VALUE_error.value()
IanBenzMaxim 8:5ea891c7d1a1 349
IanBenzMaxim 8:5ea891c7d1a1 350 #endif