[Lemon-commits] Balazs Dezso: Simplifying exceptions

Lemon HG hg at lemon.cs.elte.hu
Wed Oct 1 13:58:24 CEST 2008


details:   http://lemon.cs.elte.hu/hg/lemon/rev/f6899946c1ac
changeset: 290:f6899946c1ac
user:      Balazs Dezso <deba [at] inf.elte.hu>
date:      Tue Sep 30 20:53:18 2008 +0200
description:
	Simplifying exceptions

	 - Using asserts instead of exceptions for unitialized parameters
	 - Only the IO exceptions are used in the lemon
	 - DataFormatError is renamed to FormatError
	 - The IoError is used for file access errors

diffstat:

11 files changed, 380 insertions(+), 511 deletions(-)
demo/lgf_demo.cc      |    2 
lemon/arg_parser.h    |    5 
lemon/assert.h        |    2 
lemon/bfs.h           |   39 +--
lemon/concepts/heap.h |    1 
lemon/dfs.h           |   38 ---
lemon/dijkstra.h      |   27 --
lemon/error.h         |  538 ++++++++++++++++++-------------------------------
lemon/graph_to_eps.h  |   15 +
lemon/lgf_reader.h    |  194 ++++++++++-------
lemon/lgf_writer.h    |   30 +-

diffs (truncated from 1725 to 300 lines):

diff -r bb40b6db0a58 -r f6899946c1ac demo/lgf_demo.cc
--- a/demo/lgf_demo.cc	Sat Sep 27 14:33:28 2008 +0200
+++ b/demo/lgf_demo.cc	Tue Sep 30 20:53:18 2008 +0200
@@ -49,7 +49,7 @@
       node("source", s).             // read 'source' node to s
       node("target", t).             // read 'target' node to t
       run();
-  } catch (DataFormatError& error) { // check if there was any error
+  } catch (Exception& error) { // check if there was any error
     std::cerr << "Error: " << error.what() << std::endl;
     return -1;
   }
diff -r bb40b6db0a58 -r f6899946c1ac lemon/arg_parser.h
--- a/lemon/arg_parser.h	Sat Sep 27 14:33:28 2008 +0200
+++ b/lemon/arg_parser.h	Tue Sep 30 20:53:18 2008 +0200
@@ -310,8 +310,9 @@
 
     ///This is the type of the return value of ArgParser::operator[]().
     ///It automatically converts to \c int, \c double, \c bool or
-    ///\c std::string if the type of the option matches, otherwise it
-    ///throws an exception (i.e. it performs runtime type checking).
+    ///\c std::string if the type of the option matches, which is checked
+    ///with an \ref LEMON_ASSERT "assertion" (i.e. it performs runtime
+    ///type checking).
     class RefType
     {
       const ArgParser &_parser;
diff -r bb40b6db0a58 -r f6899946c1ac lemon/assert.h
--- a/lemon/assert.h	Sat Sep 27 14:33:28 2008 +0200
+++ b/lemon/assert.h	Tue Sep 30 20:53:18 2008 +0200
@@ -108,7 +108,7 @@
 ///
 /// \brief Macro for assertion with customizable message
 ///
-/// Macro for assertion with customizable message.  
+/// Macro for assertion with customizable message.
 /// \param exp An expression that must be convertible to \c bool.  If it is \c
 /// false, then an assertion is raised. The concrete behaviour depends on the
 /// settings of the assertion system.
diff -r bb40b6db0a58 -r f6899946c1ac lemon/bfs.h
--- a/lemon/bfs.h	Sat Sep 27 14:33:28 2008 +0200
+++ b/lemon/bfs.h	Tue Sep 30 20:53:18 2008 +0200
@@ -135,16 +135,6 @@
 #endif
   class Bfs {
   public:
-    ///\ref Exception for uninitialized parameters.
-
-    ///This error represents problems in the initialization of the
-    ///parameters of the algorithm.
-    class UninitializedParameter : public lemon::UninitializedParameter {
-    public:
-      virtual const char* what() const throw() {
-        return "lemon::Bfs::UninitializedParameter";
-      }
-    };
 
     ///The type of the digraph the algorithm runs on.
     typedef typename TR::Digraph Digraph;
@@ -232,7 +222,8 @@
       typedef T PredMap;
       static PredMap *createPredMap(const Digraph &)
       {
-        throw UninitializedParameter();
+        LEMON_ASSERT(false, "PredMap is not initialized");
+        return 0; // ignore warnings
       }
     };
     ///\brief \ref named-templ-param "Named parameter" for setting
@@ -250,7 +241,8 @@
       typedef T DistMap;
       static DistMap *createDistMap(const Digraph &)
       {
-        throw UninitializedParameter();
+        LEMON_ASSERT(false, "DistMap is not initialized");
+        return 0; // ignore warnings
       }
     };
     ///\brief \ref named-templ-param "Named parameter" for setting
