| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2018 Jaroslav Jindrak
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | #include <cstring>
|
|---|
| 8 | #include <functional>
|
|---|
| 9 | #include <string>
|
|---|
| 10 | #include <system_error>
|
|---|
| 11 |
|
|---|
| 12 | namespace std
|
|---|
| 13 | {
|
|---|
| 14 | error_category::~error_category()
|
|---|
| 15 | { /* DUMMY BODY */ }
|
|---|
| 16 |
|
|---|
| 17 | error_condition error_category::default_error_condition(int code) const noexcept
|
|---|
| 18 | {
|
|---|
| 19 | return error_condition{code, *this};
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | bool error_category::equivalent(int code, const error_condition& cond) const noexcept
|
|---|
| 23 | {
|
|---|
| 24 | return default_error_condition(code) == cond;
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | bool error_category::equivalent(const error_code& ec, int cond) const noexcept
|
|---|
| 28 | {
|
|---|
| 29 | return *this == ec.category() && ec.value() == cond;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | bool error_category::operator==(const error_category& rhs) const noexcept
|
|---|
| 33 | {
|
|---|
| 34 | return this == &rhs;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | bool error_category::operator!=(const error_category& rhs) const noexcept
|
|---|
| 38 | {
|
|---|
| 39 | return !(*this == rhs);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | bool error_category::operator<(const error_category& rhs) const noexcept
|
|---|
| 43 | {
|
|---|
| 44 | return less<const error_category*>{}(this, &rhs);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | namespace aux
|
|---|
| 48 | {
|
|---|
| 49 | class generic_category_t: public error_category
|
|---|
| 50 | {
|
|---|
| 51 | public:
|
|---|
| 52 | generic_category_t()
|
|---|
| 53 | : error_category{}
|
|---|
| 54 | { /* DUMMY BODY */ }
|
|---|
| 55 |
|
|---|
| 56 | ~generic_category_t() = default;
|
|---|
| 57 |
|
|---|
| 58 | const char* name() const noexcept override
|
|---|
| 59 | {
|
|---|
| 60 | return "generic";
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | string message(int ev) const override
|
|---|
| 64 | {
|
|---|
| 65 | return "ev: " + std::to_string(ev);
|
|---|
| 66 | }
|
|---|
| 67 | };
|
|---|
| 68 |
|
|---|
| 69 | class system_category_t: public error_category
|
|---|
| 70 | {
|
|---|
| 71 | public:
|
|---|
| 72 | system_category_t()
|
|---|
| 73 | : error_category{}
|
|---|
| 74 | { /* DUMMY BODY */ }
|
|---|
| 75 |
|
|---|
| 76 | ~system_category_t() = default;
|
|---|
| 77 |
|
|---|
| 78 | const char* name() const noexcept override
|
|---|
| 79 | {
|
|---|
| 80 | return "system";
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | string message(int ev) const override
|
|---|
| 84 | {
|
|---|
| 85 | return "ev: " + std::to_string(ev);
|
|---|
| 86 | }
|
|---|
| 87 | };
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | const error_category& generic_category() noexcept
|
|---|
| 91 | {
|
|---|
| 92 | static aux::generic_category_t instance{};
|
|---|
| 93 |
|
|---|
| 94 | return instance;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | const error_category& system_category() noexcept
|
|---|
| 98 | {
|
|---|
| 99 | static aux::system_category_t instance{};
|
|---|
| 100 |
|
|---|
| 101 | return instance;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | error_code::error_code() noexcept
|
|---|
| 105 | : val_{}, cat_{&system_category()}
|
|---|
| 106 | { /* DUMMY BODY */ }
|
|---|
| 107 |
|
|---|
| 108 | error_code::error_code(int val, const error_category& cat) noexcept
|
|---|
| 109 | : val_{val}, cat_{&cat}
|
|---|
| 110 | { /* DUMMY BODY */ }
|
|---|
| 111 |
|
|---|
| 112 | void error_code::assign(int val, const error_category& cat) noexcept
|
|---|
| 113 | {
|
|---|
| 114 | val_ = val;
|
|---|
| 115 | cat_ = &cat;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | void error_code::clear() noexcept
|
|---|
| 119 | {
|
|---|
| 120 | val_ = 0;
|
|---|
| 121 | cat_ = &system_category();
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | int error_code::value() const noexcept
|
|---|
| 125 | {
|
|---|
| 126 | return val_;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | const error_category& error_code::category() const noexcept
|
|---|
| 130 | {
|
|---|
| 131 | return *cat_;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | error_condition error_code::default_error_condition() const noexcept
|
|---|
| 135 | {
|
|---|
| 136 | return cat_->default_error_condition(val_);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | string error_code::message() const
|
|---|
| 140 | {
|
|---|
| 141 | return cat_->message(val_);
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | error_code make_error_code(errc e) noexcept
|
|---|
| 145 | {
|
|---|
| 146 | return error_code{static_cast<int>(e), generic_category()};
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | bool operator<(const error_code& lhs, const error_code& rhs) noexcept
|
|---|
| 150 | {
|
|---|
| 151 | return lhs.category() < rhs.category() ||
|
|---|
| 152 | (lhs.category() == rhs.category() && lhs.value() < rhs.value());
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | error_condition::error_condition() noexcept
|
|---|
| 156 | : val_{}, cat_{&generic_category()}
|
|---|
| 157 | { /* DUMMY BODY */ }
|
|---|
| 158 |
|
|---|
| 159 | error_condition::error_condition(int val, const error_category& cat) noexcept
|
|---|
| 160 | : val_{val}, cat_{&cat}
|
|---|
| 161 | { /* DUMMY BODY */ }
|
|---|
| 162 |
|
|---|
| 163 | void error_condition::assign(int val, const error_category& cat) noexcept
|
|---|
| 164 | {
|
|---|
| 165 | val_ = val;
|
|---|
| 166 | cat_ = &cat;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | void error_condition::clear() noexcept
|
|---|
| 170 | {
|
|---|
| 171 | val_ = 0;
|
|---|
| 172 | cat_ = &generic_category();
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | int error_condition::value() const noexcept
|
|---|
| 176 | {
|
|---|
| 177 | return val_;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | const error_category& error_condition::category() const noexcept
|
|---|
| 181 | {
|
|---|
| 182 | return *cat_;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | string error_condition::message() const
|
|---|
| 186 | {
|
|---|
| 187 | return cat_->message(val_);
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | error_condition make_error_condition(errc e) noexcept
|
|---|
| 191 | {
|
|---|
| 192 | return error_condition{static_cast<int>(e), generic_category()};
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | bool operator<(const error_condition& lhs, const error_condition& rhs) noexcept
|
|---|
| 196 | {
|
|---|
| 197 | return lhs.category() < rhs.category() ||
|
|---|
| 198 | (lhs.category() == rhs.category() && lhs.value() < rhs.value());
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | bool operator==(const error_code& lhs, const error_code& rhs) noexcept
|
|---|
| 202 | {
|
|---|
| 203 | return lhs.category() == rhs.category() && lhs.value() == rhs.value();
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | bool operator==(const error_code& lhs, const error_condition& rhs) noexcept
|
|---|
| 207 | {
|
|---|
| 208 | return lhs.category().equivalent(lhs.value(), rhs)
|
|---|
| 209 | || rhs.category().equivalent(lhs, rhs.value());
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | bool operator==(const error_condition& lhs, const error_code& rhs) noexcept
|
|---|
| 213 | {
|
|---|
| 214 | return rhs.category().equivalent(rhs.value(), lhs)
|
|---|
| 215 | || lhs.category().equivalent(rhs, lhs.value());
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | bool operator==(const error_condition& lhs, const error_condition& rhs) noexcept
|
|---|
| 219 | {
|
|---|
| 220 | return lhs.category() == rhs.category() && lhs.value() == rhs.value();
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | bool operator!=(const error_code& lhs, const error_code& rhs) noexcept
|
|---|
| 224 | {
|
|---|
| 225 | return !(lhs == rhs);
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | bool operator!=(const error_code& lhs, const error_condition& rhs) noexcept
|
|---|
| 229 | {
|
|---|
| 230 | return !(lhs == rhs);
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | bool operator!=(const error_condition& lhs, const error_code& rhs) noexcept
|
|---|
| 234 | {
|
|---|
| 235 | return !(lhs == rhs);
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | bool operator!=(const error_condition& lhs, const error_condition& rhs) noexcept
|
|---|
| 239 | {
|
|---|
| 240 | return !(lhs == rhs);
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | system_error::system_error(error_code ec, const string& what_arg)
|
|---|
| 244 | : runtime_error{what_arg.c_str()}, code_{ec}
|
|---|
| 245 | { /* DUMMY BODY */ }
|
|---|
| 246 |
|
|---|
| 247 | system_error::system_error(error_code ec, const char* what_arg)
|
|---|
| 248 | : runtime_error{what_arg}, code_{ec}
|
|---|
| 249 | { /* DUMMY BODY */ }
|
|---|
| 250 |
|
|---|
| 251 | system_error::system_error(error_code ec)
|
|---|
| 252 | : runtime_error{"system_error"}, code_{ec}
|
|---|
| 253 | { /* DUMMY BODY */ }
|
|---|
| 254 |
|
|---|
| 255 | system_error::system_error(int code, const error_category& cat,
|
|---|
| 256 | const string& what_arg)
|
|---|
| 257 | : runtime_error{what_arg.c_str()}, code_{code, cat}
|
|---|
| 258 | { /* DUMMY BODY */ }
|
|---|
| 259 |
|
|---|
| 260 | system_error::system_error(int code, const error_category& cat,
|
|---|
| 261 | const char* what_arg)
|
|---|
| 262 | : runtime_error{what_arg}, code_{code, cat}
|
|---|
| 263 | { /* DUMMY BODY */ }
|
|---|
| 264 |
|
|---|
| 265 | system_error::system_error(int code, const error_category& cat)
|
|---|
| 266 | : runtime_error{"system_error"}, code_{code, cat}
|
|---|
| 267 | { /* DUMMY BODY */ }
|
|---|
| 268 |
|
|---|
| 269 | const error_code& system_error::code() const noexcept
|
|---|
| 270 | {
|
|---|
| 271 | return code_;
|
|---|
| 272 | }
|
|---|
| 273 | }
|
|---|