From feb44ad276e996ca244e071f936c61b009bd73f4 Mon Sep 17 00:00:00 2001 From: johanengelen Date: Sat, 17 Nov 2007 01:44:59 +0000 Subject: [PATCH] 2geom tryout: new exceptions --- src/2geom/basic-intersection.cpp | 2 +- src/2geom/path.cpp | 10 ++--- src/2geom/path.h | 5 ++- src/2geom/piecewise.h | 2 +- src/2geom/sbasis.h | 3 +- src/2geom/shape.cpp | 2 +- src/2geom/utils.h | 67 +++++++++++++++++++++++++++++--- src/live_effects/effect.cpp | 4 +- 8 files changed, 77 insertions(+), 18 deletions(-) diff --git a/src/2geom/basic-intersection.cpp b/src/2geom/basic-intersection.cpp index 0d84ee7f3..ee1244a73 100644 --- a/src/2geom/basic-intersection.cpp +++ b/src/2geom/basic-intersection.cpp @@ -60,7 +60,7 @@ find_intersections( vector const & A, std::vector > find_self_intersections(OldBezier const &Sb) { - throw NotImplemented(); + throwNotImplemented(); } std::vector > diff --git a/src/2geom/path.cpp b/src/2geom/path.cpp index 98fec6e24..ba6643ae4 100644 --- a/src/2geom/path.cpp +++ b/src/2geom/path.cpp @@ -222,21 +222,21 @@ void Path::check_continuity(Sequence::iterator first_replaced, } Rect SVGEllipticalArc::boundsFast() const { - throw NotImplemented(); + throwNotImplemented(); } Rect SVGEllipticalArc::boundsExact() const { - throw NotImplemented(); + throwNotImplemented(); } Rect SVGEllipticalArc::boundsLocal(Interval i, unsigned deg) const { - //throw NotImplemented(); + throwNotImplemented(); } std::vector SVGEllipticalArc::pointAndDerivatives(Coord t, unsigned n) const { - throw NotImplemented(); + throwNotImplemented(); } std::vector SVGEllipticalArc::roots(double v, Dim2 d) const { - //throw NotImplemented(); + throwNotImplemented(); } D2 SVGEllipticalArc::toSBasis() const { diff --git a/src/2geom/path.h b/src/2geom/path.h index 46d7f230a..053bc06c9 100644 --- a/src/2geom/path.h +++ b/src/2geom/path.h @@ -39,6 +39,7 @@ #include "matrix.h" #include "bezier.h" #include "crossing.h" +#include "utils.h" namespace Geom { @@ -325,7 +326,7 @@ public: return ret; } - Curve *derivative() const { throw NotImplemented(); } + Curve *derivative() const { throwNotImplemented(); } std::vector pointAndDerivatives(Coord t, unsigned n) const; @@ -767,7 +768,7 @@ class PathPortion : public Curve { Rect boundsFast() const { return actualPath().boundsFast; } Rect boundsExact() const { return actualPath().boundsFast; } - Rect boundsLocal(Interval i) const { throw NotImplemented(); } + Rect boundsLocal(Interval i) const { throwNotImplemented(); } std::vector roots(double v, Dim2 d) const = 0; diff --git a/src/2geom/piecewise.h b/src/2geom/piecewise.h index b4f000dc4..c70ecd42c 100644 --- a/src/2geom/piecewise.h +++ b/src/2geom/piecewise.h @@ -90,7 +90,7 @@ class Piecewise { } //Convenience/implementation hiding function to add cuts. inline void push_cut(double c) { - assert(cuts.empty() || c > cuts.back()); + assert_invariants(cuts.empty() || c > cuts.back()); cuts.push_back(c); } //Convenience/implementation hiding function to add segments. diff --git a/src/2geom/sbasis.h b/src/2geom/sbasis.h index 9fc16f1c6..df41dca58 100644 --- a/src/2geom/sbasis.h +++ b/src/2geom/sbasis.h @@ -106,7 +106,8 @@ public: std::vector valueAndDerivatives(double /*t*/, unsigned /*n*/) const { //TODO - throw NotImplemented(); + throwNotImplemented(); + //throw(NotImplemented(__FILE__, __LINE__)); } SBasis toSBasis() const { return SBasis(*this); } diff --git a/src/2geom/shape.cpp b/src/2geom/shape.cpp index 3cf6d0bc0..70a7d0884 100644 --- a/src/2geom/shape.cpp +++ b/src/2geom/shape.cpp @@ -191,7 +191,7 @@ Shape shape_boolean_rb(bool rev, Shape const &a, Shape const &b, CrossingSet con * NOTE: currently doesn't work, as the CrossingSet reversal functions crash */ Shape boolop(Shape const &a, Shape const &b, unsigned flags, CrossingSet const &crs) { - throw NotImplemented(); + throwNotImplemented(); flags &= 15; if(flags <= BOOLOP_UNION) { switch(flags) { diff --git a/src/2geom/utils.h b/src/2geom/utils.h index 50dfa82fe..cd2a9c26c 100644 --- a/src/2geom/utils.h +++ b/src/2geom/utils.h @@ -3,6 +3,7 @@ /** Various utility functions. * + * Copyright 2007 Johan Engelen * Copyright 2006 Michael G. Sloan * * This library is free software; you can redistribute it and/or @@ -35,15 +36,71 @@ namespace Geom { -class NotImplemented : public std::logic_error { + +//####################################################################### +// Base exception class, all 2geom exceptions should be derrived from this one. +class Exception : public std::exception { +public: + Exception(const char * message, const char *file, const int line) { + msgstr = "Exception thrown: "; + msgstr += message; + msgstr += " ("; + msgstr += file; + msgstr += ":"; + msgstr += line; + msgstr += ")"; + }; + virtual ~Exception() throw() {}; // necessary to destroy the string object!!! + virtual const char * what() const throw () { return msgstr.c_str(); } +protected: + std::string msgstr; +}; + +//----------------------------------------------------------------------- +// Two main exception classes: LogicalError and RangeError. +// Logical errors are 2geom faults/bugs, RangeErrors are 'user' faults. +// This way, the 'user' can distinguish between groups of exceptions +// ('user' is the coder that uses lib2geom) +class LogicalError : public Exception { +public: + LogicalError(const char * message, const char *file, const int line) + : Exception(message, file, line) {}; +}; +#define throwLogicalError(message) throw(LogicalError(message, __FILE__, __LINE__)) + +class RangeError : public Exception { +public: + RangeError(const char * message, const char *file, const int line) + : Exception(message, file, line) {}; +}; +#define throwRangeError(message) throw(RangeError(message, __FILE__, __LINE__)) + +//----------------------------------------------------------------------- +// Special case exceptions. Best used with the defines :) + +class NotImplemented : public LogicalError { +public: + NotImplemented(const char *file, const int line) + : LogicalError("Method not implemented", file, line) {}; +}; +#define throwNotImplemented(i) throw(NotImplemented(__FILE__, __LINE__)) + +class InvariantsViolation : public LogicalError { public: - NotImplemented() : std::logic_error("method not implemented") {} + InvariantsViolation(const char *file, const int line) + : LogicalError("Invariants violation", file, line) {}; }; +#define throwInvariantsViolation(i) throw(InvariantsViolation(__FILE__, __LINE__)) +#define assert_invariants(e) ((e) ? (void)0 : throwInvariantsViolation()) -class NotInvertible : public std::range_error { - public: - NotInvertible() : std::range_error("function does not have a unique inverse") {} +class NotInvertible : public RangeError { +public: + NotInvertible(const char *file, const int line) + : RangeError("Function does not have a unique inverse", file, line) {}; }; +#define throwNotInvertible(i) throw(NotInvertible(__FILE__, __LINE__)) + +//####################################################################### // proper logical xor inline bool logical_xor (bool a, bool b) { return (a || b) && !(a && b); } diff --git a/src/live_effects/effect.cpp b/src/live_effects/effect.cpp index f87e365bc..4ccfa5952 100644 --- a/src/live_effects/effect.cpp +++ b/src/live_effects/effect.cpp @@ -145,9 +145,9 @@ Effect::doEffect_nartbpath (NArtBpath * path_in) return new_bpath; } catch (std::exception e) { - g_warning("An exception occurred during execution of an LPE - %s", e.what()); + g_warning("Exception during LPE %s execution. \n %s", getName().c_str(), e.what()); SP_ACTIVE_DESKTOP->messageStack()->flash( Inkscape::WARNING_MESSAGE, - _("An exception occurred during execution of a Path Effect.") ); + _("An exception occurred during execution of the Path Effect.") ); NArtBpath *path_out; -- 2.30.2