Metro style apps
View                          Metro style Apps                        Desktop Apps

                              XAML                      HTML / CSS
Controller




                        C                C#             JavaScript
 Model




                                                                      HTML         C      C#
                       C++               VB              (Chakra)     JavaScrip
                                                                          t       C++     VB
                                    WinRT APIs
  System Services




                    Communication      Graphics &         Devices &
                       & Data            Media             Printing

                                    Application Model                 Internet            .NET
                                                                      Explorer
                                                                                  Win32    / SL
  Core




                                     Windows Core OS Services
The Sweet Spot for C++…
  Power,
    Performance
      and Portability.
XAML
Demo
Language Basics
1.   Person^ p = nullptr;
2.   {
3.       Person^ newP = ref new Person();   //   refcount   =   1
4.       newP->Name = “John”;               //   refcount   =   1
5.       p = newP;                          //   refcount   =   2
6.   }                                      //   refcount   =   1
7.   p = nullptr;                           //   refcount   =   0; ~Person()
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 &    ^                 Strong pointer (“hat” or “handle”)
Reference       %                 Strong reference
More Language
1. delegate void CarouselInitDoneHandler(IUIAnimationVariable^ rotation);
2. void CarouselAnimation::Initialize(double angle, CarouselInitDoneHandler^ callback) {
3.      // Create Animation Manager
4.      using namespace Engine::Graphics;
5.      UIAnimationManager animationManager;
6.      // Create rotation animation variable
7.      IUIAnimationVariable^ rotation = animationManager.CreateAnimationVariable(angle);
8.      // Set the event handler for the story board
9.      rotation->GetCurrentStoryBoard()->SetStoryBoardEventHandler(
10.                 ref new CarouselStoryBoardHandler(this, CarouselAnimation::StoryBoard)
11.              );
12.      // Invoke the callback when done
13.      callback(rotation);
14. }
public ref class Person {
    public:    Person(String^ name, String^ email);
               void Greet(Person^ other);
    internal: ~Person();
               void SetPassword(const std::wstring& passwd);
};




