// C++/WinRT v2.0.200609.3 // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #ifndef WINRT_BASE_H #define WINRT_BASE_H #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if __has_include() #define WINRT_IMPL_NUMERICS #include #endif #ifndef __clang__ #include #else namespace std::experimental { template struct coroutine_traits { using promise_type = typename R::promise_type; }; template struct coroutine_handle; template <> struct coroutine_handle { coroutine_handle(decltype(nullptr)) noexcept { } coroutine_handle() noexcept { } static coroutine_handle from_address(void* address) noexcept { coroutine_handle result; result.ptr = address; return result; } coroutine_handle& operator=(decltype(nullptr)) noexcept { ptr = nullptr; return *this; } explicit operator bool() const noexcept { return ptr; } void* address() const noexcept { return ptr; } void operator()() const { resume(); } void resume() const { __builtin_coro_resume(ptr); } void destroy() const { __builtin_coro_destroy(ptr); } bool done() const { return __builtin_coro_done(ptr); } protected: void* ptr{}; }; template struct coroutine_handle : coroutine_handle<> { using coroutine_handle<>::operator=; static coroutine_handle from_address(void* address) noexcept { coroutine_handle result; result.ptr = address; return result; } Promise& promise() const { return *reinterpret_cast(__builtin_coro_promise(ptr, alignof(Promise), false)); } static coroutine_handle from_promise(Promise& promise) { coroutine_handle result; result.ptr = __builtin_coro_promise(&promise, alignof(Promise), true); return result; } }; template bool operator==(coroutine_handle const& left, coroutine_handle const& right) noexcept { return left.address() == right.address(); } template bool operator!=(coroutine_handle const& left, coroutine_handle const& right) noexcept { return !(left == right); } struct suspend_always { bool await_ready() noexcept { return false; } void await_suspend(coroutine_handle<>) noexcept {} void await_resume() noexcept {} }; struct suspend_never { bool await_ready() noexcept { return true; } void await_suspend(coroutine_handle<>) noexcept {} void await_resume() noexcept {} }; } #endif #ifdef _DEBUG #define WINRT_ASSERT _ASSERTE #define WINRT_VERIFY WINRT_ASSERT #define WINRT_VERIFY_(result, expression) WINRT_ASSERT(result == expression) #else #define WINRT_ASSERT(expression) ((void)0) #define WINRT_VERIFY(expression) (void)(expression) #define WINRT_VERIFY_(result, expression) (void)(expression) #endif #define WINRT_IMPL_SHIM(...) (*(abi_t<__VA_ARGS__>**)&static_cast<__VA_ARGS__ const&>(static_cast(*this))) #ifdef __INTELLISENSE__ #define WINRT_IMPL_AUTO(...) __VA_ARGS__ #else #define WINRT_IMPL_AUTO(...) auto #endif // Note: this is a workaround for a false-positive warning produced by the Visual C++ 15.9 compiler. #pragma warning(disable : 5046) // Note: this is a workaround for a false-positive warning produced by the Visual C++ 16.3 compiler. #pragma warning(disable : 4268) #if defined(_RESUMABLE_FUNCTIONS_SUPPORTED) && !defined(__cpp_coroutines) #define __cpp_coroutines #endif #ifndef WINRT_EXPORT #define WINRT_EXPORT #endif #ifdef WINRT_IMPL_NUMERICS #define _WINDOWS_NUMERICS_NAMESPACE_ winrt::Windows::Foundation::Numerics #define _WINDOWS_NUMERICS_BEGIN_NAMESPACE_ WINRT_EXPORT namespace winrt::Windows::Foundation::Numerics #define _WINDOWS_NUMERICS_END_NAMESPACE_ #include #undef _WINDOWS_NUMERICS_NAMESPACE_ #undef _WINDOWS_NUMERICS_BEGIN_NAMESPACE_ #undef _WINDOWS_NUMERICS_END_NAMESPACE_ #endif #ifdef __IUnknown_INTERFACE_DEFINED__ #define WINRT_IMPL_IUNKNOWN_DEFINED #endif namespace winrt::impl { using ptp_io = struct tp_io*; using ptp_timer = struct tp_timer*; using ptp_wait = struct tp_wait*; using ptp_pool = struct tp_pool*; using srwlock = struct srwlock_*; using condition_variable = struct condition_variable_*; using bstr = wchar_t*; using filetime_period = std::ratio_multiply, std::nano>; struct IAgileObject; struct com_callback_args { uint32_t reserved1; uint32_t reserved2; void* data; }; } WINRT_EXPORT namespace winrt { struct event_token; struct hstring; struct clock; struct hresult { int32_t value{}; constexpr hresult() noexcept = default; constexpr hresult(int32_t const value) noexcept : value(value) { } constexpr operator int32_t() const noexcept { return value; } }; struct guid { uint32_t Data1; uint16_t Data2; uint16_t Data3; uint8_t Data4[8]; guid() noexcept = default; constexpr guid(uint32_t const Data1, uint16_t const Data2, uint16_t const Data3, std::array const& Data4) noexcept : Data1(Data1), Data2(Data2), Data3(Data3), Data4{ Data4[0], Data4[1], Data4[2], Data4[3], Data4[4], Data4[5], Data4[6], Data4[7] } { } #ifdef WINRT_IMPL_IUNKNOWN_DEFINED constexpr guid(GUID const& value) noexcept : Data1(value.Data1), Data2(value.Data2), Data3(value.Data3), Data4{ value.Data4[0], value.Data4[1], value.Data4[2], value.Data4[3], value.Data4[4], value.Data4[5], value.Data4[6], value.Data4[7] } { } operator GUID const&() const noexcept { return reinterpret_cast(*this); } #endif }; inline bool operator==(guid const& left, guid const& right) noexcept { return !memcmp(&left, &right, sizeof(left)); } inline bool operator!=(guid const& left, guid const& right) noexcept { return !(left == right); } inline bool operator<(guid const& left, guid const& right) noexcept { return memcmp(&left, &right, sizeof(left)) < 0; } } WINRT_EXPORT namespace winrt::Windows::Foundation { enum class TrustLevel : int32_t { BaseTrust, PartialTrust, FullTrust }; struct IUnknown; struct IInspectable; struct IActivationFactory; using TimeSpan = std::chrono::duration; using DateTime = std::chrono::time_point; } namespace winrt::impl { #ifdef WINRT_IMPL_IUNKNOWN_DEFINED using hresult_type = long; using count_type = unsigned long; using guid_type = GUID; #else using hresult_type = int32_t; using count_type = uint32_t; using guid_type = guid; #endif #ifdef __IInspectable_INTERFACE_DEFINED__ using hstring_type = HSTRING; using trust_level_type = ::TrustLevel; #else using hstring_type = void*; using trust_level_type = Windows::Foundation::TrustLevel; #endif constexpr hresult error_ok{ 0 }; // S_OK constexpr hresult error_fail{ static_cast(0x80004005) }; // E_FAIL constexpr hresult error_access_denied{ static_cast(0x80070005) }; // E_ACCESSDENIED constexpr hresult error_wrong_thread{ static_cast(0x8001010E) }; // RPC_E_WRONG_THREAD constexpr hresult error_not_implemented{ static_cast(0x80004001) }; // E_NOTIMPL constexpr hresult error_invalid_argument{ static_cast(0x80070057) }; // E_INVALIDARG constexpr hresult error_out_of_bounds{ static_cast(0x8000000B) }; // E_BOUNDS constexpr hresult error_no_interface{ static_cast(0x80004002) }; // E_NOINTERFACE constexpr hresult error_class_not_available{ static_cast(0x80040111) }; // CLASS_E_CLASSNOTAVAILABLE constexpr hresult error_class_not_registered{ static_cast(0x80040154) }; // REGDB_E_CLASSNOTREG constexpr hresult error_changed_state{ static_cast(0x8000000C) }; // E_CHANGED_STATE constexpr hresult error_illegal_method_call{ static_cast(0x8000000E) }; // E_ILLEGAL_METHOD_CALL constexpr hresult error_illegal_state_change{ static_cast(0x8000000D) }; // E_ILLEGAL_STATE_CHANGE constexpr hresult error_illegal_delegate_assignment{ static_cast(0x80000018) }; // E_ILLEGAL_DELEGATE_ASSIGNMENT constexpr hresult error_canceled{ static_cast(0x800704C7) }; // HRESULT_FROM_WIN32(ERROR_CANCELLED) constexpr hresult error_bad_alloc{ static_cast(0x8007000E) }; // E_OUTOFMEMORY constexpr hresult error_not_initialized{ static_cast(0x800401F0) }; // CO_E_NOTINITIALIZED } __declspec(selectany) int32_t(__stdcall* winrt_to_hresult_handler)(void* address) noexcept {}; __declspec(selectany) void(__stdcall* winrt_suspend_handler)(void const* token) noexcept {}; __declspec(selectany) void(__stdcall* winrt_resume_handler)(void const* token) noexcept {}; __declspec(selectany) int32_t(__stdcall* winrt_activation_handler)(void* classId, winrt::guid const& iid, void** factory) noexcept {}; extern "C" { void* __stdcall WINRT_IMPL_LoadLibraryW(wchar_t const* name) noexcept; int32_t __stdcall WINRT_IMPL_FreeLibrary(void* library) noexcept; void* __stdcall WINRT_IMPL_GetProcAddress(void* library, char const* name) noexcept; int32_t __stdcall WINRT_IMPL_SetErrorInfo(uint32_t reserved, void* info) noexcept; int32_t __stdcall WINRT_IMPL_GetErrorInfo(uint32_t reserved, void** info) noexcept; int32_t __stdcall WINRT_IMPL_CoInitializeEx(void*, uint32_t type) noexcept; void __stdcall WINRT_IMPL_CoUninitialize() noexcept; int32_t __stdcall WINRT_IMPL_CoCreateFreeThreadedMarshaler(void* outer, void** marshaler) noexcept; int32_t __stdcall WINRT_IMPL_CoCreateInstance(winrt::guid const& clsid, void* outer, uint32_t context, winrt::guid const& iid, void** object) noexcept; int32_t __stdcall WINRT_IMPL_CoGetCallContext(winrt::guid const& iid, void** object) noexcept; int32_t __stdcall WINRT_IMPL_CoGetObjectContext(winrt::guid const& iid, void** object) noexcept; int32_t __stdcall WINRT_IMPL_CoGetApartmentType(int32_t* type, int32_t* qualifier) noexcept; void* __stdcall WINRT_IMPL_CoTaskMemAlloc(std::size_t size) noexcept; void __stdcall WINRT_IMPL_CoTaskMemFree(void* ptr) noexcept; winrt::impl::bstr __stdcall WINRT_IMPL_SysAllocString(wchar_t const* value) noexcept; void __stdcall WINRT_IMPL_SysFreeString(winrt::impl::bstr string) noexcept; uint32_t __stdcall WINRT_IMPL_SysStringLen(winrt::impl::bstr string) noexcept; int32_t __stdcall WINRT_IMPL_IIDFromString(wchar_t const* string, winrt::guid* iid) noexcept; int32_t __stdcall WINRT_IMPL_MultiByteToWideChar(uint32_t codepage, uint32_t flags, char const* in_string, int32_t in_size, wchar_t* out_string, int32_t out_size) noexcept; int32_t __stdcall WINRT_IMPL_WideCharToMultiByte(uint32_t codepage, uint32_t flags, wchar_t const* int_string, int32_t in_size, char* out_string, int32_t out_size, char const* default_char, int32_t* default_used) noexcept; void* __stdcall WINRT_IMPL_HeapAlloc(void* heap, uint32_t flags, size_t bytes) noexcept; int32_t __stdcall WINRT_IMPL_HeapFree(void* heap, uint32_t flags, void* value) noexcept; void* __stdcall WINRT_IMPL_GetProcessHeap() noexcept; uint32_t __stdcall WINRT_IMPL_FormatMessageW(uint32_t flags, void const* source, uint32_t code, uint32_t language, wchar_t* buffer, uint32_t size, va_list* arguments) noexcept; uint32_t __stdcall WINRT_IMPL_GetLastError() noexcept; void __stdcall WINRT_IMPL_GetSystemTimePreciseAsFileTime(void* result) noexcept; uintptr_t __stdcall WINRT_IMPL_VirtualQuery(void* address, void* buffer, uintptr_t length) noexcept; void* __stdcall WINRT_IMPL_EncodePointer(void* ptr) noexcept; int32_t __stdcall WINRT_IMPL_OpenProcessToken(void* process, uint32_t access, void** token) noexcept; void* __stdcall WINRT_IMPL_GetCurrentProcess() noexcept; int32_t __stdcall WINRT_IMPL_DuplicateToken(void* existing, uint32_t level, void** duplicate) noexcept; int32_t __stdcall WINRT_IMPL_OpenThreadToken(void* thread, uint32_t access, int32_t self, void** token) noexcept; void* __stdcall WINRT_IMPL_GetCurrentThread() noexcept; int32_t __stdcall WINRT_IMPL_SetThreadToken(void** thread, void* token) noexcept; void __stdcall WINRT_IMPL_AcquireSRWLockExclusive(winrt::impl::srwlock* lock) noexcept; void __stdcall WINRT_IMPL_AcquireSRWLockShared(winrt::impl::srwlock* lock) noexcept; uint8_t __stdcall WINRT_IMPL_TryAcquireSRWLockExclusive(winrt::impl::srwlock* lock) noexcept; uint8_t __stdcall WINRT_IMPL_TryAcquireSRWLockShared(winrt::impl::srwlock* lock) noexcept; void __stdcall WINRT_IMPL_ReleaseSRWLockExclusive(winrt::impl::srwlock* lock) noexcept; void __stdcall WINRT_IMPL_ReleaseSRWLockShared(winrt::impl::srwlock* lock) noexcept; int32_t __stdcall WINRT_IMPL_SleepConditionVariableSRW(winrt::impl::condition_variable* cv, winrt::impl::srwlock* lock, uint32_t milliseconds, uint32_t flags) noexcept; void __stdcall WINRT_IMPL_WakeConditionVariable(winrt::impl::condition_variable* cv) noexcept; void __stdcall WINRT_IMPL_WakeAllConditionVariable(winrt::impl::condition_variable* cv) noexcept; void* __stdcall WINRT_IMPL_InterlockedPushEntrySList(void* head, void* entry) noexcept; void* __stdcall WINRT_IMPL_InterlockedFlushSList(void* head) noexcept; void* __stdcall WINRT_IMPL_CreateEventW(void*, int32_t, int32_t, void*) noexcept; int32_t __stdcall WINRT_IMPL_SetEvent(void*) noexcept; int32_t __stdcall WINRT_IMPL_CloseHandle(void* hObject) noexcept; uint32_t __stdcall WINRT_IMPL_WaitForSingleObject(void* handle, uint32_t milliseconds) noexcept; int32_t __stdcall WINRT_IMPL_TrySubmitThreadpoolCallback(void(__stdcall *callback)(void*, void* context), void* context, void*) noexcept; winrt::impl::ptp_timer __stdcall WINRT_IMPL_CreateThreadpoolTimer(void(__stdcall *callback)(void*, void* context, void*), void* context, void*) noexcept; void __stdcall WINRT_IMPL_SetThreadpoolTimer(winrt::impl::ptp_timer timer, void* time, uint32_t period, uint32_t window) noexcept; void __stdcall WINRT_IMPL_CloseThreadpoolTimer(winrt::impl::ptp_timer timer) noexcept; winrt::impl::ptp_wait __stdcall WINRT_IMPL_CreateThreadpoolWait(void(__stdcall *callback)(void*, void* context, void*, uint32_t result), void* context, void*) noexcept; void __stdcall WINRT_IMPL_SetThreadpoolWait(winrt::impl::ptp_wait wait, void* handle, void* timeout) noexcept; void __stdcall WINRT_IMPL_CloseThreadpoolWait(winrt::impl::ptp_wait wait) noexcept; winrt::impl::ptp_io __stdcall WINRT_IMPL_CreateThreadpoolIo(void* object, void(__stdcall *callback)(void*, void* context, void* overlapped, uint32_t result, std::size_t bytes, void*) noexcept, void* context, void*) noexcept; void __stdcall WINRT_IMPL_StartThreadpoolIo(winrt::impl::ptp_io io) noexcept; void __stdcall WINRT_IMPL_CancelThreadpoolIo(winrt::impl::ptp_io io) noexcept; void __stdcall WINRT_IMPL_CloseThreadpoolIo(winrt::impl::ptp_io io) noexcept; winrt::impl::ptp_pool __stdcall WINRT_IMPL_CreateThreadpool(void* reserved) noexcept; void __stdcall WINRT_IMPL_SetThreadpoolThreadMaximum(winrt::impl::ptp_pool pool, uint32_t value) noexcept; int32_t __stdcall WINRT_IMPL_SetThreadpoolThreadMinimum(winrt::impl::ptp_pool pool, uint32_t value) noexcept; void __stdcall WINRT_IMPL_CloseThreadpool(winrt::impl::ptp_pool pool) noexcept; int32_t __stdcall WINRT_CanUnloadNow() noexcept; int32_t __stdcall WINRT_GetActivationFactory(void* classId, void** factory) noexcept; } #ifdef _M_HYBRID #define WINRT_IMPL_LINK(function, count) __pragma(comment(linker, "/alternatename:#WINRT_IMPL_" #function "@" #count "=#" #function "@" #count)) #elif _M_ARM64EC #define WINRT_IMPL_LINK(function, count) __pragma(comment(linker, "/alternatename:#WINRT_IMPL_" #function "=#" #function)) #elif _M_IX86 #define WINRT_IMPL_LINK(function, count) __pragma(comment(linker, "/alternatename:_WINRT_IMPL_" #function "@" #count "=_" #function "@" #count)) #else #define WINRT_IMPL_LINK(function, count) __pragma(comment(linker, "/alternatename:WINRT_IMPL_" #function "=" #function)) #endif WINRT_IMPL_LINK(LoadLibraryW, 4) WINRT_IMPL_LINK(FreeLibrary, 4) WINRT_IMPL_LINK(GetProcAddress, 8) WINRT_IMPL_LINK(SetErrorInfo, 8) WINRT_IMPL_LINK(GetErrorInfo, 8) WINRT_IMPL_LINK(CoInitializeEx, 8) WINRT_IMPL_LINK(CoUninitialize, 0) WINRT_IMPL_LINK(CoCreateFreeThreadedMarshaler, 8) WINRT_IMPL_LINK(CoCreateInstance, 20) WINRT_IMPL_LINK(CoGetCallContext, 8) WINRT_IMPL_LINK(CoGetObjectContext, 8) WINRT_IMPL_LINK(CoGetApartmentType, 8) WINRT_IMPL_LINK(CoTaskMemAlloc, 4) WINRT_IMPL_LINK(CoTaskMemFree, 4) WINRT_IMPL_LINK(SysAllocString, 4) WINRT_IMPL_LINK(SysFreeString, 4) WINRT_IMPL_LINK(SysStringLen, 4) WINRT_IMPL_LINK(IIDFromString, 8) WINRT_IMPL_LINK(MultiByteToWideChar, 24) WINRT_IMPL_LINK(WideCharToMultiByte, 32) WINRT_IMPL_LINK(HeapAlloc, 12) WINRT_IMPL_LINK(HeapFree, 12) WINRT_IMPL_LINK(GetProcessHeap, 0) WINRT_IMPL_LINK(FormatMessageW, 28) WINRT_IMPL_LINK(GetLastError, 0) WINRT_IMPL_LINK(GetSystemTimePreciseAsFileTime, 4) WINRT_IMPL_LINK(VirtualQuery, 12) WINRT_IMPL_LINK(EncodePointer, 4) WINRT_IMPL_LINK(OpenProcessToken, 12) WINRT_IMPL_LINK(GetCurrentProcess, 0) WINRT_IMPL_LINK(DuplicateToken, 12) WINRT_IMPL_LINK(OpenThreadToken, 16) WINRT_IMPL_LINK(GetCurrentThread, 0) WINRT_IMPL_LINK(SetThreadToken, 8) WINRT_IMPL_LINK(AcquireSRWLockExclusive, 4) WINRT_IMPL_LINK(AcquireSRWLockShared, 4) WINRT_IMPL_LINK(TryAcquireSRWLockExclusive, 4) WINRT_IMPL_LINK(TryAcquireSRWLockShared, 4) WINRT_IMPL_LINK(ReleaseSRWLockExclusive, 4) WINRT_IMPL_LINK(ReleaseSRWLockShared, 4) WINRT_IMPL_LINK(SleepConditionVariableSRW, 16) WINRT_IMPL_LINK(WakeConditionVariable, 4) WINRT_IMPL_LINK(WakeAllConditionVariable, 4) WINRT_IMPL_LINK(InterlockedPushEntrySList, 8) WINRT_IMPL_LINK(InterlockedFlushSList, 4) WINRT_IMPL_LINK(CreateEventW, 16) WINRT_IMPL_LINK(SetEvent, 4) WINRT_IMPL_LINK(CloseHandle, 4) WINRT_IMPL_LINK(WaitForSingleObject, 8) WINRT_IMPL_LINK(TrySubmitThreadpoolCallback, 12) WINRT_IMPL_LINK(CreateThreadpoolTimer, 12) WINRT_IMPL_LINK(SetThreadpoolTimer, 16) WINRT_IMPL_LINK(CloseThreadpoolTimer, 4) WINRT_IMPL_LINK(CreateThreadpoolWait, 12) WINRT_IMPL_LINK(SetThreadpoolWait, 12) WINRT_IMPL_LINK(CloseThreadpoolWait, 4) WINRT_IMPL_LINK(CreateThreadpoolIo, 16) WINRT_IMPL_LINK(StartThreadpoolIo, 4) WINRT_IMPL_LINK(CancelThreadpoolIo, 4) WINRT_IMPL_LINK(CloseThreadpoolIo, 4) WINRT_IMPL_LINK(CreateThreadpool, 4) WINRT_IMPL_LINK(SetThreadpoolThreadMaximum, 8) WINRT_IMPL_LINK(SetThreadpoolThreadMinimum, 8) WINRT_IMPL_LINK(CloseThreadpool, 4) #undef WINRT_IMPL_LINK WINRT_EXPORT namespace winrt { void check_hresult(hresult const result); hresult to_hresult() noexcept; template D* get_self(I const& from) noexcept; struct take_ownership_from_abi_t {}; inline constexpr take_ownership_from_abi_t take_ownership_from_abi{}; template struct com_ptr; namespace param { template struct iterable; template struct async_iterable; template struct map_view; template struct async_map_view; template struct map; template struct vector_view; template struct async_vector_view; template struct vector; } } namespace winrt::impl { using namespace std::literals; template struct reference_traits; template struct identity { using type = T; }; template struct abi { using type = T; }; template struct abi>> { using type = std::underlying_type_t; }; template using abi_t = typename abi::type; template struct consume; template using consume_t = typename consume::template type; template struct delegate; template > struct default_interface { using type = T; }; struct basic_category; struct interface_category; struct delegate_category; struct enum_category; struct class_category; template struct category { using type = void; }; template using category_t = typename category::type; template inline constexpr bool has_category_v = !std::is_same_v, void>; template struct generic_category; template struct struct_category; template struct category_signature; template struct signature { static constexpr auto data{ category_signature, T>::data }; }; template #ifdef WINRT_IMPL_IUNKNOWN_DEFINED #ifdef __clang__ inline const guid guid_v{ __uuidof(T) }; #else inline constexpr guid guid_v{ __uuidof(T) }; #endif #else inline constexpr guid guid_v{}; #endif template constexpr auto to_underlying_type(T const value) noexcept { return static_cast>(value); } template > struct is_implements : std::false_type {}; template struct is_implements> : std::true_type {}; template inline constexpr bool is_implements_v = is_implements::value; template struct require_one : consume_t { operator I() const noexcept { return static_cast(this)->template try_as(); } }; template struct __declspec(empty_bases) require : require_one... {}; template struct base_one { operator I() const noexcept { return static_cast(this)->template try_as(); } }; template struct __declspec(empty_bases) base : base_one... {}; template T empty_value() noexcept { if constexpr (std::is_base_of_v) { return nullptr; } else { return {}; } } template struct arg { using in = abi_t; }; template struct arg>> { using in = void*; }; template using arg_in = typename arg::in; template using arg_out = arg_in*; template struct produce_base; template struct produce; template struct produce : produce_base { }; template struct wrapped_type { using type = T; }; template struct wrapped_type> { using type = T; }; template using wrapped_type_t = typename wrapped_type::type; template