Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-errors.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
86# ifndef AH_ERRORS_H
87# define AH_ERRORS_H
88
89# include <stdexcept>
90# include <sstream>
91
92namespace Aleph
93{
94
105 template<class E>
107 {
116 int operator,(const std::ostream &s)
117 {
118 throw E(static_cast<const std::ostringstream &>(s).str());
119 return 0;
120 }
121 };
122
123}
124
125// ============================================================================
126// WARNING MACROS
127// ============================================================================
128
141#define ah_warning_unless(out, C) \
142 if (not (C)) \
143 [[unlikely]](out) << "WARNING (" << __FILE__ << ":" << __LINE__ << ") | "
144
157#define ah_warning_if(out, C) \
158 if (C) \
159 [[unlikely]](out) << "WARNING (" << __FILE__ << ":" << __LINE__ << ") | "
160
172#define ah_warning(out) \
173 (out) << "WARNING (" << __FILE__ << ":" << __LINE__ << ") | "
174
175// ============================================================================
176// EXCEPTION MACROS: RANGE_ERROR
177// ============================================================================
178
191#define ah_range_error_unless(C) \
192 if (not (C)) \
193 [[unlikely]]Aleph::ExceptionBuilder<std::range_error>(), \
194 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
195
207#define ah_range_error_if(C) \
208 if (C) \
209 [[unlikely]]Aleph::ExceptionBuilder<std::range_error>(), \
210 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
211
223#define ah_range_error() \
224 Aleph::ExceptionBuilder<std::range_error>(), \
225 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
226
227#define ah_range_error_if_constexpr(C) \
228 if constexpr (C) \
229 ah_range_error()
230
231#define ah_range_error_unless_constexpr(C) \
232 if constexpr (not (C)) \
233 ah_range_error()
234
235// ============================================================================
236// EXCEPTION MACROS: RUNTIME_ERROR
237// ============================================================================
238
250#define ah_runtime_error_unless(C) \
251 if (not (C)) \
252 [[unlikely]]Aleph::ExceptionBuilder<std::runtime_error>(), \
253 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
254
266#define ah_runtime_error_if(C) \
267 if (C) \
268 [[unlikely]]Aleph::ExceptionBuilder<std::runtime_error>(), \
269 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
270
282#define ah_runtime_error() \
283 Aleph::ExceptionBuilder<std::runtime_error>(), \
284 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
285
286#define ah_runtime_error_if_constexpr(C) \
287 if constexpr (C) \
288 ah_runtime_error()
289
290#define ah_runtime_error_unless_constexpr(C) \
291 if constexpr (not (C)) \
292 ah_runtime_error()
293
294// ============================================================================
295// EXCEPTION MACROS: LOGIC_ERROR
296// ============================================================================
297
309#define ah_logic_error_unless(C) \
310 if (not (C)) \
311 [[unlikely]]Aleph::ExceptionBuilder<std::logic_error>(), \
312 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
313
325#define ah_logic_error_if(C) \
326 if (C) \
327 [[unlikely]]Aleph::ExceptionBuilder<std::logic_error>(), \
328 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
329
341#define ah_logic_error() \
342 Aleph::ExceptionBuilder<std::logic_error>(), \
343 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
344
345#define ah_logic_error_if_constexpr(C) \
346 if constexpr (C) \
347 ah_logic_error()
348
349#define ah_logic_error_unless_constexpr(C) \
350 if constexpr (not (C)) \
351 ah_logic_error()
352
353// ============================================================================
354// EXCEPTION MACROS: UNDERFLOW_ERROR
355// ============================================================================
356
368#define ah_underflow_error_if(C) \
369 if (C) \
370 [[unlikely]]Aleph::ExceptionBuilder<std::underflow_error>(), \
371 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
372
384#define ah_underflow_error_unless(C) \
385 if (not (C)) \
386 [[unlikely]]Aleph::ExceptionBuilder<std::underflow_error>(), \
387 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
388
400#define ah_underflow_error() \
401 Aleph::ExceptionBuilder<std::underflow_error>(), \
402 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
403
404#define ah_underflow_error_if_constexpr(C) \
405 if constexpr (C) \
406 ah_underflow_error()
407
408#define ah_underflow_error_unless_constexpr(C) \
409 if constexpr (not (C)) \
410 ah_underflow_error()
411
412// ============================================================================
413// EXCEPTION MACROS: BAD_ALLOC (NO MESSAGE)
414// ============================================================================
415
421#define ah_bad_alloc() \
422 do { [[unlikely]] throw std::bad_alloc(); } while (false)
423
429#define ah_bad_alloc_if(C) \
430 do { if (C) [[unlikely]] throw std::bad_alloc(); } while (false)
431
437#define ah_bad_alloc_unless(C) \
438 do { if (!(C)) [[unlikely]] throw std::bad_alloc(); } while (false)
439
440#define ah_bad_alloc_if_constexpr(C) \
441 if constexpr (C) \
442 ah_bad_alloc()
443
444#define ah_bad_alloc_unless_constexpr(C) \
445 if constexpr (not (C)) \
446 ah_bad_alloc()
447
448// ============================================================================
449// EXCEPTION MACROS: OVERFLOW_ERROR
450// ============================================================================
451
463#define ah_overflow_error_if(C) \
464 if (C) \
465 [[unlikely]]Aleph::ExceptionBuilder<std::overflow_error>(), \
466 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
467
479#define ah_overflow_error_unless(C) \
480 if (not (C)) \
481 [[unlikely]]Aleph::ExceptionBuilder<std::overflow_error>(), \
482 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
483
495#define ah_overflow_error() \
496 Aleph::ExceptionBuilder<std::overflow_error>(), \
497 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
498
499#define ah_overflow_error_if_constexpr(C) \
500 if constexpr (C) \
501 ah_overflow_error()
502
503#define ah_overflow_error_unless_constexpr(C) \
504 if constexpr (not (C)) \
505 ah_overflow_error()
506
507// ============================================================================
508// EXCEPTION MACROS: DOMAIN_ERROR
509// ============================================================================
510
522#define ah_domain_error_if(C) \
523 if (C) \
524 [[unlikely]]Aleph::ExceptionBuilder<std::domain_error>(), \
525 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
526
538#define ah_domain_error_unless(C) \
539 if (not (C)) \
540 [[unlikely]]Aleph::ExceptionBuilder<std::domain_error>(), \
541 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
542
554#define ah_domain_error() \
555 Aleph::ExceptionBuilder<std::domain_error>(), \
556 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
557#define ah_domain_error_if_constexpr(C) \
558 if constexpr (C) \
559 ah_domain_error()
560#define ah_domain_error_unless_constexpr(C) \
561 if constexpr (not (C)) \
562 ah_domain_error()
563
564// ============================================================================
565// EXCEPTION MACROS: OUT_OF_RANGE
566// ============================================================================
567
579#define ah_out_of_range_error_if(C) \
580 if (C) \
581 [[unlikely]]Aleph::ExceptionBuilder<std::out_of_range>(), \
582 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
583
595#define ah_out_of_range_error_unless(C) \
596 if (not (C)) \
597 [[unlikely]]Aleph::ExceptionBuilder<std::out_of_range>(), \
598 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
599
611#define ah_out_of_range_error() \
612 Aleph::ExceptionBuilder<std::out_of_range>(), \
613 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
614
615#define ah_out_of_range_error_if_constexpr(C) \
616 if constexpr (C) \
617 ah_out_of_range_error()
618
619#define ah_out_of_range_error_unless_constexpr(C) \
620 if constexpr (not (C)) \
621 ah_out_of_range_error()
622
623// ============================================================================
624// EXCEPTION MACROS: INVALID_ARGUMENT
625// ============================================================================
626
639#define ah_invalid_argument_if(C) \
640 if (C) \
641 [[unlikely]]Aleph::ExceptionBuilder<std::invalid_argument>(), \
642 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
643
655#define ah_invalid_argument_unless(C) \
656 if (not (C)) \
657 [[unlikely]]Aleph::ExceptionBuilder<std::invalid_argument>(), \
658 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
659
671#define ah_invalid_argument() \
672 Aleph::ExceptionBuilder<std::invalid_argument>(), \
673 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
674
675#define ah_invalid_argument_if_constexpr(C) \
676 if constexpr (C) \
677 ah_invalid_argument()
678
679#define ah_invalid_argument_unless_constexpr(C) \
680 if constexpr (not (C)) \
681 ah_invalid_argument()
682
683// ============================================================================
684// EXCEPTION MACROS: LENGTH_ERROR
685// ============================================================================
686
698#define ah_length_error_if(C) \
699 if (C) \
700 [[unlikely]]Aleph::ExceptionBuilder<std::length_error>(), \
701 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
702
714#define ah_length_error_unless(C) \
715 if (not (C)) \
716 [[unlikely]]Aleph::ExceptionBuilder<std::length_error>(), \
717 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
718
730#define ah_length_error() \
731 Aleph::ExceptionBuilder<std::length_error>(), \
732 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
733
734#define ah_length_error_if_constexpr(C) \
735 if constexpr (C) \
736 ah_length_error()
737
738#define ah_length_error_unless_constexpr(C) \
739 if constexpr (not (C)) \
740 ah_length_error()
741
742// ============================================================================
743// FATAL ERROR MACRO
744// ============================================================================
745
759#define ah_fatal_error() \
760 Aleph::ExceptionBuilder<std::runtime_error>(), \
761 std::stringstream() << "(" << __FILE__ << ":" << __LINE__ << ") | "
762
763# endif
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Exception constructor with formatted message.
Definition ah-errors.H:107
int operator,(const std::ostream &s)
Comma operator that throws the exception with the constructed message.
Definition ah-errors.H:116