eric mittelette – microsoft france [email protected] eric vernié – microsoft france...

22
Eric Mittelette – Microsoft France [email protected] Eric Vernié – Microsoft France [email protected]

Upload: devereux-millot

Post on 03-Apr-2015

120 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

Eric Mittelette – Microsoft [email protected]

Eric Vernié – Microsoft [email protected]

Page 2: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

Introduction : Pourquoi le C++/CLI ? C++/CLI : Eléments Syntaxiques C++/CLI : Garbage collection et

destruction déterministe ? C++/CLI : Code hybride ou comment

mixer les codes natifs et managés Conclusion : C++/CLI « first class langage

for .NET » Bonus : restez jusqu’à la fin

Page 3: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

Une brève histoire du C++ On arrive du B en passant par le C On arrive a du C avec classe (79) C++ avec Héritage et virtuel (88) Template « Generic Programming » (91) ANSI-C++ et ISO-C++ (96-98) « Dynamic Component Programming » C++/CLI

(2005)

Page 4: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

19981998 Visual C++ 6.0Visual C++ 6.0 ATL3/MFC42 (6.0)ATL3/MFC42 (6.0)

20022002 Visual Studio .NETVisual Studio .NET

Visual C++ 7.0Visual C++ 7.0

Unified Visual Studio IDE & DebuggerUnified Visual Studio IDE & Debugger

MFC7/ATL7, attributed programming, ATL ServerMFC7/ATL7, attributed programming, ATL Server

Whole Program OptimizationWhole Program Optimization

C++ Managed ExtensionsC++ Managed Extensions

Fixed STL concurrency and DLL issuesFixed STL concurrency and DLL issues

/GS Runtime Security Check/GS Runtime Security Check

20032003 Visual Studio 2003Visual Studio 2003

Visual C++ 7.1Visual C++ 7.1

ISO Standard C++ Conformance ISO Standard C++ Conformance

/Arch:SSE/SSE2 – floating point code generation/Arch:SSE/SSE2 – floating point code generation

Windows Forms Designer for Managed ExtensionsWindows Forms Designer for Managed Extensions

20052005 Visual Studio 2005Visual Studio 2005

Visual C++ 8.0Visual C++ 8.0

C++/CLI LanguageC++/CLI Language

Integrated 64-bit Compiler and ToolsIntegrated 64-bit Compiler and Tools

Profile Guided OptimizationProfile Guided Optimization

Safe Extension for CRTSafe Extension for CRT

Project Property Manager, Source Navigation/BrowsingProject Property Manager, Source Navigation/Browsing

MFC/Windows Forms IntegrationMFC/Windows Forms Integration

Page 5: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

Ré utiliser les compétences « fines/pointues » des développeurs C++

On ne va pas ré écrire des millions de lignes de code pour une nouvelle plateforme : .NET « We love your C++ Code »

Syntaxe déposée à l’ECMA et ISO Travail initié en 2002 avec les MC++ (Managed

Extension for C++) Intégration de C++/CLI avec ISO-C++ « LE » language de l’interopérabilité

Utiliser le Framework .NET de façon naturelle en C++

Page 6: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

Une option de compilation « primordiale » /CLR : émettre du code MSIL Support de la syntaxe CLI « activée » Option pour le projet ou pour un fichier

individuellement Différentes sous option à /CLR

/CLR : Pure /CLR : Safe

Page 7: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

Notion d’ “adjective class”Notion d’ “adjective class”

Types C++ et CLR cohérentsTypes C++ et CLR cohérents

Attention : pas d ’héritage multiple de « ref class »Attention : pas d ’héritage multiple de « ref class »Attention : implémentation multiple d’ « interface Attention : implémentation multiple d’ « interface class »class »

ref class A abstract { }; // abstractref class B sealed : A { }; // no further derivation ref class C : B { }; // error, B is sealed

ref class A abstract { }; // abstractref class B sealed : A { }; // no further derivation ref class C : B { }; // error, B is sealed

class N { /*…*/ }; // nativeref class R { /*…*/ }; // CLR reference type

value class V { /*…*/ }; // CLR value typeinterface class I { /*…*/ }; // CLR interface type

enum class E { /*…*/ }; // CLR enumeration type

class N { /*…*/ }; // nativeref class R { /*…*/ }; // CLR reference type

value class V { /*…*/ }; // CLR value typeinterface class I { /*…*/ }; // CLR interface type

enum class E { /*…*/ }; // CLR enumeration type

Page 8: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

Pointers et HandlesPointers et Handles

Sur le tas natifSur le tas natif

Comme d’hab pointeurs (*) stables indépendant du GCComme d’hab pointeurs (*) stables indépendant du GC

Comme d’hab, attention aux fuites mémoire si pas de Comme d’hab, attention aux fuites mémoire si pas de delete delete

Sur le tas managéSur le tas managé

Handles (^) sont des références d’ objetsHandles (^) sont des références d’ objets

Pas d’opérations arithmétiques dessus (cf. interior_ptr)Pas d’opérations arithmétiques dessus (cf. interior_ptr)

delete devient optionneldelete devient optionnel

Sur la pile (vraiment ?)Sur la pile (vraiment ?)

Permet des destructions déterministesPermet des destructions déterministes

TNatif* t1 = new TNatif;TNatif* t1 = new TNatif;

TNet^ t2 = gcnew TNet;TNet^ t2 = gcnew TNet;

T t3;T t3;

Page 9: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

* est à ^ ce que & est à %* est à ^ ce que & est à %

