PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lw/error/Error.h

https://github.com/LifeWanted/liblw-old
C Header | 185 lines | 120 code | 41 blank | 24 comment | 1 complexity | 655980ef590f85669bbd73b83ed2e6bf MD5 | raw file
  1. ///
  2. /// @file lw/error/Error.h
  3. ///
  4. /// A basic error class which all other errors in the LW Common library build upon.
  5. ///
  6. /// @author Oz <oz@lifewanted.com>
  7. ///
  8. #pragma once
  9. #include <exception>
  10. #include <sstream>
  11. #include <string>
  12. #include <type_traits>
  13. #include "lw/defines.h"
  14. #include "lw/error/errorCodes.h"
  15. namespace lw {
  16. namespace error {
  17. #define LW_ERROR( ErrorClass, message ) \
  18. static_assert( \
  19. std::is_base_of< lw::error::Error, ErrorClass >::value, \
  20. #ErrorClass " is not an LW Error." \
  21. ); \
  22. throw ErrorClass( message, __PRETTY_FUNCTION__, __FILE__, __LINE__ )
  23. class Error : public virtual std::exception {
  24. public:
  25. typedef std::exception_ptr Pointer;
  26. private:
  27. s64 m_errorCode;
  28. std::string m_errorName;
  29. std::string m_message;
  30. std::string m_function;
  31. std::string m_file;
  32. u16 m_line;
  33. public:
  34. Error( void );
  35. Error(
  36. const std::string& message,
  37. const std::string& function,
  38. const std::string& file,
  39. const u16 line
  40. );
  41. Error(
  42. const s64 errorCode,
  43. const std::string& errorName,
  44. const std::string& message,
  45. const std::string& function,
  46. const std::string& file,
  47. const u16 line
  48. );
  49. const char* what( void ) const LW_NOEXCEPT LW_OVERRIDE;
  50. virtual const std::string whatStr( void ) const LW_NOEXCEPT;
  51. const s64& getErrorCode( void ) const LW_NOEXCEPT;
  52. const std::string& getErrorName( void ) const LW_NOEXCEPT;
  53. const std::string& getMessage( void ) const LW_NOEXCEPT;
  54. const std::string& getFunction( void ) const LW_NOEXCEPT;
  55. const std::string& getFile( void ) const LW_NOEXCEPT;
  56. std::string getFileName( void ) const LW_NOEXCEPT;
  57. const u16& getLineNumber( void ) const LW_NOEXCEPT;
  58. operator bool( void ) const LW_NOEXCEPT;
  59. bool operator!( void ) const LW_NOEXCEPT;
  60. }; // end class Error
  61. // -------------------------------------------------------------------------- //
  62. inline Error::Error( void ): Error( lw::error::Code::None, "", "", "", "", 0 ){
  63. // This space intentionally left blank.
  64. }
  65. // -------------------------------------------------------------------------- //
  66. inline Error::Error(
  67. const std::string& message,
  68. const std::string& function,
  69. const std::string& file,
  70. const u16 line
  71. ):
  72. Error( lw::error::Code::Generic, "Generic", message, function, file, line )
  73. {
  74. // This space intentionally left blank.
  75. }
  76. // -------------------------------------------------------------------------- //
  77. inline Error::Error(
  78. const s64 errorCode,
  79. const std::string& errorName,
  80. const std::string& message,
  81. const std::string& function,
  82. const std::string& file,
  83. const u16 line
  84. ):
  85. m_errorCode( errorCode ),
  86. m_errorName( errorName ),
  87. m_message( message ),
  88. m_function( function ),
  89. m_file( file ),
  90. m_line( line )
  91. {
  92. // This space intentionally left blank.
  93. }
  94. // -------------------------------------------------------------------------- //
  95. inline const char* Error::what( void ) const LW_NOEXCEPT {
  96. return whatStr().c_str();
  97. }
  98. // -------------------------------------------------------------------------- //
  99. inline const std::string Error::whatStr( void ) const LW_NOEXCEPT {
  100. std::stringstream stream;
  101. stream
  102. << getErrorName() << " Error (" << getErrorCode() << ") [" << getFileName() << ":"
  103. << getLineNumber() << "]: " << getMessage();
  104. return stream.str();
  105. }
  106. // -------------------------------------------------------------------------- //
  107. inline const s64& Error::getErrorCode( void ) const LW_NOEXCEPT {
  108. return m_errorCode;
  109. }
  110. // -------------------------------------------------------------------------- //
  111. inline const std::string& Error::getErrorName( void ) const LW_NOEXCEPT {
  112. return m_errorName;
  113. }
  114. // -------------------------------------------------------------------------- //
  115. inline const std::string& Error::getMessage( void ) const LW_NOEXCEPT {
  116. return m_message;
  117. }
  118. // -------------------------------------------------------------------------- //
  119. inline const std::string& Error::getFunction( void ) const LW_NOEXCEPT {
  120. return m_function;
  121. }
  122. // -------------------------------------------------------------------------- //
  123. inline const std::string& Error::getFile( void ) const LW_NOEXCEPT {
  124. return m_file;
  125. }
  126. // -------------------------------------------------------------------------- //
  127. inline std::string Error::getFileName( void ) const LW_NOEXCEPT {
  128. const std::size_t slashPos = m_file.rfind( '/' );
  129. return slashPos == std::string::npos ? m_file : m_file.substr( slashPos + 1 );
  130. }
  131. // -------------------------------------------------------------------------- //
  132. inline const u16& Error::getLineNumber( void ) const LW_NOEXCEPT {
  133. return m_line;
  134. }
  135. // -------------------------------------------------------------------------- //
  136. inline Error::operator bool( void ) const LW_NOEXCEPT {
  137. return (bool)m_errorCode;
  138. }
  139. // -------------------------------------------------------------------------- //
  140. inline bool Error::operator!( void ) const LW_NOEXCEPT {
  141. return !(bool)m_errorCode;
  142. }
  143. } // end namespace error
  144. } // end namespace lw