1 // Copyright (c) 2000-2011 Joerg Walter, Mathias Koch, David Bellot
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef _BOOST_UBLAS_EXCEPTION_
8 #define _BOOST_UBLAS_EXCEPTION_
10 #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS)
15 #ifndef BOOST_UBLAS_NO_STD_CERR
19 #include <boost/numeric/ublas/detail/config.hpp>
21 namespace boost { namespace numeric { namespace ublas {
23 /** \brief Exception raised when a division by zero occurs
26 #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS)
27 // Inherit from standard exceptions as requested during review.
28 : public std::runtime_error
30 explicit divide_by_zero (const char *s = "divide by zero") :
31 std::runtime_error (s) {}
39 explicit divide_by_zero (const char *)
47 /** \brief Expception raised when some interal errors occurs like computations errors, zeros values where you should not have zeros, etc...
50 #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS)
51 // Inherit from standard exceptions as requested during review.
52 : public std::logic_error {
53 explicit internal_logic (const char *s = "internal logic") :
54 std::logic_error (s) {}
62 explicit internal_logic (const char *)
71 #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS)
72 // Inherit from standard exceptions as requested during review.
73 : public std::logic_error {
74 explicit external_logic (const char *s = "external logic") :
75 std::logic_error (s) {}
76 // virtual const char *what () const throw () {
77 // return "exception: external logic";
86 explicit external_logic (const char *)
95 #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS)
96 // Inherit from standard exceptions as requested during review.
97 : public std::invalid_argument {
98 explicit bad_argument (const char *s = "bad argument") :
99 std::invalid_argument (s) {}
107 explicit bad_argument (const char *)
118 #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS)
119 // Inherit from standard exceptions as requested during review.
120 : public std::domain_error {
121 explicit bad_size (const char *s = "bad size") :
122 std::domain_error (s) {}
130 explicit bad_size (const char *)
139 #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS)
140 // Inherit from standard exceptions as requested during review.
141 : public std::out_of_range {
142 explicit bad_index (const char *s = "bad index") :
143 std::out_of_range (s) {}
151 explicit bad_index (const char *)
160 #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS)
161 // Inherit from standard exceptions as requested during review.
162 : public std::runtime_error {
163 explicit singular (const char *s = "singular") :
164 std::runtime_error (s) {}
172 explicit singular (const char *)
181 #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS)
182 // Inherit from standard exceptions as requested during review.
183 : public std::domain_error {
184 explicit non_real (const char *s = "exception: non real") :
185 std::domain_error (s) {}
193 explicit non_real (const char *)
201 #if BOOST_UBLAS_CHECK_ENABLE
202 // Macros are equivilent to
204 // BOOST_UBLAS_INLINE
205 // void check (bool expression, const E &e) {
210 // BOOST_UBLAS_INLINE
211 // void check_ex (bool expression, const char *file, int line, const E &e) {
215 #ifndef BOOST_UBLAS_NO_STD_CERR
216 #define BOOST_UBLAS_CHECK_FALSE(e) \
217 std::cerr << "Check failed in file " << __FILE__ << " at line " << __LINE__ << ":" << std::endl; \
219 #define BOOST_UBLAS_CHECK(expression, e) \
220 if (! (expression)) { \
221 std::cerr << "Check failed in file " << __FILE__ << " at line " << __LINE__ << ":" << std::endl; \
222 std::cerr << #expression << std::endl; \
225 #define BOOST_UBLAS_CHECK_EX(expression, file, line, e) \
226 if (! (expression)) { \
227 std::cerr << "Check failed in file " << (file) << " at line " << (line) << ":" << std::endl; \
228 std::cerr << #expression << std::endl; \
232 #define BOOST_UBLAS_CHECK_FALSE(e) \
234 #define BOOST_UBLAS_CHECK(expression, e) \
235 if (! (expression)) { \
238 #define BOOST_UBLAS_CHECK_EX(expression, file, line, e) \
239 if (! (expression)) { \
244 // Macros are equivilent to
246 // BOOST_UBLAS_INLINE
247 // void check (bool expression, const E &e) {}
249 // BOOST_UBLAS_INLINE
250 // void check_ex (bool expression, const char *file, int line, const E &e) {}
251 #define BOOST_UBLAS_CHECK_FALSE(e)
252 #define BOOST_UBLAS_CHECK(expression, e)
253 #define BOOST_UBLAS_CHECK_EX(expression, file, line, e)
257 #ifndef BOOST_UBLAS_USE_FAST_SAME
258 // Macro is equivilent to
260 // BOOST_UBLAS_INLINE
261 // const T &same_impl (const T &size1, const T &size2) {
262 // BOOST_UBLAS_CHECK (size1 == size2, bad_argument ());
263 // return (std::min) (size1, size2);
265 // #define BOOST_UBLAS_SAME(size1, size2) same_impl ((size1), (size2))
266 // need two types here because different containers can have
267 // different size_types (especially sparse types)
268 template<class T1, class T2>
270 // Kresimir Fresl and Dan Muller reported problems with COMO.
271 // We better change the signature instead of libcomo ;-)
272 // const T &same_impl_ex (const T &size1, const T &size2, const char *file, int line) {
273 T1 same_impl_ex (const T1 &size1, const T2 &size2, const char *file, int line) {
274 BOOST_UBLAS_CHECK_EX (size1 == size2, file, line, bad_argument ());
275 return (size1 < size2)?(size1):(size2);
279 T same_impl_ex (const T &size1, const T &size2, const char *file, int line) {
280 BOOST_UBLAS_CHECK_EX (size1 == size2, file, line, bad_argument ());
281 return (std::min) (size1, size2);
283 #define BOOST_UBLAS_SAME(size1, size2) same_impl_ex ((size1), (size2), __FILE__, __LINE__)
285 // Macros are equivilent to
287 // BOOST_UBLAS_INLINE
288 // const T &same_impl (const T &size1, const T &size2) {
291 // #define BOOST_UBLAS_SAME(size1, size2) same_impl ((size1), (size2))
292 #define BOOST_UBLAS_SAME(size1, size2) (size1)