Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
kcenon::common::error_category Class Referenceabstract

Abstract base class for error code categories. More...

#include <error_category.h>

Inheritance diagram for kcenon::common::error_category:
Inheritance graph
Collaboration diagram for kcenon::common::error_category:
Collaboration graph

Public Member Functions

virtual ~error_category ()=default
 Virtual destructor for proper cleanup of derived classes.
 
virtual std::string_view name () const noexcept=0
 Returns the unique name of this error category.
 
virtual std::string message (int code) const =0
 Returns a human-readable message for the given error code.
 
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
 Equality comparison between categories.
 
bool operator!= (const error_category &other) const noexcept
 Inequality comparison between categories.
 
bool operator< (const error_category &other) const noexcept
 Less-than comparison for use in ordered containers.
 

Protected Member Functions

 error_category ()=default
 Protected default constructor.
 
 error_category (const error_category &)=delete
 
error_categoryoperator= (const error_category &)=delete
 
 error_category (error_category &&)=delete
 
error_categoryoperator= (error_category &&)=delete
 

Detailed Description

Abstract base class for error code categories.

Each system can define its own error category by inheriting from this class. Categories provide:

  • A unique name for identification
  • Human-readable messages for error codes
  • Equivalence comparison between different categories

Example implementation:

class network_error_category : public error_category {
public:
static const network_error_category& instance() {
static network_error_category instance;
return instance;
}
std::string_view name() const noexcept override { return "network"; }
std::string message(int code) const override {
switch (code) {
case 1: return "Connection failed";
case 2: return "Timeout";
default: return "Unknown network error";
}
}
};
Abstract base class for error code categories.

Definition at line 74 of file error_category.h.

Constructor & Destructor Documentation

◆ ~error_category()

virtual kcenon::common::error_category::~error_category ( )
virtualdefault

Virtual destructor for proper cleanup of derived classes.

◆ error_category() [1/3]

kcenon::common::error_category::error_category ( )
protecteddefault

Protected default constructor.

Categories should be accessed through their singleton instance(), not constructed directly.

◆ error_category() [2/3]

kcenon::common::error_category::error_category ( const error_category & )
protecteddelete

◆ error_category() [3/3]

kcenon::common::error_category::error_category ( error_category && )
protecteddelete

Member Function Documentation

◆ equivalent()

virtual bool kcenon::common::error_category::equivalent ( int code,
const error_category & other_category,
int other_code ) const
inlinevirtualnoexcept

Checks if an error code in this category is equivalent to another.

The default implementation checks for exact category and code match. Derived classes can override this to provide semantic equivalence (e.g., "timeout" errors from different systems may be equivalent).

Parameters
codeError code in this category
other_categoryCategory of the other error code
other_codeThe other error code value
Returns
true if the error codes are semantically equivalent

Definition at line 111 of file error_category.h.

113 {
114 return (this == &other_category) && (code == other_code);
115 }

◆ message()

virtual std::string kcenon::common::error_category::message ( int code) const
pure virtual

Returns a human-readable message for the given error code.

Parameters
codeThe error code value
Returns
Human-readable error message

Implemented in kcenon::common::common_error_category.

Referenced by kcenon::common::typed_error_code::message().

Here is the caller graph for this function:

◆ name()

virtual std::string_view kcenon::common::error_category::name ( ) const
pure virtualnoexcept

Returns the unique name of this error category.

The name should be a short, descriptive identifier for the category. Examples: "common", "network", "database", "logger"

Returns
Category name as a string view (must be valid for lifetime of category)

Implemented in kcenon::common::common_error_category.

Referenced by kcenon::common::typed_error_code::category_name().

Here is the caller graph for this function:

◆ operator!=()

bool kcenon::common::error_category::operator!= ( const error_category & other) const
inlinenoexcept

Inequality comparison between categories.

Definition at line 134 of file error_category.h.

134 {
135 return !(*this == other);
136 }

◆ operator<()

bool kcenon::common::error_category::operator< ( const error_category & other) const
inlinenoexcept

Less-than comparison for use in ordered containers.

Definition at line 141 of file error_category.h.

141 {
142 return std::less<const error_category*>()(this, &other);
143 }

◆ operator=() [1/2]

error_category & kcenon::common::error_category::operator= ( const error_category & )
protecteddelete

◆ operator=() [2/2]

error_category & kcenon::common::error_category::operator= ( error_category && )
protecteddelete

◆ operator==()

bool kcenon::common::error_category::operator== ( const error_category & other) const
inlinenoexcept

Equality comparison between categories.

Categories are compared by identity (address), not by name. This ensures that two different categories with the same name are not considered equal.

Parameters
otherCategory to compare with
Returns
true if same category instance

Definition at line 127 of file error_category.h.

127 {
128 return this == &other;
129 }

The documentation for this class was generated from the following file: