microsoft / cppwinrt

C++/WinRT
MIT License
1.65k stars 238 forks source link

Simply creating a projected type with added ref count #1382

Closed JaiganeshKumaran closed 8 months ago

JaiganeshKumaran commented 9 months ago

Currently, C++/WinRT provides the copy_from_abi function template that internally calls AddRef and assigns the pointer value to a projected type.

::IUnknown* unknownPtr = ...;
winrt::Windows::Foundation::IUnknown unknown;
copy_from_abi(unknown, unknownPtr);

However, often times you want to construct a new projected type instance with the ref count incremented. At the moment you either have to call AddRef yourself and use the constructor taking take_ownership_from_abi_t, or use reinterpret_cast.

::IUnknown* unknownPtr = ...;
unknownPtr->AddRef();
winrt::Windows::Foundation::IUnknown unknown{ unknownPtr, take_ownership_from_abi };
(or)
auto unknown = reinterpret_cast<winrt::Windows::Foundation::IUnknown const&>(unknownPtr);

I propose having a new marker type, duplicate_from_abi_t, to simply this pattern.

::IUnknown* unknownPtr = ...;
winrt::Windows::Foundation::IUnknown unknown{ unknownPtr, duplicate_from_abi };
github-actions[bot] commented 9 months ago

This issue is stale because it has been open 10 days with no activity. Remove stale label or comment or this will be closed in 5 days.