ILifetimeWatcher.h (1664B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #pragma once 4 #include <Unknwn.h> 5 #include <winrt/Windows.Foundation.h> 6 7 namespace AppInstaller::WinRT 8 { 9 MIDL_INTERFACE("59b5623f-d03e-41f8-b400-89ee04ea02d7") 10 ILifetimeWatcher : public IUnknown 11 { 12 public: 13 // Due to the way the winrt types are set up, this watcher will not have AddRef called. 14 virtual /* [local] */ HRESULT STDMETHODCALLTYPE SetLifetimeWatcher( 15 IUnknown* watcher) = 0; 16 }; 17 18 // Implements ILifetimeWatcher functionality. 19 struct LifetimeWatcherBase 20 { 21 HRESULT STDMETHODCALLTYPE SetLifetimeWatcher(IUnknown* watcher) 22 { 23 m_lifetimeWatcher = winrt::Windows::Foundation::IUnknown(watcher, winrt::take_ownership_from_abi); 24 return S_OK; 25 } 26 27 void PropagateLifetimeWatcher(const winrt::Windows::Foundation::IUnknown& child) 28 { 29 if (m_lifetimeWatcher && child) 30 { 31 // Require that any call to this function is for an object that implements watching to prevent 32 // accidental assumptions about the child object and lifetime management. 33 auto watcher = child.as<ILifetimeWatcher>(); 34 35 // Create a copy of the lifetime watcher (to add_ref), then detach and pass it to the child to own. 36 watcher->SetLifetimeWatcher(static_cast<IUnknown*>(winrt::detach_abi(winrt::Windows::Foundation::IUnknown{ m_lifetimeWatcher }))); 37 } 38 } 39 40 private: 41 winrt::Windows::Foundation::IUnknown m_lifetimeWatcher; 42 }; 43 }