ITCamp 2013 - Raffaele Rialdi - Windows Runtime (WinRT) deep dive
Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime...
Transcript of Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime...
![Page 1: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/1.jpg)
Embracing Standard C++ for the Windows RuntimeKenny Kerr
Microsoft Windows
@KennyKerr
James McNellis
Microsoft Visual C++
@JamesMcNellis
![Page 2: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/2.jpg)
“Hello, World!”
![Page 3: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/3.jpg)
sealed partial class App : Application
{
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
TextBlock block = new TextBlock();
block.FontFamily = new FontFamily("Segoe UI Semibold");
block.FontSize = 140.0;
block.Foreground = new SolidColorBrush(Colors.HotPink);
block.VerticalAlignment = VerticalAlignment.Center;
block.TextAlignment = TextAlignment.Center;
block.Text = "Hello CppCon!";
Window window = Window.Current;
window.Content = block;
window.Activate();
}
}
![Page 4: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/4.jpg)
sealed partial class App : Application
{
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
TextBlock block = new TextBlock();
block.FontFamily = new FontFamily("Segoe UI Semibold");
block.FontSize = 140.0;
block.Foreground = new SolidColorBrush(Colors.HotPink);
block.VerticalAlignment = VerticalAlignment.Center;
block.TextAlignment = TextAlignment.Center;
block.Text = "Hello CppCon!";
Window window = Window.Current;
window.Content = block;
window.Activate();
}
}
![Page 5: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/5.jpg)
C++ “Hello, World!”
![Page 6: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/6.jpg)
// The Windows Runtime is a set of C and COM APIs, so we need a little helper
void check_hresult(HRESULT const hr)
{
if (hr != S_OK)
{
std::terminate();
}
}
![Page 7: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/7.jpg)
// sealed partial class App : Application
class App : public RuntimeClass<IApplicationOverrides, ComposableBase<IApplicationFactory>>
{
public:
App()
{
ComPtr<IApplicationFactory> factory;
check_hresult(GetActivationFactory(
HStringReference(RuntimeClass_Windows_UI_Xaml_Application).Get(),
factory.GetAddressOf()));
ComPtr<IInspectable> inner_inspectable;
ComPtr<IApplication> inner_instance;
check_hresult(factory->CreateInstance(
this,
inner_inspectable.GetAddressOf(),
inner_instance.GetAddressOf()));
check_hresult(SetComposableBasePointers(inner_inspectable.Get(), factory.Get()));
}
// ...
![Page 8: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/8.jpg)
// IApplicationOverrides has these virtual functions:
virtual HRESULT __stdcall OnActivated(IActivatedEventArgs*);
virtual HRESULT __stdcall OnFileActivated(IFileActivatedEventArgs*);
virtual HRESULT __stdcall OnSearchActivated(ISearchActivatedEventArgs*);
virtual HRESULT __stdcall OnShareTargetActivated(IShareTargetActivatedEventArgs*);
virtual HRESULT __stdcall OnFileOpenPickerActivated(IFileOpenPickerActivatedEventArgs*);
virtual HRESULT __stdcall OnFileSavePickerActivated(IFileSavePickerActivatedEventArgs*);
virtual HRESULT __stdcall OnCachedFileUpdaterActivated(ICachedFileUpdaterActivatedEventArgs*);
virtual HRESULT __stdcall OnWindowCreated(IWindowCreatedEventArgs*);
virtual HRESULT __stdcall OnLaunched(ILaunchActivatedEventArgs*);
// We have to define them all, but we can just return success. E.g.,
virtual HRESULT __stdcall OnWindowCreated(IWindowCreatedEventArgs*)
{
return S_OK;
}
![Page 9: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/9.jpg)
// protected override void OnLaunched(LaunchActivatedEventArgs e)
virtual HRESULT __stdcall OnLaunched(ILaunchActivatedEventArgs*)
{
// TextBlock block = new TextBlock();
ComPtr<IInspectable> block_inspectable;
check_hresult(RoActivateInstance(
HStringReference(RuntimeClass_Windows_UI_Xaml_Controls_TextBlock).Get(),
block_inspectable.GetAddressOf()));
ComPtr<ITextBlock> block;
check_hresult(block_inspectable.As(&block));
// ...
![Page 10: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/10.jpg)
// protected override void OnLaunched(LaunchActivatedEventArgs e)
virtual HRESULT __stdcall OnLaunched(ILaunchActivatedEventArgs*)
{
// ...
// block.FontFamily = new FontFamily("Segoe UI Semibold");
ComPtr<IFontFamilyFactory> font_family_factory;
check_hresult(GetActivationFactory(
HStringReference(RuntimeClass_Windows_UI_Xaml_Media_FontFamily).Get(),
font_family_factory.GetAddressOf()));
ComPtr<IFontFamily> font_family;
check_hresult(font_family_factory->CreateInstanceWithName(
HStringReference(L"Segoe UI Semibold").Get(),
nullptr,
nullptr,
font_family.GetAddressOf()));
check_hresult(block->put_FontFamily(font_family.Get()));
// ...
![Page 11: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/11.jpg)
// protected override void OnLaunched(LaunchActivatedEventArgs e)
virtual HRESULT __stdcall OnLaunched(ILaunchActivatedEventArgs*)
{
// ...
// block.FontSize = 140.0;
check_hresult(block->put_FontSize(140.00));
// ...
![Page 12: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/12.jpg)
// protected override void OnLaunched(LaunchActivatedEventArgs e)
virtual HRESULT __stdcall OnLaunched(ILaunchActivatedEventArgs*)
{
// ...
// block.Foreground = new SolidColorBrush(Colors.HotPink); (Part 1)
ComPtr<IColorsStatics> colors_statics;
check_hresult(GetActivationFactory(
HStringReference(RuntimeClass_Windows_UI_Colors).Get(),
colors_statics.GetAddressOf()));
Color hot_pink;
check_hresult(colors_statics->get_HotPink(&hot_pink));
// ...
![Page 13: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/13.jpg)
// protected override void OnLaunched(LaunchActivatedEventArgs e)
virtual HRESULT __stdcall OnLaunched(ILaunchActivatedEventArgs*)
{
// ...
// block.Foreground = new SolidColorBrush(Colors.HotPink); (Part 2)
ComPtr<ISolidColorBrushFactory> brush_factory;
check_hresult(GetActivationFactory(
HStringReference(RuntimeClass_Windows_UI_Xaml_Media_SolidColorBrush).Get(),
brush_factory.GetAddressOf()));
ComPtr<ISolidColorBrush> hot_pink_brush;
check_hresult(brush_factory->CreateInstanceWithColor(
hot_pink,
hot_pink_brush.GetAddressOf()));
ComPtr<IBrush> foreground_brush;
check_hresult(hot_pink_brush.As(&foreground_brush));
check_hresult(block->put_Foreground(foreground_brush.Get()));
// ...
![Page 14: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/14.jpg)
// protected override void OnLaunched(LaunchActivatedEventArgs e)
virtual HRESULT __stdcall OnLaunched(ILaunchActivatedEventArgs*)
{
// ...
// block.VerticalAlignment = VerticalAlignment.Center;
ComPtr<IFrameworkElement> block_framework_element;
check_hresult(block.As(&block_framework_element));
check_hresult(block_framework_element->put_VerticalAlignment(VerticalAlignment_Center));
// block.TextAlignment = TextAlignment.Center;
check_hresult(block->put_TextAlignment(TextAlignment_Center));
// block.Text = "Hello CppCon!";
check_hresult(block->put_Text(HStringReference(L"Hello CppCon!").Get()));
// ...
![Page 15: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/15.jpg)
// protected override void OnLaunched(LaunchActivatedEventArgs e)
virtual HRESULT __stdcall OnLaunched(ILaunchActivatedEventArgs*)
{
// ...
// Window window = Window.Current;
ComPtr<IWindowStatics> window_statics;
check_hresult(GetActivationFactory(
HStringReference(RuntimeClass_Windows_UI_Xaml_Window).Get(),
window_statics.GetAddressOf()));
ComPtr<IWindow> window;
check_hresult(window_statics->get_Current(window.GetAddressOf()));
// ...
![Page 16: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/16.jpg)
// protected override void OnLaunched(LaunchActivatedEventArgs e)
virtual HRESULT __stdcall OnLaunched(ILaunchActivatedEventArgs*)
{
// ...
// window.Content = block;
ComPtr<IUIElement> block_ui_element;
check_hresult(block.As(&block_ui_element));
check_hresult(window->put_Content(block_ui_element.Get()));
// window.Activate();
check_hresult(window->Activate());
return S_OK;
}
![Page 17: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/17.jpg)
class App : public RuntimeClass<IApplicationOverrides, ComposableBase<IApplicationFactory>>{public:
App(){
ComPtr<IApplicationFactory> factory;check_hr(GetActivationFactory(
HStringReference(RuntimeClass_Windows_UI_Xaml_Application).Get(),factory.GetAddressOf()));
ComPtr<IInspectable> inner_inspectable;ComPtr<IApplication> inner_instance;check_hr(factory->CreateInstance(
this,inner_inspectable.GetAddressOf(),inner_instance.GetAddressOf()));
check_hr(SetComposableBasePointers(inner_inspectable.Get(), factory.Get()));}
virtual HRESULT __stdcall OnActivated(IActivatedEventArgs*){
return S_OK;}
virtual HRESULT __stdcall OnFileActivated(IFileActivatedEventArgs*){
return S_OK;}
virtual HRESULT __stdcall OnSearchActivated(ISearchActivatedEventArgs*){
return S_OK;}
virtual HRESULT __stdcall OnShareTargetActivated(IShareTargetActivatedEventArgs*){
return S_OK;}
virtual HRESULT __stdcall OnFileOpenPickerActivated(IFileOpenPickerActivatedEventArgs*){
return S_OK;}
virtual HRESULT __stdcall OnFileSavePickerActivated(IFileSavePickerActivatedEventArgs*){
return S_OK;}
virtual HRESULT __stdcall OnCachedFileUpdaterActivated(ICachedFileUpdaterActivatedEventArgs*){
return S_OK;}
virtual HRESULT __stdcall OnWindowCreated(IWindowCreatedEventArgs*){
return S_OK;}
virtual HRESULT __stdcall OnLaunched(ILaunchActivatedEventArgs*){
ComPtr<IInspectable> block_inspectable;check_hresult(RoActivateInstance(
HStringReference(RuntimeClass_Windows_UI_Xaml_Controls_TextBlock).Get(),
block_inspectable.GetAddressOf()));
ComPtr<ITextBlock> block;check_hresult(block_inspectable.As(&block));
ComPtr<IFontFamilyFactory> font_family_factory;check_hresult(GetActivationFactory(
HStringReference(RuntimeClass_Windows_UI_Xaml_Media_FontFamily).Get(),font_family_factory.GetAddressOf()));
ComPtr<IFontFamily> font_family;check_hresult(font_family_factory->CreateInstanceWithName(
HStringReference(L"Segoe UI Semibold").Get(),nullptr,nullptr,font_family.GetAddressOf()));
check_hresult(block->put_FontFamily(font_family.Get()));check_hresult(block->put_FontSize(140.00));
ComPtr<IColorsStatics> colors_statics;check_hresult(GetActivationFactory(
HStringReference(RuntimeClass_Windows_UI_Colors).Get(),colors_statics.GetAddressOf()));
Color hot_pink;check_hresult(colors_statics->get_HotPink(&hot_pink));
ComPtr<ISolidColorBrushFactory> brush_factory;check_hresult(GetActivationFactory(
HStringReference(RuntimeClass_Windows_UI_Xaml_Media_SolidColorBrush).Get(),brush_factory.GetAddressOf()));
ComPtr<ISolidColorBrush> hot_pink_brush;check_hresult(brush_factory->CreateInstanceWithColor(hot_pink, hot_pink_brush.GetAddressOf()));
ComPtr<IBrush> foreground_brush;check_hresult(hot_pink_brush.As(&foreground_brush));check_hresult(block->put_Foreground(foreground_brush.Get()));
ComPtr<IFrameworkElement> block_framework_element;check_hresult(block.As(&block_framework_element));check_hresult(block_framework_element->put_VerticalAlignment(VerticalAlignment_Center));
check_hresult(block->put_TextAlignment(TextAlignment_Center));
check_hresult(block->put_Text(HStringReference(L"Hello CppCon!").Get()));
ComPtr<IWindowStatics> window_statics;check_hresult(GetActivationFactory(
HStringReference(RuntimeClass_Windows_UI_Xaml_Window).Get(),window_statics.GetAddressOf()));
ComPtr<IWindow> window;check_hresult(window_statics->get_Current(window.GetAddressOf()));
ComPtr<IUIElement> block_ui_element;check_hresult(block.As(&block_ui_element));check_hresult(window->put_Content(block_ui_element.Get()));check_hresult(window->Activate());
return S_OK;}
};
![Page 18: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/18.jpg)
C++/CXin a Nutshell
![Page 19: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/19.jpg)
sealed partial class App : Application
{
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
TextBlock block = new TextBlock();
block. FontFamily = new FontFamily("Segoe UI Semibold");
block. FontSize = 140.0;
block. Foreground = new SolidColorBrush(Colors. HotPink);
block. VerticalAlignment = VerticalAlignment. Center;
block. TextAlignment = TextAlignment. Center;
block. Text = "Hello CppCon!";
Window window = Window. Current;
window. Content = block;
window. Activate();
}
} C#
![Page 20: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/20.jpg)
ref class App sealed : public Application
{
protected:
virtual void OnLaunched(LaunchActivatedEventArgs^ e) override
{
TextBlock^ block = ref new TextBlock();
block->FontFamily = ref new FontFamily("Segoe UI Semibold");
block->FontSize = 140.0;
block->Foreground = ref new SolidColorBrush(Colors::HotPink);
block->VerticalAlignment = VerticalAlignment::Center;
block->TextAlignment = TextAlignment::Center;
block->Text = "Hello CppCon!";
Window^ window = Window::Current;
window->Content = block;
window->Activate();
}
}; C++/CX
![Page 21: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/21.jpg)
Challenges with C++/CX◦ Memory management not customizable
◦ Code bloat in exception/HRESULT translation
◦ Interop between standard and WinRT types
◦ Arrays perform poorly by default
◦ No visibility into abstractions
◦ Debuggability
◦ Syntax differences
◦ And more…
![Page 22: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/22.jpg)
C++/WinRTin a Nutshell
![Page 23: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/23.jpg)
C++/WinRTStandard C++
Header-only library
Classy type system
Natural, productive, safe
Best performance, smallest binaries
Language projection for the systems programmer
…but also for app developers and other programmers!
Succinct
![Page 24: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/24.jpg)
sealed partial class App : Application
{
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
TextBlock block = new TextBlock();
block.FontFamily = new FontFamily("Segoe UI Semibold");
block.FontSize = 140.0;
block.Foreground = new SolidColorBrush(Colors.HotPink);
block.VerticalAlignment = VerticalAlignment.Center;
block.TextAlignment = TextAlignment.Center;
block.Text = "Hello CppCon!";
Window window = Window.Current;
window.Content = block;
window.Activate();
}
}
C#
![Page 25: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/25.jpg)
struct App : ApplicationT<App>
{
void OnLaunched(LaunchActivatedEventArgs const &)
{
TextBlock block;
block.FontFamily(FontFamily(L"Segoe UI Semibold"));
block.FontSize(140.0);
block.Foreground(SolidColorBrush(Colors::HotPink()));
block.VerticalAlignment(VerticalAlignment::Center);
block.TextAlignment(TextAlignment::Center);
block.Text(L"Hello CppCon!");
Window window = Window::Current();
window.Content(block);
window.Activate();
}
};
C++/WinRT
![Page 26: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/26.jpg)
Let’s Build Something a Bit More Interesting…
![Page 27: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/27.jpg)
![Page 28: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/28.jpg)
Warning: C#
class App : Application{
protected override void OnLaunched(LaunchActivatedEventArgs args){ ... }
async void ForegroundAsync(TextBlock block){ ... }
IAsyncOperation<string> BackgroundAsync(StorageFile file){ ... }
static void Main(){
Application.Start((param) => { new App(); });}
}
![Page 29: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/29.jpg)
1/3 OnLaunched in C#
protected override void OnLaunched(LaunchActivatedEventArgs args){
TextBlock block = new TextBlock();
block.FontFamily = new FontFamily("Segoe UI Semibold");block.FontSize = 72.0;block.Foreground = new SolidColorBrush(Colors.Orange);block.VerticalAlignment = VerticalAlignment.Center;block.TextAlignment = TextAlignment.Center;block.TextWrapping = TextWrapping.Wrap;
Window window = Window.Current;window.Content = block;window.Activate();
ForegroundAsync(block);}
![Page 30: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/30.jpg)
2/3 ForegroundAsync in C#
async void ForegroundAsync(TextBlock block){
FileOpenPicker picker = new FileOpenPicker();picker.FileTypeFilter.Add(".png");picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;var file = await picker.PickSingleFileAsync();
if (file == null){
return;}
block.Text = await BackgroundAsync(file);}
![Page 31: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/31.jpg)
3/3 BackgroundAsync in C#
IAsyncOperation<string> BackgroundAsync(StorageFile file){
return Task<string>.Run(async () =>{
var stream = await file.OpenAsync(FileAccessMode.Read);var decoder = await BitmapDecoder.CreateAsync(stream);var bitmap = await decoder.GetSoftwareBitmapAsync();
var engine = OcrEngine.TryCreateFromUserProfileLanguages();var result = await engine.RecognizeAsync(bitmap);return result.Text;
}).AsAsyncOperation<string>();
}
1. Get work on thread pool
2. Get IAsync… representing task
![Page 32: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/32.jpg)
![Page 33: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/33.jpg)
Warning: C++/CX
ref class App : Application{protected:
void OnLaunched(LaunchActivatedEventArgs ^) override;
private:task<void> ForegroundAsync(TextBlock ^ block);IAsyncOperation<String ^> ^ BackgroundAsync(StorageFile ^ file);
};
int main(Array<String ^> ^){
Application::Start(ref new ApplicationInitializationCallback([](auto &&){
ref new App;}));
}
![Page 34: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/34.jpg)
1/3 OnLaunched in C++/CX
void OnLaunched(LaunchActivatedEventArgs ^) override{
TextBlock ^ block = ref new TextBlock();
block->FontFamily = ref new FontFamily("Segoe UI Semibold");block->FontSize = 72.0;block->Foreground = ref new SolidColorBrush(Colors::Orange);block->VerticalAlignment = VerticalAlignment::Center;block->TextAlignment = TextAlignment::Center;block->TextWrapping = TextWrapping::Wrap;
Window ^ window = Window::Current;window->Content = block;window->Activate();
ForegroundAsync(block);}
![Page 35: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/35.jpg)
2/3 ForegroundAsync in C++/CX
task<void> ForegroundAsync(TextBlock ^ block){
FileOpenPicker ^ picker = ref new FileOpenPicker();picker->FileTypeFilter->Append(".png");picker->SuggestedStartLocation = PickerLocationId::PicturesLibrary;auto file = co_await picker->PickSingleFileAsync();
if (file == nullptr){
return;}
block->Text = co_await BackgroundAsync(file);}
![Page 36: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/36.jpg)
3/3 BackgroundAsync in C++/CX
IAsyncOperation<String ^> ^ BackgroundAsync(StorageFile ^ file){
return create_async([=]{
return create_task([=] () -> task<String ^>{
auto stream = co_await file->OpenAsync(FileAccessMode::Read);auto decoder = co_await BitmapDecoder::CreateAsync(stream);auto bitmap = co_await decoder->GetSoftwareBitmapAsync();
auto engine = OcrEngine::TryCreateFromUserProfileLanguages();auto result = co_await engine->RecognizeAsync(bitmap);return result->Text;
});});
}
1. Get work on thread pool
2. Get IAsync… representing task
![Page 37: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/37.jpg)
![Page 38: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/38.jpg)
There’s a better way!
![Page 39: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/39.jpg)
C++/WinRT
struct App : ApplicationT<App>{
void OnLaunched(LaunchActivatedEventArgs const &);
fire_and_forget ForegroundAsync(TextBlock block);
IAsyncOperation<hstring> BackgroundAsync(StorageFile file);};
int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int){
Application::Start([](auto &&) { make<App>(); });}
![Page 40: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/40.jpg)
1/3 OnLaunched in C++/WinRT
void OnLaunched(LaunchActivatedEventArgs const &){
TextBlock block;
block.FontFamily(FontFamily(L"Segoe UI Semibold"));block.FontSize(72.0);block.Foreground(SolidColorBrush(Colors::Orange()));block.VerticalAlignment(VerticalAlignment::Center);block.TextAlignment(TextAlignment::Center);block.TextWrapping(TextWrapping::Wrap);
Window window = Window::Current();window.Content(block);window.Activate();
ForegroundAsync(block);}
![Page 41: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/41.jpg)
2/3 ForegroundAsync in C++/WinRT
fire_and_forget ForegroundAsync(TextBlock block){
FileOpenPicker picker;picker.FileTypeFilter().Append(L".png");picker.SuggestedStartLocation(PickerLocationId::PicturesLibrary);auto file = co_await picker.PickSingleFileAsync();
if (file == nullptr){
return;}
block.Text(co_await BackgroundAsync(file));}
![Page 42: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/42.jpg)
3/3 BackgroundAsync in C++/WinRT
IAsyncOperation<hstring> BackgroundAsync(StorageFile file){
co_await resume_background();
auto stream = co_await file.OpenAsync(FileAccessMode::Read);auto decoder = co_await BitmapDecoder::CreateAsync(stream);auto bitmap = co_await decoder.GetSoftwareBitmapAsync();
auto engine = OcrEngine::TryCreateFromUserProfileLanguages();auto result = co_await engine.RecognizeAsync(bitmap);return result.Text();
}
2. Resume on thread pool
1. Produce IAsync…
![Page 43: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/43.jpg)
![Page 44: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/44.jpg)
Interfaces
![Page 45: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/45.jpg)
IUnknown & IInspectable…
struct IUnknown{
virtual HRESULT QueryInterface(GUID const & id,void ** object) = 0;
virtual uint32_t AddRef() = 0;virtual uint32_t Release() = 0;
};
struct IInspectable : IUnknown{
virtual HRESULT GetIids(uint32_t * count, GUID ** iids) = 0;virtual HRESULT GetRuntimeClassName(HSTRING * className) = 0;virtual HRESULT GetTrustLevel(TrustLevel * trustLevel) = 0;
};
![Page 46: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/46.jpg)
ITextBlock
struct ITextBlock : IInspectable{
virtual HRESULT get_FontSize(double * value) = 0;virtual HRESULT put_FontSize(double value) = 0;
// ...}
![Page 47: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/47.jpg)
ITextBlock with raw pointers
ITextBlock * block = ...
HRESULT hr = block->put_FontSize(72.0);
if (hr != S_OK){
// pain and suffering...}
block->Release();
![Page 48: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/48.jpg)
ITextBlock with smart pointers
ComPtr<ITextBlock> block = ...
HRESULT hr = block->put_FontSize(72.0);
if (hr != S_OK){
// pain and suffering...}
![Page 49: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/49.jpg)
ITextBlock with C++/WinRT
ITextBlock block = ...
block.FontSize(72.0);
![Page 50: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/50.jpg)
Interfaces at the heart of classes…
using namespace Windows::UI::Xaml::Controls;
TextBlock block;
ITextBlock block = TextBlock(); // This works... but don't do this :)
![Page 51: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/51.jpg)
Reference counting is automatic…
void Scope(){
ITextBlock block = TextBlock();
ITextBlock block2 = block;}
ConstructionMove
AddRefRelease x 2
![Page 52: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/52.jpg)
Moves are cheaper…
void Scope(){
ITextBlock block = TextBlock();
ITextBlock block2 = std::move(block);}
ConstructionMove
MoveRelease x 1
![Page 53: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/53.jpg)
Classic inheritance good but shallow…
using namespace Windows::Storage;
IStorageFile file = ...
IInspectable in = file;
IUnknown un = in; AddRef
AddRef
![Page 54: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/54.jpg)
Good for synchronous parameters…
using namespace Windows::Storage;
IStorageFile file = ...
IInspectable const & in = file;
IUnknown const & un = in;
No AddRef
![Page 55: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/55.jpg)
Explicit queries are explicit…
IUnknown un = ...
IInspectable in = un.as<IInspectable>();
IStorageFile file = in.as<IStorageFile>();
QueryInterface
![Page 56: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/56.jpg)
IStorageFile
FileType
ContentType
OpenAsync
OpenTransactedWriteAsync
CopyAsync
CopyAndReplaceAsync
MoveAsync
MoveAndReplaceAsync
IStorageItem
RenameAsync
DeleteAsync
GetBasicPropertiesAsync
Name
Path
Attributes
DateCreated
IsOfType
Requires
![Page 57: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/57.jpg)
Implicit queries are implicit…
IStorageFile file = ...
IInspectable in = file;
IStorageItem item = file;
QueryInterface
AddRef
![Page 58: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/58.jpg)
Implicit queries lead to implicit methods…
IStorageFile file = ...
IStorageItem item = file;
hstring in = item.Name();
hstring fn = file.Name();
hstring ft = file.FileType();
hstring it = item.FileType();
QueryInterface
QueryInterface & v-call
v-call only
v-call only
error: 'FileType': is not a member of 'IStorageItem'
![Page 59: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/59.jpg)
Calling Methods
![Page 60: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/60.jpg)
Looks good & almost works…
struct IStorageItem : IInspectable{
abi<IStorageItem> * operator->() const noexcept;
hstring Name() const{
hstring value;check_hresult((*this)->get_Name(put(value)));return value;
}};
Returns vptr (pointer to vtable)
Which vptr?
![Page 61: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/61.jpg)
But then this happens…
struct IStorageFile : IInspectable{
abi<IStorageFile> * operator->() const noexcept;
operator IStorageItem() const{
return as<IStorageItem>();}
hstring Name() const{
return as<IStorageItem>().Name();}
};
x Number of interfaces
x Number of methods
![Page 62: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/62.jpg)
Consuming and producing interfaces…
template <typename D, typename I = D>struct consume;
template <typename D, typename I>struct produce;
template <typename D>struct consume<D, Windows::Storage::IStorageItem>{
hstring Name() const;// ...
};
template <typename D>struct produce<D, Windows::Storage::IStorageItem> // ...
![Page 63: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/63.jpg)
Symmetry…
struct IStorageItem{
hstring Name() const;hstring Path() const;IAsyncAction RenameAsync(hstring_ref desiredName) const;
// ...
struct MyStorage : implements<MyStorage, IStorageItem, IStorageItem2>{
hstring Name() const { return L"Hello world.txt"; }hstring Path() const { return L"C:\\CppCon"; }
IAsyncAction RenameAsync(hstring_ref desiredName) const{
co_await ...}
// ...
![Page 64: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/64.jpg)
Consuming in the real world…
template <typename D, typename I = D>struct consume;
template <typename D>struct consume<D, Windows::Storage::IStorageItem>{ /* shims */ }
template <typename D>struct impl_IStorageItem{ /* shims */ }
template <> struct traits<Windows::Storage::IStorageItem>{
template <typename D> using consume = Windows::Storage::impl_IStorageItem<D>;};
template <typename D, typename I = D>using consume = typename traits<I>::template consume<D>;
Simple :)
Not so simple :(
![Page 65: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/65.jpg)
Shims that almost works…
template <typename D>struct impl_IStorageItem{
hstring Name() const{
hstring value;
check_hresult(static_cast<const D &>(*this)
->get_Name(put(value)));
return value;}
};
1. CRTP
2. v-call
![Page 66: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/66.jpg)
Touch of compile-time indirection…
template <typename D>struct impl_IStorageItem{
hstring Name() const{
hstring value;
check_hresult(static_cast<const IStorageItem &>(
static_cast<const D &>(*this))->get_Name(put(value)));
return value;}
};
1. CRTP
2. Redundant?
3. v-call
![Page 67: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/67.jpg)
Rough assembly…
struct IStorageFile :IInspectable,impl_IStorageFile<IStorageFile>,impl_IStorageItem<IStorageFile>
{abi<IStorageFile> * operator->() const noexcept;
operator IStorageItem() const{
return as<IStorageItem>();}
};
x Number of interfaces
OwnershipDefault
Required…
![Page 68: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/68.jpg)
Variadic scaffolding…
template <typename D, typename I = D>using consume = typename traits<I>::template consume<D>;
template <typename D, typename I>struct require_one : consume<D, I>{
operator I() const{
return static_cast<const D *>(this)->template as<I>();}
};
template <typename D, typename ... I>struct require : require_one<D, I> ... {};
Glue
Code generator
![Page 69: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/69.jpg)
Variadic (and elegant) assembly
struct IStorageFile :IInspectable,consume<IStorageFile>,require<IStorageFile, IStorageItem,
IInputStreamReference,IRandomAccessStreamReference>
{abi<IStorageFile> * operator->() const noexcept;
};
![Page 70: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/70.jpg)
Runtime Classes
![Page 71: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/71.jpg)
Class assembly
struct StorageFile :IStorageFile,require<StorageFile, IStorageItem2,
IStorageItemProperties, IStorageItemProperties2, IStorageItemPropertiesWithProvider, IStorageFilePropertiesWithAvailability, IStorageFile2>
{};
Ownership &default interface
CRTP
Additionalinterfaces
![Page 72: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/72.jpg)
Behind default constructors…
using namespace Windows::Storage::Pickers;
FileOpenPicker picker;
FileOpenPicker::FileOpenPicker() :FileOpenPicker(activate_instance<FileOpenPicker>())
{}
Defaultconstructor
Delegatingconstructor
“RoActivateInstance”
![Page 73: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/73.jpg)
Behind constructors with params…
using namespace Windows::Networking;
HostName name(L"moderncpp.com");
HostName::HostName(hstring_ref hostName) :HostName(get_activation_factory<HostName, IHostNameFactory>().
CreateHostName(hostName)){}
“RoGetActivationFactory”
![Page 74: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/74.jpg)
You can do this yourself!
// StorageFile file;
StorageFile file = activate_instance<StorageFile>();
Defaultinterface
Request alternative interface
IStorageFile2 file =activate_instance<StorageFile, IStorageFile2>();
![Page 75: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/75.jpg)
You can do this too!
using namespace Windows::Networking;
// HostName name(L"moderncpp.com");
auto factory = get_activation_factory<HostName, IHostNameFactory>();
HostName name = factory.CreateHostName(L"moderncpp.com");
![Page 76: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/76.jpg)
You can go deeper still!
using namespace Windows::Networking;
// HostName name(L"moderncpp.com");
auto factory = get_activation_factory<HostName, IHostNameFactory>();
// HostName name = factory.CreateHostName(L"moderncpp.com");
HostName name = nullptr;
HRESULT hr = factory->abi_CreateHostName(get(hstring_ref(L"moderncpp.com")), put(name));
![Page 77: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/77.jpg)
Metadata as traits
template <> struct traits<Windows::Storage::IStorageFile>{
using abi = ABI::Windows::Storage::IStorageFile;
template <typename D> using consume =Windows::Storage::impl_IStorageFile<D>;
};
template <> struct traits<Windows::Storage::StorageFile>{
using abi = ABI::Windows::Storage::StorageFile;
static constexpr wchar_t const * name() noexcept{
return L"Windows.Storage.StorageFile";}
};
![Page 78: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/78.jpg)
Default activation…
template <typename C, typename I = C>I activate_instance(){
return get_activation_factory<C>().ActivateInstance().
template as<I>();}
2. Default activation
1. Get factory
3. Query for desired interface
![Page 79: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/79.jpg)
Get activation factory…
template <typename C, typename I = IActivationFactory>I get_activation_factory(){
static I factory = impl::get_agile_activation_factory<C, I>();
if (!factory){
return impl::get_activation_factory<C, I>();}
return factory;}
1. Try get agile factory
2. Fallback to non-agile
![Page 80: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/80.jpg)
Performance
![Page 81: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/81.jpg)
It’s not just about syntax
C++/WinRT C++/CX C#
Smallest binary 53 KB + 594 KB 86 KB + 594 KB 261 KB + 3.31 MB
![Page 82: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/82.jpg)
0
0.5
1
1.5
2
2.5
3
3.5
4
4.5
Calling static methods (PropertyValue::CreateEmpty with 4,000,000 iterations)
C# C++/CX C++/WinRT
![Page 83: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/83.jpg)
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
Calling required methods (Uri.ToString with 10,000,000 iterations)
C# C++/CX C++/WinRT
![Page 84: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/84.jpg)
0
0.5
1
1.5
2
2.5
3
3.5
Arrays (CertificateQuery.Thumbprint with 10,000,000 iterations)
C# C++/CX C++/WinRT
![Page 85: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/85.jpg)
0
0.5
1
1.5
2
2.5
3
Collections (IVectorView<hstring> with 10,000,000 elements)
C# C++/CX C++/WinRT
![Page 86: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/86.jpg)
Winmd files
Winmd files
Compilers & metadata
Winmd files*.winmd
cppwinrt.exe
winrt/*.h
cl.exeapp.cpp app.exe
C++/WinRT compiler
Visual C++ compileror Clang :)
![Page 87: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/87.jpg)
Visual C++ optimizationsEmpty base classes
strlen/wcslen
Magic statics
Pure functions
Coroutines
Modules
![Page 88: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/88.jpg)
More informationCome to CoroutineCon tomorrow!
◦ 9:00am: An Introduction to C++ Coroutines
◦ 2:00pm: C++ Coroutines: Under the covers
◦ 3:15pm: Putting Coroutines to Work with the Windows Runtime
Web: https://moderncpp.com
Email: [email protected]
Twitter: @KennyKerr & @JamesMcNellis
![Page 89: Embracing Standard C++ for the Windows Runtime · Embracing Standard C++ for the Windows Runtime Kenny Kerr Microsoft Windows @KennyKerr James McNellis Microsoft Visual C++ @JamesMcNellis](https://reader030.fdocuments.in/reader030/viewer/2022040108/5e069c58eae5c624da125e4c/html5/thumbnails/89.jpg)