Skip to content

Commit

Permalink
Switch Nil Exception properties from wstrings to UTF-8 in anticipatio…
Browse files Browse the repository at this point in the history
…n of possible multi-platform developments in the future
  • Loading branch information
noorus committed Nov 15, 2014
1 parent ea55c65 commit 5facd02
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 56 deletions.
15 changes: 7 additions & 8 deletions include/nilException.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,27 @@ namespace Nil {
Exception();

Type mType; //!< Exception type
wideString mDescription; //!< Exception description
wideString mSource; //!< Exception source
mutable wideString mFullDescription; //!< Full, extended description
mutable utf8String mUTF8Description; //!< Full, extended description in UTF-8
utf8String mDescription; //!< Exception description
utf8String mSource; //!< Exception source
mutable utf8String mFullDescription; //!< Full, extended description
variant<WinAPIError> mAdditional; //!< \b Internal Additional exception data

//! \b Internal Handle additional exception data for WinAPI/DI exceptions.
void handleAdditional( HRESULT hr = 0 );
public:
//! Generic constructor.
Exception( const wideString& description, Type type = Generic );
Exception( const utf8String& description, Type type = Generic );

//! Constructor with source.
Exception( const wideString& description, const wideString& source,
Exception( const utf8String& description, const utf8String& source,
Type type = Generic );

//! Constructor with source and a WinAPI/DirectInput error code.
Exception( const wideString& description, const wideString& source,
Exception( const utf8String& description, const utf8String& source,
HRESULT hr, Type type = Generic );

//! Get the full, extended description of the exception.
virtual const wideString& getFullDescription() const;
virtual const utf8String& getFullDescription() const;

//! Get the std::exception compatible exception description.
virtual const char* what() const throw();
Expand Down
1 change: 1 addition & 0 deletions include/nilTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ namespace Nil {
using std::list;
using std::queue;
using std::vector;
using std::stringstream;
using std::wstringstream;
using boost::variant;

Expand Down
6 changes: 3 additions & 3 deletions include/nilUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ namespace Nil {
# error NIL_EXCEPT* macro already defined!
# else
//! Fire a generic exception.
# define NIL_EXCEPT(description) {throw Nil::Exception(description,__FUNCTIONW__,Nil::Exception::Generic);}
# define NIL_EXCEPT(description) {throw Nil::Exception(description,__FUNCTION__,Nil::Exception::Generic);}
//! Fire a WinAPI exception.
# define NIL_EXCEPT_WINAPI(description) {throw Nil::Exception(description,__FUNCTIONW__,Nil::Exception::WinAPI);}
# define NIL_EXCEPT_WINAPI(description) {throw Nil::Exception(description,__FUNCTION__,Nil::Exception::WinAPI);}
//! Fire a DirectInput exception.
# define NIL_EXCEPT_DINPUT(hr,description) {throw Nil::Exception(description,__FUNCTIONW__,hr,Nil::Exception::DirectInput);}
# define NIL_EXCEPT_DINPUT(hr,description) {throw Nil::Exception(description,__FUNCTION__,hr,Nil::Exception::DirectInput);}
# endif

static GUID g_HIDInterfaceGUID = { 0x4D1E55B2, 0xF16F, 0x11CF, { 0x88, 0xCB, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30 } };
Expand Down
16 changes: 8 additions & 8 deletions src/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,33 @@ namespace Nil {
{
XInputDevice* xDevice = dynamic_cast<XInputDevice*>( this );
if ( !xDevice )
NIL_EXCEPT( L"Dynamic cast failed for XInputDevice" );
NIL_EXCEPT( "Dynamic cast failed for XInputDevice" );
if ( getType() == Device_Controller )
{
mInstance = new XInputController( xDevice );
mSystem->controllerEnabled( this, (Controller*)mInstance );
}
else
NIL_EXCEPT( L"Unsupport device type for XInput; Cannot instantiate device!" );
NIL_EXCEPT( "Unsupport device type for XInput; Cannot instantiate device!" );
}
else if ( getHandler() == Handler_DirectInput )
{
DirectInputDevice* diDevice = dynamic_cast<DirectInputDevice*>( this );
if ( !diDevice )
NIL_EXCEPT( L"Dynamic cast failed for DirectInputDevice" );
NIL_EXCEPT( "Dynamic cast failed for DirectInputDevice" );
if ( getType() == Device_Controller )
{
mInstance = new DirectInputController( diDevice, mSystem->mCooperation );
mSystem->controllerEnabled( this, (Controller*)mInstance );
}
else
NIL_EXCEPT( L"Unsupported device type for DirectInput; Cannot instantiate device!" );
NIL_EXCEPT( "Unsupported device type for DirectInput; Cannot instantiate device!" );
}
else if ( getHandler() == Handler_RawInput )
{
RawInputDevice* rawDevice = dynamic_cast<RawInputDevice*>( this );
if ( !rawDevice )
NIL_EXCEPT( L"Dynamic cast failed for RawInputDevice" );
NIL_EXCEPT( "Dynamic cast failed for RawInputDevice" );
if ( getType() == Device_Mouse )
{
mInstance = new RawInputMouse( rawDevice );
Expand All @@ -73,10 +73,10 @@ namespace Nil {
mSystem->keyboardEnabled( this, (Keyboard*)mInstance );
}
else
NIL_EXCEPT( L"Unsupported device type for RawInput; cannot instantiate device!" );
NIL_EXCEPT( "Unsupported device type for RawInput; cannot instantiate device!" );
}
else
NIL_EXCEPT( L"Unsupported device handler; Cannot instantiate device!" );
NIL_EXCEPT( "Unsupported device handler; Cannot instantiate device!" );
}

DeviceInstance* Device::getInstance()
Expand Down Expand Up @@ -107,7 +107,7 @@ namespace Nil {
mSystem->keyboardDisabled( this, (Keyboard*)mInstance );
break;
default:
NIL_EXCEPT( L"Unimplemented device type" );
NIL_EXCEPT( "Unimplemented device type" );
break;
}

Expand Down
12 changes: 6 additions & 6 deletions src/DirectInputController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ namespace Nil {
HRESULT hr = device->getSystem()->mDirectInput->CreateDevice(
device->getInstanceID(), &mDIDevice, NULL );
if ( FAILED( hr ) )
NIL_EXCEPT_DINPUT( hr, L"Could not create DirectInput8 device" );
NIL_EXCEPT_DINPUT( hr, "Could not create DirectInput8 device" );

hr = mDIDevice->SetDataFormat( &c_dfDIJoystick2 );
if ( FAILED( hr ) )
NIL_EXCEPT_DINPUT( hr, L"Could not set DirectInput8 device data format" );
NIL_EXCEPT_DINPUT( hr, "Could not set DirectInput8 device data format" );

// Force feedback, to be implemented later, requires exclusive access.
hr = mDIDevice->SetCooperativeLevel( device->getSystem()->mWindow,
( mCooperation == Cooperation_Background )
? DISCL_BACKGROUND | DISCL_EXCLUSIVE
: DISCL_FOREGROUND | DISCL_EXCLUSIVE );
if ( FAILED( hr ) )
NIL_EXCEPT_DINPUT( hr, L"Could not set DirectInput8 device cooperation level" );
NIL_EXCEPT_DINPUT( hr, "Could not set DirectInput8 device cooperation level" );

DIPROPDWORD bufsize;
bufsize.diph.dwSize = sizeof( DIPROPDWORD );
Expand All @@ -45,12 +45,12 @@ namespace Nil {

hr = mDIDevice->SetProperty( DIPROP_BUFFERSIZE, &bufsize.diph );
if ( FAILED( hr ) )
NIL_EXCEPT_DINPUT( hr, L"Could not set DirectInput8 device buffer size" );
NIL_EXCEPT_DINPUT( hr, "Could not set DirectInput8 device buffer size" );

mDICapabilities.dwSize = sizeof( DIDEVCAPS );
hr = mDIDevice->GetCapabilities( &mDICapabilities );
if ( FAILED( hr ) )
NIL_EXCEPT_DINPUT( hr, L"Could not get DirectInput8 device capabilities" );
NIL_EXCEPT_DINPUT( hr, "Could not get DirectInput8 device capabilities" );

// Identify a more specific controller type, if available
switch ( mDICapabilities.dwDevType )
Expand Down Expand Up @@ -127,7 +127,7 @@ namespace Nil {

HRESULT hr = controller->mDIDevice->SetProperty( DIPROP_RANGE, &range.diph );
if ( FAILED( hr ) )
NIL_EXCEPT_DINPUT( hr, L"Could not set axis range property on DirectInput8 device" );
NIL_EXCEPT_DINPUT( hr, "Could not set axis range property on DirectInput8 device" );

return DIENUM_CONTINUE;
}
Expand Down
12 changes: 6 additions & 6 deletions src/EventMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ namespace Nil {

mClass = RegisterClassExW( &wx );
if ( !mClass )
NIL_EXCEPT_WINAPI( L"Window class registration failed" );
NIL_EXCEPT_WINAPI( "Window class registration failed" );

mWindow = CreateWindowExW(
0, (LPCWSTR)mClass, nullptr, 0, 0, 0, 0, 0, 0, 0, mInstance, this ); //-V542
if ( !mWindow )
NIL_EXCEPT_WINAPI( L"Window creation failed" );
NIL_EXCEPT_WINAPI( "Window creation failed" );

mInputBuffer = malloc( (size_t)mInputBufferSize );
if ( !mInputBuffer )
NIL_EXCEPT( L"Couldn't allocate input read buffer" );
NIL_EXCEPT( "Couldn't allocate input read buffer" );

registerNotifications();
}
Expand Down Expand Up @@ -89,7 +89,7 @@ namespace Nil {
mInputBufferSize = dataSize;
mInputBuffer = realloc( mInputBuffer, (size_t)dataSize );
if ( !mInputBuffer )
NIL_EXCEPT( L"Couldn't reallocate input read buffer" );
NIL_EXCEPT( "Couldn't reallocate input read buffer" );
}

if ( GetRawInputData( input, RID_INPUT, mInputBuffer, &dataSize, sizeof( RAWINPUTHEADER ) ) == (UINT)-1 )
Expand Down Expand Up @@ -128,7 +128,7 @@ namespace Nil {
mNotifications = RegisterDeviceNotificationW( mWindow, &filter,
DEVICE_NOTIFY_WINDOW_HANDLE );
if ( !mNotifications )
NIL_EXCEPT_WINAPI( L"Couldn't register for device interface notifications" );
NIL_EXCEPT_WINAPI( "Couldn't register for device interface notifications" );

RAWINPUTDEVICE rawDevices[2];

Expand All @@ -149,7 +149,7 @@ namespace Nil {
rawDevices[1].usUsage = USBDesktopUsage_Keyboards;

if ( !RegisterRawInputDevices( rawDevices, 2, sizeof( RAWINPUTDEVICE ) ) )
NIL_EXCEPT_WINAPI( L"Couldn't register for raw input notifications" );
NIL_EXCEPT_WINAPI( "Couldn't register for raw input notifications" );
}

LRESULT CALLBACK EventMonitor::wndProc( HWND window, UINT message,
Expand Down
23 changes: 11 additions & 12 deletions src/Exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ namespace Nil {
// Stub
}

Exception::Exception( const wideString& description, Type type ):
Exception::Exception( const utf8String& description, Type type ):
mDescription( description ), mType( type )
{
handleAdditional();
}

Exception::Exception( const wideString& description, const wideString& source, Type type ):
Exception::Exception( const utf8String& description, const utf8String& source, Type type ):
mDescription( description ), mSource( source ), mType( type )
{
handleAdditional();
}

Exception::Exception( const wideString& description, const wideString& source, HRESULT hr, Type type ):
Exception::Exception( const utf8String& description, const utf8String& source, HRESULT hr, Type type ):
mDescription( description ), mSource( source ), mType( type )
{
handleAdditional( hr );
Expand All @@ -77,13 +77,13 @@ namespace Nil {
{
error.code = GetLastError();

LPWSTR message = nullptr;
wchar_t* message = nullptr;
FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, error.code, MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
(LPWSTR)&message, 0, NULL );
(wchar_t*)&message, 0, NULL );

if ( message )
{
Expand All @@ -108,26 +108,26 @@ namespace Nil {
}
}

const wideString& Exception::getFullDescription() const
const utf8String& Exception::getFullDescription() const
{
if ( mFullDescription.empty() )
{
wstringstream stream;
stringstream stream;

stream << mDescription;

if ( !mSource.empty() )
stream << L"\r\nIn function " << mSource;
stream << "\r\nIn function " << mSource;

if ( mType == WinAPI )
{
const WinAPIError& error = boost::get<WinAPIError>( mAdditional );
stream << L"\r\nWinAPI error code " << std::hex << error.code << L":\r\n" << error.description;
stream << "\r\nWinAPI error code " << std::hex << error.code << ":\r\n" << Util::wideToUtf8( error.description );
}
else if ( mType == DirectInput )
{
const WinAPIError& error = boost::get<WinAPIError>( mAdditional );
stream << L"\r\nDirectInput error code " << std::hex << error.code << L":\r\n" << error.description;
stream << "\r\nDirectInput error code " << std::hex << error.code << ":\r\n" << Util::wideToUtf8( error.description );
}

mFullDescription = stream.str();
Expand All @@ -137,8 +137,7 @@ namespace Nil {

const char* Exception::what() const
{
mUTF8Description = Util::wideToUtf8( getFullDescription() );
return mUTF8Description.c_str();
return mFullDescription.c_str();
}

}
4 changes: 2 additions & 2 deletions src/HIDManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace Nil {
NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE );

if ( info == INVALID_HANDLE_VALUE )
NIL_EXCEPT_WINAPI( L"SetupDiGetClassDevsW failed" );
NIL_EXCEPT_WINAPI( "SetupDiGetClassDevsW failed" );

for ( unsigned long i = 0; SetupDiEnumDeviceInterfaces(
info, 0, &g_HIDInterfaceGUID, i, &interfaceData ); i++ )
Expand All @@ -85,7 +85,7 @@ namespace Nil {
SetupDiGetDeviceInterfaceDetailW( info, &interfaceData, NULL, 0, &length, NULL );

if ( GetLastError() != ERROR_INSUFFICIENT_BUFFER )
NIL_EXCEPT_WINAPI( L"SetupDiGetDeviceInterfaceDetailW failed" );
NIL_EXCEPT_WINAPI( "SetupDiGetDeviceInterfaceDetailW failed" );

auto detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA_W)malloc( (size_t)length );
detailData->cbSize = sizeof( SP_INTERFACE_DEVICE_DETAIL_DATA_W );
Expand Down
4 changes: 2 additions & 2 deletions src/HIDRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Nil {
HIDD_ATTRIBUTES attributes = { sizeof( HIDD_ATTRIBUTES ) };

if ( !HidD_GetAttributes( handle, &attributes ) )
NIL_EXCEPT( L"HidD_GetAttributes failed" );
NIL_EXCEPT( "HidD_GetAttributes failed" );

mVendorID = attributes.VendorID;
mProductID = attributes.ProductID;
Expand All @@ -18,7 +18,7 @@ namespace Nil {

PHIDP_PREPARSED_DATA preparsedData;
if ( !HidD_GetPreparsedData( handle, &preparsedData ) )
NIL_EXCEPT( L"HIdD_GetPreparsedData failed" );
NIL_EXCEPT( "HIdD_GetPreparsedData failed" );

HidP_GetCaps( preparsedData, &mCapabilities );
HidD_FreePreparsedData( preparsedData );
Expand Down
6 changes: 3 additions & 3 deletions src/RawInputDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ namespace Nil {
{
UINT size = 0;
if ( GetRawInputDeviceInfoW( handle, RIDI_DEVICEINFO, NULL, &size ) != 0 )
NIL_EXCEPT_WINAPI( L"GetRawInputDeviceInfoW failed" );
NIL_EXCEPT_WINAPI( "GetRawInputDeviceInfoW failed" );

mRawInfo = (RID_DEVICE_INFO*)malloc( (size_t)size );
if ( !mRawInfo )
NIL_EXCEPT( L"Memory allocation failed" );
NIL_EXCEPT( "Memory allocation failed" );

if ( !GetRawInputDeviceInfoW( handle, RIDI_DEVICEINFO, mRawInfo, &size ) )
NIL_EXCEPT_WINAPI( L"GetRawInputDeviceInfoW failed" );
NIL_EXCEPT_WINAPI( "GetRawInputDeviceInfoW failed" );
}

const Device::Type RawInputDeviceInfo::rawInfoResolveType() const
Expand Down
10 changes: 5 additions & 5 deletions src/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Nil {

// Validate the passes window handle
if ( !IsWindow( mWindow ) )
NIL_EXCEPT( L"Window handle is invalid" );
NIL_EXCEPT( "Window handle is invalid" );

mLogitechGKeys = new Logitech::GKeySDK();
mLogitechLEDs = new Logitech::LedSDK();
Expand All @@ -25,7 +25,7 @@ namespace Nil {
auto hr = DirectInput8Create( mInstance, DIRECTINPUT_VERSION,
IID_IDirectInput8W, (LPVOID*)&mDirectInput, NULL );
if ( FAILED( hr ) )
NIL_EXCEPT_DINPUT( hr, L"Could not instance DirectInput 8" );
NIL_EXCEPT_DINPUT( hr, "Could not instantiate DirectInput 8" );

// Initialize our event monitor
mMonitor = new EventMonitor( mInstance, mCooperation );
Expand Down Expand Up @@ -74,7 +74,7 @@ namespace Nil {
UINT pathLength = 0;

if ( GetRawInputDeviceInfoW( handle, RIDI_DEVICENAME, NULL, &pathLength ) )
NIL_EXCEPT_WINAPI( L"GetRawInputDeviceInfoW failed" );
NIL_EXCEPT_WINAPI( "GetRawInputDeviceInfoW failed" );

wideString rawPath( pathLength, '\0' );

Expand Down Expand Up @@ -189,7 +189,7 @@ namespace Nil {
auto hr = mDirectInput->EnumDevices( DI8DEVCLASS_GAMECTRL,
diDeviceEnumCallback, this, DIEDFL_ATTACHEDONLY );
if ( FAILED( hr ) )
NIL_EXCEPT_DINPUT( hr, L"Could not enumerate DirectInput devices!" );
NIL_EXCEPT_DINPUT( hr, "Could not enumerate DirectInput devices!" );

for ( Device* device : mDevices )
if ( device->getHandler() == Device::Handler_DirectInput
Expand Down Expand Up @@ -219,7 +219,7 @@ namespace Nil {
xDevice->setStatus( Device::Status_Connected );
}
else
NIL_EXCEPT( L"XInputGetState failed" );
NIL_EXCEPT( "XInputGetState failed" );
}
}
}
Expand Down
Loading

0 comments on commit 5facd02

Please sign in to comment.