sl@0: // sl@0: // Copyright (c) 2000-2002 sl@0: // Joerg Walter, Mathias Koch sl@0: // sl@0: // Permission to use, copy, modify, distribute and sell this software sl@0: // and its documentation for any purpose is hereby granted without fee, sl@0: // provided that the above copyright notice appear in all copies and sl@0: // that both that copyright notice and this permission notice appear sl@0: // in supporting documentation. The authors make no representations sl@0: // about the suitability of this software for any purpose. sl@0: // It is provided "as is" without express or implied warranty. sl@0: // sl@0: // The authors gratefully acknowledge the support of sl@0: // GeNeSys mbH & Co. KG in producing this work. sl@0: // sl@0: sl@0: #ifndef _BOOST_UBLAS_EXCEPTION_ sl@0: #define _BOOST_UBLAS_EXCEPTION_ sl@0: sl@0: #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS) sl@0: #include sl@0: #else sl@0: #include sl@0: #endif sl@0: #ifndef BOOST_UBLAS_NO_STD_CERR sl@0: #include sl@0: #endif sl@0: sl@0: #include sl@0: sl@0: namespace boost { namespace numeric { namespace ublas { sl@0: sl@0: struct divide_by_zero sl@0: #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS) sl@0: // Inherit from standard exceptions as requested during review. sl@0: : public std::runtime_error { sl@0: explicit divide_by_zero (const char *s = "divide by zero") : sl@0: std::runtime_error (s) {} sl@0: void raise () { sl@0: throw *this; sl@0: } sl@0: #else sl@0: { sl@0: divide_by_zero () sl@0: {} sl@0: explicit divide_by_zero (const char *) sl@0: {} sl@0: void raise () { sl@0: std::abort (); sl@0: } sl@0: #endif sl@0: }; sl@0: sl@0: struct internal_logic sl@0: #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS) sl@0: // Inherit from standard exceptions as requested during review. sl@0: : public std::logic_error { sl@0: explicit internal_logic (const char *s = "internal logic") : sl@0: std::logic_error (s) {} sl@0: void raise () { sl@0: throw *this; sl@0: } sl@0: #else sl@0: { sl@0: internal_logic () sl@0: {} sl@0: explicit internal_logic (const char *) sl@0: {} sl@0: void raise () { sl@0: std::abort (); sl@0: } sl@0: #endif sl@0: }; sl@0: sl@0: struct external_logic sl@0: #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS) sl@0: // Inherit from standard exceptions as requested during review. sl@0: : public std::logic_error { sl@0: explicit external_logic (const char *s = "external logic") : sl@0: std::logic_error (s) {} sl@0: // virtual const char *what () const throw () { sl@0: // return "exception: external logic"; sl@0: // } sl@0: void raise () { sl@0: throw *this; sl@0: } sl@0: #else sl@0: { sl@0: external_logic () sl@0: {} sl@0: explicit external_logic (const char *) sl@0: {} sl@0: void raise () { sl@0: std::abort (); sl@0: } sl@0: #endif sl@0: }; sl@0: sl@0: struct bad_argument sl@0: #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS) sl@0: // Inherit from standard exceptions as requested during review. sl@0: : public std::invalid_argument { sl@0: explicit bad_argument (const char *s = "bad argument") : sl@0: std::invalid_argument (s) {} sl@0: void raise () { sl@0: throw *this; sl@0: } sl@0: #else sl@0: { sl@0: bad_argument () sl@0: {} sl@0: explicit bad_argument (const char *) sl@0: {} sl@0: void raise () { sl@0: std::abort (); sl@0: } sl@0: #endif sl@0: }; sl@0: sl@0: struct bad_size sl@0: #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS) sl@0: // Inherit from standard exceptions as requested during review. sl@0: : public std::domain_error { sl@0: explicit bad_size (const char *s = "bad size") : sl@0: std::domain_error (s) {} sl@0: void raise () { sl@0: throw *this; sl@0: } sl@0: #else sl@0: { sl@0: bad_size () sl@0: {} sl@0: explicit bad_size (const char *) sl@0: {} sl@0: void raise () { sl@0: std::abort (); sl@0: } sl@0: #endif sl@0: }; sl@0: sl@0: struct bad_index sl@0: #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS) sl@0: // Inherit from standard exceptions as requested during review. sl@0: : public std::out_of_range { sl@0: explicit bad_index (const char *s = "bad index") : sl@0: std::out_of_range (s) {} sl@0: void raise () { sl@0: throw *this; sl@0: } sl@0: #else sl@0: { sl@0: bad_index () sl@0: {} sl@0: explicit bad_index (const char *) sl@0: {} sl@0: void raise () { sl@0: std::abort (); sl@0: } sl@0: #endif sl@0: }; sl@0: sl@0: struct singular sl@0: #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS) sl@0: // Inherit from standard exceptions as requested during review. sl@0: : public std::runtime_error { sl@0: explicit singular (const char *s = "singular") : sl@0: std::runtime_error (s) {} sl@0: void raise () { sl@0: throw *this; sl@0: } sl@0: #else sl@0: { sl@0: singular () sl@0: {} sl@0: explicit singular (const char *) sl@0: {} sl@0: void raise () { sl@0: throw *this; sl@0: std::abort (); sl@0: } sl@0: #endif sl@0: }; sl@0: sl@0: struct non_real sl@0: #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS) sl@0: // Inherit from standard exceptions as requested during review. sl@0: : public std::domain_error { sl@0: explicit non_real (const char *s = "exception: non real") : sl@0: std::domain_error (s) {} sl@0: void raise () { sl@0: throw *this; sl@0: } sl@0: #else sl@0: { sl@0: non_real () sl@0: {} sl@0: explicit non_real (const char *) sl@0: {} sl@0: void raise () { sl@0: std::abort (); sl@0: } sl@0: #endif sl@0: }; sl@0: sl@0: #if BOOST_UBLAS_CHECK_ENABLE sl@0: // Macros are equivilent to sl@0: // template sl@0: // BOOST_UBLAS_INLINE sl@0: // void check (bool expression, const E &e) { sl@0: // if (! expression) sl@0: // e.raise (); sl@0: // } sl@0: // template sl@0: // BOOST_UBLAS_INLINE sl@0: // void check_ex (bool expression, const char *file, int line, const E &e) { sl@0: // if (! expression) sl@0: // e.raise (); sl@0: // } sl@0: #ifndef BOOST_UBLAS_NO_STD_CERR sl@0: #define BOOST_UBLAS_CHECK_FALSE(e) \ sl@0: std::cerr << "Check failed in file " << __FILE__ << " at line " << __LINE__ << ":" << std::endl; \ sl@0: e.raise (); sl@0: #define BOOST_UBLAS_CHECK(expression, e) \ sl@0: if (! (expression)) { \ sl@0: std::cerr << "Check failed in file " << __FILE__ << " at line " << __LINE__ << ":" << std::endl; \ sl@0: std::cerr << #expression << std::endl; \ sl@0: e.raise (); \ sl@0: } sl@0: #define BOOST_UBLAS_CHECK_EX(expression, file, line, e) \ sl@0: if (! (expression)) { \ sl@0: std::cerr << "Check failed in file " << (file) << " at line " << (line) << ":" << std::endl; \ sl@0: std::cerr << #expression << std::endl; \ sl@0: e.raise (); \ sl@0: } sl@0: #else sl@0: #define BOOST_UBLAS_CHECK_FALSE(e) \ sl@0: e.raise (); sl@0: #define BOOST_UBLAS_CHECK(expression, e) \ sl@0: if (! (expression)) { \ sl@0: e.raise (); \ sl@0: } sl@0: #define BOOST_UBLAS_CHECK_EX(expression, file, line, e) \ sl@0: if (! (expression)) { \ sl@0: e.raise (); \ sl@0: } sl@0: #endif sl@0: #else sl@0: // Macros are equivilent to sl@0: // template sl@0: // BOOST_UBLAS_INLINE sl@0: // void check (bool expression, const E &e) {} sl@0: // template sl@0: // BOOST_UBLAS_INLINE sl@0: // void check_ex (bool expression, const char *file, int line, const E &e) {} sl@0: #define BOOST_UBLAS_CHECK_FALSE(e) sl@0: #define BOOST_UBLAS_CHECK(expression, e) sl@0: #define BOOST_UBLAS_CHECK_EX(expression, file, line, e) sl@0: #endif sl@0: sl@0: sl@0: #ifndef BOOST_UBLAS_USE_FAST_SAME sl@0: // Macro is equivilent to sl@0: // template sl@0: // BOOST_UBLAS_INLINE sl@0: // const T &same_impl (const T &size1, const T &size2) { sl@0: // BOOST_UBLAS_CHECK (size1 == size2, bad_argument ()); sl@0: // return (std::min) (size1, size2); sl@0: // } sl@0: // #define BOOST_UBLAS_SAME(size1, size2) same_impl ((size1), (size2)) sl@0: template sl@0: BOOST_UBLAS_INLINE sl@0: // Kresimir Fresl and Dan Muller reported problems with COMO. sl@0: // We better change the signature instead of libcomo ;-) sl@0: // const T &same_impl_ex (const T &size1, const T &size2, const char *file, int line) { sl@0: T same_impl_ex (const T &size1, const T &size2, const char *file, int line) { sl@0: BOOST_UBLAS_CHECK_EX (size1 == size2, file, line, bad_argument ()); sl@0: return (std::min) (size1, size2); sl@0: } sl@0: #define BOOST_UBLAS_SAME(size1, size2) same_impl_ex ((size1), (size2), __FILE__, __LINE__) sl@0: #else sl@0: // Macros are equivilent to sl@0: // template sl@0: // BOOST_UBLAS_INLINE sl@0: // const T &same_impl (const T &size1, const T &size2) { sl@0: // return size1; sl@0: // } sl@0: // #define BOOST_UBLAS_SAME(size1, size2) same_impl ((size1), (size2)) sl@0: #define BOOST_UBLAS_SAME(size1, size2) (size1) sl@0: #endif sl@0: sl@0: }}} sl@0: sl@0: #endif