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
29 struct ErrorCode
30 {
31 constexpr explicit ErrorCode(char const* definition) noexcept;
32
34 constexpr auto type() const noexcept -> char;
35
37 constexpr auto code() const noexcept -> ice::i32;
38
40 constexpr auto category() const noexcept -> std::string_view;
41
43 constexpr auto description() const noexcept -> std::string_view;
44
47 constexpr operator bool() const noexcept { return type() != 'E'; }
48
50 constexpr explicit operator i32() const noexcept { return code(); }
51
52 char const* value;
53 };
54
55 constexpr ErrorCode::ErrorCode(char const* definition) noexcept
56 : value{ definition }
57 {
58#if ICE_DEBUG || ICE_DEVELOP
59 assert(std::is_constant_evaluated());
60 assert(value[1] == '.');
61 assert(value[2] >= '0' && value[2] <= '9');
62 assert(value[3] >= '0' && value[3] <= '9');
63 assert(value[4] >= '0' && value[4] <= '9');
64 assert(value[5] >= '0' && value[5] <= '9');
65 assert(value[6] == ':');
66 char const* category = value + 7;
67 while(*category != ':' && *category != '\0') category += 1;
68 assert(*category == ':');
69
70 assert(type() != 'E' || code() != 0);
71#endif
72 }
73
74 constexpr auto ErrorCode::type() const noexcept -> char
75 {
76 return value[0];
77 }
78
79 constexpr auto ErrorCode::code() const noexcept -> ice::i32
80 {
81 ice::i32 const code_base = type() == 'E' ? -1 : 1;
82 ice::u32 constexpr code_offset = 2;
83 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'));
84 }
85
86 constexpr auto ErrorCode::category() const noexcept -> std::string_view
87 {
88 char const* const start = value + 7;
89 char const* end = start;
90 while(*end != ':') ++end;
91 return std::string_view{ start, size_t(end - start) };
92 }
93
94 constexpr auto ErrorCode::description() const noexcept -> std::string_view
95 {
96 char const* start = value + 7;
97 while(*start != ':') ++start;
98 return std::string_view{ start + 1 };
99 }
100
101 inline constexpr bool operator==(ErrorCode lhs, ErrorCode rhs) noexcept
102 {
103 return lhs.code() == rhs.code();
104 }
105
106 inline constexpr bool operator==(ErrorCode lhs, bool value) noexcept
107 {
108 return (bool)lhs == value;
109 }
110
111 // Special types to allow using S_Ok/S_Success and S_Fail/S_Error as general success/error checks.
113 {
114 constexpr explicit ErrorCodeSuccess(char const* definition) noexcept
115 : ErrorCode{ definition }
116 {
117 assert(definition[0] == 'S');
118 }
119 };
121 {
122 constexpr explicit ErrorCodeError(char const* definition) noexcept
123 : ErrorCode{ definition }
124 {
125 assert(definition[0] == 'E');
126 }
127 };
128
129 inline constexpr bool operator==(ErrorCodeSuccess lhs, ErrorCodeSuccess) noexcept
130 {
131 return true;
132 }
133
134 inline constexpr bool operator==(ErrorCode lhs, ErrorCodeSuccess) noexcept
135 {
136 return lhs.type() == 'S';
137 }
138
139 inline constexpr bool operator==(ErrorCodeError lhs, ErrorCodeError) noexcept
140 {
141 return true;
142 }
143
144 inline constexpr bool operator==(ErrorCode lhs, ErrorCodeError) noexcept
145 {
146 return lhs.type() == 'E';
147 }
148
149 inline constexpr bool operator==(ErrorCodeSuccess lhs, ErrorCodeError) noexcept
150 {
151 return false;
152 }
153
154} // namespace ice
155
156#undef assert
Root namespace for all APIs part of the IceShard engine project.
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:121
constexpr ErrorCodeError(char const *definition) noexcept
Definition error.hxx:122
Compile-time defined error code with extended information.
Definition error.hxx:30
constexpr auto description() const noexcept -> std::string_view
Definition error.hxx:94
constexpr auto code() const noexcept -> ice::i32
Definition error.hxx:79
constexpr ErrorCode(char const *definition) noexcept
Definition error.hxx:55
char const * value
Definition error.hxx:52
constexpr auto category() const noexcept -> std::string_view
Definition error.hxx:86
constexpr auto type() const noexcept -> char
Definition error.hxx:74
Definition error.hxx:113
constexpr ErrorCodeSuccess(char const *definition) noexcept
Definition error.hxx:114