From 556cf073d61875368fe8511b75f5cb7db04ccb52 Mon Sep 17 00:00:00 2001 From: Marc Sunet Date: Wed, 19 Nov 2025 11:24:21 -0800 Subject: Use C++ modules --- dxcommon/CMakeLists.txt | 16 +++++++++---- dxcommon/dxcommon.h | 17 +++++++++++++ dxcommon/dxcommon.ixx | 53 +++++++++++++++++++++++++++++++++++++++++ dxcommon/include/dxcommon.h | 58 --------------------------------------------- dxcommon/src/dxcommon.cc | 8 ------- 5 files changed, 82 insertions(+), 70 deletions(-) create mode 100644 dxcommon/dxcommon.h create mode 100644 dxcommon/dxcommon.ixx delete mode 100644 dxcommon/include/dxcommon.h delete mode 100644 dxcommon/src/dxcommon.cc (limited to 'dxcommon') diff --git a/dxcommon/CMakeLists.txt b/dxcommon/CMakeLists.txt index 08fa178..dfb52bf 100644 --- a/dxcommon/CMakeLists.txt +++ b/dxcommon/CMakeLists.txt @@ -1,10 +1,18 @@ -cmake_minimum_required(VERSION 3.20) +cmake_minimum_required(VERSION 3.25) -add_library(dxcommon - src/dxcommon.cc) +project(dxcommon) + +add_library(dxcommon) + +target_sources(dxcommon PUBLIC + dxcommon.h) + +target_sources(dxcommon PUBLIC + FILE_SET cxx_modules TYPE CXX_MODULES FILES + dxcommon.ixx) target_include_directories(dxcommon PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/include) + .) target_link_libraries(dxcommon PUBLIC DirectX-Headers diff --git a/dxcommon/dxcommon.h b/dxcommon/dxcommon.h new file mode 100644 index 0000000..addb8c3 --- /dev/null +++ b/dxcommon/dxcommon.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include +#include + +#define THROW(error) throw exception(error, __FILE__, __LINE__) + +#define ThrowIfFailed(result) \ +{\ + if (result != S_OK) \ + {\ + THROW(result);\ + }\ +} + +//#define IID_PPV_ARGS(ppType) __uuidof(**(ppType)), static_cast(ppType) diff --git a/dxcommon/dxcommon.ixx b/dxcommon/dxcommon.ixx new file mode 100644 index 0000000..b06ae95 --- /dev/null +++ b/dxcommon/dxcommon.ixx @@ -0,0 +1,53 @@ +module; + +#include +#include + +export module dxcommon; + +using Microsoft::WRL::ComPtr; + +namespace dx { + +export { + +class exception : public std::exception +{ +public: + exception() noexcept = default; + + 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(result)); + } + + exception(const char* error, const char* file, int line) noexcept + { + sprintf_s(m_error, sizeof(m_error), "%s:%d %s", file, line, error); + } + + [[nodiscard]] const char* what() const noexcept final + { + return m_error; + } + +private: + static thread_local char m_error[1024]; +}; + +template +void SafeRelease(ComPtr& ptr) +{ + if (ptr) + { + ptr->Release(); + ptr = nullptr; + } +} + +} // export + +thread_local char exception::m_error[1024]; + +} // dx diff --git a/dxcommon/include/dxcommon.h b/dxcommon/include/dxcommon.h deleted file mode 100644 index 35ec0e2..0000000 --- a/dxcommon/include/dxcommon.h +++ /dev/null @@ -1,58 +0,0 @@ -#pragma once - -#include - -#include - -//#define IID_PPV_ARGS(ppType) __uuidof(**(ppType)), static_cast(ppType) - -namespace dx { - -using Microsoft::WRL::ComPtr; - -class exception : public std::exception -{ -public: - exception() noexcept = default; - - 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(result)); - } - - exception(const char* error, const char* file, int line) noexcept - { - sprintf_s(m_error, sizeof(m_error), "%s:%d %s", file, line, error); - } - - [[nodiscard]] const char* what() const noexcept final - { - return m_error; - } - -private: - static thread_local char m_error[1024]; -}; - -#define THROW(error) throw exception(error, __FILE__, __LINE__) - -#define ThrowIfFailed(result) \ -{\ - if (result != S_OK) \ - {\ - THROW(result);\ - }\ -} - -template -void SafeRelease(ComPtr& ptr) -{ - if (ptr) - { - ptr->Release(); - ptr = nullptr; - } -} - -} // dx diff --git a/dxcommon/src/dxcommon.cc b/dxcommon/src/dxcommon.cc deleted file mode 100644 index 2139136..0000000 --- a/dxcommon/src/dxcommon.cc +++ /dev/null @@ -1,8 +0,0 @@ -#include "dxcommon.h" - -namespace dx -{ - -thread_local char exception::m_error[1024]; - -} // namespace dx -- cgit v1.2.3