-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor/better error codes #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Katze719
wants to merge
13
commits into
main
Choose a base branch
from
refactor/better-error-codes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
edd214c
Refactor status codes and remove deprecated header
Katze719 1616b1c
Refactor status code definitions in status_codes.h
Katze719 f9aef38
Enhance status code structure in status_codes.h
Katze719 be05f7b
Refactor status code organization in status_codes.h
Katze719 6ea7683
Remove the Monitor struct from status_codes.h to streamline status co…
Katze719 f4bf824
Add status code definitions in a new header file
Katze719 b833253
Refactor CategoryBase template in status_code.h
Katze719 8b235ba
Update kCategoryMultiplier initialization in CategoryBase template
Katze719 176158d
Add new status code generation macros and enhance error handling
Katze719 2b63c69
Refactor error handling functions in StatusCode
Katze719 a5ce350
Enhance status code structure in status_code.h
Katze719 9375730
Update include/cpp_core/status_code.h
Katze719 ba2838b
Enhance CategoryBase template in status_code.h
Katze719 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <limits> | ||
| #include <string_view> | ||
|
|
||
| namespace cpp_core::status_codes | ||
| { | ||
|
|
||
| namespace detail | ||
| { | ||
|
|
||
| using ValueType = std::int64_t; | ||
|
|
||
| inline constexpr ValueType kCategoryMultiplier{100}; | ||
|
|
||
| template <typename Category, ValueType NumericValue> struct Code | ||
| { | ||
| static constexpr ValueType kValue = NumericValue; | ||
| std::string_view kName; // NOLINT(readability-identifier-naming) | ||
|
|
||
| // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions) | ||
| constexpr operator ValueType() const noexcept | ||
| { | ||
| return kValue; | ||
| } | ||
| [[nodiscard]] constexpr auto value() const noexcept -> ValueType | ||
| { | ||
| return kValue; | ||
| } | ||
| [[nodiscard]] constexpr auto name() const noexcept -> std::string_view | ||
| { | ||
| return kName; | ||
| } | ||
| [[nodiscard]] constexpr auto category() const noexcept -> std::string_view | ||
| { | ||
| return Category::kCategoryName; | ||
| } | ||
| }; | ||
|
|
||
| template <typename Derived> struct CategoryBase | ||
| { | ||
| private: | ||
| constexpr CategoryBase() = default; | ||
|
|
||
| protected: | ||
| template <ValueType LocalCode> static consteval auto computeValue() -> ValueType | ||
| { | ||
| static_assert(Derived::kCategoryCode >= 0, "Category code must not be negative"); | ||
| static_assert(LocalCode >= 0, "Code index must not be negative"); | ||
| static_assert(LocalCode < kCategoryMultiplier, "Category overflow (max 99 codes)"); | ||
| static_assert(Derived::kCategoryCode <= | ||
| (std::numeric_limits<ValueType>::max() - kCategoryMultiplier + 1) / kCategoryMultiplier, | ||
| "Category code too large, multiplication would overflow"); | ||
| return -((Derived::kCategoryCode * kCategoryMultiplier) + LocalCode); | ||
| } | ||
|
|
||
| template <ValueType LocalCode> using Code = detail::Code<Derived, computeValue<LocalCode>()>; | ||
|
|
||
| friend Derived; | ||
| }; | ||
|
|
||
| } // namespace detail | ||
|
|
||
| struct StatusCode | ||
| { | ||
| using ValueType = detail::ValueType; | ||
| static constexpr ValueType kSuccess = 0; | ||
|
|
||
| struct Configuration : detail::CategoryBase<Configuration> | ||
| { | ||
| static constexpr ValueType kCategoryCode = 1; | ||
| static constexpr std::string_view kCategoryName{"Configuration"}; | ||
|
|
||
| static constexpr Code<0> kSetBaudrateError{"SetBaudrateError"}; | ||
| static constexpr Code<1> kSetDataBitsError{"SetDataBitsError"}; | ||
| static constexpr Code<2> kSetParityError{"SetParityError"}; | ||
| static constexpr Code<3> kSetStopBitsError{"SetStopBitsError"}; | ||
| static constexpr Code<4> kSetFlowControlError{"SetFlowControlError"}; | ||
| static constexpr Code<5> kSetTimeoutError{"SetTimeoutError"}; | ||
| }; | ||
|
|
||
| struct Connection : detail::CategoryBase<Connection> | ||
| { | ||
| static constexpr ValueType kCategoryCode = 2; | ||
| static constexpr std::string_view kCategoryName{"Connection"}; | ||
|
|
||
| static constexpr Code<0> kNotFoundError{"NotFoundError"}; | ||
| static constexpr Code<1> kInvalidHandleError{"InvalidHandleError"}; | ||
| static constexpr Code<2> kCloseHandleError{"CloseHandleError"}; | ||
| }; | ||
|
|
||
| struct Io : detail::CategoryBase<Io> | ||
| { | ||
| static constexpr ValueType kCategoryCode = 3; | ||
| static constexpr std::string_view kCategoryName{"Io"}; | ||
|
|
||
| static constexpr Code<0> kReadError{"ReadError"}; | ||
| static constexpr Code<1> kWriteError{"WriteError"}; | ||
| static constexpr Code<2> kAbortReadError{"AbortReadError"}; | ||
| static constexpr Code<3> kAbortWriteError{"AbortWriteError"}; | ||
| static constexpr Code<4> kBufferError{"BufferError"}; | ||
| static constexpr Code<5> kClearBufferInError{"ClearBufferInError"}; | ||
| static constexpr Code<6> kClearBufferOutError{"ClearBufferOutError"}; | ||
| }; | ||
|
|
||
| struct Control : detail::CategoryBase<Control> | ||
| { | ||
| static constexpr ValueType kCategoryCode = 4; | ||
| static constexpr std::string_view kCategoryName{"Control"}; | ||
|
|
||
| static constexpr Code<0> kSetDtrError{"SetDtrError"}; | ||
| static constexpr Code<1> kSetRtsError{"SetRtsError"}; | ||
| static constexpr Code<2> kGetModemStatusError{"GetModemStatusError"}; | ||
| static constexpr Code<3> kSendBreakError{"SendBreakError"}; | ||
| static constexpr Code<4> kGetStateError{"GetStateError"}; | ||
| static constexpr Code<5> kSetStateError{"SetStateError"}; | ||
| }; | ||
|
|
||
| [[nodiscard]] static constexpr auto isError(ValueType code) noexcept -> bool | ||
| { | ||
| return code < 0; | ||
| } | ||
| [[nodiscard]] static constexpr auto isSuccess(ValueType code) noexcept -> bool | ||
| { | ||
| return code >= 0; | ||
| } | ||
| template <typename Category> [[nodiscard]] static constexpr auto belongsTo(ValueType code) noexcept -> bool | ||
| { | ||
| return code < 0 && (-code) / detail::kCategoryMultiplier == Category::kCategoryCode; | ||
| } | ||
| }; | ||
|
|
||
| } // namespace cpp_core::status_codes | ||
|
|
||
| namespace cpp_core | ||
| { | ||
| using ::cpp_core::status_codes::StatusCode; | ||
| } // namespace cpp_core | ||
|
|
||
| namespace cpp_core::status_codes::detail::tests | ||
| { | ||
|
|
||
| template <int64_t CatCode> struct FakeCategory : CategoryBase<FakeCategory<CatCode>> | ||
| { | ||
| static constexpr ValueType kCategoryCode = CatCode; | ||
|
|
||
| template <ValueType Local> static consteval auto call() -> ValueType | ||
| { | ||
| return CategoryBase<FakeCategory>::template computeValue<Local>(); | ||
| } | ||
| }; | ||
|
|
||
| // Formula: result == -(kCategoryCode * 100 + LocalCode) | ||
| static_assert(FakeCategory<1>::call<0>() == -100); | ||
| static_assert(FakeCategory<1>::call<42>() == -142); | ||
| static_assert(FakeCategory<3>::call<7>() == -307); | ||
|
|
||
| // Edge: kCategoryCode == 0 -> call<0>() produces 0 (not negative) | ||
| static_assert(FakeCategory<0>::call<0>() == 0); | ||
| static_assert(FakeCategory<0>::call<1>() == -1); | ||
|
|
||
| // Edge: LocalCode == 99 (max before overflow guard) | ||
| static_assert(FakeCategory<2>::call<99>() == -299); | ||
|
|
||
| // Consecutive codes differ by exactly -1 | ||
| static_assert(FakeCategory<1>::call<1>() - FakeCategory<1>::call<0>() == -1); | ||
|
|
||
| // Adjacent category ranges don't overlap (last of cat N > first of cat N+1) | ||
| static_assert(FakeCategory<1>::call<99>() > FakeCategory<2>::call<0>()); | ||
|
|
||
| // Overflow: largest safe category still produces correct results | ||
| inline constexpr ValueType kMaxSafeCat = | ||
| (std::numeric_limits<ValueType>::max() - kCategoryMultiplier + 1) / kCategoryMultiplier; | ||
| static_assert(kMaxSafeCat > 1'000'000); | ||
| static_assert(FakeCategory<kMaxSafeCat>::call<0>() == -(kMaxSafeCat * kCategoryMultiplier)); | ||
| static_assert(FakeCategory<kMaxSafeCat>::call<99>() > std::numeric_limits<ValueType>::min()); | ||
|
|
||
| } // namespace cpp_core::status_codes::detail::tests |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.