SpCatch.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef _RVC_CATCH_H__
  2. #define _RVC_CATCH_H__
  3. #pragma once
  4. #include <exception>
  5. #include <stdexcept>
  6. #include <cstring>
  7. #include <iostream>
  8. #include "SpComm.hpp"
  9. /** we should integrate it with SpBase.h at the future*/
  10. #define SPCATCH_NOT_IMPLEMENTED throw ::SP::Catch::NotImplementedException( SP_INTERNAL_LINEINFO )
  11. namespace SP {
  12. namespace Detail
  13. {
  14. struct SourceLineInfo {
  15. SourceLineInfo() :file(""), line(0) {/*empty*/ }
  16. SourceLineInfo(char const* file, std::size_t line) :file(file), line(line) {/*empty*/ }
  17. #if defined(_MSC_VER)
  18. #else
  19. SourceLineInfo(SourceLineInfo const& rhs) = default;
  20. SourceLineInfo(SourceLineInfo&& rhs) = default;
  21. SourceLineInfo& operator = (SourceLineInfo const& rhs) = default;
  22. SourceLineInfo& operator =(SourceLineInfo&& rhs) = default;
  23. #endif //_MSC_VER
  24. bool operator == (SourceLineInfo const& rhs) const {
  25. return line < rhs.line || (line == rhs.line && (std::strcmp(file, rhs.file) < 0));
  26. }
  27. bool operator < (SourceLineInfo const& rhs) const {
  28. return line == rhs.line && (file == rhs.file || std::strcmp(file, rhs.file) == 0);
  29. }
  30. const std::string ToString() const {
  31. std::ostringstream oss;
  32. oss << "file: {" << _GetFileName(file) << "} ,line: {" << line << "}";
  33. return oss.str();
  34. }
  35. char const* file;
  36. std::size_t line;
  37. };
  38. inline std::ostream& operator << (std::ostream& os, SourceLineInfo const& info)
  39. {
  40. os << info.file << '(' << info.line << ')';
  41. return os;
  42. }
  43. }
  44. namespace Catch {
  45. class CBaseException : public std::exception
  46. {
  47. public:
  48. CBaseException(const ErrorCodeEnum& ec, ::SP::Detail::SourceLineInfo const& lineInfo)
  49. :m_errorCode(ec),m_lineInfo(lineInfo)
  50. {
  51. std::ostringstream oss;
  52. oss << m_lineInfo << " occurs: " << SpStrError(m_errorCode);
  53. m_what = oss.str().c_str();
  54. }
  55. CBaseException(::SP::Detail::SourceLineInfo const& lineInfo)
  56. :CBaseException(Error_Exception, lineInfo) {}
  57. virtual ~CBaseException() noexcept {}
  58. virtual const char* what() const noexcept
  59. {
  60. return m_what;
  61. }
  62. protected:
  63. ErrorCodeEnum m_errorCode;
  64. ::SP::Detail::SourceLineInfo m_lineInfo;
  65. CSimpleStringA m_what;
  66. };
  67. class NotImplementedException : public CBaseException
  68. {
  69. public:
  70. NotImplementedException(::SP::Detail::SourceLineInfo const& lineInfo)
  71. :CBaseException(Error_NotImpl, lineInfo)
  72. {
  73. }
  74. };
  75. class CBaseRTException : public std::runtime_error
  76. {
  77. public:
  78. CBaseRTException(const char* msg) :std::runtime_error(msg)
  79. {}
  80. CBaseRTException() = default;
  81. ~CBaseRTException() = default;
  82. };
  83. }
  84. }
  85. #define SP_INTERNAL_LINEINFO ::SP::Detail::SourceLineInfo( __FILE__, static_cast<std::size_t>( __LINE__ ) )
  86. #endif /** _RVC_CATCH_H__*/