Code

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