PageRenderTime 93ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/_3rdParty/dlib/error.h

https://bitbucket.org/byzhang/mldemos
C Header | 420 lines | 221 code | 46 blank | 153 comment | 104 complexity | 9e3d78ceeb48b60aac6c8d6bc27c6bb5 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. // Copyright (C) 2003 Davis E. King (davis@dlib.net)
  2. // License: Boost Software License See LICENSE.txt for the full license.
  3. #ifndef DLIB_ERROr_
  4. #define DLIB_ERROr_
  5. #include <string>
  6. #include <new> // for std::bad_alloc
  7. #include <iostream>
  8. #include <cassert>
  9. #include <cstdlib>
  10. #include <exception>
  11. // -------------------------------
  12. // ------ exception classes ------
  13. // -------------------------------
  14. namespace dlib
  15. {
  16. // ----------------------------------------------------------------------------------------
  17. enum error_type
  18. {
  19. EOTHER,
  20. EPORT_IN_USE,
  21. ETIMEOUT,
  22. ECONNECTION,
  23. ELISTENER,
  24. ERESOLVE,
  25. EMONITOR,
  26. ECREATE_THREAD,
  27. ECREATE_MUTEX,
  28. ECREATE_SIGNALER,
  29. EUNSPECIFIED,
  30. EGENERAL_TYPE1,
  31. EGENERAL_TYPE2,
  32. EGENERAL_TYPE3,
  33. EINVALID_OPTION,
  34. ETOO_FEW_ARGS,
  35. ETOO_MANY_ARGS,
  36. ESOCKET,
  37. ETHREAD,
  38. EGUI,
  39. EFATAL,
  40. EBROKEN_ASSERT,
  41. EIMAGE_LOAD,
  42. EDIR_CREATE,
  43. EINCOMPATIBLE_OPTIONS,
  44. EMISSING_REQUIRED_OPTION,
  45. EINVALID_OPTION_ARG,
  46. EMULTIPLE_OCCURANCES,
  47. ECONFIG_READER,
  48. EIMAGE_SAVE,
  49. ECAST_TO_STRING,
  50. ESTRING_CAST,
  51. EUTF8_TO_UTF32
  52. };
  53. // ----------------------------------------------------------------------------------------
  54. // the base exception class
  55. class error : public std::exception
  56. {
  57. /*!
  58. WHAT THIS OBJECT REPRESENTS
  59. This is the base exception class for the dlib library. i.e. all
  60. exceptions in this library inherit from this class.
  61. !*/
  62. public:
  63. error(
  64. error_type t,
  65. const std::string& a
  66. ): info(a), type(t) {}
  67. /*!
  68. ensures
  69. - #type == t
  70. - #info == a
  71. !*/
  72. error(
  73. error_type t
  74. ): type(t) {}
  75. /*!
  76. ensures
  77. - #type == t
  78. - #info == ""
  79. !*/
  80. error(
  81. const std::string& a
  82. ): info(a), type(EUNSPECIFIED) {}
  83. /*!
  84. ensures
  85. - #type == EUNSPECIFIED
  86. - #info == a
  87. !*/
  88. error(
  89. ): type(EUNSPECIFIED) {}
  90. /*!
  91. ensures
  92. - #type == EUNSPECIFIED
  93. - #info == ""
  94. !*/
  95. virtual ~error(
  96. ) throw() {}
  97. /*!
  98. ensures
  99. - does nothing
  100. !*/
  101. const char* what(
  102. ) const throw()
  103. /*!
  104. ensures
  105. - if (info.size() != 0) then
  106. - returns info.c_str()
  107. - else
  108. - returns type_to_string(type)
  109. !*/
  110. {
  111. if (info.size() > 0)
  112. return info.c_str();
  113. else
  114. return type_to_string();
  115. }
  116. const char* type_to_string (
  117. ) const throw()
  118. /*!
  119. ensures
  120. - returns a string that names the contents of the type member.
  121. !*/
  122. {
  123. if (type == EOTHER) return "EOTHER";
  124. else if ( type == EPORT_IN_USE) return "EPORT_IN_USE";
  125. else if ( type == ETIMEOUT) return "ETIMEOUT";
  126. else if ( type == ECONNECTION) return "ECONNECTION";
  127. else if ( type == ELISTENER) return "ELISTENER";
  128. else if ( type == ERESOLVE) return "ERESOLVE";
  129. else if ( type == EMONITOR) return "EMONITOR";
  130. else if ( type == ECREATE_THREAD) return "ECREATE_THREAD";
  131. else if ( type == ECREATE_MUTEX) return "ECREATE_MUTEX";
  132. else if ( type == ECREATE_SIGNALER) return "ECREATE_SIGNALER";
  133. else if ( type == EUNSPECIFIED) return "EUNSPECIFIED";
  134. else if ( type == EGENERAL_TYPE1) return "EGENERAL_TYPE1";
  135. else if ( type == EGENERAL_TYPE2) return "EGENERAL_TYPE2";
  136. else if ( type == EGENERAL_TYPE3) return "EGENERAL_TYPE3";
  137. else if ( type == EINVALID_OPTION) return "EINVALID_OPTION";
  138. else if ( type == ETOO_FEW_ARGS) return "ETOO_FEW_ARGS";
  139. else if ( type == ETOO_MANY_ARGS) return "ETOO_MANY_ARGS";
  140. else if ( type == ESOCKET) return "ESOCKET";
  141. else if ( type == ETHREAD) return "ETHREAD";
  142. else if ( type == EGUI) return "EGUI";
  143. else if ( type == EFATAL) return "EFATAL";
  144. else if ( type == EBROKEN_ASSERT) return "EBROKEN_ASSERT";
  145. else if ( type == EIMAGE_LOAD) return "EIMAGE_LOAD";
  146. else if ( type == EDIR_CREATE) return "EDIR_CREATE";
  147. else if ( type == EINCOMPATIBLE_OPTIONS) return "EINCOMPATIBLE_OPTIONS";
  148. else if ( type == EMISSING_REQUIRED_OPTION) return "EMISSING_REQUIRED_OPTION";
  149. else if ( type == EINVALID_OPTION_ARG) return "EINVALID_OPTION_ARG";
  150. else if ( type == EMULTIPLE_OCCURANCES) return "EMULTIPLE_OCCURANCES";
  151. else if ( type == ECONFIG_READER) return "ECONFIG_READER";
  152. else if ( type == EIMAGE_SAVE) return "EIMAGE_SAVE";
  153. else if ( type == ECAST_TO_STRING) return "ECAST_TO_STRING";
  154. else if ( type == ESTRING_CAST) return "ESTRING_CAST";
  155. else if ( type == EUTF8_TO_UTF32) return "EUTF8_TO_UTF32";
  156. else return "undefined error type";
  157. }
  158. const std::string info; // info about the error
  159. const error_type type; // the type of the error
  160. private:
  161. const error& operator=(const error&);
  162. };
  163. // ----------------------------------------------------------------------------------------
  164. class fatal_error : public error
  165. {
  166. /*!
  167. WHAT THIS OBJECT REPRESENTS
  168. As the name says, this object represents some kind of fatal error.
  169. That is, it represents an unrecoverable error and any program that
  170. throws this exception is, by definition, buggy and needs to be fixed.
  171. Note that a fatal_error exception can only be thrown once. The second
  172. time an application attempts to construct a fatal_error it will be
  173. immediately aborted and an error message will be printed to std::cerr.
  174. The reason for this is because the first fatal_error was apparently ignored
  175. so the second fatal_error is going to make itself impossible to ignore
  176. by calling abort. The lesson here is that you should not try to ignore
  177. fatal errors.
  178. This is also the exception thrown by the DLIB_ASSERT and DLIB_CASSERT macros.
  179. !*/
  180. public:
  181. fatal_error(
  182. error_type t,
  183. const std::string& a
  184. ): error(t,a) {check_for_previous_fatal_errors();}
  185. /*!
  186. ensures
  187. - #type == t
  188. - #info == a
  189. !*/
  190. fatal_error(
  191. error_type t
  192. ): error(t) {check_for_previous_fatal_errors();}
  193. /*!
  194. ensures
  195. - #type == t
  196. - #info == ""
  197. !*/
  198. fatal_error(
  199. const std::string& a
  200. ): error(EFATAL,a) {check_for_previous_fatal_errors();}
  201. /*!
  202. ensures
  203. - #type == EFATAL
  204. - #info == a
  205. !*/
  206. fatal_error(
  207. ): error(EFATAL) {check_for_previous_fatal_errors();}
  208. /*!
  209. ensures
  210. - #type == EFATAL
  211. - #info == ""
  212. !*/
  213. private:
  214. static inline char* message ()
  215. {
  216. static char buf[2000];
  217. buf[1999] = '\0'; // just to be extra safe
  218. return buf;
  219. }
  220. static inline void dlib_fatal_error_terminate (
  221. )
  222. {
  223. std::cerr << "\n**************************** FATAL ERROR DETECTED ****************************";
  224. std::cerr << message() << "\n";
  225. std::cerr << "******************************************************************************\n" << "\n";
  226. }
  227. void check_for_previous_fatal_errors()
  228. {
  229. static bool is_first_fatal_error = true;
  230. if (is_first_fatal_error == false)
  231. {
  232. std::cerr << "\n\n ************************** FATAL ERROR DETECTED ************************** " << "\n";
  233. std::cerr << " ************************** FATAL ERROR DETECTED ************************** " << "\n";
  234. std::cerr << " ************************** FATAL ERROR DETECTED ************************** \n" << "\n";
  235. std::cerr << "Two fatal errors have been detected, the first was inappropriately ignored. \n"
  236. << "To prevent further fatal errors from being ignored this application will be \n"
  237. << "terminated immediately and you should go fix this buggy program.\n\n"
  238. << "The error message from this fatal error was:\n" << this->what() << "\n\n" << "\n";
  239. using namespace std;
  240. assert(false);
  241. abort();
  242. }
  243. else
  244. {
  245. // copy the message into the fixed message buffer so that it can be recalled by dlib_fatal_error_terminate
  246. // if needed.
  247. char* msg = message();
  248. unsigned long i;
  249. for (i = 0; i < 2000-1 && i < this->info.size(); ++i)
  250. msg[i] = info[i];
  251. msg[i] = '\0';
  252. // set this termination handler so that if the user doesn't catch this dlib::fatal_error that is being
  253. // thrown then it will eventually be printed to standard error
  254. std::set_terminate(&dlib_fatal_error_terminate);
  255. }
  256. is_first_fatal_error = false;
  257. }
  258. };
  259. // ----------------------------------------------------------------------------------------
  260. class gui_error : public error
  261. {
  262. public:
  263. gui_error(
  264. error_type t,
  265. const std::string& a
  266. ): error(t,a) {}
  267. /*!
  268. ensures
  269. - #type == t
  270. - #info == a
  271. !*/
  272. gui_error(
  273. error_type t
  274. ): error(t) {}
  275. /*!
  276. ensures
  277. - #type == t
  278. - #info == ""
  279. !*/
  280. gui_error(
  281. const std::string& a
  282. ): error(EGUI,a) {}
  283. /*!
  284. ensures
  285. - #type == EGUI
  286. - #info == a
  287. !*/
  288. gui_error(
  289. ): error(EGUI) {}
  290. /*!
  291. ensures
  292. - #type == EGUI
  293. - #info == ""
  294. !*/
  295. };
  296. // ----------------------------------------------------------------------------------------
  297. class socket_error : public error
  298. {
  299. public:
  300. socket_error(
  301. error_type t,
  302. const std::string& a
  303. ): error(t,a) {}
  304. /*!
  305. ensures
  306. - #type == t
  307. - #info == a
  308. !*/
  309. socket_error(
  310. error_type t
  311. ): error(t) {}
  312. /*!
  313. ensures
  314. - #type == t
  315. - #info == ""
  316. !*/
  317. socket_error(
  318. const std::string& a
  319. ): error(ESOCKET,a) {}
  320. /*!
  321. ensures
  322. - #type == ESOCKET
  323. - #info == a
  324. !*/
  325. socket_error(
  326. ): error(ESOCKET) {}
  327. /*!
  328. ensures
  329. - #type == ESOCKET
  330. - #info == ""
  331. !*/
  332. };
  333. // ----------------------------------------------------------------------------------------
  334. class thread_error : public error
  335. {
  336. public:
  337. thread_error(
  338. error_type t,
  339. const std::string& a
  340. ): error(t,a) {}
  341. /*!
  342. ensures
  343. - #type == t
  344. - #info == a
  345. !*/
  346. thread_error(
  347. error_type t
  348. ): error(t) {}
  349. /*!
  350. ensures
  351. - #type == t
  352. - #info == ""
  353. !*/
  354. thread_error(
  355. const std::string& a
  356. ): error(ETHREAD,a) {}
  357. /*!
  358. ensures
  359. - #type == ETHREAD
  360. - #info == a
  361. !*/
  362. thread_error(
  363. ): error(ETHREAD) {}
  364. /*!
  365. ensures
  366. - #type == ETHREAD
  367. - #info == ""
  368. !*/
  369. };
  370. // ----------------------------------------------------------------------------------------
  371. }
  372. #endif // DLIB_ERROr_