exception.hpp 8.6 KB

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