Person^ p = ref new Person(“John Surname”);
p->Greet(ref new Person(“Jim Surname”);
public interface class IAnimal {   public interface class IFeline :
    void Play();                   IAnimal {
};                                     void Scratch();
                                   };



IAnimal^ animal = ref new Cat();   ref class Cat : IFeline {
animal->Play();                        public: virtual void Play();
                                                  virtual void Scratch();
                                   };
public ref class DatabaseConnection {
    public: ~ DatabaseConnection();
};


{
    DatabaseConnection db();
    db.SetName( “Employees”);
    // …
    // … lots of queries, updates, etc. …
    // …
}        // ~DatabaseConnection()
public:   property String^ Name;

public:  property Person^ Sibling {
           Person^ get() { InitSiblings(); return _sibling; }
           void set(Person^ value) { _sibling = value; NotifySibling(); }
         }
private: Person^ _sibling;



      Person^ p = ref new Person(“John”);
      p->Sibling = ref new Person(p->Name);
public delegate void PropertyChanged( String^ propName, String^ propValue );



  auto p = ref new PropertyChanged(
        [](String^ pn, String^ pv) {
              cout << pn << ” = “ << pv;
        } );

  auto p = ref new PropertyChanged( UIPropertyChanged );

  auto p = ref new PropertyChanged( this, MainPage::OnPropertyChanged );


          p( “Visible”, false );
public: event PropertyChanged^ OnPropertyChanged;

public: event PropertyChanged^ OnNetworkChanged {
     EventRegistrationToken add(PropertyChanged^);
     void remove(EventRegistrationToken t);
     void raise(String^, String^);
}


person->OnPropertyChanged += propertyChangedDelegate;
auto token = person->OnPropertyChanged::add(propertyChangedDelegate);

    person->OnPropertyChanged -= token;
    person->OnPropertyChanged::remove(token);
HRESULT               Exception
                                                E_OUTOFMEMORY         OutOfMemoryException
                                                E_INVALIDARG          InvalidArgumentException
                                                E_NOINTERFACE         InvalidCastException
                                                E_POINTER             NullReferenceException
                                                E_NOTIMPL             NotImplementedException
                                                E_ACCESSDENIED        AccessDeniedException
throw ref new InvalidArgumentException();       E_FAIL                FailureException
throw ref new COMException(E_*);                E_BOUNDS              OutOfBoundsException
                                                E_CHANGED_STATE       ChangedStateException
                                                REGDB_E_CLASSNOTREG   ClassNotRegisteredException

try { … } catch (OutOfMemoryException^ ex) E_DISCONNECTED
                                            { … }
                                           E_ABORT
                                                                      DisconnectedException
                                                                      OperationCanceledException
                       ex->HResult

• catch (Platform::Exception^)
generic<typename T, typename U>               ref class PairStringUri:
public interface class IPair {                      IPair<String^, Uri^> {
   property T First;                          public:
   property U Second;                            property String^ First;
};                                               property Uri^ Second;
                                              };


IPair<String^, Uri^>^ uri = GetUri();
auto first = uri->First; // type is String^
auto second = uri->Second; // type is Uri^
template<typename T, typename U>
ref class Pair: IPair<T, U>
{
public:
  property T First;
  property U Second;

  Pair(T first, U second) {   First = first; Second = second; }
};

IPair<String^, Uri^>^ pair = ref new Pair<String^, Uri^>(
       “//BUILD/”, ref new Uri(“http://www.buildwindows.com”));
#using <Company.Component.winmd>




     public
private partial ref class MainPage: UserControl, IComponentConnector
{
public:
    void InitializeComponent();
    void Connect() { btn1->Click += ref new EventHandler(this, &MainPage::Button_Click); }
};



ref class MainPage
{
public:
    MainPage() { InitializeComponent(); }
    void Button_Click(Object^ sender, RoutedEventArgs^ e);
};
Libraries
using namespace Platform;
      Vector<String^>^ items = ref new Vector<String^>();



      items->Append(“Hello”);

•   Returning a read-only view of the vector
      IVectorView<String^>^ GetItems () {
          return items->GetView();
      }



                 items->VectorChanged +=
                        ref new VectorChangedEventHandler<String^>
                                (this, &MyClass::VectorChanged);
using namespace Platform;
Map<String^, Uri^> favorites = ref new Map<String^, Uri^>();



favorites->Insert(“MSDN”, ref new Uri(“http://msdn.com”));



if (favorites->HasKey(“MSDN”))
    favorites->Remove(“MSDN”);
Begin()/End()

                                   std::vector<int> v;
          begin()    end()         v.push_back(10);
                                   auto items = ref new Vector<int>(v);
IVector<int>^ v = GetItems();
int sum = 0;                       Vector<int>^ items = …;
std::for_each( begin(v), end(v),   std::vector<int> v = to_vector(items);
   [&sum](int element) {
      sum += element;
   } );
Building your first Windows Metro style app
  Roadmap for creating Metro style apps
  Tour of the IDE
• XAML designer
  C++ Reference for Windows Runtime
  Windows Runtime Components in C++
  Asynchronous Programming in C++ Using PPL
(Since 10th of January 2012)



                                    http://BeCPP.org/

  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!)
  Announcements?               subscribe to our blog to stay up-to-date
  Questions?                   BeCPP@BeCPP.org
                               or visit the TechDays MEET Corner during breaks
MICROSOFTC++




                                                 2012
PARTICIPATE IN C++
                                     MICROSOFT
                                     DEVELOPER
                                     DIVISION
DEVELOPMENT USER                     DESIGN
                                     RESEARCH
RESEARCH
        SIGN UP ONLINE AT
        http://bit.ly/cppdeveloper
http://aka.ms/mbl-win8




                   http://aka.ms/mbl-win8/build
                             http://aka.ms/mbl-win8/devprev
                                         http://aka.ms/mbl-win8/store
Using the Windows 8 Runtime from C++
Using the Windows 8 Runtime from C++

Using the Windows 8 Runtime from C++

  • 4.
  • 6.
    View Metro style Apps Desktop Apps XAML HTML / CSS Controller C C# JavaScript Model HTML C C# C++ VB (Chakra) JavaScrip t C++ VB WinRT APIs System Services Communication Graphics & Devices & & Data Media Printing Application Model Internet .NET Explorer Win32 / SL Core Windows Core OS Services
  • 7.
    The Sweet Spotfor C++… Power, Performance and Portability.
  • 9.
  • 11.
  • 12.
  • 15.
    1. Person^ p = nullptr; 2. { 3. Person^ newP = ref new Person(); // refcount = 1 4. newP->Name = “John”; // refcount = 1 5. p = newP; // refcount = 2 6. } // refcount = 1 7. p = nullptr; // refcount = 0; ~Person()
  • 16.
    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 & ^ Strong pointer (“hat” or “handle”) Reference % Strong reference
  • 18.
  • 19.
    1. delegate voidCarouselInitDoneHandler(IUIAnimationVariable^ rotation); 2. void CarouselAnimation::Initialize(double angle, CarouselInitDoneHandler^ callback) { 3. // Create Animation Manager 4. using namespace Engine::Graphics; 5. UIAnimationManager animationManager; 6. // Create rotation animation variable 7. IUIAnimationVariable^ rotation = animationManager.CreateAnimationVariable(angle); 8. // Set the event handler for the story board 9. rotation->GetCurrentStoryBoard()->SetStoryBoardEventHandler( 10. ref new CarouselStoryBoardHandler(this, CarouselAnimation::StoryBoard) 11. ); 12. // Invoke the callback when done 13. callback(rotation); 14. }
  • 20.
    public ref classPerson { public: Person(String^ name, String^ email); void Greet(Person^ other); internal: ~Person(); void SetPassword(const std::wstring& passwd); }; Person^ p = ref new Person(“John Surname”); p->Greet(ref new Person(“Jim Surname”);
  • 21.
    public interface classIAnimal { public interface class IFeline : void Play(); IAnimal { }; void Scratch(); }; IAnimal^ animal = ref new Cat(); ref class Cat : IFeline { animal->Play(); public: virtual void Play(); virtual void Scratch(); };
  • 22.
    public ref classDatabaseConnection { public: ~ DatabaseConnection(); }; { DatabaseConnection db(); db.SetName( “Employees”); // … // … lots of queries, updates, etc. … // … } // ~DatabaseConnection()
  • 23.
    public: property String^ Name; public: property Person^ Sibling { Person^ get() { InitSiblings(); return _sibling; } void set(Person^ value) { _sibling = value; NotifySibling(); } } private: Person^ _sibling; Person^ p = ref new Person(“John”); p->Sibling = ref new Person(p->Name);
  • 24.
    public delegate voidPropertyChanged( String^ propName, String^ propValue ); auto p = ref new PropertyChanged( [](String^ pn, String^ pv) { cout << pn << ” = “ << pv; } ); auto p = ref new PropertyChanged( UIPropertyChanged ); auto p = ref new PropertyChanged( this, MainPage::OnPropertyChanged ); p( “Visible”, false );
  • 25.
    public: event PropertyChanged^OnPropertyChanged; public: event PropertyChanged^ OnNetworkChanged { EventRegistrationToken add(PropertyChanged^); void remove(EventRegistrationToken t); void raise(String^, String^); } person->OnPropertyChanged += propertyChangedDelegate; auto token = person->OnPropertyChanged::add(propertyChangedDelegate); person->OnPropertyChanged -= token; person->OnPropertyChanged::remove(token);
  • 26.
    HRESULT Exception E_OUTOFMEMORY OutOfMemoryException E_INVALIDARG InvalidArgumentException E_NOINTERFACE InvalidCastException E_POINTER NullReferenceException E_NOTIMPL NotImplementedException E_ACCESSDENIED AccessDeniedException throw ref new InvalidArgumentException(); E_FAIL FailureException throw ref new COMException(E_*); E_BOUNDS OutOfBoundsException E_CHANGED_STATE ChangedStateException REGDB_E_CLASSNOTREG ClassNotRegisteredException try { … } catch (OutOfMemoryException^ ex) E_DISCONNECTED { … } E_ABORT DisconnectedException OperationCanceledException ex->HResult • catch (Platform::Exception^)
  • 27.
    generic<typename T, typenameU> ref class PairStringUri: public interface class IPair { IPair<String^, Uri^> { property T First; public: property U Second; property String^ First; }; property Uri^ Second; }; IPair<String^, Uri^>^ uri = GetUri(); auto first = uri->First; // type is String^ auto second = uri->Second; // type is Uri^
  • 28.
    template<typename T, typenameU> ref class Pair: IPair<T, U> { public: property T First; property U Second; Pair(T first, U second) { First = first; Second = second; } }; IPair<String^, Uri^>^ pair = ref new Pair<String^, Uri^>( “//BUILD/”, ref new Uri(“http://www.buildwindows.com”));
  • 29.
  • 30.
    private partial refclass MainPage: UserControl, IComponentConnector { public: void InitializeComponent(); void Connect() { btn1->Click += ref new EventHandler(this, &MainPage::Button_Click); } }; ref class MainPage { public: MainPage() { InitializeComponent(); } void Button_Click(Object^ sender, RoutedEventArgs^ e); };
  • 31.
  • 32.
    using namespace Platform; Vector<String^>^ items = ref new Vector<String^>(); items->Append(“Hello”); • Returning a read-only view of the vector IVectorView<String^>^ GetItems () { return items->GetView(); } items->VectorChanged += ref new VectorChangedEventHandler<String^> (this, &MyClass::VectorChanged);
  • 33.
    using namespace Platform; Map<String^,Uri^> favorites = ref new Map<String^, Uri^>(); favorites->Insert(“MSDN”, ref new Uri(“http://msdn.com”)); if (favorites->HasKey(“MSDN”)) favorites->Remove(“MSDN”);
  • 34.
    Begin()/End() std::vector<int> v; begin() end() v.push_back(10); auto items = ref new Vector<int>(v); IVector<int>^ v = GetItems(); int sum = 0; Vector<int>^ items = …; std::for_each( begin(v), end(v), std::vector<int> v = to_vector(items); [&sum](int element) { sum += element; } );
  • 36.
    Building your firstWindows Metro style app Roadmap for creating Metro style apps Tour of the IDE • XAML designer C++ Reference for Windows Runtime Windows Runtime Components in C++ Asynchronous Programming in C++ Using PPL
  • 37.
    (Since 10th ofJanuary 2012) http://BeCPP.org/ 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!) Announcements? subscribe to our blog to stay up-to-date Questions? BeCPP@BeCPP.org or visit the TechDays MEET Corner during breaks
  • 38.
    MICROSOFTC++ 2012 PARTICIPATE IN C++ MICROSOFT DEVELOPER DIVISION DEVELOPMENT USER DESIGN RESEARCH RESEARCH SIGN UP ONLINE AT http://bit.ly/cppdeveloper
  • 39.
    http://aka.ms/mbl-win8 http://aka.ms/mbl-win8/build http://aka.ms/mbl-win8/devprev http://aka.ms/mbl-win8/store

Editor's Notes