Presentation is loading. Please wait.

Presentation is loading. Please wait.

Windows 8 and WinRT from a C++ perspective

Similar presentations


Presentation on theme: "Windows 8 and WinRT from a C++ perspective"— Presentation transcript:

1 Windows 8 and WinRT from a C++ perspective
Marc Grégoire

2 Who am I? Marc Grégoire MVP VC++ / Microsoft MEET member
Founder of the Belgian C++ Users Group (BeCPP)

3 Agenda Windows 8 Platform Windows Runtime (WinRT)
C++ Extensions (C++/CX) Asynchronous programming model Libraries (WinRT   STL) C++ and XAML

4 Windows 8 Platform

5 Windows Core OS Services
Windows 8 Platform Metro style Apps Desktop Apps View C C++ C C++ XAML XAML HTML / CSS HTML JavaScript Model Controller C# VB JavaScript (Chakra) C C++ C# VB Windows Runtime APIs Communication & Data Graphics & Media Devices & Printing System Services Application Model Internet Explorer Win32 .NET / SL Core Windows Core OS Services

6 Windows 8 Platform – Where C++?
Metro Hybrid Apps HTML5 + JavaScript front-end C++ components Metro XAML Apps UI using XAML C++ code behind Metro high performance 2D/3D Apps C++ with DirectX rendering

7 Windows Runtime (WinRT)

8 Windows Runtime (WinRT)
Foundation for building Metro style apps Strongly-typed system Automatically reference counted Exception-based Well defined binary contract across module boundaries, ABI-safe (For C++) Deep integration with STL

9 Windows Runtime (WinRT)
Contains classes for UI, Pickers, Controls, Media, XAML, Storage, Network, … A subset of the Win32 APIs can still be used

10 Windows Runtime (WinRT)
User Interface HTML5/CSS XAML DirectX Controls Input Accessibility Printing Data Binding Tiles SVG Devices Sensors Geolocation Portable NFC Communications & Data Contracts XML Web SMS Networking Notifications Local & Cloud Storage Streams Background Transfer Media Visual Effects Playback PlayTo Capture Fundamentals Application Services Authentication Cryptography Globalization Memory Management Threading/Timers

11 Windows Runtime (WinRT)
Includes detailed metadata = complete description of the Windows Runtime Metadata allows projection into different languages: C++, C#, VB, JavaScript Metadata = similar to the CLI metadata

12 C++ Extensions (C++/CX)

13 C++ Extensions (C++/CX)
Set of language extensions and libraries to allow direct consumption and authoring of WinRT types WinRT is based on COM, but you don’t have to deal with it directly.

14 C++ Extensions (C++/CX)
Key Bindings Feature Summary 1. Data Types ref class Reference type value class Value type interface class Interface property Property with get/set event “Delegate property” with add/remove/raise delegate Type-safe function pointer generic Type-safe generics 2. Allocation ref new Reference-counted allocation 3. Pointer & Reference ^ Strong pointer (“hat” or “handle”) % Strong reference C++ extensions are needed to bind WinRT, which is a foreign type system, to C++.

15 C++ Extensions (C++/CX)
C/C++ External Surface for native callers/callees WinRT External Surface for WinRT callers/callees Module Internals written in C++ Don’t use WinRT objects in your entire application, only on the WinRT boundaries. For example, if you want to write a WinRT component in C++ that you would like to use from another language than C++.

