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
31namespace original {
32
56 public:
71 template<typename Callback, typename Ret_TYPE, typename ...Args_TYPE>
72 static void check();
73 };
74
90 class error
91 : public std::exception,
92 public printable {
93 #define ORIGINAL_ERROR_MSG "An error thrown"
94 protected:
95 mutable std::string msg_;
96
101 virtual std::string defaultMsg() const {
102 return ORIGINAL_ERROR_MSG;
103 }
104 public:
109 explicit error(std::string msg) : msg_(std::move(msg)) {}
110
115 std::string className() const override {
116 return "error";
117 }
118
123 std::string message() const noexcept {
124 std::stringstream ss;
125 ss << "Original::" << this->className() << ": ";
126 if (!this->msg_.empty()){
127 ss << this->msg_ << ".";
128 } else {
129 ss << this->defaultMsg() << ".";
130 }
131 return ss.str();
132 }
133
138 const char* what() const noexcept override {
139 this->msg_ = this->message();
140 return this->msg_.c_str();
141 }
142 };
143
164 template<typename ERR, const bool TRIGGERING_CONDITION>
165 requires ExtendsOf<error, ERR>
167 {
168 public:
174 static void asserts(){
175 static_assert(!TRIGGERING_CONDITION);
176 }
177 };
178
179// ----------------- Exception Classes -----------------
180
193public:
194 #define ORIGINAL_OUT_OF_BOUND_ERROR_MSG "Wrong value given"
195
196 explicit outOfBoundError(std::string msg = "")
197 : error(std::move(msg)) {}
198
199 std::string className() const override{
200 return "outOfBoundError";
201 }
202
203 std::string defaultMsg() const override {
204 return ORIGINAL_OUT_OF_BOUND_ERROR_MSG;
205 }
206};
207
219class valueError final : public error {
220public:
221 #define ORIGINAL_VALUE_ERROR_ERROR_MSG "Wrong value given"
222
223 explicit valueError(std::string msg = "")
224 : error(std::move(msg)) {}
225
226 std::string className() const override{
227 return "valueError";
228 }
229
230 std::string defaultMsg() const override {
231 return ORIGINAL_VALUE_ERROR_ERROR_MSG;
232 }
233};
234
246public:
247 #define ORIGINAL_NULL_POINTER_ERROR_MSG "Attempting to access null pointer"
248
249 explicit nullPointerError(std::string msg = "")
250 : error(std::move(msg)) {}
251
252 std::string className() const override{
253 return "nullPointerError";
254 }
255
256 std::string defaultMsg() const override {
257 return ORIGINAL_NULL_POINTER_ERROR_MSG;
258 }
259};
260
272public:
273 #define ORIGINAL_UNSUPPORTED_METHOD_ERROR_MSG "Unsupported Method for the call"
274
275 explicit unSupportedMethodError(std::string msg = "")
276 : error(std::move(msg)) {}
277
278 std::string className() const override{
279 return "unSupportedMethodError";
280 }
281
282 std::string defaultMsg() const override {
283 return ORIGINAL_UNSUPPORTED_METHOD_ERROR_MSG;
284 }
285};
286
298class noElementError final : public error {
299public:
300 #define ORIGINAL_NO_ELEMENT_ERROR_MSG "No such element"
301
302 explicit noElementError(std::string msg = "")
303 : error(std::move(msg)) {}
304
305 std::string className() const override{
306 return "noElementError";
307 }
308
309 std::string defaultMsg() const override {
310 return ORIGINAL_NO_ELEMENT_ERROR_MSG;
311 }
312};
313
325public:
326 #define ORIGINAL_CALLBACK_SIGNATURE_ERROR_MSG "Callback signature mismatch"
327
328 explicit callbackSignatureError(std::string msg = "")
329 : error(std::move(msg)) {}
330
331 std::string className() const override{
332 return "callbackSignatureError";
333 }
334
335 std::string defaultMsg() const override {
336 return ORIGINAL_CALLBACK_SIGNATURE_ERROR_MSG;
337 }
338};
339
351public:
352 #define ORIGINAL_CALLBACK_RETURN_TYPE_ERROR_MSG "Return type of callback mismatch"
353
354 explicit callbackReturnTypeError(std::string msg = "")
355 : error(std::move(msg)) {}
356
357 std::string className() const override{
358 return "callbackReturnTypeError";
359 }
360
361 std::string defaultMsg() const override {
362 return ORIGINAL_CALLBACK_RETURN_TYPE_ERROR_MSG;
363 }
364};
365
385{
386public:
387 #define ORIGINAL_ALLOCATE_ERROR_MSG "Can not allocate memory"
388
389 explicit allocateError(std::string msg = "")
390 : error(std::move(msg)) {}
391
392 std::string className() const override{
393 return "allocateError";
394 }
395
396 std::string defaultMsg() const override {
397 return ORIGINAL_ALLOCATE_ERROR_MSG;
398 }
399};
400
412class sysError final : public error
413{
414public:
415 #define ORIGINAL_SYS_ERROR_MSG "A system error triggered"
416
417 explicit sysError(std::string msg = "")
418 : error(std::move(msg)) {}
419
420 std::string className() const override{
421 return "sysError";
422 }
423
424 std::string defaultMsg() const override {
425 return ORIGINAL_SYS_ERROR_MSG;
426 }
427};
428
429} // namespace original
430
431// ----------------- Definitions of error.h -----------------
432
433template<typename Callback, typename Ret_TYPE, typename... Args_TYPE>
435 if constexpr (constexpr bool is_valid = std::is_invocable_v<Callback, Args_TYPE...>;
436 !is_valid){
438 } else{
439 using result_type = std::invoke_result_t<Callback, Args_TYPE...>;
440 if constexpr (constexpr bool is_valid_return_type = std::is_same_v<result_type, Ret_TYPE>;
443 }
444}
445
446template <const bool TRIGGERING_CONDITION>
448{
449public:
450 static void asserts(){
451 static_assert(!TRIGGERING_CONDITION, ORIGINAL_ERROR_MSG);
452 }
453};
454
455template <const bool TRIGGERING_CONDITION>
457{
458public:
459 static void asserts(){
460 static_assert(!TRIGGERING_CONDITION, ORIGINAL_OUT_OF_BOUND_ERROR_MSG);
461 }
462};
463
464template <const bool TRIGGERING_CONDITION>
466{
467public:
468 static void asserts(){
469 static_assert(!TRIGGERING_CONDITION, ORIGINAL_VALUE_ERROR_ERROR_MSG);
470 }
471};
472
473template <const bool TRIGGERING_CONDITION>
475{
476public:
477 static void asserts(){
478 static_assert(!TRIGGERING_CONDITION, ORIGINAL_NULL_POINTER_ERROR_MSG);
479 }
480};
481
482template <const bool TRIGGERING_CONDITION>
484{
485public:
486 static void asserts(){
487 static_assert(!TRIGGERING_CONDITION, ORIGINAL_UNSUPPORTED_METHOD_ERROR_MSG);
488 }
489};
490
491template <const bool TRIGGERING_CONDITION>
493{
494public:
495 static void asserts(){
496 static_assert(!TRIGGERING_CONDITION, ORIGINAL_NO_ELEMENT_ERROR_MSG);
497 }
498};
499
500template <const bool TRIGGERING_CONDITION>
502{
503public:
504 static void asserts(){
505 static_assert(!TRIGGERING_CONDITION, ORIGINAL_CALLBACK_SIGNATURE_ERROR_MSG);
506 }
507};
508
509template <const bool TRIGGERING_CONDITION>
511{
512public:
513 static void asserts(){
514 static_assert(!TRIGGERING_CONDITION, ORIGINAL_CALLBACK_RETURN_TYPE_ERROR_MSG);
515 }
516};
517
518template <const bool TRIGGERING_CONDITION>
520{
521public:
522 static void asserts(){
523 static_assert(!TRIGGERING_CONDITION, ORIGINAL_ALLOCATE_ERROR_MSG);
524 }
525};
526
527template <const bool TRIGGERING_CONDITION>
529{
530public:
531 static void asserts(){
532 static_assert(!TRIGGERING_CONDITION, ORIGINAL_SYS_ERROR_MSG);
533 }
534};
535
536#endif // ERROR_H
Exception for memory allocation failures.
Definition error.h:385
std::string className() const override
Gets the class name for type identification.
Definition error.h:392
std::string defaultMsg() const override
Provides default message when no custom message is supplied.
Definition error.h:396
Static utility for validating callback signatures.
Definition error.h:55
static void check()
Validates a callback's signature and return type.
Definition error.h:434
Exception for callback return type mismatch.
Definition error.h:350
std::string defaultMsg() const override
Provides default message when no custom message is supplied.
Definition error.h:361
std::string className() const override
Gets the class name for type identification.
Definition error.h:357
Exception for callback argument mismatch.
Definition error.h:324
std::string className() const override
Gets the class name for type identification.
Definition error.h:331
std::string defaultMsg() const override
Provides default message when no custom message is supplied.
Definition error.h:335
Base class for all exceptions in the Original project.
Definition error.h:92
error(std::string msg)
Constructs an exception with an optional message.
Definition error.h:109
const char * what() const noexcept override
Returns the full error message.
Definition error.h:138
std::string className() const override
Returns the class name as string.
Definition error.h:115
virtual std::string defaultMsg() const
Provides default message when no custom message is supplied.
Definition error.h:101
std::string message() const noexcept
Generates formatted error message.
Definition error.h:123
Exception for missing element requests.
Definition error.h:298
std::string className() const override
Returns the class name as string.
Definition error.h:305
std::string defaultMsg() const override
Provides default message when no custom message is supplied.
Definition error.h:309
Exception for null pointer dereference attempts.
Definition error.h:245
std::string defaultMsg() const override
Provides default message when no custom message is supplied.
Definition error.h:256
std::string className() const override
Returns the class name as string.
Definition error.h:252
Exception for container index out-of-range errors.
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
Unique ownership smart pointer with move semantics.
Definition ownerPtr.h:37
Base class providing polymorphic string conversion capabilities.
Definition printable.h:39
Compile-time error triggering utility.
Definition error.h:167
static void asserts()
Triggers static assertion if the condition is true.
Definition error.h:174
Exception for generic system failure.
Definition error.h:413
std::string className() const override
Returns the class name as string.
Definition error.h:420
std::string defaultMsg() const override
Provides default message when no custom message is supplied.
Definition error.h:424
Exception for unimplemented method calls.
Definition error.h:271
std::string defaultMsg() const override
Provides default message when no custom message is supplied.
Definition error.h:282
std::string className() const override
Returns the class name as string.
Definition error.h:278
Exception for invalid parameter values.
Definition error.h:219
std::string className() const override
Returns the class name as string.
Definition error.h:226
std::string defaultMsg() const override
Provides default message when no custom message is supplied.
Definition error.h:230
Main namespace for the project Original.
Definition algorithms.h:21
Standard namespace extensions for original::alternative.
Definition allocator.h:351
Interface for polymorphic string formatting and output.
Core type system foundations and concept definitions.