microsoft / cppwinrt

C++/WinRT
MIT License
1.61k stars 232 forks source link

Project interfaces without forwarders to required interfaces #1392

Closed JaiganeshKumaran closed 4 months ago

JaiganeshKumaran commented 4 months ago

For efficiency reasons, sometimes I want to have references to all interface that I am going to use separately. However, since projected types have forwarders for methods on the required interfaces, I cannot easily tell if an implicit query was made.

github-actions[bot] commented 4 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.

JaiganeshKumaran commented 4 months ago

I have this in my project.

template <typename T>
struct exclusive
{
    T underlying;

    winrt::impl::consume_t<T>* operator->() const noexcept
    {
        return &underlying;
    }
};

struct multi_interface_cache
{
    exclusive<IInterface1> one;
    exclusive<IInterface2> two;
};

IInspectable obj = ...;
multi_interface_cache cache;
obj.as(cache.one.underlying);
obj.as(cache.two.underlying);

The exclusive wrapper prevents accidental query-interface calls by disallowing calls to methods on other required interfaces.