powerpoint presentation › ... › edm › images › keynote2_visualstudio2012_se… · •...
TRANSCRIPT
Optimized for Windows Store
ref class ref new ^
collection.h
PPL Tasks Library
property
event
delegate
partial class
Defining public ref class Person {
public:
Person(String^ name);
void Greet(Person^ other);
private:
void SetPassword(const std::wstring& passwd);
std::wstring pwd;
};
Using Person^ p = ref new Person(“John Surname”);
p->Greet(ref new Person(“Jim Surname”);
• Public methods restricted to WinRT typed
parameters
• Private/internal methods can use any legal C++
type parameters
• Handle (^) is a pointer to a Windows Runtime object for
which the compiler performs automatic reference
counting
• ref new instantiates or activates a Windows Runtime class
Person^ p; { Person^ p2 = ref new Person(); // refcount = 1 p2->Name = “John”; // refcount = 1 p = p2; // refcount = 2 } // refcount = 1 p = nullptr; // refcount = 0; ~Person()
8. String^ str6(str1); 9. String^ str7 = str2; // using wchar_t’s 10.wchar_t msg[] = L"Test"; 11.String^ str8 = ref new
String(msg); // using wstring’s 12.std::wstring wstr1(L"Test"); 13.String^ str9 = ref new
String(wstr1.c_str()); //init wstring from String^ 14.std::wstring wstr(str1->Data());
• Platform::String^ as a boundary type
• Immutable
• Interop with std::wstring and wchar_t*
1. using namespace Platform; 2. #include <string>
// init String^ from literal 3. String^ str1 = "Test"; // no need for L 4. String^ str2("Test"); 5. String^ str3 = L"Test"; 6. String^ str4(L"Test"); 7. String^ str5 = ref new
String(L"Test");
Vector Map
VectorView MapView
IVector
IVectorView
IObservable
Vector
IMap
IMapView
IObservable
Map
IIterable
IIterator
Windows Runtime
Interfaces
Concrete collection
types for C++
Producing and Consuming
1. #include <vector>
2. #include <algorithm>
3. #include <utility>
4. #include <collection.h>
5. namespace WFC = Windows::Foundation::Collections;
6.
7. WFC::IVectorView<int>^ Producer() {
8. std::vector<int> v;
9. v.push_back(1);
10. v.push_back(2);
11.
12. return ref new Platform::VectorView<int>(std::move(v));
13. }
14.
15. int sum = 0;
16. WFC::IVectorView<int>^ v = Producer();
17. std::for_each(begin(v), end(v), [&sum](int i) { sum += i; });
• WinRT objects are
STL friendly
• WinRT collections
are STL friendly
• STL containers are
the backing
stores for C++
collections
PPL tasks simplify async Windows Runtime programming
StorageFolder^ item = KnownFolders::PicturesLibrary; StorageFileRetrievalOperation^ createStorageFileOp = item->CreateFileAsync("myfile.txt"); createStorageFileOp->Completed = ref new AsyncOperationCompletedHandler<StorageFile^>( [](IAsyncOperation<StorageFile^>^ asyncOp) { StorageFile^ storageFile = asyncOp->GetResults(); }); createStorageFileOp->Start();
StorageFolder^ item = KnownFolders::PicturesLibrary; task<StorageFile^> (item->CreateFileAsync("myfile.txt")).then([](StorageFile^ createdFile) {});
Without PPL tasks
With PPL tasks
• Properties: provide a flexible mechanism to read, write, or
compute the value of a private field
• Delegates: A type that safely encapsulates a method,
similar to a function pointer in C and C++.
• Events: enable a class or object to notify other classes or
objects when something of interest occurs
C++ WinRT Component
Extension SDKs
http://www.microsoft.com/visualstudio/en-us
http://blogs.msdn.com/b/jasonz/
http://www.facebook.com/visualstudio
http://twitter.com/#!/visualstudio
Somasegar’s Blog http://blogs.msdn.com/b/somasegar/