// C++/WinRT v2.0.250303.1 // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #pragma once #ifndef WINRT_Windows_System_Threading_H #define WINRT_Windows_System_Threading_H #include "winrt/base.h" static_assert(winrt::check_version(CPPWINRT_VERSION, "2.0.250303.1"), "Mismatched C++/WinRT headers."); #define CPPWINRT_VERSION "2.0.250303.1" #include "winrt/Windows.System.h" #include "winrt/impl/Windows.Foundation.2.h" #include "winrt/impl/Windows.System.Threading.2.h" namespace winrt::impl { template auto consume_Windows_System_Threading_IThreadPoolStatics::RunAsync(winrt::Windows::System::Threading::WorkItemHandler const& handler) const { void* operation{}; if constexpr (!std::is_same_v) { winrt::hresult _winrt_cast_result_code; auto const _winrt_casted_result = impl::try_as_with_reason(static_cast(this), _winrt_cast_result_code); check_hresult(_winrt_cast_result_code); auto const _winrt_abi_type = *(abi_t**)&_winrt_casted_result; check_hresult(_winrt_abi_type->RunAsync(*(void**)(&handler), &operation)); } else { auto const _winrt_abi_type = *(abi_t**)this; check_hresult(_winrt_abi_type->RunAsync(*(void**)(&handler), &operation)); } return winrt::Windows::Foundation::IAsyncAction{ operation, take_ownership_from_abi }; } template auto consume_Windows_System_Threading_IThreadPoolStatics::RunAsync(winrt::Windows::System::Threading::WorkItemHandler const& handler, winrt::Windows::System::Threading::WorkItemPriority const& priority) const { void* operation{}; if constexpr (!std::is_same_v) { winrt::hresult _winrt_cast_result_code; auto const _winrt_casted_result = impl::try_as_with_reason(static_cast(this), _winrt_cast_result_code); check_hresult(_winrt_cast_result_code); auto const _winrt_abi_type = *(abi_t**)&_winrt_casted_result; check_hresult(_winrt_abi_type->RunWithPriorityAsync(*(void**)(&handler), static_cast(priority), &operation)); } else { auto const _winrt_abi_type = *(abi_t**)this; check_hresult(_winrt_abi_type->RunWithPriorityAsync(*(void**)(&handler), static_cast(priority), &operation)); } return winrt::Windows::Foundation::IAsyncAction{ operation, take_ownership_from_abi }; } template auto consume_Windows_System_Threading_IThreadPoolStatics::RunAsync(winrt::Windows::System::Threading::WorkItemHandler const& handler, winrt::Windows::System::Threading::WorkItemPriority const& priority, winrt::Windows::System::Threading::WorkItemOptions const& options) const { void* operation{}; if constexpr (!std::is_same_v) { winrt::hresult _winrt_cast_result_code; auto const _winrt_casted_result = impl::try_as_with_reason(static_cast(this), _winrt_cast_result_code); check_hresult(_winrt_cast_result_code); auto const _winrt_abi_type = *(abi_t**)&_winrt_casted_result; check_hresult(_winrt_abi_type->RunWithPriorityAndOptionsAsync(*(void**)(&handler), static_cast(priority), static_cast(options), &operation)); } else { auto const _winrt_abi_type = *(abi_t**)this; check_hresult(_winrt_abi_type->RunWithPriorityAndOptionsAsync(*(void**)(&handler), static_cast(priority), static_cast(options), &operation)); } return winrt::Windows::Foundation::IAsyncAction{ operation, take_ownership_from_abi }; } template auto consume_Windows_System_Threading_IThreadPoolTimer::Period() const { winrt::Windows::Foundation::TimeSpan value{}; if constexpr (!std::is_same_v) { winrt::hresult _winrt_cast_result_code; auto const _winrt_casted_result = impl::try_as_with_reason(static_cast(this), _winrt_cast_result_code); check_hresult(_winrt_cast_result_code); auto const _winrt_abi_type = *(abi_t**)&_winrt_casted_result; check_hresult(_winrt_abi_type->get_Period(put_abi(value))); } else { auto const _winrt_abi_type = *(abi_t**)this; check_hresult(_winrt_abi_type->get_Period(put_abi(value))); } return value; } template auto consume_Windows_System_Threading_IThreadPoolTimer::Delay() const { winrt::Windows::Foundation::TimeSpan value{}; if constexpr (!std::is_same_v) { winrt::hresult _winrt_cast_result_code; auto const _winrt_casted_result = impl::try_as_with_reason(static_cast(this), _winrt_cast_result_code); check_hresult(_winrt_cast_result_code); auto const _winrt_abi_type = *(abi_t**)&_winrt_casted_result; check_hresult(_winrt_abi_type->get_Delay(put_abi(value))); } else { auto const _winrt_abi_type = *(abi_t**)this; check_hresult(_winrt_abi_type->get_Delay(put_abi(value))); } return value; } template auto consume_Windows_System_Threading_IThreadPoolTimer::Cancel() const { if constexpr (!std::is_same_v) { winrt::hresult _winrt_cast_result_code; auto const _winrt_casted_result = impl::try_as_with_reason(static_cast(this), _winrt_cast_result_code); check_hresult(_winrt_cast_result_code); auto const _winrt_abi_type = *(abi_t**)&_winrt_casted_result; check_hresult(_winrt_abi_type->Cancel()); } else { auto const _winrt_abi_type = *(abi_t**)this; check_hresult(_winrt_abi_type->Cancel()); } } template auto consume_Windows_System_Threading_IThreadPoolTimerStatics::CreatePeriodicTimer(winrt::Windows::System::Threading::TimerElapsedHandler const& handler, winrt::Windows::Foundation::TimeSpan const& period) const { void* timer{}; if constexpr (!std::is_same_v) { winrt::hresult _winrt_cast_result_code; auto const _winrt_casted_result = impl::try_as_with_reason(static_cast(this), _winrt_cast_result_code); check_hresult(_winrt_cast_result_code); auto const _winrt_abi_type = *(abi_t**)&_winrt_casted_result; check_hresult(_winrt_abi_type->CreatePeriodicTimer(*(void**)(&handler), impl::bind_in(period), &timer)); } else { auto const _winrt_abi_type = *(abi_t**)this; check_hresult(_winrt_abi_type->CreatePeriodicTimer(*(void**)(&handler), impl::bind_in(period), &timer)); } return winrt::Windows::System::Threading::ThreadPoolTimer{ timer, take_ownership_from_abi }; } template auto consume_Windows_System_Threading_IThreadPoolTimerStatics::CreateTimer(winrt::Windows::System::Threading::TimerElapsedHandler const& handler, winrt::Windows::Foundation::TimeSpan const& delay) const { void* timer{}; if constexpr (!std::is_same_v) { winrt::hresult _winrt_cast_result_code; auto const _winrt_casted_result = impl::try_as_with_reason(static_cast(this), _winrt_cast_result_code); check_hresult(_winrt_cast_result_code); auto const _winrt_abi_type = *(abi_t**)&_winrt_casted_result; check_hresult(_winrt_abi_type->CreateTimer(*(void**)(&handler), impl::bind_in(delay), &timer)); } else { auto const _winrt_abi_type = *(abi_t**)this; check_hresult(_winrt_abi_type->CreateTimer(*(void**)(&handler), impl::bind_in(delay), &timer)); } return winrt::Windows::System::Threading::ThreadPoolTimer{ timer, take_ownership_from_abi }; } template auto consume_Windows_System_Threading_IThreadPoolTimerStatics::CreatePeriodicTimer(winrt::Windows::System::Threading::TimerElapsedHandler const& handler, winrt::Windows::Foundation::TimeSpan const& period, winrt::Windows::System::Threading::TimerDestroyedHandler const& destroyed) const { void* timer{}; if constexpr (!std::is_same_v) { winrt::hresult _winrt_cast_result_code; auto const _winrt_casted_result = impl::try_as_with_reason(static_cast(this), _winrt_cast_result_code); check_hresult(_winrt_cast_result_code); auto const _winrt_abi_type = *(abi_t**)&_winrt_casted_result; check_hresult(_winrt_abi_type->CreatePeriodicTimerWithCompletion(*(void**)(&handler), impl::bind_in(period), *(void**)(&destroyed), &timer)); } else { auto const _winrt_abi_type = *(abi_t**)this; check_hresult(_winrt_abi_type->CreatePeriodicTimerWithCompletion(*(void**)(&handler), impl::bind_in(period), *(void**)(&destroyed), &timer)); } return winrt::Windows::System::Threading::ThreadPoolTimer{ timer, take_ownership_from_abi }; } template auto consume_Windows_System_Threading_IThreadPoolTimerStatics::CreateTimer(winrt::Windows::System::Threading::TimerElapsedHandler const& handler, winrt::Windows::Foundation::TimeSpan const& delay, winrt::Windows::System::Threading::TimerDestroyedHandler const& destroyed) const { void* timer{}; if constexpr (!std::is_same_v) { winrt::hresult _winrt_cast_result_code; auto const _winrt_casted_result = impl::try_as_with_reason(static_cast(this), _winrt_cast_result_code); check_hresult(_winrt_cast_result_code); auto const _winrt_abi_type = *(abi_t**)&_winrt_casted_result; check_hresult(_winrt_abi_type->CreateTimerWithCompletion(*(void**)(&handler), impl::bind_in(delay), *(void**)(&destroyed), &timer)); } else { auto const _winrt_abi_type = *(abi_t**)this; check_hresult(_winrt_abi_type->CreateTimerWithCompletion(*(void**)(&handler), impl::bind_in(delay), *(void**)(&destroyed), &timer)); } return winrt::Windows::System::Threading::ThreadPoolTimer{ timer, take_ownership_from_abi }; } template struct delegate final : implements_delegate { delegate(H&& handler) : implements_delegate(std::forward(handler)) {} int32_t __stdcall Invoke(void* timer) noexcept final try { (*this)(*reinterpret_cast(&timer)); return 0; } catch (...) { return to_hresult(); } }; template struct delegate final : implements_delegate { delegate(H&& handler) : implements_delegate(std::forward(handler)) {} int32_t __stdcall Invoke(void* timer) noexcept final try { (*this)(*reinterpret_cast(&timer)); return 0; } catch (...) { return to_hresult(); } }; template struct delegate final : implements_delegate { delegate(H&& handler) : implements_delegate(std::forward(handler)) {} int32_t __stdcall Invoke(void* operation) noexcept final try { (*this)(*reinterpret_cast(&operation)); return 0; } catch (...) { return to_hresult(); } }; #ifndef WINRT_LEAN_AND_MEAN template struct produce : produce_base { int32_t __stdcall RunAsync(void* handler, void** operation) noexcept final try { clear_abi(operation); typename D::abi_guard guard(this->shim()); *operation = detach_from(this->shim().RunAsync(*reinterpret_cast(&handler))); return 0; } catch (...) { return to_hresult(); } int32_t __stdcall RunWithPriorityAsync(void* handler, int32_t priority, void** operation) noexcept final try { clear_abi(operation); typename D::abi_guard guard(this->shim()); *operation = detach_from(this->shim().RunAsync(*reinterpret_cast(&handler), *reinterpret_cast(&priority))); return 0; } catch (...) { return to_hresult(); } int32_t __stdcall RunWithPriorityAndOptionsAsync(void* handler, int32_t priority, uint32_t options, void** operation) noexcept final try { clear_abi(operation); typename D::abi_guard guard(this->shim()); *operation = detach_from(this->shim().RunAsync(*reinterpret_cast(&handler), *reinterpret_cast(&priority), *reinterpret_cast(&options))); return 0; } catch (...) { return to_hresult(); } }; #endif #ifndef WINRT_LEAN_AND_MEAN template struct produce : produce_base { int32_t __stdcall get_Period(int64_t* value) noexcept final try { zero_abi(value); typename D::abi_guard guard(this->shim()); *value = detach_from(this->shim().Period()); return 0; } catch (...) { return to_hresult(); } int32_t __stdcall get_Delay(int64_t* value) noexcept final try { zero_abi(value); typename D::abi_guard guard(this->shim()); *value = detach_from(this->shim().Delay()); return 0; } catch (...) { return to_hresult(); } int32_t __stdcall Cancel() noexcept final try { typename D::abi_guard guard(this->shim()); this->shim().Cancel(); return 0; } catch (...) { return to_hresult(); } }; #endif #ifndef WINRT_LEAN_AND_MEAN template struct produce : produce_base { int32_t __stdcall CreatePeriodicTimer(void* handler, int64_t period, void** timer) noexcept final try { clear_abi(timer); typename D::abi_guard guard(this->shim()); *timer = detach_from(this->shim().CreatePeriodicTimer(*reinterpret_cast(&handler), *reinterpret_cast(&period))); return 0; } catch (...) { return to_hresult(); } int32_t __stdcall CreateTimer(void* handler, int64_t delay, void** timer) noexcept final try { clear_abi(timer); typename D::abi_guard guard(this->shim()); *timer = detach_from(this->shim().CreateTimer(*reinterpret_cast(&handler), *reinterpret_cast(&delay))); return 0; } catch (...) { return to_hresult(); } int32_t __stdcall CreatePeriodicTimerWithCompletion(void* handler, int64_t period, void* destroyed, void** timer) noexcept final try { clear_abi(timer); typename D::abi_guard guard(this->shim()); *timer = detach_from(this->shim().CreatePeriodicTimer(*reinterpret_cast(&handler), *reinterpret_cast(&period), *reinterpret_cast(&destroyed))); return 0; } catch (...) { return to_hresult(); } int32_t __stdcall CreateTimerWithCompletion(void* handler, int64_t delay, void* destroyed, void** timer) noexcept final try { clear_abi(timer); typename D::abi_guard guard(this->shim()); *timer = detach_from(this->shim().CreateTimer(*reinterpret_cast(&handler), *reinterpret_cast(&delay), *reinterpret_cast(&destroyed))); return 0; } catch (...) { return to_hresult(); } }; #endif } WINRT_EXPORT namespace winrt::Windows::System::Threading { constexpr auto operator|(WorkItemOptions const left, WorkItemOptions const right) noexcept { return static_cast(impl::to_underlying_type(left) | impl::to_underlying_type(right)); } constexpr auto operator|=(WorkItemOptions& left, WorkItemOptions const right) noexcept { left = left | right; return left; } constexpr auto operator&(WorkItemOptions const left, WorkItemOptions const right) noexcept { return static_cast(impl::to_underlying_type(left) & impl::to_underlying_type(right)); } constexpr auto operator&=(WorkItemOptions& left, WorkItemOptions const right) noexcept { left = left & right; return left; } constexpr auto operator~(WorkItemOptions const value) noexcept { return static_cast(~impl::to_underlying_type(value)); } constexpr auto operator^(WorkItemOptions const left, WorkItemOptions const right) noexcept { return static_cast(impl::to_underlying_type(left) ^ impl::to_underlying_type(right)); } constexpr auto operator^=(WorkItemOptions& left, WorkItemOptions const right) noexcept { left = left ^ right; return left; } inline auto ThreadPool::RunAsync(winrt::Windows::System::Threading::WorkItemHandler const& handler) { return impl::call_factory([&](IThreadPoolStatics const& f) { return f.RunAsync(handler); }); } inline auto ThreadPool::RunAsync(winrt::Windows::System::Threading::WorkItemHandler const& handler, winrt::Windows::System::Threading::WorkItemPriority const& priority) { return impl::call_factory([&](IThreadPoolStatics const& f) { return f.RunAsync(handler, priority); }); } inline auto ThreadPool::RunAsync(winrt::Windows::System::Threading::WorkItemHandler const& handler, winrt::Windows::System::Threading::WorkItemPriority const& priority, winrt::Windows::System::Threading::WorkItemOptions const& options) { return impl::call_factory([&](IThreadPoolStatics const& f) { return f.RunAsync(handler, priority, options); }); } inline auto ThreadPoolTimer::CreatePeriodicTimer(winrt::Windows::System::Threading::TimerElapsedHandler const& handler, winrt::Windows::Foundation::TimeSpan const& period) { return impl::call_factory([&](IThreadPoolTimerStatics const& f) { return f.CreatePeriodicTimer(handler, period); }); } inline auto ThreadPoolTimer::CreateTimer(winrt::Windows::System::Threading::TimerElapsedHandler const& handler, winrt::Windows::Foundation::TimeSpan const& delay) { return impl::call_factory([&](IThreadPoolTimerStatics const& f) { return f.CreateTimer(handler, delay); }); } inline auto ThreadPoolTimer::CreatePeriodicTimer(winrt::Windows::System::Threading::TimerElapsedHandler const& handler, winrt::Windows::Foundation::TimeSpan const& period, winrt::Windows::System::Threading::TimerDestroyedHandler const& destroyed) { return impl::call_factory([&](IThreadPoolTimerStatics const& f) { return f.CreatePeriodicTimer(handler, period, destroyed); }); } inline auto ThreadPoolTimer::CreateTimer(winrt::Windows::System::Threading::TimerElapsedHandler const& handler, winrt::Windows::Foundation::TimeSpan const& delay, winrt::Windows::System::Threading::TimerDestroyedHandler const& destroyed) { return impl::call_factory([&](IThreadPoolTimerStatics const& f) { return f.CreateTimer(handler, delay, destroyed); }); } template TimerDestroyedHandler::TimerDestroyedHandler(L handler) : TimerDestroyedHandler(impl::make_delegate(std::forward(handler))) { } template TimerDestroyedHandler::TimerDestroyedHandler(F* handler) : TimerDestroyedHandler([=](auto&&... args) { return handler(args...); }) { } template TimerDestroyedHandler::TimerDestroyedHandler(O* object, M method) : TimerDestroyedHandler([=](auto&&... args) { return ((*object).*(method))(args...); }) { } template TimerDestroyedHandler::TimerDestroyedHandler(com_ptr&& object, M method) : TimerDestroyedHandler([o = std::move(object), method](auto&&... args) { return ((*o).*(method))(args...); }) { } template TimerDestroyedHandler::TimerDestroyedHandler(weak_ref&& object, LM&& lambda_or_method) : TimerDestroyedHandler([o = std::move(object), lm = std::forward(lambda_or_method)](auto&&... args) { if (auto s = o.get()) { if constexpr (std::is_member_function_pointer_v) ((*s).*(lm))(args...); else lm(args...); } }) { } template TimerDestroyedHandler::TimerDestroyedHandler(std::shared_ptr&& object, M method) : TimerDestroyedHandler([o = std::move(object), method](auto&&... args) { return ((*o).*(method))(args...); }) { } template TimerDestroyedHandler::TimerDestroyedHandler(std::weak_ptr&& object, LM&& lambda_or_method) : TimerDestroyedHandler([o = std::move(object), lm = std::forward(lambda_or_method)](auto&&... args) { if (auto s = o.lock()) { if constexpr (std::is_member_function_pointer_v) ((*s).*(lm))(args...); else lm(args...); } }) { } inline auto TimerDestroyedHandler::operator()(winrt::Windows::System::Threading::ThreadPoolTimer const& timer) const { check_hresult((*(impl::abi_t**)this)->Invoke(*(void**)(&timer))); } template TimerElapsedHandler::TimerElapsedHandler(L handler) : TimerElapsedHandler(impl::make_delegate(std::forward(handler))) { } template TimerElapsedHandler::TimerElapsedHandler(F* handler) : TimerElapsedHandler([=](auto&&... args) { return handler(args...); }) { } template TimerElapsedHandler::TimerElapsedHandler(O* object, M method) : TimerElapsedHandler([=](auto&&... args) { return ((*object).*(method))(args...); }) { } template TimerElapsedHandler::TimerElapsedHandler(com_ptr&& object, M method) : TimerElapsedHandler([o = std::move(object), method](auto&&... args) { return ((*o).*(method))(args...); }) { } template TimerElapsedHandler::TimerElapsedHandler(weak_ref&& object, LM&& lambda_or_method) : TimerElapsedHandler([o = std::move(object), lm = std::forward(lambda_or_method)](auto&&... args) { if (auto s = o.get()) { if constexpr (std::is_member_function_pointer_v) ((*s).*(lm))(args...); else lm(args...); } }) { } template TimerElapsedHandler::TimerElapsedHandler(std::shared_ptr&& object, M method) : TimerElapsedHandler([o = std::move(object), method](auto&&... args) { return ((*o).*(method))(args...); }) { } template TimerElapsedHandler::TimerElapsedHandler(std::weak_ptr&& object, LM&& lambda_or_method) : TimerElapsedHandler([o = std::move(object), lm = std::forward(lambda_or_method)](auto&&... args) { if (auto s = o.lock()) { if constexpr (std::is_member_function_pointer_v) ((*s).*(lm))(args...); else lm(args...); } }) { } inline auto TimerElapsedHandler::operator()(winrt::Windows::System::Threading::ThreadPoolTimer const& timer) const { check_hresult((*(impl::abi_t**)this)->Invoke(*(void**)(&timer))); } template WorkItemHandler::WorkItemHandler(L handler) : WorkItemHandler(impl::make_delegate(std::forward(handler))) { } template WorkItemHandler::WorkItemHandler(F* handler) : WorkItemHandler([=](auto&&... args) { return handler(args...); }) { } template WorkItemHandler::WorkItemHandler(O* object, M method) : WorkItemHandler([=](auto&&... args) { return ((*object).*(method))(args...); }) { } template WorkItemHandler::WorkItemHandler(com_ptr&& object, M method) : WorkItemHandler([o = std::move(object), method](auto&&... args) { return ((*o).*(method))(args...); }) { } template WorkItemHandler::WorkItemHandler(weak_ref&& object, LM&& lambda_or_method) : WorkItemHandler([o = std::move(object), lm = std::forward(lambda_or_method)](auto&&... args) { if (auto s = o.get()) { if constexpr (std::is_member_function_pointer_v) ((*s).*(lm))(args...); else lm(args...); } }) { } template WorkItemHandler::WorkItemHandler(std::shared_ptr&& object, M method) : WorkItemHandler([o = std::move(object), method](auto&&... args) { return ((*o).*(method))(args...); }) { } template WorkItemHandler::WorkItemHandler(std::weak_ptr&& object, LM&& lambda_or_method) : WorkItemHandler([o = std::move(object), lm = std::forward(lambda_or_method)](auto&&... args) { if (auto s = o.lock()) { if constexpr (std::is_member_function_pointer_v) ((*s).*(lm))(args...); else lm(args...); } }) { } inline auto WorkItemHandler::operator()(winrt::Windows::Foundation::IAsyncAction const& operation) const { check_hresult((*(impl::abi_t**)this)->Invoke(*(void**)(&operation))); } } namespace std { #ifndef WINRT_LEAN_AND_MEAN template<> struct hash : winrt::impl::hash_base {}; template<> struct hash : winrt::impl::hash_base {}; template<> struct hash : winrt::impl::hash_base {}; template<> struct hash : winrt::impl::hash_base {}; template<> struct hash : winrt::impl::hash_base {}; #endif #ifdef __cpp_lib_format #endif } #endif