error.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Error handling and diagnostic utilities
  2. //
  3. // Platform: ISO C++ 98/11
  4. // $Id$
  5. //
  6. // (c) __vic 2007
  7. #ifndef __VIC_ERROR_H
  8. #define __VIC_ERROR_H
  9. #include<__vic/defs.h>
  10. #include<__vic/readonly_cstring.h>
  11. #include<cerrno>
  12. #include<exception>
  13. namespace __vic {
  14. //////////////////////////////////////////////////////////////////////////////
  15. // Simple exception class
  16. //////////////////////////////////////////////////////////////////////////////
  17. class exception : public std::exception
  18. {
  19. readonly_cstring msg;
  20. public:
  21. explicit exception(const char *msg) : msg(msg) {}
  22. const char *what() const noexcept { return msg; }
  23. protected:
  24. exception() __VIC_DEFAULT_CTR
  25. void set_message(const char *st) { msg = st; }
  26. };
  27. //////////////////////////////////////////////////////////////////////////////
  28. //////////////////////////////////////////////////////////////////////////////
  29. // Replacement for C error reporting mechanism with C++ exceptions
  30. //////////////////////////////////////////////////////////////////////////////
  31. class libc_error : public std::exception
  32. {
  33. int code_;
  34. mutable bool formatted;
  35. mutable readonly_cstring msg;
  36. public:
  37. explicit libc_error(int = errno);
  38. explicit libc_error(const char * , int = errno);
  39. const char *what() const noexcept;
  40. int code() const { return code_; }
  41. int get_errno() const { return code(); }
  42. };
  43. //////////////////////////////////////////////////////////////////////////////
  44. } // namespace
  45. #endif // header guard