cppwinrt/strings
Raymond Chen fc587f31f9
Allow delegates to be created with weak reference + lambda (#1372)
We have found that a very common pattern for event handlers is
to capture a weak reference into a lambda, and in the event handler,
try to upgrade the weak reference to a strong one, and if so, do some work:

```cpp
widget.Closed([weak = get_weak(), data](auto&& sender, auto&& args)
{
    if (auto strongThis = weak.get())
    {
        strongThis->do_all_the_things(data);
    }
});
```

This commit extends the existing delegate constructors to permit a
`winrt::weak_ref` + lambda (or `std::weak_ptr` + lambda), which
simplifies the above to

```cpp
widget.Closed({ get_weak(), [this, data](auto&& sender, auto&& args)
{
    do_all_the_things(data);
} });
```

## Implementation notes

A lambda and pointer to member function are hard to distinguish
in a template parameter list. In theory, we could use SFINAE or
partial specialization, but a simpler solution is to distinguish
the two inside the body of the constructor, via
`std::is_member_function_pointer_v`.

The `com_ptr` and `shared_ptr` variants of the test were
unified, since I found myself editing two nearly identical tests.

Fixes #1371

Co-authored-by: Jon Wiswall <jonwis@microsoft.com>
2023-11-25 23:18:59 -08:00
..
base_abi.h Improve compatibility with mingw-w64 in generated headers (#1200) 2022-10-13 09:40:49 -05:00
base_activation.h Remove old Windows 7 support code (#1348) 2023-08-28 08:35:09 -05:00
base_agile_ref.h Remove old Windows 7 support code (#1348) 2023-08-28 08:35:09 -05:00
base_array.h Support for std::span for winrt::array_view and winrt::com_array (#1343) 2023-08-28 10:24:04 -07:00
base_chrono.h Make compatible with GCC (#1245) 2022-12-13 11:32:15 -06:00
base_collections.h C++20 ranges support (#900) 2021-03-29 10:47:25 -07:00
base_collections_base.h Make vector<bool> work again (#703) 2020-07-29 12:14:59 -07:00
base_collections_input_iterable.h port 2019-10-08 08:19:10 -07:00
base_collections_input_map.h Add multi-threaded collections (#692) 2020-07-28 12:53:45 -07:00
base_collections_input_map_view.h port 2019-10-08 08:19:10 -07:00
base_collections_input_vector.h Fix Clang 15 style warnings (#1196) 2022-10-10 10:10:21 -05:00
base_collections_input_vector_view.h port 2019-10-08 08:19:10 -07:00
base_collections_map.h Add multi-threaded collections (#692) 2020-07-28 12:53:45 -07:00
base_collections_vector.h Avoid exception in observable IndexOf implementation (#1017) 2021-09-13 10:00:52 -07:00
base_com_ptr.h Add `capture` support for unconventional result types (#1301) 2023-04-29 14:53:13 -05:00
base_composable.h Improve compatibility with mingw-w64 in generated headers (#1200) 2022-10-13 09:40:49 -05:00
base_coroutine_foundation.h Allow resume_agile to be stored in a variable (#1358) 2023-09-14 16:19:23 -07:00
base_coroutine_system.h Define co_await operators for C++20 coroutines as well (#764) (#765) 2020-10-15 11:12:24 -07:00
base_coroutine_system_winui.h Define co_await operators for C++20 coroutines as well (#764) (#765) 2020-10-15 11:12:24 -07:00
base_coroutine_threadpool.h Add `resume_agile` to allow coroutine to resume in any apartment (#1356) 2023-09-12 13:25:16 -05:00
base_coroutine_ui_core.h Define co_await operators for C++20 coroutines as well (#764) (#765) 2020-10-15 11:12:24 -07:00
base_deferral.h Define co_await operators for C++20 coroutines as well (#764) (#765) 2020-10-15 11:12:24 -07:00
base_delegate.h Allow delegates to be created with weak reference + lambda (#1372) 2023-11-25 23:18:59 -08:00
base_error.h Remove old Windows 7 support code (#1348) 2023-08-28 08:35:09 -05:00
base_events.h Remove old Windows 7 support code (#1348) 2023-08-28 08:35:09 -05:00
base_extern.h Remove old Windows 7 support code (#1348) 2023-08-28 08:35:09 -05:00
base_fast_forward.h Improve compatibility with mingw-w64 in generated headers (#1200) 2022-10-13 09:40:49 -05:00
base_foundation.h Revert utf-8 encoding of runtime class names (#621) 2020-05-13 14:18:42 -07:00
base_handle.h Harden put functions against misuse (#986) 2021-07-21 15:47:20 -07:00
base_identity.h Cleaning up some warnings for Clang and GCC (#1255) 2023-01-09 08:57:33 -06:00
base_implements.h Improve GCC compatibility (#1352) 2023-09-02 12:14:52 -05:00
base_includes.h Support for std::span for winrt::array_view and winrt::com_array (#1343) 2023-08-28 10:24:04 -07:00
base_iterator.h C++20 ranges support (#900) 2021-03-29 10:47:25 -07:00
base_lock.h Add multi-threaded collections (#692) 2020-07-28 12:53:45 -07:00
base_macros.h Add a mechanism to suppress `std::source_location` (#1260) 2023-01-09 16:52:54 -06:00
base_marshaler.h Move externs into "impl" category (#475) 2020-01-16 14:17:18 -08:00
base_meta.h Add `resume_agile` to allow coroutine to resume in any apartment (#1356) 2023-09-12 13:25:16 -05:00
base_natvis.h Avoid spurious WINRT_NATVIS redefinition warning (#1044) 2021-10-21 09:52:05 -07:00
base_reference_produce.h Improve diagnostics when trying to use classic COM without <unknwn.h> (#1022) 2021-09-20 10:20:06 -07:00
base_reference_produce_1.h IReference operators in level-1 header should be forward declarations (#965) 2021-06-24 12:42:43 -07:00
base_security.h Revert "Prevent inadvertent assignment to temporary object" (#976) 2021-07-07 07:14:04 -07:00
base_std_hash.h Support for Windows 7 (#469) 2020-01-14 11:13:12 -08:00
base_string.h Make headers partially usable with LLVM/libc++ 13 (#1257) 2023-01-03 11:33:53 -06:00
base_string_input.h Cleaning up some warnings for Clang and GCC (#1255) 2023-01-09 08:57:33 -06:00
base_string_operators.h Partial GCC compatibility improvements (#1234) 2022-11-21 11:59:13 -06:00
base_stringable_format.h Make the formatter for IStringable const (#1270) 2023-02-05 15:21:16 -06:00
base_stringable_format_1.h Make the formatter for IStringable const (#1270) 2023-02-05 15:21:16 -06:00
base_stringable_streams.h C++ Streams support for hstring and IStringable (#1221) 2022-11-01 09:53:33 -05:00
base_stringable_to_hstring.h Add `to_hstring` overload for `IStringable` (#1271) 2023-02-20 09:38:23 -06:00
base_types.h Add guid brace parsing (#1109) 2022-02-24 09:38:15 -08:00
base_version.h Cleaning up some warnings for Clang and GCC (#1255) 2023-01-09 08:57:33 -06:00
base_version_odr.h Improve detection of mismatched header versions (#683) 2020-07-09 07:37:03 -07:00
base_weak_ref.h Fix null pointer dereference in weak_ref::get() (#1232) 2022-11-21 11:57:55 -06:00
base_windows.h Improve diagnostics when trying to use classic COM without <unknwn.h> (#1022) 2021-09-20 10:20:06 -07:00
base_xaml_component_connector.h Projects with a nested Windows namespace (e.g., Microsoft::Windows) fail to compile (#1223) 2022-11-04 10:02:01 -07:00
base_xaml_component_connector_winui.h Include WinUI too (sigh) (#1224) 2022-11-04 11:14:51 -07:00
base_xaml_typename.h C++20 ranges support (#900) 2021-03-29 10:47:25 -07:00