Add win32 error class for throwing

This commit is contained in:
Danny Robson 2012-05-08 15:01:56 +10:00
parent 6a4f6c7d8b
commit 5ef9f02a19
2 changed files with 56 additions and 0 deletions

View File

@ -19,6 +19,7 @@
#include "except.hpp" #include "except.hpp"
#include "debug.hpp" #include "debug.hpp"
#include "platform.hpp"
#include <cstring> #include <cstring>
#include <cerrno> #include <cerrno>
@ -70,3 +71,40 @@ errno_error::throw_code (int code) {
check_hard (code != 0); check_hard (code != 0);
throw errno_error (code); throw errno_error (code);
} }
#if defined(PLATFORM_WIN32)
win32_error::win32_error (DWORD _id):
runtime_error ("Win32 error"),
id (_id)
{
check_soft (id != ERROR_SUCCESS);
}
win32_error::win32_error (void):
runtime_error ("Win32 error"),
id (GetLastError ())
{
check_soft (id != ERROR_SUCCESS);
}
void
win32_error::try_code (void) {
const auto id = GetLastError ();
if (id == ERROR_SUCCESS)
return;
throw win32_error (id);
}
void
win32_error::throw_code (void) {
const auto id = GetLastError ();
check (id != ERROR_SUCCESS);
throw win32_error (id);
}
#endif

View File

@ -21,6 +21,8 @@
#ifndef __EXCEPT_HPP #ifndef __EXCEPT_HPP
#define __EXCEPT_HPP #define __EXCEPT_HPP
#include "platform.hpp"
#include <stdexcept> #include <stdexcept>
@ -55,4 +57,20 @@ class errno_error : public std::runtime_error {
}; };
#if defined(PLATFORM_WIN32)
#include <windows.h>
class win32_error : public std::runtime_error {
public:
DWORD id;
win32_error (DWORD _id);
win32_error ();
static void try_code (void);
static void throw_code (void);
};
#endif
#endif #endif