GCC Code Coverage Report


Directory: ./
File: include/2geom/exception.h
Date: 2024-03-18 17:01:34
Exec Total Coverage
Lines: 6 26 23.1%
Functions: 2 12 16.7%
Branches: 0 18 0.0%

Line Branch Exec Source
1 /**
2 * \file
3 * \brief Defines the different types of exceptions that 2geom can throw.
4 *
5 * There are two main exception classes: LogicalError and RangeError.
6 * Logical errors are 2geom faults/bugs; RangeErrors are 'user' faults,
7 * e.g. invalid arguments to lib2geom methods.
8 * This way, the 'user' can distinguish between groups of exceptions
9 * ('user' is the coder that uses lib2geom)
10 *
11 * Several macro's are defined for easily throwing exceptions
12 * (e.g. THROW_CONTINUITYERROR).
13 */
14 /* Copyright 2007 Johan Engelen <goejendaagh@zonnet.nl>
15 *
16 * This library is free software; you can redistribute it and/or
17 * modify it either under the terms of the GNU Lesser General Public
18 * License version 2.1 as published by the Free Software Foundation
19 * (the "LGPL") or, at your option, under the terms of the Mozilla
20 * Public License Version 1.1 (the "MPL"). If you do not alter this
21 * notice, a recipient may use your version of this file under either
22 * the MPL or the LGPL.
23 *
24 * You should have received a copy of the LGPL along with this library
25 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * You should have received a copy of the MPL along with this library
28 * in the file COPYING-MPL-1.1
29 *
30 * The contents of this file are subject to the Mozilla Public License
31 * Version 1.1 (the "License"); you may not use this file except in
32 * compliance with the License. You may obtain a copy of the License at
33 * http://www.mozilla.org/MPL/
34 *
35 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
36 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
37 * the specific language governing rights and limitations.
38 *
39 */
40
41 #ifndef LIB2GEOM_SEEN_EXCEPTION_H
42 #define LIB2GEOM_SEEN_EXCEPTION_H
43
44 #include <exception>
45 #include <sstream>
46 #include <string>
47
48 namespace Geom {
49
50 /**
51 * Base exception class, all 2geom exceptions should be derived from this one.
52 */
53 class Exception : public std::exception {
54 public:
55 Exception(const char * message, const char *file, const int line) {
56 std::ostringstream os;
57 os << "lib2geom exception: " << message << " (" << file << ":" << line << ")";
58 msgstr = os.str();
59 }
60
61 ~Exception() noexcept override {} // necessary to destroy the string object!!!
62
63 const char* what() const noexcept override {
64 return msgstr.c_str();
65 }
66 protected:
67 std::string msgstr;
68 };
69 #define THROW_EXCEPTION(message) throw(Geom::Exception(message, __FILE__, __LINE__))
70
71 //-----------------------------------------------------------------------
72
73 class LogicalError : public Exception {
74 public:
75 LogicalError(const char * message, const char *file, const int line)
76 : Exception(message, file, line) {}
77 };
78 #define THROW_LOGICALERROR(message) throw(LogicalError(message, __FILE__, __LINE__))
79
80 class RangeError : public Exception {
81 public:
82 9 RangeError(const char * message, const char *file, const int line)
83 9 : Exception(message, file, line) {}
84 };
85 #define THROW_RANGEERROR(message) throw(RangeError(message, __FILE__, __LINE__))
86
87 //-----------------------------------------------------------------------
88 // Special case exceptions. Best used with the defines :)
89
90 class NotImplemented : public LogicalError {
91 public:
92 NotImplemented(const char *file, const int line)
93 : LogicalError("Method not implemented", file, line) {}
94 };
95 #define THROW_NOTIMPLEMENTED(i) throw(NotImplemented(__FILE__, __LINE__))
96
97 class InvariantsViolation : public LogicalError {
98 public:
99 InvariantsViolation(const char *file, const int line)
100 : LogicalError("Invariants violation", file, line) {}
101 };
102 #define THROW_INVARIANTSVIOLATION(i) throw(InvariantsViolation(__FILE__, __LINE__))
103 #define ASSERT_INVARIANTS(e) ((e) ? (void)0 : THROW_INVARIANTSVIOLATION())
104
105 class NotInvertible : public RangeError {
106 public:
107 NotInvertible(const char *file, const int line)
108 : RangeError("Function does not have a unique inverse", file, line) {}
109 };
110 #define THROW_NOTINVERTIBLE(i) throw(NotInvertible(__FILE__, __LINE__))
111
112 class InfiniteSolutions : public RangeError {
113 public:
114 InfiniteSolutions(const char *file, const int line)
115 : RangeError("There are infinite solutions", file, line) {}
116 };
117 #define THROW_INFINITESOLUTIONS(i) throw(InfiniteSolutions(__FILE__, __LINE__))
118
119 class InfinitelyManySolutions : public RangeError {
120 private:
121 char const *const _message;
122 public:
123 9 InfinitelyManySolutions(const char *file, const int line, char const *message)
124 9 : RangeError("There are infinitely many solutions", file, line)
125 9 , _message{message}
126 9 {}
127 char const *what() const noexcept override { return _message; }
128 };
129 #define THROW_INFINITELY_MANY_SOLUTIONS(msg) throw(InfinitelyManySolutions(__FILE__, __LINE__, msg))
130
131 class ContinuityError : public RangeError {
132 public:
133 ContinuityError(const char *file, const int line)
134 : RangeError("Non-contiguous path", file, line) {}
135 };
136 #define THROW_CONTINUITYERROR(i) throw(ContinuityError(__FILE__, __LINE__))
137
138 struct SVGPathParseError : public std::exception {
139 char const *what() const noexcept override { return "parse error"; }
140 };
141
142
143 } // namespace Geom
144
145 #endif
146
147
148 /*
149 Local Variables:
150 mode:c++
151 c-file-style:"stroustrup"
152 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
153 indent-tabs-mode:nil
154 fill-column:99
155 End:
156 */
157 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
158