-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy paththrow.cc
52 lines (42 loc) · 1.5 KB
/
throw.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// -*- mode: c++; coding: utf-8 -*-
// ra-ra/examples - Customize ra:: reaction to errors.
// (c) Daniel Llorens - 2019-2024
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option) any
// later version.
#ifdef RA_DO_CHECK
#undef RA_DO_CHECK
#endif
#define RA_DO_CHECK 1 // kind of the point here
// "ra/base.hh" doesn't depend directly on RA_ASSERT, so the following override is able to use ra::format.
#include "ra/base.hh"
#include <exception>
struct ra_error: public std::exception
{
std::string s;
template <class ... A> ra_error(A && ... a): s(ra::format(std::forward<A>(a) ...)) {}
virtual char const * what() const throw () { return s.c_str(); }
};
#define RA_ASSERT( cond, ... ) \
{ if (!( cond )) [[unlikely]] throw ra_error("ra::", std::source_location::current(), " (" STRINGIZE(cond) ") " __VA_OPT__(,) __VA_ARGS__); }
// The override will be in effect for the rest of ra::.
#include "ra/ra.hh"
#include "ra/test.hh"
#include <iostream>
using std::cout, std::endl, ra::TestRecorder;
int main()
{
TestRecorder tr(cout);
bool yes = false;
ra::Big<int> a({2, 3}, 9);
std::string msg;
try {
cout << a(2, 3) << endl;
} catch (ra_error & e) {
msg = e.what();
yes = true;
}
tr.info(msg).test(yes);
return tr.summary();
}