diff options
Diffstat (limited to 'dxcommon/include')
| -rw-r--r-- | dxcommon/include/dxcommon.h | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/dxcommon/include/dxcommon.h b/dxcommon/include/dxcommon.h new file mode 100644 index 0000000..0270629 --- /dev/null +++ b/dxcommon/include/dxcommon.h | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <wrl.h> | ||
| 4 | |||
| 5 | #include <stdexcept> | ||
| 6 | |||
| 7 | //#define IID_PPV_ARGS(ppType) __uuidof(**(ppType)), static_cast<void**>(ppType) | ||
| 8 | |||
| 9 | namespace dx { | ||
| 10 | |||
| 11 | using Microsoft::WRL::ComPtr; | ||
| 12 | |||
| 13 | class exception : public std::exception { | ||
| 14 | public: | ||
| 15 | exception() noexcept {} | ||
| 16 | |||
| 17 | exception(HRESULT result, const char* file, int line) noexcept | ||
| 18 | { | ||
| 19 | sprintf_s(m_error, sizeof(m_error), "%s:%d Failed with HRESULT = %08X", | ||
| 20 | file, line, static_cast<unsigned int>(result)); | ||
| 21 | } | ||
| 22 | |||
| 23 | exception(const char* error, const char* file, int line) noexcept | ||
| 24 | { | ||
| 25 | sprintf_s(m_error, sizeof(m_error), "%s:%d %s", file, line, error); | ||
| 26 | } | ||
| 27 | |||
| 28 | const char* what() const noexcept | ||
| 29 | { | ||
| 30 | return m_error; | ||
| 31 | } | ||
| 32 | |||
| 33 | private: | ||
| 34 | static char m_error[1024]; | ||
| 35 | }; | ||
| 36 | |||
| 37 | #define THROW(error) throw exception(error, __FILE__, __LINE__) | ||
| 38 | |||
| 39 | #define ThrowIfFailed(result) {\ | ||
| 40 | if (result != S_OK) {\ | ||
| 41 | THROW(result);\ | ||
| 42 | }\ | ||
| 43 | } | ||
| 44 | |||
| 45 | template <typename T> | ||
| 46 | void SafeRelease(ComPtr<T>& ptr) | ||
| 47 | { | ||
| 48 | if (ptr) | ||
| 49 | { | ||
| 50 | ptr->Release(); | ||
| 51 | ptr = nullptr; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | } // dx | ||
