1 | // |
---|
2 | // Copyright (c) 2000-2002 |
---|
3 | // Joerg Walter, Mathias Koch |
---|
4 | // |
---|
5 | // Permission to use, copy, modify, distribute and sell this software |
---|
6 | // and its documentation for any purpose is hereby granted without fee, |
---|
7 | // provided that the above copyright notice appear in all copies and |
---|
8 | // that both that copyright notice and this permission notice appear |
---|
9 | // in supporting documentation. The authors make no representations |
---|
10 | // about the suitability of this software for any purpose. |
---|
11 | // It is provided "as is" without express or implied warranty. |
---|
12 | // |
---|
13 | // The authors gratefully acknowledge the support of |
---|
14 | // GeNeSys mbH & Co. KG in producing this work. |
---|
15 | // |
---|
16 | |
---|
17 | #ifndef _BOOST_UBLAS_EXCEPTION_ |
---|
18 | #define _BOOST_UBLAS_EXCEPTION_ |
---|
19 | |
---|
20 | #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS) |
---|
21 | #include <stdexcept> |
---|
22 | #else |
---|
23 | #include <cstdlib> |
---|
24 | #endif |
---|
25 | #ifndef BOOST_UBLAS_NO_STD_CERR |
---|
26 | #include <iostream> |
---|
27 | #endif |
---|
28 | |
---|
29 | #include <boost/numeric/ublas/detail/config.hpp> |
---|
30 | |
---|
31 | namespace boost { namespace numeric { namespace ublas { |
---|
32 | |
---|
33 | struct divide_by_zero |
---|
34 | #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS) |
---|
35 | // Inherit from standard exceptions as requested during review. |
---|
36 | : public std::runtime_error { |
---|
37 | explicit divide_by_zero (const char *s = "divide by zero") : |
---|
38 | std::runtime_error (s) {} |
---|
39 | void raise () { |
---|
40 | throw *this; |
---|
41 | } |
---|
42 | #else |
---|
43 | { |
---|
44 | divide_by_zero () |
---|
45 | {} |
---|
46 | explicit divide_by_zero (const char *) |
---|
47 | {} |
---|
48 | void raise () { |
---|
49 | std::abort (); |
---|
50 | } |
---|
51 | #endif |
---|
52 | }; |
---|
53 | |
---|
54 | struct internal_logic |
---|
55 | #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS) |
---|
56 | // Inherit from standard exceptions as requested during review. |
---|
57 | : public std::logic_error { |
---|
58 | explicit internal_logic (const char *s = "internal logic") : |
---|
59 | std::logic_error (s) {} |
---|
60 | void raise () { |
---|
61 | throw *this; |
---|
62 | } |
---|
63 | #else |
---|
64 | { |
---|
65 | internal_logic () |
---|
66 | {} |
---|
67 | explicit internal_logic (const char *) |
---|
68 | {} |
---|
69 | void raise () { |
---|
70 | std::abort (); |
---|
71 | } |
---|
72 | #endif |
---|
73 | }; |
---|
74 | |
---|
75 | struct external_logic |
---|
76 | #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS) |
---|
77 | // Inherit from standard exceptions as requested during review. |
---|
78 | : public std::logic_error { |
---|
79 | explicit external_logic (const char *s = "external logic") : |
---|
80 | std::logic_error (s) {} |
---|
81 | // virtual const char *what () const throw () { |
---|
82 | // return "exception: external logic"; |
---|
83 | // } |
---|
84 | void raise () { |
---|
85 | throw *this; |
---|
86 | } |
---|
87 | #else |
---|
88 | { |
---|
89 | external_logic () |
---|
90 | {} |
---|
91 | explicit external_logic (const char *) |
---|
92 | {} |
---|
93 | void raise () { |
---|
94 | std::abort (); |
---|
95 | } |
---|
96 | #endif |
---|
97 | }; |
---|
98 | |
---|
99 | struct bad_argument |
---|
100 | #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS) |
---|
101 | // Inherit from standard exceptions as requested during review. |
---|
102 | : public std::invalid_argument { |
---|
103 | explicit bad_argument (const char *s = "bad argument") : |
---|
104 | std::invalid_argument (s) {} |
---|
105 | void raise () { |
---|
106 | throw *this; |
---|
107 | } |
---|
108 | #else |
---|
109 | { |
---|
110 | bad_argument () |
---|
111 | {} |
---|
112 | explicit bad_argument (const char *) |
---|
113 | {} |
---|
114 | void raise () { |
---|
115 | std::abort (); |
---|
116 | } |
---|
117 | #endif |
---|
118 | }; |
---|
119 | |
---|
120 | struct bad_size |
---|
121 | #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS) |
---|
122 | // Inherit from standard exceptions as requested during review. |
---|
123 | : public std::domain_error { |
---|
124 | explicit bad_size (const char *s = "bad size") : |
---|
125 | std::domain_error (s) {} |
---|
126 | void raise () { |
---|
127 | throw *this; |
---|
128 | } |
---|
129 | #else |
---|
130 | { |
---|
131 | bad_size () |
---|
132 | {} |
---|
133 | explicit bad_size (const char *) |
---|
134 | {} |
---|
135 | void raise () { |
---|
136 | std::abort (); |
---|
137 | } |
---|
138 | #endif |
---|
139 | }; |
---|
140 | |
---|
141 | struct bad_index |
---|
142 | #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS) |
---|
143 | // Inherit from standard exceptions as requested during review. |
---|
144 | : public std::out_of_range { |
---|
145 | explicit bad_index (const char *s = "bad index") : |
---|
146 | std::out_of_range (s) {} |
---|
147 | void raise () { |
---|
148 | throw *this; |
---|
149 | } |
---|
150 | #else |
---|
151 | { |
---|
152 | bad_index () |
---|
153 | {} |
---|
154 | explicit bad_index (const char *) |
---|
155 | {} |
---|
156 | void raise () { |
---|
157 | std::abort (); |
---|
158 | } |
---|
159 | #endif |
---|
160 | }; |
---|
161 | |
---|
162 | struct singular |
---|
163 | #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS) |
---|
164 | // Inherit from standard exceptions as requested during review. |
---|
165 | : public std::runtime_error { |
---|
166 | explicit singular (const char *s = "singular") : |
---|
167 | std::runtime_error (s) {} |
---|
168 | void raise () { |
---|
169 | throw *this; |
---|
170 | } |
---|
171 | #else |
---|
172 | { |
---|
173 | singular () |
---|
174 | {} |
---|
175 | explicit singular (const char *) |
---|
176 | {} |
---|
177 | void raise () { |
---|
178 | throw *this; |
---|
179 | std::abort (); |
---|
180 | } |
---|
181 | #endif |
---|
182 | }; |
---|
183 | |
---|
184 | struct non_real |
---|
185 | #if ! defined (BOOST_NO_EXCEPTIONS) && ! defined (BOOST_UBLAS_NO_EXCEPTIONS) |
---|
186 | // Inherit from standard exceptions as requested during review. |
---|
187 | : public std::domain_error { |
---|
188 | explicit non_real (const char *s = "exception: non real") : |
---|
189 | std::domain_error (s) {} |
---|
190 | void raise () { |
---|
191 | throw *this; |
---|
192 | } |
---|
193 | #else |
---|
194 | { |
---|
195 | non_real () |
---|
196 | {} |
---|
197 | explicit non_real (const char *) |
---|
198 | {} |
---|
199 | void raise () { |
---|
200 | std::abort (); |
---|
201 | } |
---|
202 | #endif |
---|
203 | }; |
---|
204 | |
---|
205 | #if BOOST_UBLAS_CHECK_ENABLE |
---|
206 | // Macros are equivilent to |
---|
207 | // template<class E> |
---|
208 | // BOOST_UBLAS_INLINE |
---|
209 | // void check (bool expression, const E &e) { |
---|
210 | // if (! expression) |
---|
211 | // e.raise (); |
---|
212 | // } |
---|
213 | // template<class E> |
---|
214 | // BOOST_UBLAS_INLINE |
---|
215 | // void check_ex (bool expression, const char *file, int line, const E &e) { |
---|
216 | // if (! expression) |
---|
217 | // e.raise (); |
---|
218 | // } |
---|
219 | #ifndef BOOST_UBLAS_NO_STD_CERR |
---|
220 | #define BOOST_UBLAS_CHECK_FALSE(e) \ |
---|
221 | std::cerr << "Check failed in file " << __FILE__ << " at line " << __LINE__ << ":" << std::endl; \ |
---|
222 | e.raise (); |
---|
223 | #define BOOST_UBLAS_CHECK(expression, e) \ |
---|
224 | if (! (expression)) { \ |
---|
225 | std::cerr << "Check failed in file " << __FILE__ << " at line " << __LINE__ << ":" << std::endl; \ |
---|
226 | std::cerr << #expression << std::endl; \ |
---|
227 | e.raise (); \ |
---|
228 | } |
---|
229 | #define BOOST_UBLAS_CHECK_EX(expression, file, line, e) \ |
---|
230 | if (! (expression)) { \ |
---|
231 | std::cerr << "Check failed in file " << (file) << " at line " << (line) << ":" << std::endl; \ |
---|
232 | std::cerr << #expression << std::endl; \ |
---|
233 | e.raise (); \ |
---|
234 | } |
---|
235 | #else |
---|
236 | #define BOOST_UBLAS_CHECK_FALSE(e) \ |
---|
237 | e.raise (); |
---|
238 | #define BOOST_UBLAS_CHECK(expression, e) \ |
---|
239 | if (! (expression)) { \ |
---|
240 | e.raise (); \ |
---|
241 | } |
---|
242 | #define BOOST_UBLAS_CHECK_EX(expression, file, line, e) \ |
---|
243 | if (! (expression)) { \ |
---|
244 | e.raise (); \ |
---|
245 | } |
---|
246 | #endif |
---|
247 | #else |
---|
248 | // Macros are equivilent to |
---|
249 | // template<class E> |
---|
250 | // BOOST_UBLAS_INLINE |
---|
251 | // void check (bool expression, const E &e) {} |
---|
252 | // template<class E> |
---|
253 | // BOOST_UBLAS_INLINE |
---|
254 | // void check_ex (bool expression, const char *file, int line, const E &e) {} |
---|
255 | #define BOOST_UBLAS_CHECK_FALSE(e) |
---|
256 | #define BOOST_UBLAS_CHECK(expression, e) |
---|
257 | #define BOOST_UBLAS_CHECK_EX(expression, file, line, e) |
---|
258 | #endif |
---|
259 | |
---|
260 | |
---|
261 | #ifndef BOOST_UBLAS_USE_FAST_SAME |
---|
262 | // Macro is equivilent to |
---|
263 | // template<class T> |
---|
264 | // BOOST_UBLAS_INLINE |
---|
265 | // const T &same_impl (const T &size1, const T &size2) { |
---|
266 | // BOOST_UBLAS_CHECK (size1 == size2, bad_argument ()); |
---|
267 | // return (std::min) (size1, size2); |
---|
268 | // } |
---|
269 | // #define BOOST_UBLAS_SAME(size1, size2) same_impl ((size1), (size2)) |
---|
270 | template<class T> |
---|
271 | BOOST_UBLAS_INLINE |
---|
272 | // Kresimir Fresl and Dan Muller reported problems with COMO. |
---|
273 | // We better change the signature instead of libcomo ;-) |
---|
274 | // const T &same_impl_ex (const T &size1, const T &size2, const char *file, int line) { |
---|
275 | T same_impl_ex (const T &size1, const T &size2, const char *file, int line) { |
---|
276 | BOOST_UBLAS_CHECK_EX (size1 == size2, file, line, bad_argument ()); |
---|
277 | return (std::min) (size1, size2); |
---|
278 | } |
---|
279 | #define BOOST_UBLAS_SAME(size1, size2) same_impl_ex ((size1), (size2), __FILE__, __LINE__) |
---|
280 | #else |
---|
281 | // Macros are equivilent to |
---|
282 | // template<class T> |
---|
283 | // BOOST_UBLAS_INLINE |
---|
284 | // const T &same_impl (const T &size1, const T &size2) { |
---|
285 | // return size1; |
---|
286 | // } |
---|
287 | // #define BOOST_UBLAS_SAME(size1, size2) same_impl ((size1), (size2)) |
---|
288 | #define BOOST_UBLAS_SAME(size1, size2) (size1) |
---|
289 | #endif |
---|
290 | |
---|
291 | }}} |
---|
292 | |
---|
293 | #endif |
---|