Page 10: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

PropriétéPropriété

ref class R { int mySize;public: property int Size { int get() { return mySize; } void set(int val) { mySize = val; } }};

R r;r.Size = 42; // use like a field

ref class R { int mySize;public: property int Size { int get() { return mySize; } void set(int val) { mySize = val; } }};

R r;r.Size = 42; // use like a field

ref class R {public: property int Size; // compiler-generated}; // get, set, and backing store

ref class R {public: property int Size; // compiler-generated}; // get, set, and backing store

Notation simplifiéeNotation simplifiée

Page 11: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

Delegate et EventDelegate et Event

delegate void D(int);

ref class R {public: event D^ e; // trivial event; // compiler-generated members

void f() { e(42); } // invoke it};

R^ r = gcnew R();r->e += gcnew D(this, &SomeMethod);r->e += gcnew D(SomeFreeFunction);r->f();

delegate void D(int);

ref class R {public: event D^ e; // trivial event; // compiler-generated members

void f() { e(42); } // invoke it};

R^ r = gcnew R();r->e += gcnew D(this, &SomeMethod);r->e += gcnew D(SomeFreeFunction);r->f();

Vous pouvez écrire, ajouter, enlever Vous pouvez écrire, ajouter, enlever et déclencher vous même les events.et déclencher vous même les events.

Page 12: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

GenericGenericSyntaxe des Generics ~comme celle des templatesSyntaxe des Generics ~comme celle des templates

Admettent des contraintes et l’héritageAdmettent des contraintes et l’héritage

Instancié à l’exécutionInstancié à l’exécution

TemplateTemplateDisponible pour le C++/CLIDisponible pour le C++/CLI

Généré au moment de la compilation Généré au moment de la compilation

Les generics et les templates peuvent se Les generics et les templates peuvent se combiner…combiner…

Attention casse tête chinois et rigueur demandée ! Attention casse tête chinois et rigueur demandée !

generic<typename T>where T : IFooref class GR { /* … */ };

generic<typename T>where T : IFooref class GR { /* … */ };

Page 13: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

CLR arraysCLR arrays Notation [] non supportée en C++/CLINotation [] non supportée en C++/CLI

array<int>^ a1 = gcnew array<int>(10);array<int>^ a2 = gcnew array<int> { 1, 2, 3 };array<int>^ a3 = { 1, 2, 3 };array<int>^ a4 = gcnew array<int>(10) { 1, 2 };

F(gcnew array<Component> { button1, button2 });

array<int, 2> a5 = gcnew array<int>(10, 20);array<int, 2> a6 = { { 1, 2, 3 }, { 4, 5, 6 } };

Page 14: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

Chaque objet .Net est sous contrôle du « Garbage Collector » La mécanique du « garbage collector » est un

pré requis essentiel à tous développeurs C++/CLI

Génération, Collection, Compactage, Pattern IDispose et Finalisation

Heap Managed > 3 private heap + 1 private heap pour les

« Large Object » Elément syntaxique

~MaClasse et !MaClasse

Page 15: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

Une classe qui implémente un Finalizer Une classe qui implémente un Finalizer doit avoir un destructeur doit avoir un destructeur

Evitons la duplication de code : appelons Evitons la duplication de code : appelons le finalizer dans le destructeurle finalizer dans le destructeurref class R { /* MORE CODE */

!R() { /* Clean-up value type resources */ }

~R() { /* Do early clean-up, clean-up ref classes */ this->!R(); }};

Page 16: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com
Page 17: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

C++/CLI : le roi de l’interopérabilité COM Interop PInvoke

Pour tous les languages .NET C++ Interop

It Just Work ! Tous les scénarii possible…et imaginable

Hosting de la CLR Contrôle fin de l’exécution du code .NET

Par exemple dans votre code MFC…

Page 18: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

Ré utiliser votre existant Migration code VC6

Breaking changes (conformité à la norme ISO98) Un seul environnement de développement

(VS2005) Compilateur 64 bits, Optimisation du code (WPO

PGO), support de l’OpenMP …. Et bien plus encore Utilisation des .lib ou dll depuis VS2005

Et inversement ! Gestion du code mixte

Passer des données, des pointeurs de fonctions, faire des appels « croisés » entre le monde managé et le monde natif

Page 19: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

Scénarii de code hybride Apps VC6 utilisent vos .lib VS2005 Apps VS2005 utilise vos .lib VC6

Code Hybride (mixed code) Utiliser une classe native dans du code CLI Utiliser une classe CLI dans du code natif Utiliser de l’assembleur dans du code CLI Utiliser la BCL dans du code natif Utiliser les « Windows Forms » dans les MFC .NETmodule et .Obj + cl.exe !

Hosting de la CLR.

Page 20: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

La boite à outils du développeur hybride vcclr.h : gcroot<>, auto_gcroot<> AfxWindowsForms.h (MAKE_DELEGATE… /CLR et ses déclinaisons System::Runtime::Interopservices pin_ptr<>, interior_ptr<> tblimp, tlbexp dllimport attributes Les APIs de hosting #pragma unmanaged TBC…

Page 21: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

Productivité du développeur accru par la BCL

Confort du développeur via l’IDE VS2005 Performance au rendez vous-même dans

le code managé pur et le code mixte C# killer

Ecrire moins de code en C++/CLI qu’en C#!

Page 22: Eric Mittelette – Microsoft France ericmitt@microsoft.com Eric Vernié – Microsoft France ericv@microsoft.com

Preview ORCAS STL / CLR