Top Banner
Deep Dive into C++/CX and WinRT Marian Luparu Senior Program Manager 3-010
28

Deep Dive into C++/CX and WinRT

Feb 23, 2016

Download

Documents

Sauda

Deep Dive into C++/CX and WinRT. Marian Luparu Senior Program Manager 3-010. Agenda. C++/CX guiding principles C++/CX good practices Correctness Performance Tips & Tricks. C++/CX. High-fidelity support for WinRT constructs and semantics Align to familiar C++ constructs - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

Deep Dive into C++/CX and WinRT

Deep Dive into C++/CX and WinRTMarian LuparuSenior Program Manager

3-01011/1/2012Windows Azure1 2011 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.C++/CX guiding principles

C++/CX good practicesCorrectnessPerformance

Tips & Tricks

Agenda

High-fidelity support for WinRT constructs and semantics

Align to familiar C++ constructs

Minimize concept countC++/CXBuild 2012 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.11/1/20123Some lessons learned from writing Windows Store apps with C++/CXGood, Better, BestBuild 2012 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.11/1/20124Some lessons learned from writing Windows Store apps with C++/CXCorrectnessExceptionsWinRT C++ Exceptions (Platform::Exception)The only way to communicate failure over an ABI boundarythrow ref new OutOfBoundsException(angle is outside range);

C++ ExceptionsWill terminate the process when reaching an ABI boundary

SEH ExceptionsWhen bad things happen to your app

Exception typesBuild 2012 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.11/1/20126ExceptionsWhen catching COMExceptionHandle only HRESULTs that you expect, re-throw everything elsecatch (COMException^ e) { switch(e->HResult) { default: throw; }}

When throwing an exception from an HRESULTusing namespace Platform;if (FAILED(hr)) { throw Exception::CreateException(hr);}Catch the most specific exception

