From 0468cd0d1a8a1cff1d838a741b050ef11f6d7461 Mon Sep 17 00:00:00 2001 From: Ellie Hermaszewska Date: Sat, 8 Apr 2023 01:11:27 +0800 Subject: Add SLANG_IID_PPV_ARGS (#2784) * Add SLANG_IID_PPV_ARGS To mirror IID_PPV_ARGS from the COM Coding Practices: https://learn.microsoft.com/en-us/windows/win32/LearnWin32/com-coding-practices#the-iid_ppv_args-macro * Make getTypeGuid constexpr --------- Co-authored-by: Yong He --- slang.h | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'slang.h') diff --git a/slang.h b/slang.h index d5630660b..5dd2cb550 100644 --- a/slang.h +++ b/slang.h @@ -932,21 +932,33 @@ extern "C" // It is not necessary to use the multiple parameters (we can wrap in parens), but this is simple. #define SLANG_COM_INTERFACE(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \ public: \ - SLANG_FORCE_INLINE static const SlangUUID& getTypeGuid() \ + SLANG_FORCE_INLINE constexpr static SlangUUID getTypeGuid() \ { \ - static const SlangUUID guid = { a, b, c, d0, d1, d2, d3, d4, d5, d6, d7 }; \ - return guid; \ + return { a, b, c, d0, d1, d2, d3, d4, d5, d6, d7 }; \ } // Sometimes it's useful to associate a guid with a class to identify it. This macro can used for this, // and the guid extracted via the getTypeGuid() function defined in the type #define SLANG_CLASS_GUID(a, b, c, d0, d1, d2, d3, d4, d5, d6, d7) \ - SLANG_FORCE_INLINE static const SlangUUID& getTypeGuid() \ + SLANG_FORCE_INLINE constexpr static SlangUUID getTypeGuid() \ { \ - static const SlangUUID guid = { a, b, c, d0, d1, d2, d3, d4, d5, d6, d7 }; \ - return guid; \ + return { a, b, c, d0, d1, d2, d3, d4, d5, d6, d7 }; \ } +// Helper to fill in pairs of GUIDs and return pointers. This ensures that the +// type of the GUID passed matches the pointer type, and that it is derived +// from ISlangUnknown, +// TODO(c++20): would is_derived_from be more appropriate here for private inheritance of ISlangUnknown? +// +// with : void createFoo(SlangUUID, void**); +// Slang::ComPtr myBar; +// call with: createFoo(SLANG_IID_PPV_ARGS(myBar.writeRef())) +// to call : createFoo(Bar::getTypeGuid(), (void**)(myBar.writeRef())) +#define SLANG_IID_PPV_ARGS(ppType) \ + std::decay_t::getTypeGuid(), \ + ((void)[]{static_assert(std::is_base_of_v>);}, reinterpret_cast(ppType)) + + /** Base interface for components exchanged through the API. This interface definition is compatible with the COM `IUnknown`, -- cgit v1.2.3