Aleph-w 7.10.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
127# ifndef AH_ERRORS_H
128# define AH_ERRORS_H
129
130# include <stdexcept>
131# include <sstream>
132# include <type_traits>
133
134// The functions that build the stream of a message and that throw are cold
135// and out of line, so that the functions that check stay small. MSVC does
136// not take the attributes of GCC, which Clang takes also as clang-cl
137# if defined(__GNUC__) || defined(__clang__)
138# define AH_ERROR_COLD [[gnu::cold, gnu::noinline]]
139# elif defined(_MSC_VER)
140# define AH_ERROR_COLD __declspec(noinline)
141# else
142# define AH_ERROR_COLD
143# endif
144
145namespace Aleph
146{
147
151 namespace error_detail
152 {
171 {
172 std::ostringstream * stream_ = nullptr;
173
174 public:
181 AH_ERROR_COLD Message_Stream(const char * file, const int line)
182 : stream_(new std::ostringstream)
183 {
184 try
185 {
186 *stream_ << "(" << file << ":" << line << ") | ";
187 }
188 catch (...)
189 {
190 delete stream_;
191 throw;
192 }
193 }
194
197
200
203
207 [[nodiscard]] std::ostringstream && stream() noexcept
208 {
209 return static_cast<std::ostringstream &&>(*stream_);
210 }
211 };
212 }
213
224 template<class E>
226 {
243 [[noreturn]] AH_ERROR_COLD int operator<<=(const std::ostream &s)
244 {
245 if (const auto * oss = dynamic_cast<const std::ostringstream *>(&s))
246 throw E(oss->str());
247
248 if (const auto * ss = dynamic_cast<const std::stringstream *>(&s))
249 throw E(ss->str());
250
251 throw E("Unknown stream type in Aleph::ExceptionBuilder");
252 }
253 };
254
255}
256
257// ============================================================================
258// WARNING MACROS
259// ============================================================================
260
261// The variable of the loop of a warning has the line of the call in its
262// name, so that it does not hide a name of the caller in C or in out
263#define AH_ERRORS_JOIN_(a, b) a ## b
264#define AH_ERRORS_JOIN(a, b) AH_ERRORS_JOIN_(a, b)
265#define AH_WARNING_ONCE AH_ERRORS_JOIN(ah_warning_once_, __LINE__)
266
267// The statement of the while of a macro that throws is unlikely. Clang warns
268// that [[unlikely]] has no effect on a loop whose condition is constant, as
269// ah_domain_error_if(true), and it gives the same code without it
270# if defined(__clang__)
271# define AH_ERROR_UNLIKELY
272# else
273# define AH_ERROR_UNLIKELY [[unlikely]]
274# endif
275
288#define ah_warning_unless(out, C) \
289 for (bool AH_WARNING_ONCE = not (C); AH_WARNING_ONCE; \
290 AH_WARNING_ONCE = false) \
291 [[unlikely]](out) << "WARNING (" << __FILE__ << ":" << __LINE__ << ") | "
292
305#define ah_warning_if(out, C) \
306 for (bool AH_WARNING_ONCE = (C) ? true : false; AH_WARNING_ONCE; \
307 AH_WARNING_ONCE = false) \
308 [[unlikely]](out) << "WARNING (" << __FILE__ << ":" << __LINE__ << ") | "
309
321#define ah_warning(out) \
322 (out) << "WARNING (" << __FILE__ << ":" << __LINE__ << ") | "
323
324// ============================================================================
325// EXCEPTION MACROS: RANGE_ERROR
326// ============================================================================
327
340#define ah_range_error_unless(C) \
341 while (not (C)) AH_ERROR_UNLIKELY \
342 Aleph::ExceptionBuilder<std::range_error>() <<= \
343 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
344
356#define ah_range_error_if(C) \
357 while ((C) ? true : false) AH_ERROR_UNLIKELY \
358 Aleph::ExceptionBuilder<std::range_error>() <<= \
359 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
360
372#define ah_range_error() \
373 Aleph::ExceptionBuilder<std::range_error>() <<= \
374 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
375
376#define ah_range_error_if_constexpr(C) \
377 while (std::bool_constant<(C) ? true : false>::value) \
378 ah_range_error()
379
380#define ah_range_error_unless_constexpr(C) \
381 while (std::bool_constant<not (C)>::value) \
382 ah_range_error()
383
384// ============================================================================
385// EXCEPTION MACROS: RUNTIME_ERROR
386// ============================================================================
387
399#define ah_runtime_error_unless(C) \
400 while (not (C)) AH_ERROR_UNLIKELY \
401 Aleph::ExceptionBuilder<std::runtime_error>() <<= \
402 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
403
415#define ah_runtime_error_if(C) \
416 while ((C) ? true : false) AH_ERROR_UNLIKELY \
417 Aleph::ExceptionBuilder<std::runtime_error>() <<= \
418 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
419
431#define ah_runtime_error() \
432 Aleph::ExceptionBuilder<std::runtime_error>() <<= \
433 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
434
435#define ah_runtime_error_if_constexpr(C) \
436 while (std::bool_constant<(C) ? true : false>::value) \
437 ah_runtime_error()
438
439#define ah_runtime_error_unless_constexpr(C) \
440 while (std::bool_constant<not (C)>::value) \
441 ah_runtime_error()
442
443// ============================================================================
444// EXCEPTION MACROS: LOGIC_ERROR
445// ============================================================================
446
458#define ah_logic_error_unless(C) \
459 while (not (C)) AH_ERROR_UNLIKELY \
460 Aleph::ExceptionBuilder<std::logic_error>() <<= \
461 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
462
474#define ah_logic_error_if(C) \
475 while ((C) ? true : false) AH_ERROR_UNLIKELY \
476 Aleph::ExceptionBuilder<std::logic_error>() <<= \
477 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
478
490#define ah_logic_error() \
491 Aleph::ExceptionBuilder<std::logic_error>() <<= \
492 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
493
494#define ah_logic_error_if_constexpr(C) \
495 while (std::bool_constant<(C) ? true : false>::value) \
496 ah_logic_error()
497
498#define ah_logic_error_unless_constexpr(C) \
499 while (std::bool_constant<not (C)>::value) \
500 ah_logic_error()
501
502// ============================================================================
503// EXCEPTION MACROS: UNDERFLOW_ERROR
504// ============================================================================
505
517#define ah_underflow_error_if(C) \
518 while ((C) ? true : false) AH_ERROR_UNLIKELY \
519 Aleph::ExceptionBuilder<std::underflow_error>() <<= \
520 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
521
533#define ah_underflow_error_unless(C) \
534 while (not (C)) AH_ERROR_UNLIKELY \
535 Aleph::ExceptionBuilder<std::underflow_error>() <<= \
536 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
537
549#define ah_underflow_error() \
550 Aleph::ExceptionBuilder<std::underflow_error>() <<= \
551 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
552
553#define ah_underflow_error_if_constexpr(C) \
554 while (std::bool_constant<(C) ? true : false>::value) \
555 ah_underflow_error()
556
557#define ah_underflow_error_unless_constexpr(C) \
558 while (std::bool_constant<not (C)>::value) \
559 ah_underflow_error()
560
561// ============================================================================
562// EXCEPTION MACROS: BAD_ALLOC (NO MESSAGE)
563// ============================================================================
564
570#define ah_bad_alloc() \
571 do { [[unlikely]] throw std::bad_alloc(); } while (false)
572
578#define ah_bad_alloc_if(C) \
579 do { if ((C) ? true : false) [[unlikely]] throw std::bad_alloc(); } \
580 while (false)
581
587#define ah_bad_alloc_unless(C) \
588 do { if (!(C)) [[unlikely]] throw std::bad_alloc(); } while (false)
589
590#define ah_bad_alloc_if_constexpr(C) \
591 do { if constexpr (C) throw std::bad_alloc(); } while (false)
592
593#define ah_bad_alloc_unless_constexpr(C) \
594 do { if constexpr (not (C)) throw std::bad_alloc(); } while (false)
595
596// ============================================================================
597// EXCEPTION MACROS: OVERFLOW_ERROR
598// ============================================================================
599
611#define ah_overflow_error_if(C) \
612 while ((C) ? true : false) AH_ERROR_UNLIKELY \
613 Aleph::ExceptionBuilder<std::overflow_error>() <<= \
614 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
615
627#define ah_overflow_error_unless(C) \
628 while (not (C)) AH_ERROR_UNLIKELY \
629 Aleph::ExceptionBuilder<std::overflow_error>() <<= \
630 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
631
643#define ah_overflow_error() \
644 Aleph::ExceptionBuilder<std::overflow_error>() <<= \
645 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
646
647#define ah_overflow_error_if_constexpr(C) \
648 while (std::bool_constant<(C) ? true : false>::value) \
649 ah_overflow_error()
650
651#define ah_overflow_error_unless_constexpr(C) \
652 while (std::bool_constant<not (C)>::value) \
653 ah_overflow_error()
654
655// ============================================================================
656// EXCEPTION MACROS: DOMAIN_ERROR
657// ============================================================================
658
670#define ah_domain_error_if(C) \
671 while ((C) ? true : false) AH_ERROR_UNLIKELY \
672 Aleph::ExceptionBuilder<std::domain_error>() <<= \
673 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
674
686#define ah_domain_error_unless(C) \
687 while (not (C)) AH_ERROR_UNLIKELY \
688 Aleph::ExceptionBuilder<std::domain_error>() <<= \
689 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
690
702#define ah_domain_error() \
703 Aleph::ExceptionBuilder<std::domain_error>() <<= \
704 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
705#define ah_domain_error_if_constexpr(C) \
706 while (std::bool_constant<(C) ? true : false>::value) \
707 ah_domain_error()
708#define ah_domain_error_unless_constexpr(C) \
709 while (std::bool_constant<not (C)>::value) \
710 ah_domain_error()
711
712// ============================================================================
713// EXCEPTION MACROS: OUT_OF_RANGE
714// ============================================================================
715
727#define ah_out_of_range_error_if(C) \
728 while ((C) ? true : false) AH_ERROR_UNLIKELY \
729 Aleph::ExceptionBuilder<std::out_of_range>() <<= \
730 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
731
743#define ah_out_of_range_error_unless(C) \
744 while (not (C)) AH_ERROR_UNLIKELY \
745 Aleph::ExceptionBuilder<std::out_of_range>() <<= \
746 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
747
759#define ah_out_of_range_error() \
760 Aleph::ExceptionBuilder<std::out_of_range>() <<= \
761 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
762
763#define ah_out_of_range_error_if_constexpr(C) \
764 while (std::bool_constant<(C) ? true : false>::value) \
765 ah_out_of_range_error()
766
767#define ah_out_of_range_error_unless_constexpr(C) \
768 while (std::bool_constant<not (C)>::value) \
769 ah_out_of_range_error()
770
771// ============================================================================
772// EXCEPTION MACROS: INVALID_ARGUMENT
773// ============================================================================
774
787#define ah_invalid_argument_if(C) \
788 while ((C) ? true : false) AH_ERROR_UNLIKELY \
789 Aleph::ExceptionBuilder<std::invalid_argument>() <<= \
790 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
791
803#define ah_invalid_argument_unless(C) \
804 while (not (C)) AH_ERROR_UNLIKELY \
805 Aleph::ExceptionBuilder<std::invalid_argument>() <<= \
806 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
807
819#define ah_invalid_argument() \
820 Aleph::ExceptionBuilder<std::invalid_argument>() <<= \
821 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
822
823#define ah_invalid_argument_if_constexpr(C) \
824 while (std::bool_constant<(C) ? true : false>::value) \
825 ah_invalid_argument()
826
827#define ah_invalid_argument_unless_constexpr(C) \
828 while (std::bool_constant<not (C)>::value) \
829 ah_invalid_argument()
830
831// ============================================================================
832// EXCEPTION MACROS: LENGTH_ERROR
833// ============================================================================
834
846#define ah_length_error_if(C) \
847 while ((C) ? true : false) AH_ERROR_UNLIKELY \
848 Aleph::ExceptionBuilder<std::length_error>() <<= \
849 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
850
862#define ah_length_error_unless(C) \
863 while (not (C)) AH_ERROR_UNLIKELY \
864 Aleph::ExceptionBuilder<std::length_error>() <<= \
865 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
866
878#define ah_length_error() \
879 Aleph::ExceptionBuilder<std::length_error>() <<= \
880 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
881
882#define ah_length_error_if_constexpr(C) \
883 while (std::bool_constant<(C) ? true : false>::value) \
884 ah_length_error()
885
886#define ah_length_error_unless_constexpr(C) \
887 while (std::bool_constant<not (C)>::value) \
888 ah_length_error()
889
890// ============================================================================
891// FATAL ERROR MACRO
892// ============================================================================
893
907#define ah_fatal_error() \
908 Aleph::ExceptionBuilder<std::runtime_error>() <<= \
909 Aleph::error_detail::Message_Stream(__FILE__, __LINE__).stream()
910
911# endif
#define AH_ERROR_COLD
Definition ah-errors.H:142
The stream of the message of a macro that throws.
Definition ah-errors.H:171
std::ostringstream && stream() noexcept
The stream, as the temporary stream of a macro was.
Definition ah-errors.H:207
Message_Stream(const char *file, const int line)
Makes the stream and writes in it the place of the error.
Definition ah-errors.H:181
Message_Stream & operator=(const Message_Stream &)=delete
A message has one stream: it is not assigned.
~Message_Stream()
Frees the stream.
Definition ah-errors.H:196
Message_Stream(const Message_Stream &)=delete
A message has one stream: it is not copied.
Main namespace for Aleph-w library functions.
STL namespace.
Exception constructor with formatted message.
Definition ah-errors.H:226
int operator<<=(const std::ostream &s)
Operator that throws the exception with the constructed message.
Definition ah-errors.H:243
ofstream file
Definition writeRb.C:51