aboutsummaryrefslogtreecommitdiff
path: root/dxcommon/include/dxcommon.h
blob: 0270629d83ca25ca2ac6485ca05adb535904ef93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#pragma once

#include <wrl.h>

#include <stdexcept>

//#define IID_PPV_ARGS(ppType) __uuidof(**(ppType)), static_cast<void**>(ppType)

namespace dx {

using Microsoft::WRL::ComPtr;

class exception : public std::exception {
public:
    exception() noexcept {}

    exception(HRESULT result, const char* file, int line) noexcept
    {
        sprintf_s(m_error, sizeof(m_error), "%s:%d Failed with HRESULT = %08X",
            file, line, static_cast<unsigned int>(result));
    }

    exception(const char* error, const char* file, int line) noexcept
    {
        sprintf_s(m_error, sizeof(m_error), "%s:%d %s", file, line, error);
    }

    const char* what() const noexcept
    {
        return m_error;
    }

private:
    static char m_error[1024];
};

#define THROW(error) throw exception(error, __FILE__, __LINE__)

#define ThrowIfFailed(result) {\
    if (result != S_OK) {\
        THROW(result);\
    }\
}

template <typename T>
void SafeRelease(ComPtr<T>& ptr)
{
    if (ptr)
    {
        ptr->Release();
        ptr = nullptr;
    }
}

}  // dx