@@ -268,7 +260,8 @@
       typedef T ReachedMap;
       static ReachedMap *createReachedMap(const Digraph &)
       {
-        throw UninitializedParameter();
+        LEMON_ASSERT(false, "ReachedMap is not initialized");
+        return 0; // ignore warnings
       }
     };
     ///\brief \ref named-templ-param "Named parameter" for setting
@@ -286,7 +279,8 @@
       typedef T ProcessedMap;
       static ProcessedMap *createProcessedMap(const Digraph &)
       {
-        throw UninitializedParameter();
+        LEMON_ASSERT(false, "ProcessedMap is not initialized");
+        return 0; // ignore warnings
       }
     };
     ///\brief \ref named-templ-param "Named parameter" for setting
@@ -304,6 +298,7 @@
       static ProcessedMap *createProcessedMap(const Digraph &g)
       {
         return new ProcessedMap(g);
+        return 0; // ignore warnings
       }
     };
     ///\brief \ref named-templ-param "Named parameter" for setting
@@ -1040,7 +1035,6 @@
     ///\return \c true if \c t is reachable form \c s.
     bool run(Node s, Node t)
     {
-      if (s==INVALID || t==INVALID) throw UninitializedParameter();
       Bfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
       if (Base::_pred)
         alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
@@ -1323,18 +1317,6 @@
   class BfsVisit {
   public:
 
-    /// \brief \ref Exception for uninitialized parameters.
-    ///
-    /// This error represents problems in the initialization
-    /// of the parameters of the algorithm.
-    class UninitializedParameter : public lemon::UninitializedParameter {
-    public:
-      virtual const char* what() const throw()
-      {
-        return "lemon::BfsVisit::UninitializedParameter";
-      }
-    };
-
     ///The traits class.
     typedef _Traits Traits;
 
@@ -1389,7 +1371,8 @@
     struct SetReachedMapTraits : public Traits {
       typedef T ReachedMap;
       static ReachedMap *createReachedMap(const Digraph &digraph) {
-        throw UninitializedParameter();
+        LEMON_ASSERT(false, "ReachedMap is not initialized");
+        return 0; // ignore warnings
       }
     };
     /// \brief \ref named-templ-param "Named parameter" for setting
diff -r bb40b6db0a58 -r f6899946c1ac lemon/concepts/heap.h
--- a/lemon/concepts/heap.h	Sat Sep 27 14:33:28 2008 +0200
+++ b/lemon/concepts/heap.h	Tue Sep 30 20:53:18 2008 +0200
@@ -129,7 +129,6 @@
       /// already stored in the heap.
       /// Otherwise it inserts the given item with the given priority.
       ///
-      /// It may throw an \ref UnderflowPriorityException.
       /// \param i The item.
       /// \param p The priority.
       void set(const Item &i, const Prio &p) {}
diff -r bb40b6db0a58 -r f6899946c1ac lemon/dfs.h
--- a/lemon/dfs.h	Sat Sep 27 14:33:28 2008 +0200
+++ b/lemon/dfs.h	Tue Sep 30 20:53:18 2008 +0200
@@ -136,16 +136,6 @@
 #endif
   class Dfs {
   public:
-    ///\ref Exception for uninitialized parameters.
-
-    ///This error represents problems in the initialization of the
-    ///parameters of the algorithm.
-    class UninitializedParameter : public lemon::UninitializedParameter {
-    public:
-      virtual const char* what() const throw() {
-        return "lemon::Dfs::UninitializedParameter";
-      }
-    };
 
     ///The type of the digraph the algorithm runs on.
     typedef typename TR::Digraph Digraph;
@@ -232,7 +222,8 @@
       typedef T PredMap;
       static PredMap *createPredMap(const Digraph &)
       {
-        throw UninitializedParameter();
+        LEMON_ASSERT(false, "PredMap is not initialized");
+        return 0; // ignore warnings
       }
     };
     ///\brief \ref named-templ-param "Named parameter" for setting
@@ -250,7 +241,8 @@
       typedef T DistMap;
       static DistMap *createDistMap(const Digraph &)
       {
-        throw UninitializedParameter();
+        LEMON_ASSERT(false, "DistMap is not initialized");
+        return 0; // ignore warnings
       }
     };
     ///\brief \ref named-templ-param "Named parameter" for setting
@@ -268,7 +260,8 @@
       typedef T ReachedMap;
       static ReachedMap *createReachedMap(const Digraph &)
       {
-        throw UninitializedParameter();
+        LEMON_ASSERT(false, "ReachedMap is not initialized");
+        return 0; // ignore warnings
       }
     };
     ///\brief \ref named-templ-param "Named parameter" for setting
