diff --git a/include/WinStd/COM.h b/include/WinStd/COM.h index 00fc2734..bf617ab2 100644 --- a/include/WinStd/COM.h +++ b/include/WinStd/COM.h @@ -29,6 +29,19 @@ namespace winstd class WINSTD_API bstr; class WINSTD_API variant; class WINSTD_API com_initializer; + + /// + /// \defgroup WinStdExceptions Exceptions + /// Additional exceptions + /// + /// @{ + + /// + /// COM runtime error + /// + typedef num_runtime_error com_runtime_error; + + /// @} } #pragma once diff --git a/include/WinStd/Common.h b/include/WinStd/Common.h index 1e7d0a1d..007b9310 100644 --- a/include/WinStd/Common.h +++ b/include/WinStd/Common.h @@ -43,6 +43,7 @@ #include #include +#include #include inline int vsnprintf(_Out_z_cap_(capacity) char *str, _In_ size_t capacity, _In_z_ _Printf_format_string_ const char *format, _In_ va_list arg); @@ -69,6 +70,17 @@ namespace winstd template class handle; template class dplhandle; + /// + /// \defgroup WinStdExceptions Exceptions + /// Additional exceptions + /// + /// @{ + + template class num_runtime_error; + class WINSTD_API win_runtime_error; + + /// @} + /// \addtogroup WinStdStrFormat /// @{ @@ -77,7 +89,7 @@ namespace winstd /// /// Single-byte character implementation of a class to support string formatting using `printf()` style templates /// - typedef basic_string_printf, std::allocator > string_printf; + typedef basic_string_printf, std::allocator > string_printf; /// /// Wide character implementation of a class to support string formatting using `printf()` style templates @@ -681,6 +693,150 @@ namespace winstd virtual handle_type duplicate_internal(_In_ handle_type h) const = 0; }; + /// @} + + /// + /// \defgroup WinStdExceptions Exceptions + /// Additional exceptions + /// + /// @{ + + /// + /// Windows runtime error + /// + template + class num_runtime_error : public std::runtime_error + { + public: + typedef _Tn error_type; + + public: + /// + /// Constructs an exception + /// + /// \param[in] error Numeric error code + /// \param[in] msg Error message + /// + inline num_runtime_error(_In_ error_type num, _In_ const std::string& msg) : + m_num(num), + runtime_error(msg.c_str()) + { + } + + + /// + /// Constructs an exception + /// + /// \param[in] num Numeric error code + /// \param[in] msg Error message + /// + inline num_runtime_error(_In_ error_type num, _In_z_ const char *msg) : + m_num(num), + runtime_error(msg) + { + } + + + /// + /// Copies an exception + /// + /// \param[in] other Exception to copy from + /// + inline num_runtime_error(const num_runtime_error<_Tn> &other) : + m_num(other.m_num), + runtime_error(other) + { + } + + + /// + /// Copies an exception + /// + /// \param[in] other Exception to copy from + /// + inline num_runtime_error& operator=(const num_runtime_error<_Tn> &other) + { + if (this != addressof(other)) { + *(runtime_error*)this = other; + m_num = other.m_num; + } + + return *this; + } + + + /// + /// Returns the Windows error number + /// + inline error_type number() const + { + return m_num; + } + + protected: + error_type m_num; ///< Numeric error code + }; + + + /// + /// Windows runtime error + /// + class WINSTD_API win_runtime_error : public num_runtime_error + { + public: + /// + /// Constructs an exception + /// + /// \param[in] error Windows error code + /// \param[in] msg Error message + /// + inline win_runtime_error(_In_ error_type num, _In_ const std::string& msg) : num_runtime_error(num, msg.c_str()) + { + } + + + /// + /// Constructs an exception + /// + /// \param[in] num Numeric error code + /// \param[in] msg Error message + /// + inline win_runtime_error(_In_ error_type num, _In_z_ const char *msg) : num_runtime_error(num, msg) + { + } + + + /// + /// Constructs an exception using `GetLastError()` + /// + /// \param[in] msg Error message + /// + inline win_runtime_error(_In_ const std::string& msg) : num_runtime_error(GetLastError(), msg.c_str()) + { + } + + + /// + /// Constructs an exception using `GetLastError()` + /// + /// \param[in] msg Error message + /// + inline win_runtime_error(_In_z_ const char *msg) : num_runtime_error(GetLastError(), msg) + { + } + + + /// + /// Copies an exception + /// + /// \param[in] other Exception to copy from + /// + inline win_runtime_error(const win_runtime_error &other) : num_runtime_error(other) + { + } + }; + + /// @} ///