Rise
The Vieneo Province
Loading...
Searching...
No Matches
PlatformHelpers.h
Go to the documentation of this file.
1//--------------------------------------------------------------------------------------
2// File: PlatformHelpers.h
3//
4// Copyright (c) Microsoft Corporation.
5// Licensed under the MIT License.
6//
7// https://go.microsoft.com/fwlink/?LinkId=248929
8// https://go.microsoft.com/fwlink/?LinkID=615561
9//--------------------------------------------------------------------------------------
10
11#pragma once
12
13#ifdef _MSC_VER
14#pragma warning(disable : 4324)
15#endif
16
17#include <cstdio>
18#include <exception>
19#include <memory>
20#include <string>
21
22#ifndef MAKEFOURCC
23#define MAKEFOURCC(ch0, ch1, ch2, ch3) \
24 (static_cast<uint32_t>(static_cast<uint8_t>(ch0)) | (static_cast<uint32_t>(static_cast<uint8_t>(ch1)) << 8) \
25 | (static_cast<uint32_t>(static_cast<uint8_t>(ch2)) << 16) | (static_cast<uint32_t>(static_cast<uint8_t>(ch3)) << 24))
26#endif /* defined(MAKEFOURCC) */
27
28// See https://walbourn.github.io/modern-c++-bitmask-types/
29#ifndef ENUM_FLAGS_CONSTEXPR
30#if defined(NTDDI_WIN10_RS1) && !defined(__MINGW32__)
31#define ENUM_FLAGS_CONSTEXPR constexpr
32#else
33#define ENUM_FLAGS_CONSTEXPR const
34#endif
35#endif
36
37namespace DirectX
38{
39 // Helper class for COM exceptions
40 class com_exception : public std::exception
41 {
42 public:
44 : result(hr)
45 {
46 char str[64] = {};
47 sprintf_s(str, "Failure with HRESULT of %08X", static_cast<unsigned int>(result));
48 message = str;
49 }
50
51 const char* what() const noexcept override { return message.c_str(); }
52
53 HRESULT get_result() const noexcept { return result; }
54
55 private:
57 std::string message;
58 };
59
60 // Helper utility converts D3D API failures into exceptions.
61 inline void ThrowIfFailed(HRESULT hr) noexcept(false)
62 {
63 if (FAILED(hr))
64 {
65 throw com_exception(hr);
66 }
67 }
68
69 // Helper for output debug tracing
70 inline void DebugTrace(_In_z_ _Printf_format_string_ const char* format, ...) noexcept
71 {
72#ifdef _DEBUG
73 va_list args;
74 va_start(args, format);
75
76 char buff[1024] = {};
77 vsprintf_s(buff, format, args);
78 OutputDebugStringA(buff);
79 va_end(args);
80#else
81 UNREFERENCED_PARAMETER(format);
82#endif
83 }
84
85 // Helper smart-pointers
86#if (_WIN32_WINNT >= _WIN32_WINNT_WIN10) || (defined(_XBOX_ONE) && defined(_TITLE)) || !defined(WINAPI_FAMILY) \
87 || (WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP)
89 {
90 void operator()(void* p) noexcept
91 {
92 if (p)
93 VirtualFree(p, 0, MEM_RELEASE);
94 }
95 };
96#endif
97
99 {
100 void operator()(HANDLE h) noexcept
101 {
102 if (h)
103 CloseHandle(h);
104 }
105 };
106
107 using ScopedHandle = std::unique_ptr<void, handle_closer>;
108
109 inline HANDLE safe_handle(HANDLE h) noexcept
110 {
111 return (h == INVALID_HANDLE_VALUE) ? nullptr : h;
112 }
113} // namespace DirectX
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
const char * what() const noexcept override
HRESULT get_result() const noexcept
Definition DDS.h:24
void DebugTrace(_In_z_ _Printf_format_string_ const char *format,...) noexcept
HANDLE safe_handle(HANDLE h) noexcept
std::unique_ptr< void, handle_closer > ScopedHandle
void ThrowIfFailed(HRESULT hr) noexcept(false)
void operator()(HANDLE h) noexcept
void operator()(void *p) noexcept