Line data Source code
1 : // Copyright (c) 2011-2021 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License
17 : // along with this program; if not, write to the Free Software
18 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 : #pragma once
20 :
21 : // self
22 : //
23 : #include <libexcept/stack_trace.h>
24 :
25 :
26 : // C++ includes
27 : //
28 : #include <stdexcept>
29 : #include <string>
30 : #include <list>
31 :
32 :
33 : /** \file
34 : * \brief Declarations of the exception library.
35 : *
36 : * This file includes the library exception declarations.
37 : *
38 : * The strong point of the library is its ability to gather a stack
39 : * trace and attach that information to an exception.
40 : */
41 :
42 :
43 : namespace libexcept
44 : {
45 :
46 :
47 :
48 : enum class collect_stack_t
49 : {
50 : COLLECT_STACK_NO, // no stack trace for exceptions
51 : COLLECT_STACK_YES, // plain stack trace (fast)
52 : COLLECT_STACK_COMPLETE, // include filenames & line numbers (slow)
53 : };
54 :
55 : collect_stack_t get_collect_stack();
56 : void set_collect_stack(collect_stack_t collect_stack);
57 :
58 :
59 : class exception_base_t
60 : {
61 : public:
62 : explicit exception_base_t(int const stack_trace_depth = STACK_TRACE_DEPTH);
63 :
64 22 : virtual ~exception_base_t() {}
65 :
66 22 : stack_trace_t const & get_stack_trace() const { return f_stack_trace; }
67 :
68 : private:
69 : stack_trace_t f_stack_trace = stack_trace_t();
70 : };
71 :
72 :
73 : class logic_exception_t
74 : : public std::logic_error
75 : , public exception_base_t
76 : {
77 : public:
78 : explicit logic_exception_t(std::string const & what, int const stack_trace_depth = STACK_TRACE_DEPTH);
79 : explicit logic_exception_t(char const * what, int const stack_trace_depth = STACK_TRACE_DEPTH);
80 :
81 8 : virtual ~logic_exception_t() override {}
82 :
83 : virtual char const * what() const throw() override;
84 : };
85 :
86 :
87 : class out_of_range_t
88 : : public std::out_of_range
89 : , public exception_base_t
90 : {
91 : public:
92 : explicit out_of_range_t(std::string const & what, int const stack_trace_depth = STACK_TRACE_DEPTH);
93 : explicit out_of_range_t(char const * what, int const stack_trace_depth = STACK_TRACE_DEPTH);
94 :
95 6 : virtual ~out_of_range_t() override {}
96 :
97 : virtual char const * what() const throw() override;
98 : };
99 :
100 :
101 : class exception_t
102 : : public std::runtime_error
103 : , public exception_base_t
104 : {
105 : public:
106 : explicit exception_t(std::string const & what, int const stack_trace_depth = STACK_TRACE_DEPTH);
107 : explicit exception_t(char const * what, int const stack_trace_depth = STACK_TRACE_DEPTH);
108 :
109 8 : virtual ~exception_t() override {}
110 :
111 : virtual char const * what() const throw() override;
112 : };
113 :
114 :
115 : #define DECLARE_LOGIC_ERROR(name) \
116 : class name : public ::libexcept::logic_exception_t { \
117 : public: name(std::string const & msg) : logic_exception_t(#name ": " + msg) {} }
118 :
119 : #define DECLARE_OUT_OF_RANGE(name) \
120 : class name : public ::libexcept::out_of_range_t { \
121 : public: name(std::string const & msg) : out_of_range_t(#name ": " + msg) {} }
122 :
123 : #define DECLARE_MAIN_EXCEPTION(name) \
124 : class name : public ::libexcept::exception_t { \
125 : public: name(std::string const & msg) : exception_t(#name ": " + msg) {} }
126 :
127 : #define DECLARE_EXCEPTION(base, name) \
128 : class name : public base { \
129 : public: name(std::string const & msg) : base(msg) {} }
130 :
131 :
132 : }
133 : // namespace libexcept
134 : // vim: ts=4 sw=4 et
|