Elementa v8.0.0
Minimalistic library for any C++ application (C++11 and up)
Loading...
Searching...
No Matches
exceptions.h
Go to the documentation of this file.
1
3#include "elementa/license.inc"
4#include "elementa/checks.inc"
5
6#ifndef ELEMENTA_BASE_EXCEPTIONS_H
7#define ELEMENTA_BASE_EXCEPTIONS_H
8
9#include <exception>
10#include <string>
11#include <sstream>
12#include <iomanip>
14
15namespace elementa
16{
17
18namespace base
19{
20
53/* ****************************************************************************
54
55 Macro: ELE_CODE_EXCOVERRIDE
56 Class: Exc
57
58******************************************************************************/
59
61
64#define ELE_CLASS_EXCOVERRIDE(C) C & asERR(const std::string & contxt) noexcept\
65 override \
66 { elementa::base::Exc::asERR(contxt); \
67 return(*this); } \
68 C & asEXC(const std::string & place, \
69 const elementa::base::RTTextWithEnum::\
70 Combination & fl =\
71 {elementa::base::RTTextWith::kAll_}) \
72 noexcept override\
73 { elementa::base::Exc::asEXC(place,fl); \
74 return(*this); }
75
77
102
104
112class Exc: public std::runtime_error
113{
114 public:
115
119 using StdBase = std::runtime_error;
120
128 Exc(const std::string & expl):StdBase{expl},iserr_{false} {}
129
130 Exc(const Exc &) = default;
131 Exc(Exc &&) = default;
132 Exc & operator=(const Exc &) = default;
133 Exc & operator=(Exc &&) = default;
134
135 virtual ~Exc(void) = default;
136
144 virtual Exc & asERR(const std::string & context) noexcept;
145
147
148 virtual Exc & asEXC(const std::string & place,
149 const RTTextWithEnum::Combination & flags =
150 {RTTextWith::kAll_}) noexcept;
151
159
161 const char * what(void) const noexcept override { return(msg_.c_str()); }
162
164 const char * explanation(void) const noexcept { return(StdBase::what()); }
165
167 const char * context(void) const noexcept { return(context_.c_str()); }
168
171 protected:
172
173 std::string msg_;
174
175 private:
176
177 bool iserr_;
178 std::string context_;
179};
180
181
182
183/* ****************************************************************************
184
185 Classes: unimplemented, todo, notreach, notfound,
186 invalid_state, internal_error
187
188******************************************************************************/
189
191
197class unimplemented: public Exc
198{
199 public:
200
201 unimplemented(const std::string & explanation): Exc{explanation} {}
202
204};
205
207
209class todo: public Exc
210{
211 public:
212
213 todo(const std::string & explanation): Exc{"TODO: " + explanation} {}
214
216};
217
219class notreach: public Exc
220{
221 public:
222
223 notreach(const std::string & explanation): Exc{explanation} {}
224
226};
227
229class invstate: public Exc
230{
231 public:
232
233 invstate(const std::string & expl): Exc{expl} {}
234
236};
237
239class internal_error: public Exc
240{
241 public:
242
243 internal_error(const std::string & expl): Exc{expl} {}
244
246};
247
249class notfound: public Exc
250{
251 public:
252
253 notfound(const std::string & expl): Exc{expl} {}
254
256};
257
259class invarg: public Exc
260{
261 public:
262
263 invarg(const std::string & expl): Exc{expl} {}
264
266};
267
269class outofrange: public Exc
270{
271 public:
272
273 outofrange(const std::string & expl): Exc{expl} {}
274
276};
277
278
279
280/* ****************************************************************************
281
282 Macros: ELE_CODE_UNIMPLEMENTED, ELE_CODE_TODO, ELE_CODE_NOTREACH,
283 ELE_CODE_INVARG, ELE_CODE_INTERNALERR, ELE_CODE_INVSTATE,
284 ELE_CODE_OUTOFRANGE
285
286******************************************************************************/
287
289#define ELE_CODE_UNIMPLEMENTED throw(elementa::base::unimplemented{\
290 "Unimplemented"}.asEXC(ELE_CODE_PLACE))
291
293#define ELE_CODE_TODO(expl) throw(elementa::base::todo{expl}.asEXC(\
294 ELE_CODE_PLACE))
295
297#define ELE_CODE_NOTREACH throw(elementa::base::notreach{\
298 "Should not reach this point"}.asEXC(\
299 ELE_CODE_PLACE))
300
302#define ELE_CODE_INTERNALERR(expl) throw(elementa::base::internal_error{expl}.\
303 asEXC(ELE_CODE_PLACE))
304
306#define ELE_CODE_INVSTATE(expl) throw(elementa::base::invstate{expl}.\
307 asEXC(ELE_CODE_PLACE))
308
310#define ELE_CODE_INVARG(expl) throw(elementa::base::invarg{expl}.\
311 asEXC(ELE_CODE_PLACE))
312
314#define ELE_CODE_OUTOFRANGE(expl) throw(elementa::base::outofrange{expl}.\
315 asEXC(ELE_CODE_PLACE))
316
318#define ELE_CODE_NOTFOUND(expl) throw(elementa::base::notfound{expl}.\
319 asEXC(ELE_CODE_PLACE))
320
321
322 // Exceptions
324
325} // end namespace base
326
327} // end namespace elementa
328
329#endif
Exc(const std::string &expl)
Constructor from an explanation. To be thrown as EXC by default.
Definition: exceptions.h:128
const char * context(void) const noexcept
Return the place (EXC) or context (ERR) only. Will live as long as this.
Definition: exceptions.h:167
virtual Exc & asEXC(const std::string &place, const RTTextWithEnum::Combination &flags={RTTextWith::kAll_}) noexcept
Transform it to be thrown as EXC with the given place and flags.
const char * what(void) const noexcept override
Return the complete message. It will live as long as this exception.
Definition: exceptions.h:161
const char * explanation(void) const noexcept
Return the explanation only. It will live as long as this exception.
Definition: exceptions.h:164
virtual Exc & asERR(const std::string &context) noexcept
Transform it to be thrown as ERR with the given context.
Base class for all errors / exceptions in Elementa. Just derive from it.
Definition: exceptions.h:113
Exception for indicating some internal error not caused by user of object.
Definition: exceptions.h:240
Exception for indicating that some argument of a method/function is invalid.
Definition: exceptions.h:260
Exception for indicating that some object is in invalid state.
Definition: exceptions.h:230
Exception for indicating that some element has not been found in some place.
Definition: exceptions.h:250
Exception for indicating in runtime that a code should not be reached.
Definition: exceptions.h:220
Exception for indicating that some value is out of range.
Definition: exceptions.h:270
Exception for indicating in runtime that a section of code is TODO.
Definition: exceptions.h:210
Exception for indicating in runtime that a section of code is unimplemented.
Definition: exceptions.h:198
#define ELE_CLASS_EXCOVERRIDE(C)
Shortening macro that must be used inside classes derived from Exc.
Definition: exceptions.h:64