Skip to content

Commit 3aff17b

Browse files
authored
Support boxing of GUID types for improved interop (#424)
1 parent 5034a43 commit 3aff17b

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

strings/base_reference_produce.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,14 @@ namespace winrt::impl
197197
static auto make(guid const& value) { return Windows::Foundation::PropertyValue::CreateGuid(value); }
198198
};
199199

200+
#ifdef __IUnknown_INTERFACE_DEFINED__
201+
template <>
202+
struct reference_traits<GUID>
203+
{
204+
static auto make(GUID const& value) { return Windows::Foundation::PropertyValue::CreateGuid(value); }
205+
};
206+
#endif
207+
200208
template <>
201209
struct reference_traits<Windows::Foundation::DateTime>
202210
{
@@ -291,6 +299,12 @@ WINRT_EXPORT namespace winrt
291299
return static_cast<T>(value.as<Windows::Foundation::IReference<std::underlying_type_t<T>>>().Value());
292300
}
293301
}
302+
#ifdef __IUnknown_INTERFACE_DEFINED__
303+
else if constexpr (std::is_same_v<T, GUID>)
304+
{
305+
return value.as<Windows::Foundation::IReference<guid>>().Value();
306+
}
307+
#endif
294308
else
295309
{
296310
return value.as<Windows::Foundation::IReference<T>>().Value();
@@ -335,6 +349,15 @@ WINRT_EXPORT namespace winrt
335349
return static_cast<T>(temp.Value());
336350
}
337351
}
352+
#ifdef __IUnknown_INTERFACE_DEFINED__
353+
else if constexpr (std::is_same_v<T, GUID>)
354+
{
355+
if (auto temp = value.try_as<Windows::Foundation::IReference<guid>>())
356+
{
357+
return temp.Value();
358+
}
359+
}
360+
#endif
338361
else
339362
{
340363
if (auto temp = value.try_as<Windows::Foundation::IReference<T>>())

test/test/box_guid.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "pch.h"
2+
3+
TEST_CASE("box_guid")
4+
{
5+
winrt::guid const winrt_guid = __uuidof(::IUnknown);
6+
GUID const sdk_guid = winrt_guid;
7+
8+
auto box_a = winrt::box_value(winrt_guid);
9+
auto box_b = winrt::box_value(sdk_guid);
10+
11+
winrt::guid unbox_a = winrt::unbox_value<winrt::guid>(box_a);
12+
GUID unbox_b = winrt::unbox_value<GUID>(box_a);
13+
14+
REQUIRE(unbox_a == winrt_guid);
15+
REQUIRE(unbox_b == sdk_guid);
16+
17+
unbox_a = winrt::unbox_value_or<winrt::guid>(box_a, winrt::guid{});
18+
unbox_b = winrt::unbox_value_or<GUID>(box_a, GUID{});
19+
20+
REQUIRE(unbox_a == winrt_guid);
21+
REQUIRE(unbox_b == sdk_guid);
22+
}

test/test/test.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@
286286
<ClCompile Include="async_auto_cancel.cpp" />
287287
<ClCompile Include="async_cancel_callback.cpp" />
288288
<ClCompile Include="async_check_cancel.cpp" />
289+
<ClCompile Include="box_guid.cpp" />
289290
<ClCompile Include="error_info.cpp" />
290291
<ClCompile Include="event_deferral.cpp" />
291292
<ClCompile Include="async_local.cpp" />

0 commit comments

Comments
 (0)