Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions strings/base_com_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,27 @@ WINRT_EXPORT namespace winrt

namespace winrt::impl
{
struct capture_decay
{
void** result;

template <typename T>
operator T** ()
{
return reinterpret_cast<T**>(result);
}
};

template <typename T, typename F, typename...Args>
int32_t capture_to(void**result, F function, Args&& ...args)
{
return function(args..., guid_of<T>(), result);
return function(args..., guid_of<T>(), capture_decay{ result });
}

template <typename T, typename O, typename M, typename...Args, std::enable_if_t<std::is_class_v<O> || std::is_union_v<O>, int> = 0>
int32_t capture_to(void** result, O* object, M method, Args&& ...args)
{
return (object->*method)(args..., guid_of<T>(), result);
return (object->*method)(args..., guid_of<T>(), capture_decay{ result });
}

template <typename T, typename O, typename M, typename...Args>
Expand Down Expand Up @@ -343,7 +354,7 @@ namespace winrt::impl
template <typename T, typename O, typename M, typename...Args>
int32_t capture_to(void** result, com_ptr<O> const& object, M method, Args&& ...args)
{
return (object.get()->*(method))(args..., guid_of<T>(), result);
return (object.get()->*(method))(args..., guid_of<T>(), capture_decay{ result });
}
}

Expand Down
39 changes: 39 additions & 0 deletions test/old_tests/UnitTests/capture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct DECLSPEC_UUID("5fb96f8d-409c-42a9-99a7-8a95c1459dbd") ICapture : ::IUnkno
{
virtual int32_t __stdcall GetValue() noexcept = 0;
virtual int32_t __stdcall CreateMemberCapture(int32_t value, GUID const& iid, void** object) noexcept = 0;
virtual int32_t __stdcall CreateMemberCapture2(int32_t value, GUID const& iid, ::IUnknown** object) noexcept = 0;
};

#ifdef __CRT_UUID_DECL
Expand All @@ -33,6 +34,12 @@ struct Capture : implements<Capture, ICapture>
auto capture = make<Capture>(value);
return capture->QueryInterface(iid, object);
}

int32_t __stdcall CreateMemberCapture2(int32_t value, GUID const& iid, ::IUnknown** object) noexcept override
{
auto capture = make<Capture>(value);
return capture->QueryInterface(iid, reinterpret_cast<void**>(object));
}
};

HRESULT __stdcall CreateCapture(int value, GUID const& iid, void** object) noexcept
Expand All @@ -41,6 +48,12 @@ HRESULT __stdcall CreateCapture(int value, GUID const& iid, void** object) noexc
return capture->QueryInterface(iid, object);
}

HRESULT __stdcall CreateCapture2(int value, GUID const& iid, ::IInspectable** object) noexcept
{
auto capture = make<Capture>(value);
return capture->QueryInterface(iid, reinterpret_cast<void**>(object));
}

TEST_CASE("capture")
{
// Capture from global function.
Expand All @@ -67,6 +80,19 @@ TEST_CASE("capture")

com_ptr<IDispatch> d;

// Capture with an unconventional result type.
auto e = capture<ICapture>(a, &ICapture::CreateMemberCapture2, 30);
REQUIRE(e->GetValue() == 30);
e = nullptr;
e.capture(a, &ICapture::CreateMemberCapture2, 40);
REQUIRE(e->GetValue() == 40);

com_ptr<ICapture> f = capture<ICapture>(CreateCapture2, 10);
REQUIRE(f->GetValue() == 10);
f = nullptr;
f.capture(CreateCapture2, 20);
REQUIRE(a->GetValue() == 20);

REQUIRE_THROWS_AS(capture<IDispatch>(CreateCapture, 0), hresult_no_interface);
REQUIRE_THROWS_AS(d.capture(CreateCapture, 0), hresult_no_interface);
REQUIRE_THROWS_AS(capture<IDispatch>(a, &ICapture::CreateMemberCapture, 0), hresult_no_interface);
Expand Down Expand Up @@ -104,6 +130,19 @@ TEST_CASE("try_capture")

com_ptr<IDispatch> d;

// Capture with an unconventional result type.
auto e = try_capture<ICapture>(a, &ICapture::CreateMemberCapture2, 30);
REQUIRE(e->GetValue() == 30);
e = nullptr;
REQUIRE(e.try_capture(a, &ICapture::CreateMemberCapture2, 40));
REQUIRE(e->GetValue() == 40);

com_ptr<ICapture> f = try_capture<ICapture>(CreateCapture2, 10);
REQUIRE(f->GetValue() == 10);
f = nullptr;
REQUIRE(f.try_capture(CreateCapture2, 20));
REQUIRE(f->GetValue() == 20);

REQUIRE(!try_capture<IDispatch>(CreateCapture, 0));
REQUIRE(!d.try_capture(CreateCapture, 0));
REQUIRE(!try_capture<IDispatch>(a, &ICapture::CreateMemberCapture, 0));
Expand Down