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