ORIGINAL
Loading...
Searching...
No Matches
error.h
Go to the documentation of this file.
1#ifndef ERROR_H
2#define ERROR_H
3#include <exception>
4#include "printable.h"
5#include "types.h"
6
14namespace original {
15
23 public:
38 template<typename Callback, typename Ret_TYPE, typename ...Args_TYPE>
39 static void check();
40 };
41
51 class error
52 : public std::exception,
53 public printable {
54 #define ORIGINAL_ERROR_MSG "An error thrown"
55 protected:
56 mutable std::string msg_;
57
61 virtual std::string defaultMsg() const {
62 return ORIGINAL_ERROR_MSG;
63 }
64 public:
69 explicit error(std::string msg) : msg_(std::move(msg)) {}
70
74 std::string className() const override {
75 return "error";
76 }
77
78 std::string message() const noexcept {
79 std::stringstream ss;
80 ss << "Original::" << this->className() << ": ";
81 if (!this->msg_.empty()){
82 ss << this->msg_ << ".";
83 } else {
84 ss << this->defaultMsg() << ".";
85 }
86 return ss.str();
87 }
88
92 const char* what() const noexcept override {
93 this->msg_ = this->message();
94 return this->msg_.c_str();
95 }
96 };
97
109 template<typename ERR, const bool TRIGGERING_CONDITION>
110 requires ExtendsOf<error, ERR>
112 {
113 public:
117 static void asserts(){
118 static_assert(!TRIGGERING_CONDITION);
119 }
120 };
121
122// ----------------- Exception Classes -----------------
123
129class outOfBoundError final : public error {
130public:
131 #define ORIGINAL_OUT_OF_BOUND_ERROR_MSG "Wrong value given"
132
133 explicit outOfBoundError(std::string msg = "")
134 : error(std::move(msg)) {}
135
136 std::string className() const override{
137 return "outOfBoundError";
138 }
139
140 std::string defaultMsg() const override {
141 return ORIGINAL_OUT_OF_BOUND_ERROR_MSG;
142 }
143};
144
150class valueError final : public error {
151public:
152 #define ORIGINAL_VALUE_ERROR_ERROR_MSG "Wrong value given"
153
154 explicit valueError(std::string msg = "")
155 : error(std::move(msg)) {}
156
157 std::string className() const override{
158 return "valueError";
159 }
160
161 std::string defaultMsg() const override {
162 return ORIGINAL_VALUE_ERROR_ERROR_MSG;
163 }
164};
165
171class nullPointerError final : public error {
172public:
173 #define ORIGINAL_NULL_POINTER_ERROR_MSG "Attempting to access null pointer"
174
175 explicit nullPointerError(std::string msg = "")
176 : error(std::move(msg)) {}
177
178 std::string className() const override{
179 return "nullPointerError";
180 }
181
182 std::string defaultMsg() const override {
183 return ORIGINAL_NULL_POINTER_ERROR_MSG;
184 }
185};
186
192class unSupportedMethodError final : public error {
193public:
194 #define ORIGINAL_UNSUPPORTED_METHOD_ERROR_MSG "Unsupported Method for the call"
195
196 explicit unSupportedMethodError(std::string msg = "")
197 : error(std::move(msg)) {}
198
199 std::string className() const override{
200 return "unSupportedMethodError";
201 }
202
203 std::string defaultMsg() const override {
204 return ORIGINAL_UNSUPPORTED_METHOD_ERROR_MSG;
205 }
206};
207
213class noElementError final : public error {
214public:
215 #define ORIGINAL_NO_ELEMENT_ERROR_MSG "No such element"
216
217 explicit noElementError(std::string msg = "")
218 : error(std::move(msg)) {}
219
220 std::string className() const override{
221 return "noElementError";
222 }
223
224 std::string defaultMsg() const override {
225 return ORIGINAL_NO_ELEMENT_ERROR_MSG;
226 }
227};
228
234class callbackSignatureError final : public error {
235public:
236 #define ORIGINAL_CALLBACK_SIGNATURE_ERROR_MSG "Callback signature mismatch"
237
238 explicit callbackSignatureError(std::string msg = "")
239 : error(std::move(msg)) {}
240
241 std::string className() const override{
242 return "callbackSignatureError";
243 }
244
245 std::string defaultMsg() const override {
246 return ORIGINAL_CALLBACK_SIGNATURE_ERROR_MSG;
247 }
248};
249
255class callbackReturnTypeError final : public error {
256public:
257 #define ORIGINAL_CALLBACK_RETURN_TYPE_ERROR_MSG "Return type of callback mismatch"
258
259 explicit callbackReturnTypeError(std::string msg = "")
260 : error(std::move(msg)) {}
261
262 std::string className() const override{
263 return "callbackReturnTypeError";
264 }
265
266 std::string defaultMsg() const override {
267 return ORIGINAL_CALLBACK_RETURN_TYPE_ERROR_MSG;
268 }
269};
270
284class allocateError final : public error
285{
286public:
287 #define ORIGINAL_ALLOCATE_ERROR_MSG "Can not allocate memory"
288
289 explicit allocateError(std::string msg = "")
290 : error(std::move(msg)) {}
291
292 std::string className() const override{
293 return "allocateError";
294 }
295
296 std::string defaultMsg() const override {
297 return ORIGINAL_ALLOCATE_ERROR_MSG;
298 }
299};
300
305class sysError final : public error
306{
307public:
308 #define ORIGINAL_SYS_ERROR_MSG "A system error triggered"
309
310 explicit sysError(std::string msg = "")
311 : error(std::move(msg)) {}
312
313 std::string className() const override{
314 return "sysError";
315 }
316
317 std::string defaultMsg() const override {
318 return ORIGINAL_SYS_ERROR_MSG;
319 }
320};
321
322} // namespace original
323
324// ----------------- Definitions of error.h -----------------
325
326template<typename Callback, typename Ret_TYPE, typename... Args_TYPE>
328 if constexpr (constexpr bool is_valid = std::is_invocable_v<Callback, Args_TYPE...>;
329 !is_valid){
331 } else{
332 using result_type = std::invoke_result_t<Callback, Args_TYPE...>;
333 if constexpr (constexpr bool is_valid_return_type = std::is_same_v<result_type, Ret_TYPE>;
334 !is_valid_return_type)
336 }
337}
338
339template <const bool TRIGGERING_CONDITION>
340class original::staticError<original::error, TRIGGERING_CONDITION>
341{
342public:
343 static void asserts(){
344 static_assert(!TRIGGERING_CONDITION, ORIGINAL_ERROR_MSG);
345 }
346};
347
348template <const bool TRIGGERING_CONDITION>
349class original::staticError<original::outOfBoundError, TRIGGERING_CONDITION>
350{
351public:
352 static void asserts(){
353 static_assert(!TRIGGERING_CONDITION, ORIGINAL_OUT_OF_BOUND_ERROR_MSG);
354 }
355};
356
357template <const bool TRIGGERING_CONDITION>
358class original::staticError<original::valueError, TRIGGERING_CONDITION>
359{
360public:
361 static void asserts(){
362 static_assert(!TRIGGERING_CONDITION, ORIGINAL_VALUE_ERROR_ERROR_MSG);
363 }
364};
365
366template <const bool TRIGGERING_CONDITION>
368{
369public:
370 static void asserts(){
371 static_assert(!TRIGGERING_CONDITION, ORIGINAL_NULL_POINTER_ERROR_MSG);
372 }
373};
374
375template <const bool TRIGGERING_CONDITION>
377{
378public:
379 static void asserts(){
380 static_assert(!TRIGGERING_CONDITION, ORIGINAL_UNSUPPORTED_METHOD_ERROR_MSG);
381 }
382};
383
384template <const bool TRIGGERING_CONDITION>
385class original::staticError<original::noElementError, TRIGGERING_CONDITION>
386{
387public:
388 static void asserts(){
389 static_assert(!TRIGGERING_CONDITION, ORIGINAL_NO_ELEMENT_ERROR_MSG);
390 }
391};
392
393template <const bool TRIGGERING_CONDITION>
395{
396public:
397 static void asserts(){
398 static_assert(!TRIGGERING_CONDITION, ORIGINAL_CALLBACK_SIGNATURE_ERROR_MSG);
399 }
400};
401
402template <const bool TRIGGERING_CONDITION>
404{
405public:
406 static void asserts(){
407 static_assert(!TRIGGERING_CONDITION, ORIGINAL_CALLBACK_RETURN_TYPE_ERROR_MSG);
408 }
409};
410
411template <const bool TRIGGERING_CONDITION>
412class original::staticError<original::allocateError, TRIGGERING_CONDITION>
413{
414public:
415 static void asserts(){
416 static_assert(!TRIGGERING_CONDITION, ORIGINAL_ALLOCATE_ERROR_MSG);
417 }
418};
419
420template <const bool TRIGGERING_CONDITION>
421class original::staticError<original::sysError, TRIGGERING_CONDITION>
422{
423public:
424 static void asserts(){
425 static_assert(!TRIGGERING_CONDITION, ORIGINAL_SYS_ERROR_MSG);
426 }
427};
428
429#endif // ERROR_H
Exception for memory allocation failures.
Definition error.h:285
std::string className() const override
Gets the class name for type identification.
Definition error.h:292
std::string defaultMsg() const override
Provides default message when no custom message is supplied.
Definition error.h:296
Static utility for validating callback signatures.
Definition error.h:22
static void check()
Validates a callback's signature and return type.
Definition error.h:327
Exception for callback return type mismatch.
Definition error.h:255
std::string defaultMsg() const override
Provides default message when no custom message is supplied.
Definition error.h:266
std::string className() const override
Gets the class name for type identification.
Definition error.h:262
Exception for callback argument mismatch.
Definition error.h:234
std::string className() const override
Gets the class name for type identification.
Definition error.h:241
std::string defaultMsg() const override
Provides default message when no custom message is supplied.
Definition error.h:245
Base class for all exceptions in the Original project.
Definition error.h:53
error(std::string msg)
Constructs an exception with an optional message.
Definition error.h:69
const char * what() const noexcept override
Returns the full error message.
Definition error.h:92
std::string className() const override
Returns the class name as string.
Definition error.h:74
virtual std::string defaultMsg() const
Provides default message when no custom message is supplied.
Definition error.h:61
Exception for missing element requests.
Definition error.h:213
std::string className() const override
Returns the class name as string.
Definition error.h:220
std::string defaultMsg() const override
Provides default message when no custom message is supplied.
Definition error.h:224
Exception for null pointer dereference attempts.
Definition error.h:171
std::string defaultMsg() const override
Provides default message when no custom message is supplied.
Definition error.h:182
std::string className() const override
Returns the class name as string.
Definition error.h:178
Exception for container index out-of-range errors.
Definition error.h:129
std::string defaultMsg() const override
Provides default message when no custom message is supplied.
Definition error.h:140
std::string className() const override
Returns the class name as string.
Definition error.h:136
Base class providing polymorphic string conversion capabilities.
Definition printable.h:39
Compile-time error triggering utility.
Definition error.h:112
static void asserts()
Triggers static assertion if the condition is true.
Definition error.h:117
Exception for generic system failure.
Definition error.h:306
std::string className() const override
Returns the class name as string.
Definition error.h:313
std::string defaultMsg() const override
Provides default message when no custom message is supplied.
Definition error.h:317
Exception for unimplemented method calls.
Definition error.h:192
std::string defaultMsg() const override
Provides default message when no custom message is supplied.
Definition error.h:203
std::string className() const override
Returns the class name as string.
Definition error.h:199
Exception for invalid parameter values.
Definition error.h:150
std::string className() const override
Returns the class name as string.
Definition error.h:157
std::string defaultMsg() const override
Provides default message when no custom message is supplied.
Definition error.h:161
Main namespace for the project Original.
Definition algorithms.h:21
Interface for polymorphic string formatting and output.
Core type system foundations and concept definitions.