Commit 9b321235 authored by chili's avatar chili
Browse files

custom exception types

parent 19b128c2
/******************************************************************************************
* Chili Direct3D Engine *
* Copyright 2018 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of Chili Direct3D Engine. *
* *
* Chili Direct3D Engine is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili Direct3D Engine is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili Direct3D Engine. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#include "ChiliException.h"
#include <sstream>
ChiliException::ChiliException( int line,const char* file ) noexcept
:
line( line ),
file( file )
{}
const char* ChiliException::what() const noexcept
{
std::ostringstream oss;
oss << GetType() << std::endl
<< GetOriginString();
whatBuffer = oss.str();
return whatBuffer.c_str();
}
const char* ChiliException::GetType() const noexcept
{
return "Chili Exception";
}
int ChiliException::GetLine() const noexcept
{
return line;
}
const std::string& ChiliException::GetFile() const noexcept
{
return file;
}
std::string ChiliException::GetOriginString() const noexcept
{
std::ostringstream oss;
oss << "[File] " << file << std::endl
<< "[Line] " << line;
return oss.str();
}
\ No newline at end of file
/******************************************************************************************
* Chili Direct3D Engine *
* Copyright 2018 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of Chili Direct3D Engine. *
* *
* Chili Direct3D Engine is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili Direct3D Engine is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili Direct3D Engine. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#pragma once
#include <exception>
#include <string>
class ChiliException : public std::exception
{
public:
ChiliException( int line,const char* file ) noexcept;
const char* what() const noexcept override;
virtual const char* GetType() const noexcept;
int GetLine() const noexcept;
const std::string& GetFile() const noexcept;
std::string GetOriginString() const noexcept;
private:
int line;
std::string file;
protected:
mutable std::string whatBuffer;
};
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
* along with The Chili Direct3D Engine. If not, see <http://www.gnu.org/licenses/>. * * along with The Chili Direct3D Engine. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/ ******************************************************************************************/
#include "Window.h" #include "Window.h"
#include <sstream>
// Window Class Stuff // Window Class Stuff
...@@ -125,3 +126,55 @@ LRESULT Window::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noex ...@@ -125,3 +126,55 @@ LRESULT Window::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noex
return DefWindowProc( hWnd,msg,wParam,lParam ); return DefWindowProc( hWnd,msg,wParam,lParam );
} }
// Window Exception Stuff
Window::Exception::Exception( int line,const char * file,HRESULT hr ) noexcept
:
ChiliException( line,file ),
hr( hr )
{}
const char * Window::Exception::what() const noexcept
{
std::ostringstream oss;
oss << GetType() << std::endl
<< "[Error Code] " << GetErrorCode() << std::endl
<< "[Description] " << GetErrorString() << std::endl
<< GetOriginString();
whatBuffer = oss.str();
return whatBuffer.c_str();
}
const char* Window::Exception::GetType() const noexcept
{
return "Chili Window Exception";
}
std::string Window::Exception::TranslateErrorCode( HRESULT hr ) noexcept
{
char* pMsgBuf = nullptr;
DWORD nMsgLen = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,hr,MAKELANGID( LANG_NEUTRAL,SUBLANG_DEFAULT ),
reinterpret_cast<LPSTR>(&pMsgBuf),0,nullptr
);
if( nMsgLen == 0 )
{
return "Unidentified error code";
}
std::string errorString = pMsgBuf;
LocalFree( pMsgBuf );
return errorString;
}
HRESULT Window::Exception::GetErrorCode() const noexcept
{
return hr;
}
std::string Window::Exception::GetErrorString() const noexcept
{
return TranslateErrorCode( hr );
}
...@@ -19,10 +19,24 @@ ...@@ -19,10 +19,24 @@
******************************************************************************************/ ******************************************************************************************/
#pragma once #pragma once
#include "ChiliWin.h" #include "ChiliWin.h"
#include "ChiliException.h"
class Window class Window
{ {
public:
class Exception : public ChiliException
{
public:
Exception( int line,const char* file,HRESULT hr ) noexcept;
const char* what() const noexcept override;
virtual const char* GetType() const noexcept;
static std::string TranslateErrorCode( HRESULT hr ) noexcept;
HRESULT GetErrorCode() const noexcept;
std::string GetErrorString() const noexcept;
private:
HRESULT hr;
};
private: private:
// singleton manages registration/cleanup of window class // singleton manages registration/cleanup of window class
class WindowClass class WindowClass
...@@ -53,3 +67,7 @@ private: ...@@ -53,3 +67,7 @@ private:
int height; int height;
HWND hWnd; HWND hWnd;
}; };
// error exception helper macro
#define CHWND_EXCEPT( hr ) Window::Exception( __LINE__,__FILE__,hr )
\ No newline at end of file
...@@ -145,11 +145,13 @@ ...@@ -145,11 +145,13 @@
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="ChiliException.cpp" />
<ClCompile Include="Window.cpp" /> <ClCompile Include="Window.cpp" />
<ClCompile Include="WindowsMessageMap.cpp" /> <ClCompile Include="WindowsMessageMap.cpp" />
<ClCompile Include="WinMain.cpp" /> <ClCompile Include="WinMain.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="ChiliException.h" />
<ClInclude Include="ChiliWin.h" /> <ClInclude Include="ChiliWin.h" />
<ClInclude Include="Window.h" /> <ClInclude Include="Window.h" />
<ClInclude Include="WindowsMessageMap.h" /> <ClInclude Include="WindowsMessageMap.h" />
......
...@@ -24,6 +24,9 @@ ...@@ -24,6 +24,9 @@
<ClCompile Include="Window.cpp"> <ClCompile Include="Window.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="ChiliException.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="WindowsMessageMap.h"> <ClInclude Include="WindowsMessageMap.h">
...@@ -35,5 +38,8 @@ ...@@ -35,5 +38,8 @@
<ClInclude Include="Window.h"> <ClInclude Include="Window.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="ChiliException.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment