C++/WinRT
Перейти к файлу
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
.github Update GitHub action LLVM version to 17.0.5 (#1373) 2023-11-25 21:48:38 -08:00
.pipelines Remove old Windows 7 support code (#1348) 2023-08-28 08:35:09 -05:00
cppwinrt Allow delegates to be created with weak reference + lambda (#1372) 2023-11-25 23:18:59 -08:00
docs Clarify contributing guide (#1324) 2023-06-21 15:16:35 -05:00
fast_fwd Several nuget tests failing to build with VS2022. Standardize on directory.build.props to use PlatformToolset v143 (#1125) 2022-03-22 15:14:59 -07:00
mingw-support Add CMake build and a limited subset of tests for llvm-mingw (#1216) 2022-11-09 10:26:19 -06:00
natvis Compliance and test cleanup (#1291) 2023-03-27 18:34:35 -07:00
nuget Increase foldability of various templates (#1338) 2023-08-09 23:40:40 -07:00
prebuild Add Linux native build (#1239) 2022-12-06 11:13:30 -06:00
scratch catch is evidently incompatible with VS 2022 (platform toolset v143) - backing off to unblock (#1127) 2022-03-23 15:35:56 -07:00
strings Allow delegates to be created with weak reference + lambda (#1372) 2023-11-25 23:18:59 -08:00
test Allow delegates to be created with weak reference + lambda (#1372) 2023-11-25 23:18:59 -08:00
vsix Add fields to filter out templates appearing in the add new item dialog for non UAP projects (#1227) 2022-11-11 20:18:34 -06:00
.gitattributes Build (#478) 2020-01-16 09:16:13 -08:00
.gitignore Add GitHub Action build and test workflow for MSVC (#1201) 2022-10-14 17:15:09 -05:00
CMakeLists.txt cmake: Allow using external winmd headers to bypass download (#1256) 2023-01-03 11:33:25 -06:00
Directory.Build.Props Update GitHub action LLVM version to 17.0.5 (#1373) 2023-11-25 21:48:38 -08:00
Directory.Build.Targets catch is evidently incompatible with VS 2022 (platform toolset v143) - backing off to unblock (#1127) 2022-03-23 15:35:56 -07:00
LICENSE clean 2019-10-08 05:14:30 -07:00
README.md Enable faster dev cycle in Visual Studio (#1340) 2023-08-17 15:51:08 -07:00
build_nuget.cmd Increase foldability of various templates (#1338) 2023-08-09 23:40:40 -07:00
build_prior_projection.cmd Hide protected and overridable members from public projections (#1319) 2023-07-11 21:38:42 -05:00
build_projection.cmd Hide protected and overridable members from public projections (#1319) 2023-07-11 21:38:42 -05:00
build_test_all.cmd Remove old Windows 7 support code (#1348) 2023-08-28 08:35:09 -05:00
build_vsix.cmd Add arm64 debug visualizer and update VS SDK package for VSIX (#1190) 2022-09-23 11:52:00 -07:00
compile_tests.cmd port 2019-10-08 08:19:10 -07:00
cppwinrt.sln Remove old Windows 7 support code (#1348) 2023-08-28 08:35:09 -05:00
cross-mingw-toolchain.cmake Support Linux cross-compilation with mingw-w64 (#1238) 2022-11-28 09:06:56 -06:00
prepare_versionless_diffs.cmd Hide protected and overridable members from public projections (#1319) 2023-07-11 21:38:42 -05:00
run_tests.cmd Remove old Windows 7 support code (#1348) 2023-08-28 08:35:09 -05:00

README.md

The C++/WinRT language projection

C++/WinRT is an entirely standard C++ language projection for Windows Runtime (WinRT) APIs, implemented as a header-file-based library, and designed to provide you with first-class access to the modern Windows API. With C++/WinRT, you can author and consume Windows Runtime APIs using any standards-compliant C++17 compiler.

Building C++/WinRT

Don't build C++/WinRT yourself - just download the latest version here: https://aka.ms/cppwinrt/nuget

Working on the compiler

If you really want to build it yourself, the simplest way to do so is to run the build_test_all.cmd script in the root directory. Developers needing to work on the C++/WinRT compiler itself should go through the following steps to arrive at an efficient inner loop:

  • Open a dev command prompt pointing at the root of the repo.
  • Open the cppwinrt.sln solution.
  • Choose a configuration (x64, x86, Release, Debug) and build projects as needed.

If you are working on an ARM64 or ARM specific issue from an x64 or x86 host, you will need to instead:

  • Open the cppwinrt.sln solution
  • Build the x86 version of the "cppwinrt" project first
  • Switch to your preferred configuration and build the test binaries and run them in your test environment

Comparing Outputs

Comparing the output of the prior release and your current changes will help show the impact of any updates. Starting from a dev command prompt at the root of the repo after following the above build instructions:

  • Run build_projection.cmd in the dev command prompt
  • Run build_prior_projection.cmd in the dev command prompt as well
  • Run prepare_versionless_diffs.cmd which removes version stamps on both current and prior projection
  • Use a directory-level differencing tool to compare _build\$(arch)\$(flavor)\winrt and _reference\$(arch)\$(flavor)\winrt