Throw the most specific exceptionBuild 2012 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.11/1/20127task ReadContent(String^ filename){ anim->Start(); auto t = create_task(KnownFolders::DocumentsLibrary-> GetFileAsync(filename)) .then( [](StorageFile^ file) { return file->OpenAsync(FileAccessMode::Read); }) .then( [](IRandomAccessStream^ stream) { auto inputStream = stream->GetInputStreamAt(0); auto dr = ref new DataReader(inputStream); return dr->LoadAsync(stream->Size); }) .then( [anim](unsigned int count) { anim->End(); // read content return content; }); return t;}

Async & Exceptions

Build 2012 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.11/1/20128anim->Start();auto t = create_task(KnownFolders::DocumentsLibrary->GetFileAsync("Log.txt")).then( [](StorageFile^ file) {return file->OpenAsync(FileAccessMode::Read);}).then( [](IRandomAccessStream^ stream) {auto inputStream = stream->GetInputStreamAt(0);auto dr = ref new DataReader(inputStream);return dr->LoadAsync(stream->Size);}).then( [anim](unsigned int count) {anim->End(); // read content});return t;

[anim](unsigned int count) {anim->End();// read content });Async & Exceptions[anim](task lt) {anim->End();// read content });[anim](task lt) {anim->End();try {unsigned int count = lt.get();// read content } catch(AccessDeniedException^ ex) {throw;}});Build 2012 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.11/1/20129Some lessons learned from writing Windows Store apps with C++/CXPerformanceAsync & ThreadingUI Thread

STA ThreadSTA ThreadBackground Threadvoid MainPage::OnNavigatedTo(NavigationEventArgs^ e) { create_task(KnownFolders::DocumentsLibrary->GetFileAsync(Surface.ini")) .then( [](StorageFile^ file) {return file->OpenAsync(FileAccessMode::Read); }) .then( [](IRandomAccessStream^ stream) {auto inputStream = stream->GetInputStreamAt(0);auto dr = ref new DataReader(inputStream);return dr->LoadAsync(stream->Size); }) .then( [](unsigned int count) {// read contenttxtContent->Text = content; });}

Background ThreadBackground ThreadOpTOpTOpTAsync & ThreadingUI Thread

STA ThreadSTA ThreadBackground Threadvoid MainPage::OnNavigatedTo(NavigationEventArgs^ e) { create_task(KnownFolders::DocumentsLibrary->GetFileAsync(Surface.ini")) .then( [](StorageFile^ file) {return file->OpenAsync(FileAccessMode::Read); }) .then( [](IRandomAccessStream^ stream) {auto inputStream = stream->GetInputStreamAt(0);auto dr = ref new DataReader(inputStream);return dr->LoadAsync(stream->Size); }) .then( [](unsigned int count) {// read contenttxtContent->Text = content; });}

Background ThreadBackground ThreadOpTOpTOpTvoid MainPage::OnNavigatedTo(NavigationEventArgs^ e) { create_task(KnownFolders::DocumentsLibrary->GetFileAsync(Surface.ini")) .then( [](StorageFile^ file) {return file->OpenAsync(FileAccessMode::Read); }) .then( [](IRandomAccessStream^ stream) {auto inputStream = stream->GetInputStreamAt(0);auto dr = ref new DataReader(inputStream);return dr->LoadAsync(stream->Size); }) .then( [](unsigned int count) {// read contenttxtContent->Text = content; });}

Async & ThreadingUI Thread

STA ThreadSTA ThreadBackground ThreadBackground ThreadBackground ThreadOpTOpTOpTvoid MainPage::OnNavigatedTo(NavigationEventArgs^ e) { create_task(KnownFolders::DocumentsLibrary->GetFileAsync(Surface.ini")) .then( [](StorageFile^ file) {return file->OpenAsync(FileAccessMode::Read); }, task_continuation_context::use_arbitrary()) .then( [](IRandomAccessStream^ stream) {auto inputStream = stream->GetInputStreamAt(0);auto dr = ref new DataReader(inputStream);return dr->LoadAsync(stream->Size); }, task_continuation_context::use_arbitrary()) .then( [](unsigned int count) {// read contenttxtContent->Text = content; }, task_continuation_context::use_current());}

Async & Threading (Back to the UI thread)2 Options: PPL contexts Dispatcher// ....then( [](unsigned int count) { // read content CoreApplication::MainView-> CoreWindow->Dispatcher->RunAsync( CoreDispatcherPriority::Normal, ref new DispatchedHandler( [content]() { txtContent->Text = content; }) );}, task_continuation_context::use_arbitrary());

// ....then([content](unsigned int count) {// read content}, task_continuation_context::use_arbitrary()).then([content]() {txtContent->Text = *content;}, task_continuation_context::use_current());

Some lessons learned from writing Windows Store apps with C++/CXClear BoundaryC++/CX and standard C++ codeBuild 2012 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.11/1/201215Performance

Portability

Coding styleIt would be a mistake, and we are absolutely not telling you, to go rush out and say, 'every class I ever wrote should now be a ref class or a value class and I shouldn't use those old standard things any more

Herb Sutter//BUILD/ 2011Clear boundary Why is it important?Build 2012 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.11/1/201216Good: Reduce chattiness at WinRT boundary

Better: Minimize conversions between WinRT and C++ typesString^ or std::wstring?Vector^ or std::vector?Keep data in its originating type for as long as possible

Best: Profile your app performance Understand bottlenecks created by conversionsOptimize accordinglyClear BoundaryData conversionsBuild 2012 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.11/1/201217MemoryThis code creates a copy of your bufferchar16* buffer; // ...xmlDoc->LoadXml(ref new String(buffer));

This one doesntStringReference sr(buffer);xmlDoc->LoadXml(sr);

Whats the catch?You need to guarantee that lifetime of the buffer exceeds the duration of the call and its content remains unchanged

StringsBuild 2012 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.11/1/201218MemoryThe catch: reentrancy//User codeStringReference sr(buffer);WinRTCall(sr);//WinRT codeHRESULT WinRTCall(HSTRING s) { Fire_OnWinRTCall(this, s); ...}// User code (callback)void myApp_OnWinRTCall(...) {buffer[n] = ...;}

Build 2012 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.11/1/201219MemorySame story as strings. Heres how to avoid a copy:

auto buffer = ref new Array(count);memcpy(buffer->Data, bytes, count);writer->WriteBytes(buffer);ArraysArrayReference buffer(bytes, count);Same catchYou need to guarantee that lifetime of the buffer exceeds the duration of the call and the its content remains unchanged

To avoid an extra copy when reading data from streams, use IBuffer rather than DataReaderBuffers (demo)Memory11/2 12:45 B33 McKinley Herb Sutter: The Future of C++11/1 8:30 B33 Baker Max McMullen: Performance tips for Windows Store apps using DirectX and C++11/1 12:00 B33 Kodiak Alan Ludwig: Building mixed-language apps11/1 16:15 B92 Magellan (or)11/2 8:30 B33 McKinley Peter Torr: WP8 Using C++ in your application Related SessionsGot Feedback?Who?You. If youre actively using Visual Studio to develop C++ appsWhat?Feedback about the VS experience needed!When?Wednesday, Oct. 31st, 3PM - 6PMThursday, Nov. 1st, 3PM 6PMWhere?Bldg. 92, Conference Room 2021 (contact Building Reception)[email protected] future design decisionsSIGN UP @http://bit.ly/x6dtHt

No time at BUILD?Build 2012 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.11/1/201223 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.Backup SlidesClear boundary (How)File compilation modeAvoids type collisions between your types and WinRT typesTurn off /ZW per compile unit

Data conversions

Code constructsIt would be a mistake, and we are absolutely not telling you, to go rush out and say, 'every class I ever wrote should now be a ref class or a value class and I shouldn't use those old standard things any more

Herb Sutter//BUILD/ 2011Not a problem in practiceBuild 2012 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.11/1/201226Ref class everywhere

Class vs. ref class

Type embedding

Clear BoundaryCode constructsBuild 2012 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.11/1/201227using namespace Windows::Devices::Sensors;class light_sensor {public: float illuminance(); bool is_available();

private: LightSensor^ _sensor;};Clear BoundaryCode constructs#include lightsensor.hauto s = make_shared();float illum = s->is_available() ? s->illuminance() : .0f;Platform-specific (C++/CX)Standard C++Build 2012 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.11/1/201228