16 Automatic Lifetime Management
Handle (^ / Hat) is smart pointer to WinRT objects Reference counted Allocated with ref new Example: Person^ p; { Person^ p2 = ref new Person(); // ref count = 1 p2->Name = "John"; // ref count = 1 p = p2; // ref count = 2 } // ref count = 1 p = nullptr; // ref count = 0  ~Person() This slide is basically the only thing you need to know for consuming WinRT types. ^ is similar to CComPtr; the difference is that ^ is baked into the compiler, so the compiler can much better optimize it, and will avoid addref/releaseref when it can.

17 Automatic Lifetime Management
Even easier… Simply on the stack, or as a member of another WinRT type Example: { Person p; p.Name = "John"; /* ... */ } // ~Person() WinRT object on the stack, the compiler will actually convert that to a reference counted smart pointer, so basically, only the COM pointer is on the stack, but cleanup is all automatic.

18 WinRT Class Use ref class to define new WinRT classes (ABI-safe cross-language classes) Example: public ref class Person { public: Person(String^ name, String^ ); void Greet(Person^ other); internal: ~Person(); void SetPassword(const std::wstring& passwd); }; Usage: Person^ p = ref new Person("John", p->Greet(ref new Person("Jim")); Public/protected methods  only WinRT parameters Private/internal methods  any C++ types as parameters The “public” at the beginning of the class is needed if you want to make this class available to the external world. “internal”: access from within the module, or assembly, not exposed.

19 WinRT Interface Defining Implementing Inheriting Using
public interface class IShape ref class Rectangle : ISelectableShape { void Draw(); public: }; virtual void Draw(); Inheriting virtual void Select(); Using public interface class ISelectableShape : IShape IShape^ shape = ref new Rectangle(); shape->Draw(); void Select(); automatically abstract no need for virtual keywords No need for pure virtual specifier (=0) all methods are automatically public No data members All these things are still ABI-safe.

20 WinRT Property Defining Using
Trivial properties (with private backing store) public: property String^ Name; User defined properties public: property Person^ Sibling { Person^ get() { InitSiblings(); return _sibling; } void set(Person^ value) { _sibling = value; NotifySibling(); } } private: Person^ _sibling; Using Person^ p = ref new Person("John"); p->Sibling = ref new Person(p->Name);

21 WinRT Delegate Declaring Instantiating Invoking From lambda:
public delegate void PropertyChanged(String^ propName, String^ propValue); Instantiating From lambda: auto p = ref new PropertyChanged( [](String^ pn, String^ pv) { cout << pn << " = " << pv; }); From free-function auto p = ref new PropertyChanged(UIPropertyChanged); From class-member auto p = ref new PropertyChanged(this, MainPage::OnPropertyChanged); Invoking p("Visible", "f"); Delegates are type-safe function pointers. Similar to std::function<>, but ABI-safe. You can bind anything that is callable to a delegate (lambda, free-function, class-member, std::bind, …)

22 WinRT Event Defining Trivial event (with private backing store)
public: event PropertyChanged^ OnPropertyChanged; User defined properties public: event PropertyChanged^ OnNetworkChanged { EventRegistrationToken add(PropertyChanged^); void remove(EventRegistrationToken t); void raise(String^, String^); } Event = a property that is a delegate type on which you can add, remove, and raise, instead of get, and set.

23 WinRT Event Using Subscribing Unsubscribing
person->OnPropertyChanged += propertyChangedDelegate; auto token = person->OnPropertyChanged::add(propertyChangedDelegate); Unsubscribing person->OnPropertyChanged -= token; person->OnPropertyChanged::remove(token); To unsubscribe, you need a token. Only ::add() returns a token.

24 WinRT Exceptions Under the covers, WinRT uses COM, thus HRESULTs
WinRT wraps HRESULTs into exceptions

25 WinRT Exceptions Only a fixed set of exceptions is available
Platform::Exception is the base for all WinRT exceptions You cannot derive your own exceptions from Platform::Exception HRESULT Exception E_OUTOFMEMORY OutOfMemoryException E_INVALIDARG InvalidArgumentException E_NOINTERFACE InvalidCastException E_POINTER NullReferenceException E_NOTIMPL NotImplementedException E_ACCESSDENIED AccessDeniedException E_FAIL FailureException E_BOUNDS OutOfBoundsException E_CHANGED_STATE ChangedStateException REGDB_E_CLASSNOTREG ClassNotRegisteredException E_DISCONNECTED DisconnectedException E_ABORT OperationCanceledException If you need to pass an HRESULT for which there is no standard exception yet, pass it as a COMException, wrapping your HRESULT.

26 WinRT Exceptions Throwing exceptions Catching exceptions
throw ref new InvalidArgumentException(); throw ref new COMException(hr); Catching exceptions try { … } catch (OutOfMemoryException^ ex) { … } Catch all WinRT exceptions: catch (Platform::Exception^ ex) { } Access HRESULT via ex->HResult

27 WinRT Generics Defining Using Implementing
generic<typename T, typename U> IPair<String^, Uri^>^ uri = public interface class IPair { ref new PairStringUri(); property T First; auto first = uri->First; // String^ property U Second; auto second = uri->Second; // Uri^ }; Implementing ref class PairStringUri: IPair<String^, Uri^> { public: property String^ First; property Uri^ Second; Generics are ABI-Safe templates.

28 WinRT .winmd Metadata Files
.winmd files Contain the metadata representation of WinRT types To consume a winmd file: Right click on project in Solution Explorer > References > Add New Reference… Or #using <Company.Component.winmd> Make sure the winmd and implementation dll is packaged together with your application To produce a .winmd file: Start from the “C++ WinRT Component Dll” template Define public types (ref classes, interfaces, delegates, etc.) C++ WinRT components can be consumed by other languages.

29 WinRT Partial Runtime Classes
Partial class definition partial ref class MainPage: UserControl, IComponentConnector { public: void InitializeComponent(); void Connect() { btn1->Click += ref new EventHandler(this, &MainPage::Button_Click); } }; Class definition ref class MainPage MainPage() { InitializeComponent(); } void Button_Click(Object^ sender, RoutedEventArgs^ e); Partial classes make it easier to develop nice tools for C++. Before, tools sometimes would insert generated code into specific locations in a file, maybe between specific comments. With partial classes, you can have 1 partial class that is always generated by the tool, and that contains no user-written code. The user-written code then goes into another partial class, and both are combined to get the final class.

30 Asynchronous programming model

31 Async with PPL Tasks Windows 8 Metro only allows asynchronous operations for anything that can take longer than a couple milliseconds For example: there are no synchronous file operations possible in Metro Advantage: keeps UI fast and fluid Disadvantage: programming can be more complex, but new PPL tasks support helps alot here

32 Async with PPL Tasks Example: Create a file Open the file
Write a string to the file Flush the file

33 Without PPL Tasks StorageFolder^ item = KnownFolders::PicturesLibrary;
auto createStorageFileOp = item->CreateFileAsync("myfile.txt"); createStorageFileOp->Completed = ref new AsyncOperationCompletedHandler<StorageFile^>( [](IAsyncOperation<StorageFile^>^ asyncOp, AsyncStatus status) { auto streamRetrievalOp = asyncOp->GetResults()->OpenAsync(FileAccessMode::ReadWrite); streamRetrievalOp->Completed = ref new AsyncOperationCompletedHandler<IRandomAccessStream^>( [](IAsyncOperation<IRandomAccessStream^>^ asyncOp, AsyncStatus status) { IOutputStream^ stream = asyncOp->GetResults()->GetOutputStreamAt(0); DataWriter^ bbrw = ref new DataWriter(stream); bbrw->WriteString("Hello async!"); auto writeBinaryStringOp = bbrw->StoreAsync(); writeBinaryStringOp->Completed = ref new AsyncOperationCompletedHandler<unsigned int>( [stream](IAsyncOperation<unsigned int>^ asyncOp, AsyncStatus status) { int bytes = asyncOp->GetResults(); auto streamFlushOp = stream->FlushAsync(); streamFlushOp->Completed = ref new AsyncOperationCompletedHandler<bool>( [](IAsyncOperation<bool>^ asyncOp, AsyncStatus status) { bool result = asyncOp->GetResults(); }); Without PPL Tasks

34 With PPL Tasks StorageFolder^ item = KnownFolders::PicturesLibrary;
auto createStorageFileOp = item->CreateFileAsync("myfile.txt"); task<StorageFile^> createFileTask(createStorageFileOp); IOutputStream^ ostream; createFileTask.then([](StorageFile^ storageFile){ return storageFile->OpenAsync(FileAccessMode::ReadWrite); }).then([](IRandomAccessStream^ rastream) -> task<bool> { ostream = rastream->GetOutputStreamAt(0); DataWriter^ bbrw = ref new DataWriter(ostream); bbrw->WriteString("Hello async!"); return bbrw->StoreAsync(); }).then([ostream](unsigned int bytesWritten) { return ostream->FlushAsync(); }).then([](bool flushed) { });

35 Libraries (WinRT   STL)

36 Vector and ObservableVector
Instantiating using namespace Platform::Collections; Vector<String^>^ items = ref new Vector<String^>(); Adding elements items->Append("Hello"); Returning a read-only view of the vector IVectorView<String^>^ view = items->GetView(); Getting notification for changes items->VectorChanged += ref new VectorChangedEventHandler<String^> (this, &MyClass::VectorChanged); IVectorView is comparable to an iterator range.

37 Map and ObservableMap Defining Adding elements
using namespace Platform::Collections; auto favorites = ref new Map<String^, Uri^>(); Adding elements favorites->Insert("MSDN", ref new Uri(" Checking and removing elements if (favorites->HasKey("MSDN")) favorites->Remove("MSDN");

38 Integration with STL Algorithms
WinRT String has Begin()/End() member methods For WinRT collections, collection.h defines begin() and end() functions Example: IVector<int>^ v = GetItems(); int sum = 0; std::for_each(begin(v), end(v), [&sum](int element) { sum += element; });

39 Integration with STL Containers
Conversion is possible between STL containers and WinRT containers String^  std::wstring (via wchar_t*) Vector  std::vector std::vector<int> v; v.push_back(10); auto items = ref new Vector<int>(v); Vector  std::vector Vector<int>^ items = ...; std::vector<int> v = Windows::Foundation::Collections::to_vector(items);

40 C++ and XAML

41 Windows 8 Platform – Where C++?
Metro Hybrid Apps HTML5 + Javascript front-end C++ components Metro XAML Apps UI using XAML C++ code behind Metro high performance 2D/3D Apps C++ with DirectX rendering Metro Hybrid Apps HTML5 + Javascript front-end C++ components Metro XAML Apps UI using XAML C++ code behind Metro high performance 2D/3D Apps C++ with DirectX rendering You can still use Win32 APIs in Metro Apps, but only a subset of the Win32 APIs.

42 C++ and XAML

43 Resources “Using Windows Runtime With C++” – Herb Sutter
“Building Windows Metro style apps in C++/XAML” – Vikas Bhatia, Joanna Mason

44 Belgian C++ Users Group (BeCPP)
Aim? quarterly meetings Audience? free for everyone Where? at Microsoft offices in Zaventem or at sponsor company locations (we’re still looking for hosts / sponsors!) Become a member? Announcements? become member or subscribe to our blog Questions?

45 Belgian C++ Users Group (BeCPP)
Next meeting: June 27th 2012 Agenda with top international speakers: Session 1: What’s new in VC (Rong Lu) Break Session 2: C++ AMP (GPGPU Computing) (Kate Gregory) Drink Register Now:

46

47 Thank you! I would like to thank Herb Sutter from Microsoft for his permission to base this presentation on one that he wrote.

48 Widescreen Test Pattern (16:9)
Aspect Ratio Test (Should appear circular) 4x3 16x9


Download ppt "Windows 8 and WinRT from a C++ perspective"

Similar presentations


Ads by Google