88ecfc51b4694fd478595773fe1dcf2eeac5b182
1 #ifndef LIB2GEOM_EXCEPTION_HEADER
2 #define LIB2GEOM_EXCEPTION_HEADER
4 /** Defines the different types of exceptions that 2geom can throw.
5 *
6 * Copyright 2007 Johan Engelen <goejendaagh@zonnet.nl>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it either under the terms of the GNU Lesser General Public
10 * License version 2.1 as published by the Free Software Foundation
11 * (the "LGPL") or, at your option, under the terms of the Mozilla
12 * Public License Version 1.1 (the "MPL"). If you do not alter this
13 * notice, a recipient may use your version of this file under either
14 * the MPL or the LGPL.
15 *
16 * You should have received a copy of the LGPL along with this library
17 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * You should have received a copy of the MPL along with this library
20 * in the file COPYING-MPL-1.1
21 *
22 * The contents of this file are subject to the Mozilla Public License
23 * Version 1.1 (the "License"); you may not use this file except in
24 * compliance with the License. You may obtain a copy of the License at
25 * http://www.mozilla.org/MPL/
26 *
27 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
28 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
29 * the specific language governing rights and limitations.
30 *
31 */
33 #include <exception>
34 #include <sstream>
35 #include <string>
37 namespace Geom {
39 // Base exception class, all 2geom exceptions should be derrived from this one.
40 class Exception : public std::exception {
41 public:
42 Exception(const char * message, const char *file, const int line) {
43 std::ostringstream os;
44 os << "lib2geom exception: " << message << " (" << file << ":" << line << ")";
45 msgstr = os.str();
46 }
48 virtual ~Exception() throw() {} // necessary to destroy the string object!!!
50 virtual const char* what() const throw () {
51 return msgstr.c_str();
52 }
53 protected:
54 std::string msgstr;
55 };
56 #define throwException(message) throw(Geom::Exception(message, __FILE__, __LINE__))
58 //-----------------------------------------------------------------------
59 // Two main exception classes: LogicalError and RangeError.
60 // Logical errors are 2geom faults/bugs, RangeErrors are 'user' faults.
61 // This way, the 'user' can distinguish between groups of exceptions
62 // ('user' is the coder that uses lib2geom)
63 class LogicalError : public Exception {
64 public:
65 LogicalError(const char * message, const char *file, const int line)
66 : Exception(message, file, line) {}
67 };
68 #define throwLogicalError(message) throw(LogicalError(message, __FILE__, __LINE__))
70 class RangeError : public Exception {
71 public:
72 RangeError(const char * message, const char *file, const int line)
73 : Exception(message, file, line) {}
74 };
75 #define throwRangeError(message) throw(RangeError(message, __FILE__, __LINE__))
77 //-----------------------------------------------------------------------
78 // Special case exceptions. Best used with the defines :)
80 class NotImplemented : public LogicalError {
81 public:
82 NotImplemented(const char *file, const int line)
83 : LogicalError("Method not implemented", file, line) {}
84 };
85 #define throwNotImplemented(i) throw(NotImplemented(__FILE__, __LINE__))
87 class InvariantsViolation : public LogicalError {
88 public:
89 InvariantsViolation(const char *file, const int line)
90 : LogicalError("Invariants violation", file, line) {}
91 };
92 #define throwInvariantsViolation(i) throw(InvariantsViolation(__FILE__, __LINE__))
93 #define assert_invariants(e) ((e) ? (void)0 : throwInvariantsViolation())
95 class NotInvertible : public RangeError {
96 public:
97 NotInvertible(const char *file, const int line)
98 : RangeError("Function does not have a unique inverse", file, line) {}
99 };
100 #define throwNotInvertible(i) throw(NotInvertible(__FILE__, __LINE__))
102 class ContinuityError : public RangeError {
103 public:
104 ContinuityError(const char *file, const int line)
105 : RangeError("Non-contiguous path", file, line) {}
106 };
107 #define throwContinuityError(i) throw(ContinuityError(__FILE__, __LINE__))
109 struct SVGPathParseError : public std::exception {
110 char const *what() const throw() { return "parse error"; }
111 };
114 } // namespace Geom
116 #endif
119 /*
120 Local Variables:
121 mode:c++
122 c-file-style:"stroustrup"
123 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
124 indent-tabs-mode:nil
125 fill-column:99
126 End:
127 */
128 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :