diff --git a/interfaces/driver/lb-utilities.h b/interfaces/driver/lb-utilities.h index c7214186a0..c4d2159296 100644 --- a/interfaces/driver/lb-utilities.h +++ b/interfaces/driver/lb-utilities.h @@ -36,7 +36,7 @@ /************************************** * definition of LinBox's exceptions * **************************************/ -typedef LinBox::LinboxError lb_runtime_error; +typedef LinBox::Error lb_runtime_error; diff --git a/linbox/algorithms/rational-cra-builder-early-single.h b/linbox/algorithms/rational-cra-builder-early-single.h index 50aaed41ad..a271654ccc 100644 --- a/linbox/algorithms/rational-cra-builder-early-single.h +++ b/linbox/algorithms/rational-cra-builder-early-single.h @@ -105,6 +105,7 @@ namespace LinBox Integer& result(Integer& d) { + // @fixme Consider a better error object throw "not a good idea calling this function here !!" ; } protected: diff --git a/linbox/matrix/densematrix/blas-matrix.h b/linbox/matrix/densematrix/blas-matrix.h index 104923b82a..0e7bc7ad8e 100644 --- a/linbox/matrix/densematrix/blas-matrix.h +++ b/linbox/matrix/densematrix/blas-matrix.h @@ -449,7 +449,8 @@ namespace LinBox /*! @internal * Get read-only pointer to the matrix data. */ - pointer getPointer() const ; + pointer getPointer() ; + const_pointer getPointer() const ; const_pointer &getConstPointer() const ; diff --git a/linbox/matrix/sparse-formats.h b/linbox/matrix/sparse-formats.h index 351190c810..2cca4da98e 100644 --- a/linbox/matrix/sparse-formats.h +++ b/linbox/matrix/sparse-formats.h @@ -30,16 +30,6 @@ #include "linbox/linbox-tags.h" namespace LinBox { - - /** Exception class for invalid matrix input - */ - namespace Exceptions { - class InvalidMatrixInput {}; - } - - - - //! Sparse matrix format (memory storage) namespace SparseMatrixFormat { class ANY {} ; diff --git a/linbox/util/debug.h b/linbox/util/debug.h index d515aa9195..de01e3452d 100644 --- a/linbox/util/debug.h +++ b/linbox/util/debug.h @@ -36,313 +36,78 @@ #ifndef __LINBOX_util_debug_H #define __LINBOX_util_debug_H +#include "linbox/util/error.h" #include +#include #include -#include "linbox/util/error.h" #include -#include -/*! Check an assertion (à la \c std::assert). +/** + * Check an assertion (à la \c std::assert). + * * If in DEBUG mode, throws a \ref PreconditionFailed exception. - * In REALEASE mode, nothing is checked. + * In RELEASE mode, nothing is checked. * @param check assertion to be checked. */ #ifdef NDEBUG // à la assert. -# define linbox_check(check) +#define linbox_check(check) +#else +#ifdef __GNUC__ +#define linbox_check(check) \ + if (!(check)) throw ::LinBox::PreconditionFailed(__func__, __FILE__, __LINE__, #check); #else -# ifdef __GNUC__ -# define linbox_check(check) \ - if (!(check)) \ - throw ::LinBox::PreconditionFailed (__func__, __FILE__, __LINE__, #check); //BB : should work on non gnu compilers too -# else -# define linbox_check(check) \ - if (!(check)) \ - throw ::LinBox::PreconditionFailed (__FILE__, __LINE__, #check); -# endif +#define linbox_check(check) \ + if (!(check)) throw ::LinBox::PreconditionFailed(__FILE__, __LINE__, #check); +#endif #endif -#define THIS_CODE_COMPILES_BUT_IS_NOT_TESTED \ - std::cout << "*** Warning *** " << std::endl << __func__ << " in " << __FILE__ << ':' << __LINE__ << " is not tested" << std::endl; - -#define THIS_CODE_MAY_NOT_COMPILE_AND_IS_NOT_TESTED \ -throw(" *** Warning *** this piece of code is not compiled by default and may not work") - -#define LB_FILE_LOC \ - __func__,__FILE__,__LINE__ - -namespace LinBox -{ /* Preconditions,Error,Failure,NotImplementedYet */ - /*! A precondition failed. - * @ingroup util - * The \c throw mechanism is usually used here as in - \code - if (!check) - throw(PreconditionFailed(__func__,__LINE__,"this check just failed"); - \endcode - * The parameters of the constructor help debugging. - */ - class PreconditionFailed {//: public LinboxError BB: otherwise, error.h:39 segfaults - static std::ostream *_errorStream; - - public: - /*! @internal - * A precondtion failed. - * @param function usually \c __func__, the function that threw the error - * @param line usually \c __LINE__, the line where it happened - * @param check a string telling what failed. - */ - PreconditionFailed (const char *function, int line, const char *check) - { - if (_errorStream == (std::ostream *) 0) - _errorStream = &std::cerr; - - (*_errorStream) << std::endl << std::endl; - (*_errorStream) << "ERROR (" << function << ":" << line << "): "; - (*_errorStream) << "Precondition not met:" << check << std::endl; - } - - /*! @internal - * A precondtion failed. - * The parameter help debugging. This is not much different from the previous - * except we can digg faster in the file where the exception was triggered. - * @param function usually \c __func__, the function that threw the error - * @param file usually \c __FILE__, the file where this function is - * @param line usually \c __LINE__, the line where it happened - * @param check a string telling what failed. - */ - PreconditionFailed (const char* function, const char *file, int line, const char *check) - { - if (_errorStream == (std::ostream *) 0) - _errorStream = &std::cerr; - - (*_errorStream) << std::endl << std::endl; - (*_errorStream) << "ERROR (at " << function << " in " << file << ':' << line << "): " << std::endl; - (*_errorStream) << "Precondition not met:" << check << std::endl; - } - - static void setErrorStream (std::ostream &stream); - - /*! @internal overload the virtual print of LinboxError. - * @param o output stream - */ - std::ostream &print (std::ostream &o) const - { - if (std::ostringstream * str = dynamic_cast(_errorStream)) - return o << str->str() ; - else - throw LinboxError("LinBox ERROR: PreconditionFailed exception is not initialized correctly"); - } - }; - - /*! @internal A function is "not implemented yet(tm)". - * where, why ? - */ - class NotImplementedYet { - protected: - static std::ostream *_errorStream; - - public: - /*! @internal - * A precondtion failed. - * The parameter help debugging. This is not much different from the previous - * except we can digg faster in the file where the exception was triggered. - * @param function usually \c __func__, the function that threw the error - * @param file usually \c __FILE__, the file where this function is - * @param line usually \c __LINE__, the line where it happened - * @param why by default, lazy people don't provide an explanation. - */ - NotImplementedYet() {} - - NotImplementedYet(const std::string& why) - : NotImplementedYet(why.c_str()) - {} - - NotImplementedYet( const char * why) - { - if (_errorStream == (std::ostream *) 0) - _errorStream = &std::cerr; - - (*_errorStream) << std::endl << std::endl; - (*_errorStream) << "*** ERROR ***" << std::endl; - (*_errorStream) << " This function is not implemented yet " ; - (*_errorStream) << " (" << why << ")" <=0) - (*_errorStream) << ':' << line ; - (*_errorStream) << ")" ; - } - } - else if (file) { // extremely unlikely... - (*_errorStream) << "(in " << file ; - if (line>=0) - (*_errorStream) << ':' << line ; - (*_errorStream) << ")" ; - } - (*_errorStream) << std::endl; - } - }; -} - +// @fixme THIS IS UGLY +#define THIS_CODE_COMPILES_BUT_IS_NOT_TESTED \ + std::cout << "*** Warning *** " << std::endl \ + << __func__ << " in " << __FILE__ << ':' << __LINE__ << " is not tested" << std::endl; -namespace LinBox -{ /* Exceptions. */ +#define THIS_CODE_MAY_NOT_COMPILE_AND_IS_NOT_TESTED \ + throw(" *** Warning *** this piece of code is not compiled by default and may not work") - /*! @defgroup exceptions Exceptions. - * @brief Exceptions in LinBox (proposal, example in \c algorithms/hermite.h). - * If the algorithms cannot return as expected, then an exception is - * thrown. Hopefully it is catched later on. Execptions, when thrown, - * don't write to any stream except in debug mode. However, they can - * explain what they are for with the - * const char *what(void) member. - * - * Any exception derives from the \c LinBox::Exception class. - */ +#define LB_FILE_LOC __func__, __FILE__, __LINE__ - /*! This is the exception class in LinBox. - * Any LinBox exception can derive from it. - */ - class Exception { - // public: - // Exception() {}; - } ; +#if defined(LinBoxSrcOnly) or defined(LinBoxTestOnly) +// for all-source compilation +#include "linbox/util/debug.C" +#endif - /*! Algorithmic exception. - */ - class algoException : public Exception { - // public: - // algoException() {}; - }; +#include - /*! Not implemented yet. - * This piece of code is not fully implemented. - */ - class NotImplementedYetException : public Exception { - }; +// @note Taken from contracts.h +#if _DEBUG == 2 - /*! Something bad an unexpected happened. - */ - class IrrecuperableException : public Exception { - }; +// The debug mode also defines the following macros. Failure of any of these macros leads to +// program termination. The user is notified of the error condition with the right file name +// and line number. The actual failing operation is also printed using the stringizing operator # - /*! The input is not as expected. - */ - class BadInputException : public Exception { - }; -} +#define ASSERT(bool_expression) assert(bool_expression) +#define REQUIRE(bool_expression) ASSERT(bool_expression) +#define STATE(expression) expression -#define CONC(a,b) a ## b +#elif _DEBUG == 1 -#define LINBOX_SILENT_EXCEPTION(name) \ - throw CONC(name,Exception) () +// @fixme InvalidOperationException does not exists +#define ASSERT(bool_expression) assert(bool_expression) +#define REQUIRE(bool_expression) \ + if (!bool_expression) throw InvalidOperationException(__FILE__, __LINE__); +#define STATE(expression) expression -#ifndef DEBUG -#define LINBOX_RAISE_EXCEPTION(name,why) \ - throw CONC(name,Exception) () -#else -#define LINBOX_RAISE_EXCEPTION(name,why) \ - do { \ - std::cerr << " *** EXCEPTION *** (at " << __func__ << " in " << __FILE__ << ':' << __LINE__ << ") " << std::endl; \ - std::cerr << " " << why << std::endl; \ - throw CONC(name,Exception) () ; \ - } while(0) -#endif +#elif _DEBUG == 0 +// When built in release mode, the _DEBUG flag would not be defined, thus there will be no overhead +// in the final release from these checks. +#define ASSERT(ignore) ((void)0) +#define REQUIRE(ignore) ((void)0) +#define STATE(ignore) ((void)0) -#if defined(LinBoxSrcOnly) or defined(LinBoxTestOnly) -// for all-source compilation -#include "linbox/util/debug.C" #endif -#include - #endif // __LINBOX_util_debug_H // Local Variables: diff --git a/linbox/util/error.h b/linbox/util/error.h index 4a373a2b38..5f24714f44 100644 --- a/linbox/util/error.h +++ b/linbox/util/error.h @@ -6,7 +6,7 @@ * ========LICENCE======== * This file is part of the library LinBox. * - * LinBox is free software: you can redistribute it and/or modify + * LinBox is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. @@ -28,6 +28,202 @@ #include #include +/** + * I feel like we need: + * CheckFailed (old PreconditionFailed, only throw by linbox_check() + * NotImplementedYet + * NotTested (old THIS_CODE_COMPILES_BUT_IS_NOT_TESTED) + * ?? (NO PROBABLY NOT AN EXCEPTION) + * InternalError (old LinBoxFailure) + * Error (old LinBoxError/LinboxError/lb_runtime_error) + * ?? (OR RuntimeError) + * InconsistentSystem (old InconsistentSystem/MathInconsistentSystem) + * ?? (SHOULD WE KEEP CERTIFICATE INSIDE? SHOULD IT BE A CHILD OF SOLVEFAILED?) + * SolveFailed + * InvalidInput (old InvalidMatrixInput) + * + * Also feels like we need a auto __LINE__ __FILE__, + * so Error(msg) might be a macro expanding to ErrorException(msg, __LINE__, __FILE__) being a class. + */ + +namespace LinBox { /* Preconditions,Error,Failure,NotImplementedYet */ + /*! A precondition failed. + * @ingroup util + * The \c throw mechanism is usually used here as in + \code + if (!check) + throw(PreconditionFailed(__func__,__LINE__,"this check just failed"); + \endcode + * The parameters of the constructor help debugging. + */ + class PreconditionFailed { //: public LinboxError BB: otherwise, error.h:39 segfaults + static std::ostream* _errorStream; + + public: + /*! @internal + * A precondtion failed. + * @param function usually \c __func__, the function that threw the error + * @param line usually \c __LINE__, the line where it happened + * @param check a string telling what failed. + */ + PreconditionFailed(const char* function, int line, const char* check) + { + if (_errorStream == (std::ostream*)0) _errorStream = &std::cerr; + + (*_errorStream) << std::endl << std::endl; + (*_errorStream) << "ERROR (" << function << ":" << line << "): "; + (*_errorStream) << "Precondition not met:" << check << std::endl; + } + + /*! @internal + * A precondtion failed. + * The parameter help debugging. This is not much different from the previous + * except we can digg faster in the file where the exception was triggered. + * @param function usually \c __func__, the function that threw the error + * @param file usually \c __FILE__, the file where this function is + * @param line usually \c __LINE__, the line where it happened + * @param check a string telling what failed. + */ + PreconditionFailed(const char* function, const char* file, int line, const char* check) + { + if (_errorStream == (std::ostream*)0) _errorStream = &std::cerr; + + (*_errorStream) << std::endl << std::endl; + (*_errorStream) << "ERROR (at " << function << " in " << file << ':' << line << "): " << std::endl; + (*_errorStream) << "Precondition not met:" << check << std::endl; + } + + static void setErrorStream(std::ostream& stream); + + /*! @internal overload the virtual print of LinboxError. + * @param o output stream + */ + std::ostream& print(std::ostream& o) const + { + if (std::ostringstream* str = dynamic_cast(_errorStream)) + return o << str->str(); + else + throw LinboxError("LinBox ERROR: PreconditionFailed exception is not initialized correctly"); + } + }; + + /*! @internal A function is "not implemented yet(tm)". + * where, why ? + */ + class NotImplementedYet { + protected: + static std::ostream* _errorStream; + + public: + /*! @internal + * A precondtion failed. + * The parameter help debugging. This is not much different from the previous + * except we can digg faster in the file where the exception was triggered. + * @param function usually \c __func__, the function that threw the error + * @param file usually \c __FILE__, the file where this function is + * @param line usually \c __LINE__, the line where it happened + * @param why by default, lazy people don't provide an explanation. + */ + NotImplementedYet() {} + + NotImplementedYet(const std::string& why) + : NotImplementedYet(why.c_str()) + { + } + + NotImplementedYet(const char* why) + { + if (_errorStream == (std::ostream*)0) _errorStream = &std::cerr; + + (*_errorStream) << std::endl << std::endl; + (*_errorStream) << "*** ERROR ***" << std::endl; + (*_errorStream) << " This function is not implemented yet "; + (*_errorStream) << " (" << why << ")" << std::endl; + } + + NotImplementedYet(const char* function, const char* file, int line, const char* why = "\0") + { + if (_errorStream == (std::ostream*)0) _errorStream = &std::cerr; + + (*_errorStream) << std::endl << std::endl; + (*_errorStream) << " *** ERROR *** (at " << function << " in " << file << ':' << line << "): " << std::endl; + (*_errorStream) << " This function is not implemented yet"; + if (why) + (*_errorStream) << " (" << why << ")" << std::endl; + else + (*_errorStream) << "." << std::endl; + } + }; + + /*! @internal Something went wrong. + * what ? + */ + class LinBoxFailure : public NotImplementedYet { + public: + /*! @internal + * LinBox failed. + * The parameter help debugging/explaining. + * @param function usually \c __func__, the function that threw the error + * @param file usually \c __FILE__, the file where this function is + * @param line usually \c __LINE__, the line where it happened + * @param what what happened ? should not be NULL... + */ + LinBoxFailure(const char* function, const char* file = "\0", int line = -1, const char* what = "\0") + { + if (_errorStream == (std::ostream*)0) _errorStream = &std::cerr; + + (*_errorStream) << std::endl << std::endl; + (*_errorStream) << "ERROR (at " << function << " in " << file << ':' << line << "): " << std::endl; + (*_errorStream) << " failure : "; + if (what) + (*_errorStream) << what << "." << std::endl; + else + (*_errorStream) << "no explanation." << std::endl; + } + }; + + /*! @internal Something is wrong. + * what ? + */ + class LinBoxError : public NotImplementedYet { + public: + LinBoxError(const std::string& what, const char* function = "\0", const char* file = "\0", int line = -1) + : LinBoxError(what.c_str(), function, file, line) + { + } + + /*! @internal + * User failed. + * The parameter help debugging/explaining. + * @param function usually \c __func__, the function that threw the error + * @param file usually \c __FILE__, the file where this function is + * @param line usually \c __LINE__, the line where it happened + * @param what what happened ? should not be NULL... + */ + LinBoxError(const char* what, const char* function = "\0", const char* file = "\0", int line = -1) + { + if (_errorStream == (std::ostream*)0) _errorStream = &std::cerr; + + (*_errorStream) << std::endl << std::endl; + (*_errorStream) << " *** ERROR *** : " << what << std::endl; + if (function) { + (*_errorStream) << "(at " << function; + if (file) { + (*_errorStream) << " in " << file; + if (line >= 0) (*_errorStream) << ':' << line; + (*_errorStream) << ")"; + } + } + else if (file) { // extremely unlikely... + (*_errorStream) << "(in " << file; + if (line >= 0) (*_errorStream) << ':' << line; + (*_errorStream) << ")"; + } + (*_errorStream) << std::endl; + } + }; +} + namespace LinBox { // ------------------------------- LinboxError @@ -72,25 +268,12 @@ namespace LinBox { : LinboxError(msg){}; }; - class LinboxMathDivZero : public LinboxMathError { - public: - LinboxMathDivZero(const char* msg) - : LinboxMathError(msg){}; - }; - class LinboxMathInconsistentSystem : public LinboxMathError { public: LinboxMathInconsistentSystem(const char* msg) : LinboxMathError(msg){}; }; - // -- Exception thrown in input of data structure - class LinboxBadFormat : public LinboxError { - public: - LinboxBadFormat(const char* msg) - : LinboxError(msg){}; - }; - // -- Exception thrown when probabilistic solve fails class SolveFailed : public LinboxError { public: @@ -119,11 +302,18 @@ namespace LinBox { }; } +/** Exception class for invalid matrix input + */ +namespace Exceptions { + class InvalidMatrixInput { + }; +} + #ifdef LinBoxSrcOnly // for all-source compilation #include "linbox/util/error.C" #endif -#endif // __LINBOX_util_error_H +#endif // Local Variables: // mode: C++