From 5ef9f02a19fece55d5dd214f8849ac2b7de21487 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 8 May 2012 15:01:56 +1000 Subject: [PATCH] Add win32 error class for throwing --- except.cpp | 38 ++++++++++++++++++++++++++++++++++++++ except.hpp | 18 ++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/except.cpp b/except.cpp index 5ce0288d..fa476089 100644 --- a/except.cpp +++ b/except.cpp @@ -19,6 +19,7 @@ #include "except.hpp" #include "debug.hpp" +#include "platform.hpp" #include #include @@ -70,3 +71,40 @@ errno_error::throw_code (int code) { check_hard (code != 0); 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 diff --git a/except.hpp b/except.hpp index 13687859..01860f94 100644 --- a/except.hpp +++ b/except.hpp @@ -21,6 +21,8 @@ #ifndef __EXCEPT_HPP #define __EXCEPT_HPP +#include "platform.hpp" + #include @@ -55,4 +57,20 @@ class errno_error : public std::runtime_error { }; +#if defined(PLATFORM_WIN32) +#include + +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