Skip to content

Commit

Permalink
Add a little more spice
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZhihao-723 authored and Bill-hbrhbr committed Jul 29, 2024
1 parent 943d5e2 commit 50bd162
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 54 deletions.
65 changes: 44 additions & 21 deletions components/core/src/clp/GenericErrorCode.hpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
//
// Created by pleia on 7/17/2024.
//

#ifndef CLP_GENERICERRORCODE_HPP
#define CLP_GENERICERRORCODE_HPP

#include <string>
#include <system_error>

namespace clp {
/**
* Concept for int-based error code enum.
*/
template <typename T>
concept ErrorEnumType = requires(T t) {
concept ErrorEnumType = std::is_enum_v<T> && requires(T t) {
{
static_cast<int>(t)
} -> std::same_as<int>;
static_cast<std::underlying_type_t<T>>(t)
} -> std::convertible_to<int>;
};

/**
* Concept for inherited error category.
*/
template <typename T>
concept ErrorCategoryType = std::is_base_of<std::error_category, T>::value;
template <ErrorEnumType ErrorEnum>
class ErrorCategory : public std::error_category {
public:
/**
* @return The class of errors.
*/
[[nodiscard]] auto name() const noexcept -> char const* override;

/**
* @param The error code encoded in int.
* @return The descriptive message for the error.
*/
[[nodiscard]] auto message(int ev) const -> std::string override;

/**
* @param The error code encoded in int.
* @return The descriptive message for the error.
*/
[[nodiscard]] auto message(ErrorEnum error_enum) const -> std::string;
};

/**
* Template class for error code. It should take an error code enum and error category.
*/
template <ErrorEnumType ErrorEnum, ErrorCategoryType ErrorCategory>
template <ErrorEnumType ErrorEnum>
class ErrorCode {
public:
ErrorCode(ErrorEnum error) : m_error{error} {}
Expand All @@ -38,28 +50,39 @@ class ErrorCode {
/**
* Returns the reference of a singleton of the error category.
*/
[[nodiscard]] static auto get_category() -> ErrorCategory const&;
[[nodiscard]] static auto get_category() -> ErrorCategory<ErrorEnum> const&;

private:
static inline ErrorCategory<ErrorEnum> const cCategory{};
ErrorEnum m_error;
};

template <ErrorEnumType ErrorEnum, ErrorCategoryType ErrorCategory>
auto ErrorCode<ErrorEnum, ErrorCategory>::get_errno() const -> int {
template <ErrorEnumType ErrorEnum>
auto ErrorCategory<ErrorEnum>::message(ErrorEnum error_enum) const -> std::string {
return message(static_cast<int>(error_enum));
}

template <ErrorEnumType ErrorEnum>
auto ErrorCode<ErrorEnum>::get_errno() const -> int {
return static_cast<int>(m_error);
}

template <ErrorEnumType ErrorEnum, ErrorCategoryType ErrorCategory>
auto ErrorCode<ErrorEnum, ErrorCategory>::get_err_enum() const -> ErrorEnum {
template <ErrorEnumType ErrorEnum>
auto ErrorCode<ErrorEnum>::get_err_enum() const -> ErrorEnum {
return m_error;
}

template <ErrorEnumType ErrorEnum>
auto ErrorCode<ErrorEnum>::get_category() -> ErrorCategory<ErrorEnum> const& {
return ErrorCode<ErrorEnum>::cCategory;
}

/**
* Converts `ErrorCode` to std::error_code.
*/
template <typename ErrorEnum, typename ErrorCategory>
[[nodiscard]] auto make_error_code(ErrorCode<ErrorEnum, ErrorCategory> e) -> std::error_code {
return {e.get_errno(), ErrorCode<ErrorEnum, ErrorCategory>::get_category()};
template <typename ErrorEnum>
[[nodiscard]] auto make_error_code(ErrorCode<ErrorEnum> e) -> std::error_code {
return {e.get_errno(), ErrorCode<ErrorEnum>::get_category()};
}
} // namespace clp

Expand Down
18 changes: 4 additions & 14 deletions components/core/src/clp/RegexErrorCode.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
//
// Created by pleia on 7/17/2024.
//

#include "RegexErrorCode.hpp"

#include <string>

#include "GenericErrorCode.hpp"

namespace clp {
namespace {
RegexErrorCategory const cRegexErrorCategory{};
} // namespace

template <>
auto RegexErrorCategory::name() const noexcept -> char const* {
return "regex utility";
}

template <>
auto RegexErrorCategory::message(int ev) const -> std::string {
switch (static_cast<RegexErrorEnum>(ev)) {
case RegexErrorEnum::Success:
Expand Down Expand Up @@ -53,10 +47,6 @@ auto RegexErrorCategory::message(int ev) const -> std::string {
}
}

template <>
auto RegexErrorCode::get_category() -> RegexErrorCategory const& {
return cRegexErrorCategory;
}

template class ErrorCode<RegexErrorEnum, RegexErrorCategory>;
template class ErrorCategory<RegexErrorEnum>;
template class ErrorCode<RegexErrorEnum>;
} // namespace clp
21 changes: 2 additions & 19 deletions components/core/src/clp/RegexErrorCode.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
//
// Created by pleia on 7/17/2024.
//

#ifndef CLP_REGEXERRORCODE_HPP
#define CLP_REGEXERRORCODE_HPP

Expand Down Expand Up @@ -29,21 +25,8 @@ enum class RegexErrorEnum : uint8_t {
TokenUnquantifiable,
};

class RegexErrorCategory : public std::error_category {
public:
/**
* @return The class of errors.
*/
[[nodiscard]] auto name() const noexcept -> char const* override;

/**
* @param The error code encoded in int.
* @return The descriptive message for the error.
*/
[[nodiscard]] auto message(int ev) const -> std::string override;
};

using RegexErrorCode = ErrorCode<RegexErrorEnum, RegexErrorCategory>;
using RegexErrorCategory = ErrorCategory<RegexErrorEnum>;
using RegexErrorCode = ErrorCode<RegexErrorEnum>;
} // namespace clp

namespace std {
Expand Down

0 comments on commit 50bd162

Please sign in to comment.