PageRenderTime 28ms CodeModel.GetById 13ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/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
  9#pragma once
 10
 11#include <exception>
 12#include <sstream>
 13#include <string>
 14#include <type_traits>
 15
 16#include "lw/defines.h"
 17#include "lw/error/errorCodes.h"
 18
 19namespace lw {
 20namespace error {
 21
 22#define LW_ERROR( ErrorClass, message )                         \
 23    static_assert(                                              \
 24        std::is_base_of< lw::error::Error, ErrorClass >::value, \
 25        #ErrorClass " is not an LW Error."                      \
 26    );                                                          \
 27    throw ErrorClass( message, __PRETTY_FUNCTION__, __FILE__, __LINE__ )
 28
 29class Error : public virtual std::exception {
 30public:
 31    typedef std::exception_ptr Pointer;
 32
 33private:
 34    s64 m_errorCode;
 35    std::string m_errorName;
 36    std::string m_message;
 37    std::string m_function;
 38    std::string m_file;
 39    u16 m_line;
 40
 41public:
 42    Error( void );
 43
 44    Error(
 45        const std::string& message,
 46        const std::string& function,
 47        const std::string& file,
 48        const u16 line
 49    );
 50
 51    Error(
 52        const s64 errorCode,
 53        const std::string& errorName,
 54        const std::string& message,
 55        const std::string& function,
 56        const std::string& file,
 57        const u16 line
 58    );
 59
 60    const char* what( void ) const LW_NOEXCEPT LW_OVERRIDE;
 61    virtual const std::string whatStr( void ) const LW_NOEXCEPT;
 62    const s64& getErrorCode( void ) const LW_NOEXCEPT;
 63    const std::string& getErrorName( void ) const LW_NOEXCEPT;
 64    const std::string& getMessage( void ) const LW_NOEXCEPT;
 65    const std::string& getFunction( void ) const LW_NOEXCEPT;
 66    const std::string& getFile( void ) const LW_NOEXCEPT;
 67    std::string getFileName( void ) const LW_NOEXCEPT;
 68    const u16& getLineNumber( void ) const LW_NOEXCEPT;
 69
 70    operator bool( void ) const LW_NOEXCEPT;
 71    bool operator!( void ) const LW_NOEXCEPT;
 72}; // end class Error
 73
 74// -------------------------------------------------------------------------- //
 75
 76inline Error::Error( void ): Error( lw::error::Code::None, "", "", "", "", 0 ){
 77    // This space intentionally left blank.
 78}
 79
 80// -------------------------------------------------------------------------- //
 81
 82inline Error::Error(
 83    const std::string& message,
 84    const std::string& function,
 85    const std::string& file,
 86    const u16 line
 87):
 88    Error( lw::error::Code::Generic, "Generic", message, function, file, line )
 89{
 90    // This space intentionally left blank.
 91}
 92
 93// -------------------------------------------------------------------------- //
 94
 95inline Error::Error(
 96    const s64 errorCode,
 97    const std::string& errorName,
 98    const std::string& message,
 99    const std::string& function,
100    const std::string& file,
101    const u16 line
102):
103    m_errorCode(    errorCode   ),
104    m_errorName(    errorName   ),
105    m_message(      message     ),
106    m_function(     function    ),
107    m_file(         file        ),
108    m_line(         line        )
109{
110    // This space intentionally left blank.
111}
112
113// -------------------------------------------------------------------------- //
114
115inline const char* Error::what( void ) const LW_NOEXCEPT {
116    return whatStr().c_str();
117}
118
119// -------------------------------------------------------------------------- //
120
121inline const std::string Error::whatStr( void ) const LW_NOEXCEPT {
122    std::stringstream stream;
123    stream 
124        << getErrorName() << " Error (" << getErrorCode() << ") [" << getFileName() << ":"
125        <<  getLineNumber() << "]: " << getMessage();
126    return stream.str();
127}
128
129// -------------------------------------------------------------------------- //
130
131inline const s64& Error::getErrorCode( void ) const LW_NOEXCEPT {
132    return m_errorCode;
133}
134
135// -------------------------------------------------------------------------- //
136
137inline const std::string& Error::getErrorName( void ) const LW_NOEXCEPT {
138    return m_errorName;
139}
140
141// -------------------------------------------------------------------------- //
142
143inline const std::string& Error::getMessage( void ) const LW_NOEXCEPT {
144    return m_message;
145}
146
147// -------------------------------------------------------------------------- //
148
149inline const std::string& Error::getFunction( void ) const LW_NOEXCEPT {
150    return m_function;
151}
152
153// -------------------------------------------------------------------------- //
154
155inline const std::string& Error::getFile( void ) const LW_NOEXCEPT {
156    return m_file;
157}
158
159// -------------------------------------------------------------------------- //
160
161inline std::string Error::getFileName( void ) const LW_NOEXCEPT {
162    const std::size_t slashPos = m_file.rfind( '/' );
163    return slashPos == std::string::npos ? m_file : m_file.substr( slashPos + 1 );
164}
165
166// -------------------------------------------------------------------------- //
167
168inline const u16& Error::getLineNumber( void ) const LW_NOEXCEPT {
169    return m_line;
170}
171
172// -------------------------------------------------------------------------- //
173
174inline Error::operator bool( void ) const LW_NOEXCEPT {
175    return (bool)m_errorCode;
176}
177
178// -------------------------------------------------------------------------- //
179
180inline bool Error::operator!( void ) const LW_NOEXCEPT {
181    return !(bool)m_errorCode;
182}
183
184} // end namespace error
185} // end namespace lw