# HG changeset patch # User klao # Date 1083278357 0 # Node ID afbdf8a3a633e40de9801fbf79dee90421f5179f # Parent 32c3548ecc2a459a5cbc33abc9e1a307320aa420 Basic error handling facilities (and possibly a g++-3.4.0 bug) diff -r 32c3548ecc2a -r afbdf8a3a633 src/include/error.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/include/error.h Thu Apr 29 22:39:17 2004 +0000 @@ -0,0 +1,64 @@ +// -*- C++ -*- // + +#ifndef HUGO_ERROR_H +#define HUGO_ERROR_H + +//! \file +//! \brief Basic error handling (signaling) routines. + +#include +#include + + +namespace hugo { + + /** + * \brief Generic exception class. + * + * \todo Do we need this? + * + * \todo Don't we need different kind of exceptions for different kind + * of errors? + * Shouldn't we use instead? + */ + class Exception : public std::exception { + protected: + std::ostringstream buf; + public: + Exception() {} + explicit Exception(const std::string &s) { buf << s; } + Exception(const Exception &e) { buf << e.buf.str(); } + virtual ~Exception() throw() {} + + virtual const char* what() const throw() { + return buf.str().c_str(); + } + + Exception& operator<<(std::string const& s) { buf << s; return *this; } + Exception& operator<<(char const *s) { buf << s; return *this; } + Exception& operator<<(int i) { buf << i; return *this; } + }; + + /** + * \brief Generic error signaling function. + * + * \todo Do we really need this? Is it helpful? + */ + inline void fault(const std::string &msg) { + throw Exception(msg); + } + + /** + * \brief Macro for mark not yet implemented features. + * + * \todo Is this the right place for this? It should be used only in + * modules under development. + */ + +# define FIXME(msg) \ + do { throw ::hugo::Exception("FIXME: " msg) << " (in: " \ + << __FILE__ << ", " << __LINE__ << ")"; \ + } while(false) + +} +#endif // HUGO_ERROR_H diff -r 32c3548ecc2a -r afbdf8a3a633 src/test/error_test.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/error_test.cc Thu Apr 29 22:39:17 2004 +0000 @@ -0,0 +1,57 @@ +#include + +#include + +using namespace hugo; +using std::cout; +using std::endl; + +void faulty_fn() { + fault("This is a fault message"); +} + +void exception_fn() { + throw Exception("This is a fn throwing excpt with some args: ") + << 5 << ", " << 18; +} + +void unfinished_fn() { + FIXME("unfinished_fn() is unfinished!"); +} + + +int main() { + bool no_errors = false; + + try { + cout << "Trying a faulty function\n"; + faulty_fn(); + no_errors = true; + cout << "FAILED!\n"; + } + catch(const Exception &e) { + cout << "E: " << e.what() << endl; + } + + try { + cout << "Trying a function throwing Exception\n"; + exception_fn(); + no_errors = true; + cout << "FAILED!\n"; + } + catch(const Exception &e) { + cout << "E: " << e.what() << endl; + } + + try { + cout << "Trying a function using FIXME\n"; + unfinished_fn(); + no_errors = true; + cout << "FAILED!\n"; + } + catch(const Exception &e) { + cout << "E: " << e.what() << endl; + } + + return no_errors ? 1 : 0; +} diff -r 32c3548ecc2a -r afbdf8a3a633 src/test/makefile --- a/src/test/makefile Thu Apr 29 19:38:53 2004 +0000 +++ b/src/test/makefile Thu Apr 29 22:39:17 2004 +0000 @@ -2,7 +2,7 @@ CXXFLAGS += -Wall -O3 -ansi -pedantic $(INCLUDEDIRS) #LEDAROOT ?= /ledasrc/LEDA-4.1 -BINARIES = dijkstra_heap_test unionfind_test +BINARIES = dijkstra_heap_test unionfind_test error_test ifdef GCCVER CXX := g++-$(GCCVER)