Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
error_category.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
35#pragma once
36
37#include <functional>
38#include <string>
39#include <string_view>
40
41namespace kcenon::common {
42
75public:
79 virtual ~error_category() = default;
80
89 virtual std::string_view name() const noexcept = 0;
90
97 virtual std::string message(int code) const = 0;
98
111 virtual bool equivalent(int code,
112 const error_category& other_category,
113 int other_code) const noexcept {
114 return (this == &other_category) && (code == other_code);
115 }
116
127 bool operator==(const error_category& other) const noexcept {
128 return this == &other;
129 }
130
134 bool operator!=(const error_category& other) const noexcept {
135 return !(*this == other);
136 }
137
141 bool operator<(const error_category& other) const noexcept {
142 return std::less<const error_category*>()(this, &other);
143 }
144
145protected:
152 error_category() = default;
153
154 // Non-copyable, non-movable (singleton pattern)
159};
160
161// Forward declaration
162class typed_error_code;
163
177public:
200
208 static const common_error_category& instance() noexcept {
209 static common_error_category inst;
210 return inst;
211 }
212
217 std::string_view name() const noexcept override {
218 return "common";
219 }
220
226 std::string message(int code) const override {
227 switch (code) {
228 case success: return "Success";
229 case unknown_error: return "Unknown error";
230 case invalid_argument: return "Invalid argument";
231 case not_found: return "Not found";
232 case permission_denied: return "Permission denied";
233 case timeout: return "Operation timed out";
234 case cancelled: return "Operation was cancelled";
235 case not_initialized: return "Not initialized";
236 case already_exists: return "Already exists";
237 case out_of_memory: return "Out of memory";
238 case io_error: return "I/O error";
239 case operation_not_supported: return "Operation not supported";
240 case internal_error: return "Internal error";
241 default: return "Unknown common error (code: " + std::to_string(code) + ")";
242 }
243 }
244
245private:
247};
248
280public:
285 : code_(0)
286 , category_(&common_error_category::instance()) {}
287
294 typed_error_code(int code, const error_category& category) noexcept
295 : code_(code)
296 , category_(&category) {}
297
306 : code_(static_cast<int>(code))
308
312 int value() const noexcept { return code_; }
313
317 const error_category& category() const noexcept { return *category_; }
318
324 std::string message() const { return category_->message(code_); }
325
331 std::string_view category_name() const noexcept { return category_->name(); }
332
338 explicit operator bool() const noexcept { return code_ != 0; }
339
343 void clear() noexcept {
344 code_ = 0;
346 }
347
351 void assign(int code, const error_category& category) noexcept {
352 code_ = code;
354 }
355
361 bool operator==(const typed_error_code& other) const noexcept {
362 return code_ == other.code_ && *category_ == *other.category_;
363 }
364
368 bool operator!=(const typed_error_code& other) const noexcept {
369 return !(*this == other);
370 }
371
375 bool operator<(const typed_error_code& other) const noexcept {
376 if (*category_ < *other.category_) return true;
377 if (*other.category_ < *category_) return false;
378 return code_ < other.code_;
379 }
380
381private:
382 int code_;
384};
385
386// ============================================================================
387// Helper Functions
388// ============================================================================
389
399
407template<typename Category>
409 return typed_error_code(code, Category::instance());
410}
411
418inline bool is_success(const typed_error_code& ec) noexcept {
419 return ec.value() == 0;
420}
421
428inline bool is_error(const typed_error_code& ec) noexcept {
429 return ec.value() != 0;
430}
431
432} // namespace kcenon::common
Error category for common/shared error codes.
std::string message(int code) const override
Returns a human-readable message for the error code.
std::string_view name() const noexcept override
Returns the category name.
static const common_error_category & instance() noexcept
Returns the singleton instance of common_error_category.
Abstract base class for error code categories.
virtual std::string_view name() const noexcept=0
Returns the unique name of this error category.
virtual bool equivalent(int code, const error_category &other_category, int other_code) const noexcept
Checks if an error code in this category is equivalent to another.
bool operator!=(const error_category &other) const noexcept
Inequality comparison between categories.
error_category & operator=(error_category &&)=delete
bool operator==(const error_category &other) const noexcept
Equality comparison between categories.
virtual std::string message(int code) const =0
Returns a human-readable message for the given error code.
bool operator<(const error_category &other) const noexcept
Less-than comparison for use in ordered containers.
error_category(error_category &&)=delete
error_category & operator=(const error_category &)=delete
error_category()=default
Protected default constructor.
error_category(const error_category &)=delete
virtual ~error_category()=default
Virtual destructor for proper cleanup of derived classes.
A type-safe error code that carries its category.
typed_error_code() noexcept
Default constructor creates a success error code.
const error_category & category() const noexcept
Returns the error category.
int value() const noexcept
Returns the error code value.
std::string_view category_name() const noexcept
Returns the category name.
bool operator<(const typed_error_code &other) const noexcept
Less-than comparison for use in ordered containers.
bool operator==(const typed_error_code &other) const noexcept
Equality comparison.
typed_error_code(common_error_category::codes code) noexcept
Constructs from common_error_category::codes enum.
bool operator!=(const typed_error_code &other) const noexcept
Inequality comparison.
void assign(int code, const error_category &category) noexcept
Assigns a new error code value and category.
const error_category * category_
std::string message() const
Returns a human-readable error message.
typed_error_code(int code, const error_category &category) noexcept
Constructs an error code with the given value and category.
void clear() noexcept
Clears the error code to success state.
Core interfaces.
Definition adapter.h:21
typed_error_code make_typed_error_code(common_error_category::codes code) noexcept
Creates a typed_error_code from common_error_category::codes enum.
bool is_success(const typed_error_code &ec) noexcept
Checks if a typed_error_code represents success (no error).
bool is_error(const typed_error_code &ec) noexcept
Checks if a typed_error_code represents an error.