IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
error.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/types.hxx>
6#include <ice/build/build.hxx>
7#include <string_view>
8#include <type_traits>
9#include <cassert>
10
11namespace ice
12{
13
18 struct ErrorCode
19 {
20 constexpr explicit ErrorCode(char const* definition) noexcept;
21
22 constexpr auto type() const noexcept -> char;
23 constexpr auto code() const noexcept -> ice::i32;
24 constexpr auto category() const noexcept -> std::string_view;
25 constexpr auto description() const noexcept -> std::string_view;
26
27 constexpr operator bool() const noexcept { return type() != 'E'; }
28 constexpr explicit operator i32() const noexcept { return code(); }
29
30 char const* value;
31 };
32
33 constexpr ErrorCode::ErrorCode(char const* definition) noexcept
34 : value{ definition }
35 {
36#if ICE_DEBUG || ICE_DEVELOP
37 assert(std::is_constant_evaluated());
38 assert(value[1] == '.');
39 assert(value[2] >= '0' && value[2] <= '9');
40 assert(value[3] >= '0' && value[3] <= '9');
41 assert(value[4] >= '0' && value[4] <= '9');
42 assert(value[5] >= '0' && value[5] <= '9');
43 assert(value[6] == ':');
44 char const* category = value + 7;
45 while(*category != ':' && *category != '\0') category += 1;
46 assert(*category == ':');
47
48 assert(type() != 'E' || code() != 0);
49#endif
50 }
51
52 constexpr auto ErrorCode::type() const noexcept -> char
53 {
54 return value[0];
55 }
56
57 constexpr auto ErrorCode::code() const noexcept -> ice::i32
58 {
59 ice::i32 const code_base = type() == 'E' ? -1 : 1;
60 ice::u32 constexpr code_offset = 2;
61 return code_base * ((value[code_offset + 0] - '0') * 1000 + (value[code_offset + 1] - '0') * 100 + (value[code_offset + 2] - '0') * 10 + (value[code_offset + 3] - '0'));
62 }
63
64 constexpr auto ErrorCode::category() const noexcept -> std::string_view
65 {
66 char const* const start = value + 7;
67 char const* end = start;
68 while(*end != ':') ++end;
69 return std::string_view{ start, size_t(end - start) };
70 }
71
72 constexpr auto ErrorCode::description() const noexcept -> std::string_view
73 {
74 char const* start = value + 7;
75 while(*start != ':') ++start;
76 return std::string_view{ start + 1 };
77 }
78
79 inline constexpr bool operator==(ErrorCode lhs, ErrorCode rhs) noexcept
80 {
81 return lhs.code() == rhs.code();
82 }
83
84 inline constexpr bool operator==(ErrorCode lhs, bool value) noexcept
85 {
86 return (bool)lhs == value;
87 }
88
89 // Special types to allow using S_Ok/S_Success and S_Fail/S_Error as general success/error checks.
91 {
92 constexpr explicit ErrorCodeSuccess(char const* definition) noexcept
93 : ErrorCode{ definition }
94 {
95 assert(definition[0] == 'S');
96 }
97 };
99 {
100 constexpr explicit ErrorCodeError(char const* definition) noexcept
101 : ErrorCode{ definition }
102 {
103 assert(definition[0] == 'E');
104 }
105 };
106
107 inline constexpr bool operator==(ErrorCodeSuccess lhs, ErrorCodeSuccess) noexcept
108 {
109 return true;
110 }
111
112 inline constexpr bool operator==(ErrorCode lhs, ErrorCodeSuccess) noexcept
113 {
114 return lhs.type() == 'S';
115 }
116
117 inline constexpr bool operator==(ErrorCodeError lhs, ErrorCodeError) noexcept
118 {
119 return true;
120 }
121
122 inline constexpr bool operator==(ErrorCode lhs, ErrorCodeError) noexcept
123 {
124 return lhs.type() == 'E';
125 }
126
127 inline constexpr bool operator==(ErrorCodeSuccess lhs, ErrorCodeError) noexcept
128 {
129 return false;
130 }
131
132} // namespace ice
133
134#undef assert
SPDX-License-Identifier: MIT.
Definition array.hxx:12
constexpr auto operator==(ice::TimeType auto left, TimeType auto right) noexcept
Definition clock_types.hxx:159
std::int32_t i32
Definition types.hxx:21
std::uint32_t u32
Definition types.hxx:26
Definition error.hxx:99
constexpr ErrorCodeError(char const *definition) noexcept
Definition error.hxx:100
Definition error.hxx:19
constexpr auto description() const noexcept -> std::string_view
Definition error.hxx:72
constexpr auto code() const noexcept -> ice::i32
Definition error.hxx:57
constexpr ErrorCode(char const *definition) noexcept
Definition error.hxx:33
char const * value
Definition error.hxx:30
constexpr auto category() const noexcept -> std::string_view
Definition error.hxx:64
constexpr auto type() const noexcept -> char
Definition error.hxx:52
Definition error.hxx:91
constexpr ErrorCodeSuccess(char const *definition) noexcept
Definition error.hxx:92