Boost.uBlas 1.49
Linear Algebra in C++: matrices, vectors and numeric algorithms

exception.hpp

Go to the documentation of this file.
00001 //  Copyright (c) 2000-2011 Joerg Walter, Mathias Koch, David Bellot
00002 //
00003 //  Distributed under the Boost Software License, Version 1.0. (See
00004 //  accompanying file LICENSE_1_0.txt or copy at
00005 //  http://www.boost.org/LICENSE_1_0.txt)
00006 
00007 #ifndef _BOOST_UBLAS_EXCEPTION_
00008 #define _BOOST_UBLAS_EXCEPTION_
00009 
00010 #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS)
00011 #include <stdexcept>
00012 #else
00013 #include <cstdlib>
00014 #endif
00015 #ifndef BOOST_UBLAS_NO_STD_CERR
00016 #include <iostream>
00017 #endif
00018 
00019 #include <boost/numeric/ublas/detail/config.hpp>
00020 
00021 namespace boost { namespace numeric { namespace ublas {
00022 
00025     struct divide_by_zero
00026 #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS)
00027         // Inherit from standard exceptions as requested during review.
00028         : public std::runtime_error 
00029     {
00030         explicit divide_by_zero (const char *s = "divide by zero") :
00031             std::runtime_error (s) {}
00032         void raise () {
00033             throw *this;
00034         }
00035 #else
00036     {
00037         divide_by_zero ()
00038             {}
00039         explicit divide_by_zero (const char *)
00040             {}
00041         void raise () {
00042             std::abort ();
00043         }
00044 #endif
00045     };
00046 
00049     struct internal_logic
00050 #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS)
00051         // Inherit from standard exceptions as requested during review.
00052         : public std::logic_error {
00053         explicit internal_logic (const char *s = "internal logic") :
00054             std::logic_error (s) {}
00055         void raise () {
00056             throw *this;
00057         }
00058 #else
00059     {
00060         internal_logic ()
00061             {}
00062         explicit internal_logic (const char *)
00063             {}
00064         void raise () {
00065             std::abort ();
00066         }
00067 #endif
00068     };
00069 
00070     struct external_logic
00071 #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS)
00072         // Inherit from standard exceptions as requested during review.
00073         : public std::logic_error {
00074         explicit external_logic (const char *s = "external logic") :
00075             std::logic_error (s) {}
00076         // virtual const char *what () const throw () {
00077         //     return "exception: external logic";
00078         // }
00079         void raise () {
00080             throw *this;
00081         }
00082 #else
00083     {
00084         external_logic ()
00085             {}
00086         explicit external_logic (const char *)
00087             {}
00088         void raise () {
00089             std::abort ();
00090         }
00091 #endif
00092     };
00093 
00094     struct bad_argument
00095 #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS)
00096         // Inherit from standard exceptions as requested during review.
00097         : public std::invalid_argument {
00098         explicit bad_argument (const char *s = "bad argument") :
00099             std::invalid_argument (s) {}
00100         void raise () {
00101             throw *this;
00102         }
00103 #else
00104     {
00105         bad_argument ()
00106             {}
00107         explicit bad_argument (const char *)
00108             {}
00109         void raise () {
00110             std::abort ();
00111         }
00112 #endif
00113     };
00114 
00117     struct bad_size
00118 #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS)
00119         // Inherit from standard exceptions as requested during review.
00120         : public std::domain_error {
00121         explicit bad_size (const char *s = "bad size") :
00122             std::domain_error (s) {}
00123         void raise () {
00124             throw *this;
00125         }
00126 #else
00127     {
00128         bad_size ()
00129             {}
00130         explicit bad_size (const char *)
00131             {}
00132         void raise () {
00133             std::abort ();
00134         }
00135 #endif
00136     };
00137 
00138     struct bad_index
00139 #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS)
00140         // Inherit from standard exceptions as requested during review.
00141         : public std::out_of_range {
00142         explicit bad_index (const char *s = "bad index") :
00143             std::out_of_range (s) {}
00144         void raise () {
00145             throw *this;
00146         }
00147 #else
00148     {
00149         bad_index ()
00150             {}
00151         explicit bad_index (const char *)
00152             {}
00153         void raise () {
00154             std::abort ();
00155         }
00156 #endif
00157     };
00158 
00159     struct singular
00160 #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS)
00161         // Inherit from standard exceptions as requested during review.
00162         : public std::runtime_error {
00163         explicit singular (const char *s = "singular") :
00164             std::runtime_error (s) {}
00165         void raise () {
00166             throw *this;
00167         }
00168 #else
00169     {
00170         singular ()
00171             {}
00172         explicit singular (const char *)
00173             {}
00174         void raise () {
00175             std::abort ();
00176         }
00177 #endif
00178     };
00179 
00180     struct non_real
00181 #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS)
00182         // Inherit from standard exceptions as requested during review.
00183         : public std::domain_error {
00184         explicit non_real (const char *s = "exception: non real") :
00185             std::domain_error (s) {}
00186         void raise () {
00187             throw *this;
00188         }
00189 #else
00190      {
00191         non_real ()
00192             {}
00193         explicit non_real (const char *)
00194             {}
00195         void raise () {
00196             std::abort ();
00197         }
00198 #endif
00199     };
00200 
00201 #if BOOST_UBLAS_CHECK_ENABLE
00202 // Macros are equivilent to 
00203 //    template<class E>
00204 //    BOOST_UBLAS_INLINE
00205 //    void check (bool expression, const E &e) {
00206 //        if (! expression)
00207 //            e.raise ();
00208 //    }
00209 //    template<class E>
00210 //    BOOST_UBLAS_INLINE
00211 //    void check_ex (bool expression, const char *file, int line, const E &e) {
00212 //        if (! expression)
00213 //            e.raise ();
00214 //    }
00215 #ifndef BOOST_UBLAS_NO_STD_CERR
00216 #define BOOST_UBLAS_CHECK_FALSE(e) \
00217     std::cerr << "Check failed in file " << __FILE__ << " at line " << __LINE__ << ":" << std::endl; \
00218     e.raise ();
00219 #define BOOST_UBLAS_CHECK(expression, e) \
00220     if (! (expression)) { \
00221         std::cerr << "Check failed in file " << __FILE__ << " at line " << __LINE__ << ":" << std::endl; \
00222         std::cerr << #expression << std::endl; \
00223         e.raise (); \
00224     }
00225 #define BOOST_UBLAS_CHECK_EX(expression, file, line, e) \
00226     if (! (expression)) { \
00227         std::cerr << "Check failed in file " << (file) << " at line " << (line) << ":" << std::endl; \
00228         std::cerr << #expression << std::endl; \
00229         e.raise (); \
00230     }
00231 #else
00232 #define BOOST_UBLAS_CHECK_FALSE(e) \
00233     e.raise ();
00234 #define BOOST_UBLAS_CHECK(expression, e) \
00235     if (! (expression)) { \
00236         e.raise (); \
00237     }
00238 #define BOOST_UBLAS_CHECK_EX(expression, file, line, e) \
00239     if (! (expression)) { \
00240         e.raise (); \
00241     }
00242 #endif
00243 #else
00244 // Macros are equivilent to 
00245 //    template<class E>
00246 //    BOOST_UBLAS_INLINE
00247 //    void check (bool expression, const E &e) {}
00248 //    template<class E>
00249 //    BOOST_UBLAS_INLINE
00250 //    void check_ex (bool expression, const char *file, int line, const E &e) {}
00251 #define BOOST_UBLAS_CHECK_FALSE(e)
00252 #define BOOST_UBLAS_CHECK(expression, e)
00253 #define BOOST_UBLAS_CHECK_EX(expression, file, line, e)
00254 #endif
00255 
00256 
00257 #ifndef BOOST_UBLAS_USE_FAST_SAME
00258 // Macro is equivilent to 
00259 //    template<class T>
00260 //    BOOST_UBLAS_INLINE
00261 //    const T &same_impl (const T &size1, const T &size2) {
00262 //        BOOST_UBLAS_CHECK (size1 == size2, bad_argument ());
00263 //        return (std::min) (size1, size2);
00264 //    }
00265 // #define BOOST_UBLAS_SAME(size1, size2) same_impl ((size1), (size2))
00266      // need two types here because different containers can have
00267      // different size_types (especially sparse types)
00268     template<class T1, class T2>
00269     BOOST_UBLAS_INLINE
00270     // Kresimir Fresl and Dan Muller reported problems with COMO.
00271     // We better change the signature instead of libcomo ;-)
00272     // const T &same_impl_ex (const T &size1, const T &size2, const char *file, int line) {
00273     T1 same_impl_ex (const T1 &size1, const T2 &size2, const char *file, int line) {
00274         BOOST_UBLAS_CHECK_EX (size1 == size2, file, line, bad_argument ());
00275         return (size1 < size2)?(size1):(size2);
00276     }
00277     template<class T>
00278     BOOST_UBLAS_INLINE
00279     T same_impl_ex (const T &size1, const T &size2, const char *file, int line) {
00280         BOOST_UBLAS_CHECK_EX (size1 == size2, file, line, bad_argument ());
00281         return (std::min) (size1, size2);
00282     }
00283 #define BOOST_UBLAS_SAME(size1, size2) same_impl_ex ((size1), (size2), __FILE__, __LINE__)
00284 #else
00285 // Macros are equivilent to 
00286 //    template<class T>
00287 //    BOOST_UBLAS_INLINE
00288 //    const T &same_impl (const T &size1, const T &size2) {
00289 //        return size1;
00290 //    }
00291 // #define BOOST_UBLAS_SAME(size1, size2) same_impl ((size1), (size2))
00292 #define BOOST_UBLAS_SAME(size1, size2) (size1)
00293 #endif
00294 
00295 }}}
00296 
00297 #endif