Line data Source code
1 : // Copyright (c) 2011-2022 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/libexcept
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 <map>
29 : #include <stdexcept>
30 : #include <string>
31 : #include <vector>
32 :
33 :
34 : /** \file
35 : * \brief Declarations of the exception library.
36 : *
37 : * This file includes the library exception declarations.
38 : *
39 : * The strong point of the library is its ability to gather a stack
40 : * trace and attach that information to an exception.
41 : */
42 :
43 :
44 : namespace libexcept
45 : {
46 :
47 :
48 :
49 : enum class collect_stack_t
50 : {
51 : COLLECT_STACK_NO, // no stack trace for exceptions
52 : COLLECT_STACK_YES, // plain stack trace (fast)
53 : COLLECT_STACK_COMPLETE, // include filenames & line numbers (slow)
54 : };
55 :
56 :
57 : typedef std::map<std::string, std::string> parameter_t;
58 :
59 :
60 : collect_stack_t get_collect_stack();
61 : void set_collect_stack(collect_stack_t collect_stack);
62 :
63 :
64 1 : class exception_base_t
65 : {
66 : public:
67 : explicit exception_base_t(int const stack_trace_depth = STACK_TRACE_DEPTH);
68 :
69 24 : virtual ~exception_base_t() {}
70 :
71 : parameter_t const & get_parameters() const;
72 : std::string get_parameter(std::string const & name) const;
73 : void set_parameter(std::string const & name, std::string const & value);
74 :
75 22 : stack_trace_t const & get_stack_trace() const { return f_stack_trace; }
76 :
77 : private:
78 : parameter_t f_parameters = parameter_t();
79 : stack_trace_t f_stack_trace = stack_trace_t();
80 : };
81 :
82 :
83 : class logic_exception_t
84 : : public std::logic_error
85 : , public exception_base_t
86 : {
87 : public:
88 : explicit logic_exception_t(std::string const & what, int const stack_trace_depth = STACK_TRACE_DEPTH);
89 : explicit logic_exception_t(char const * what, int const stack_trace_depth = STACK_TRACE_DEPTH);
90 :
91 8 : virtual ~logic_exception_t() override {}
92 :
93 : virtual char const * what() const throw() override;
94 : };
95 :
96 :
97 : class out_of_range_t
98 : : public std::out_of_range
99 : , public exception_base_t
100 : {
101 : public:
102 : explicit out_of_range_t(std::string const & what, int const stack_trace_depth = STACK_TRACE_DEPTH);
103 : explicit out_of_range_t(char const * what, int const stack_trace_depth = STACK_TRACE_DEPTH);
104 :
105 6 : virtual ~out_of_range_t() override {}
106 :
107 : virtual char const * what() const throw() override;
108 : };
109 :
110 :
111 1 : class exception_t
112 : : public std::runtime_error
113 : , public exception_base_t
114 : {
115 : public:
116 : explicit exception_t(std::string const & what, int const stack_trace_depth = STACK_TRACE_DEPTH);
117 : explicit exception_t(char const * what, int const stack_trace_depth = STACK_TRACE_DEPTH);
118 :
119 10 : virtual ~exception_t() override {}
120 :
121 : virtual char const * what() const throw() override;
122 : };
123 :
124 :
125 : #define DECLARE_LOGIC_ERROR(name) \
126 : class name : public ::libexcept::logic_exception_t { \
127 : public: name(std::string const & msg) : logic_exception_t(#name ": " + msg) {} }
128 :
129 : #define DECLARE_OUT_OF_RANGE(name) \
130 : class name : public ::libexcept::out_of_range_t { \
131 : public: name(std::string const & msg) : out_of_range_t(#name ": " + msg) {} }
132 :
133 : #define DECLARE_MAIN_EXCEPTION(name) \
134 : class name : public ::libexcept::exception_t { \
135 : public: name(std::string const & msg) : exception_t(#name ": " + msg) {} }
136 :
137 : #define DECLARE_EXCEPTION(base, name) \
138 : class name : public base { \
139 : public: name(std::string const & msg) : base(msg) {} }
140 :
141 :
142 : }
143 : // namespace libexcept
144 : // vim: ts=4 sw=4 et
|