@@ -286,7 +279,8 @@
       typedef T ProcessedMap;
       static ProcessedMap *createProcessedMap(const Digraph &)
       {
-        throw UninitializedParameter();
+        LEMON_ASSERT(false, "ProcessedMap is not initialized");
+        return 0; // ignore warnings
       }
     };
     ///\brief \ref named-templ-param "Named parameter" for setting
@@ -974,7 +968,6 @@
     ///\return \c true if \c t is reachable form \c s.
     bool run(Node s, Node t)
     {
-      if (s==INVALID || t==INVALID) throw UninitializedParameter();
       Dfs<Digraph,TR> alg(*reinterpret_cast<const Digraph*>(Base::_g));
       if (Base::_pred)
         alg.predMap(*reinterpret_cast<PredMap*>(Base::_pred));
@@ -1270,18 +1263,6 @@
   class DfsVisit {
   public:
 
-    /// \brief \ref Exception for uninitialized parameters.
-    ///
-    /// This error represents problems in the initialization
-    /// of the parameters of the algorithm.
-    class UninitializedParameter : public lemon::UninitializedParameter {
-    public:
-      virtual const char* what() const throw()
-      {
-        return "lemon::DfsVisit::UninitializedParameter";
-      }
-    };
-
     ///The traits class.
     typedef _Traits Traits;
 
@@ -1336,7 +1317,8 @@
     struct SetReachedMapTraits : public Traits {
       typedef T ReachedMap;
       static ReachedMap *createReachedMap(const Digraph &digraph) {
-        throw UninitializedParameter();
+        LEMON_ASSERT(false, "ReachedMap is not initialized");
+        return 0; // ignore warnings
       }
     };
     /// \brief \ref named-templ-param "Named parameter" for setting
diff -r bb40b6db0a58 -r f6899946c1ac lemon/dijkstra.h
--- a/lemon/dijkstra.h	Sat Sep 27 14:33:28 2008 +0200
+++ b/lemon/dijkstra.h	Tue Sep 30 20:53:18 2008 +0200
@@ -225,16 +225,6 @@
 #endif
   class Dijkstra {
   public:
-    ///\ref Exception for uninitialized parameters.
-
-    ///This error represents problems in the initialization of the
-    ///parameters of the algorithm.
-    class UninitializedParameter : public lemon::UninitializedParameter {
-    public:
-      virtual const char* what() const throw() {
-        return "lemon::Dijkstra::UninitializedParameter";
-      }
-    };
 
     ///The type of the digraph the algorithm runs on.
     typedef typename TR::Digraph Digraph;
@@ -332,7 +322,8 @@
       typedef T PredMap;
       static PredMap *createPredMap(const Digraph &)
       {
-        throw UninitializedParameter();
+        LEMON_ASSERT(false, "PredMap is not initialized");
+        return 0; // ignore warnings
       }
     };
     ///\brief \ref named-templ-param "Named parameter" for setting
@@ -351,7 +342,8 @@
       typedef T DistMap;
       static DistMap *createDistMap(const Digraph &)
       {
-        throw UninitializedParameter();
+        LEMON_ASSERT(false, "DistMap is not initialized");
+        return 0; // ignore warnings
       }
     };
     ///\brief \ref named-templ-param "Named parameter" for setting
@@ -370,7 +362,8 @@
       typedef T ProcessedMap;
       static ProcessedMap *createProcessedMap(const Digraph &)
       {
-        throw UninitializedParameter();
+        LEMON_ASSERT(false, "ProcessedMap is not initialized");
+        return 0; // ignore warnings
       }



More information about the Lemon-commits mailing list