libart-paperback 0.1.0-a.1.20260122225059.e5fea306241a
PDF (1.4) library for C++
except.hxx
1#ifndef art__paperback__exception_hxx_
2#define art__paperback__exception_hxx_
3
4#include <art/paperback/types.hxx>
5
6#include <source_location>
7
8namespace Art::Paperback
9{
10
11 /// Base class for errors.
12 ///
13 class Fault
14 : public runtime_error
15 {
16 public:
17 /// Constructor.
18 ///
19 /// \param origin The C++ source origin of the exception.
20 /// \param what A description of the error.
21 ///
22 Fault(std::source_location origin, string what)
23 : runtime_error{std::move(what)},
24 _origin{std::move(origin)}
25 {}
26
27 /// Access the C++ source origin of the exception.
28 ///
29 std::source_location const&
30 origin() const;
31
32 private:
33 std::source_location _origin;
34
35 };
36
37 /// Exception class used to indicate internal errors, typically the result
38 /// of an insect hiding somewhere. If found, please be so kind and squash it
39 /// mercilessly.
40 ///
42 : public Fault
43 {
44 public:
45 using Fault::Fault;
46
47 };
48
49 /// Exception class used to indicate too low document version.
50 ///
52 : public Fault
53 {
54 public:
55 using Fault::Fault;
56
57 };
58
59 /// Exception class used to indicate an invalid operation.
60 ///
62 : public Fault
63 {
64 public:
65 using Fault::Fault;
66
67 };
68
69 /// Helper to throw exceptions.
70 ///
71 template<typename T>
72 class raise
73 {
74 public:
75 raise(std::source_location origin = std::source_location::current())
76 : _origin{origin}
77 {}
78
79 /// Copy-construction is prohibited.
80 ///
81 raise(raise const&) = delete;
82
83 /// Move-construction is prohibited.
84 ///
85 raise(raise&&) = delete;
86
87 [[noreturn]]
88 ~raise() noexcept(false)
89 {
90 throw T{_origin, _str.str()};
91 }
92
93 /// Copy-assignment is prohibited.
94 ///
95 raise& operator=(raise const&) = delete;
96
97 /// Move-assignment is prohibited.
98 ///
99 raise& operator=(raise&&) = delete;
100
101 template<typename U>
102 raise<T>&
103 operator<<(U const& other)
104 {
105 _str << other;
106 return *this;
107 }
108
109 private:
110 std::source_location _origin;
111 stringstream _str;
112
113 };
114
115} // namespace Art::Paperback
116
117#endif
std::source_location const & origin() const
Access the C++ source origin of the exception.
Fault(std::source_location origin, string what)
Constructor.
Definition except.hxx:22
Exception class used to indicate internal errors, typically the result of an insect hiding somewhere.
Definition except.hxx:43
Fault(std::source_location origin, string what)
Constructor.
Definition except.hxx:22
Exception class used to indicate an invalid operation.
Definition except.hxx:63
Fault(std::source_location origin, string what)
Constructor.
Definition except.hxx:22
Exception class used to indicate too low document version.
Definition except.hxx:53
Fault(std::source_location origin, string what)
Constructor.
Definition except.hxx:22
raise & operator=(raise &&)=delete
Move-assignment is prohibited.
raise & operator=(raise const &)=delete
Copy-assignment is prohibited.
raise(raise const &)=delete
Copy-construction is prohibited.
raise(raise &&)=delete
Move-construction is prohibited.
Primary paperback namespace.
Definition array.cxx:6