David Bednárek Jakub Yaghob Filip Zavoral http://data.ksi.ms.mff.cuni.cz/svn/nprg051pub/html/nprg051.html
Samostatný jazyk standardizovaný ECMA snaha o maximální kompatibilitu s C++ CLI Common Language Infrastructure standard ECMA - virtual machine - framework (libraries) CLR Common Language Runtime implementace VM CIL Common Intermediate Language jazyk interpretovaný VM MSIL MSIL :-) konkrétní MS implementace CIL CTS Common Type System jednotný typový systém CLI.Net Framework Mono managed code spravovaný.net frameworkem přístup k CLI (.Net) knihovnám snadná interoperabilita C#, F#, VisualBasic,... COBOL, Eiffel, Mercury... MS implementace nadmnožiny CLI multiplatformní implementace CLI http://www.mono-project.com
Source code VB Compiler C# Compiler C++ Compiler Unmanaged Component Managed code Assembly IL Code Assembly IL Code Assembly IL Code Common Language Runtime JIT Compiler Native Code Operating System Services
Services Description Discovery Protocols Caching Configuration System.Web UI HtmlControls WebControls Security SessionState System.Windows.Forms Design ComponentModel System.Drawing Drawing2D Printing Imaging Text System.Data System.Xml OleDb SqlClient XSLT Serialization Common SQLTypes XPath System Collections Configuration Diagnostics Globalization IO Net Reflection Resources Security ServiceProcess Text Threading Runtime InteropServices Remoting Serialization
Type Value Types Reference Types Built-In Value Types Self Describing Types Pointer Types Interface Types Enumerations User-Defined Value Types (structs) Class Types Arrays User-Defined Classes Boxed Value Types Delegates
Ve skutečnosti dva nezávislé typové systémy native original ISO C++ managed fuj - manažovaný - řízený CTS string vs. String, vector vs. array Garbage collection pouze CTS managed heap handle není to ukazatel může se měnit nové operátory: gcnew ^ % reference vs. value type class Native {... ref class Managed {... { Native* n = new N; Managed^ m = gcnew Managed; delete n; handle native heap managed heap
Typy hodnotové referenční data primitivní typy, malé kolekce složitější struktury umístění přímo v paměti (zásobník) vždy na [managed] heapu přístup přímo přes [tracking] handle přiřazení, parametry hodnotou odkazem dědičnost ne jednoduchá (více interfaces) copy constr ano ne default sémantika stack heap s.: boxing heap stack s.: autoalokace value struct B { int x; int main() { B b; b.x = 0; ref class A { int f(); int main() { A^ a = gcnew A; a->f();
ref class, ref struct nesmí obsahovat nemanaged struktury nemají automatický copy constructor a operator = jednoduchá dědičnost, vícenásobná dědičnost interface value class, value struct nepodporují dědičnost! enum class rozšíření enumu o několik metod - ToString value type interface class abstract class bez dat možnost vícenásobné dědičnosti array typované vícerozměrné pole generic
Referenční typy proměnné reference na data neinicilizované: nullptr přístup přes nullptr NullReferenceException přiřazení kopírování referencí, nikoliv dat není automaticky definován copy konstruktor konec existence poslední reference někdy garbage collection možná i zásobníková sémantika technicky ale stále heap, na konci platnosti destruktor ref class A { int f();... { A^ ra = gcnew A; A va; ra->f(); va.f(); ra va ra va ra destruktor va někdy možná GC a finalizer
Hodnotové typy proměnné vlastní data přiřazení kopírování dat nelze dědičnost boxing - alokace na heapu pozor na automatické konverze value struct B { int x; fnc( B^ b) { b->x = 2; rb vb: 1 rb: 1 rb vb: 1 b rb: 2 b: 2 { B^ rb = gcnew B; B vb; rb->x = vb.x = 1; fnc( rb); fnc( vb); boxing vb
handle T^ x = gcnew T; x->a, (*x).a unmanaged: * value class T; T a; a b tracking reference T% y = *x; T% y = var; y.a unmanaged: & T^ b = gcnew T; T^ c = %a; T% d = a; T% e = *b; T* f = &a; // T* g = b; pin_ptr<t> g = &*b; b c d e f a přímo nelze // T* h = &d; pin_ptr<t> h = &d; T& i = a; g h jen na objekty na zásobníku // T& j = *b; i technicky: ukazatel dovnitř managed objektu kopie do managed heapu!!
ISO C++ volá se metoda aktuálně konstruovaného podobjektu C++/CLI volá se metoda skutečného (vnějšího) objektu class A { A() : x(0) { x = f(); virtual int f() { return x + 1; int x; class B : public A { public: B() { x = f(); virtual int f() { return x + 2; A* b = new B; ref class C { public: C() : x(0) { x = f(); virtual int f() { return x + 1; int x; ref class D : C { public: D() { x = f(); virtual int f() override { return x + 2; C^ d = gcnew D; A() { A::f(), B() { B::f() x = 3 C() { D::f(), D() { D::f() x = 4
interface class IA { int f(); ref class A abstract : IA { public: A8() { x_ = 1; virtual int f() abstract; ~A8() { cout << "~";!A8() { cout << "!"; private: initonly int x_; ref class B8 sealed : A8 { public: virtual int f() override sealed {... rozhraní vícenásobná dědičnost rozhraní destruktor deterministicky finalizer - při GC nedeterministicky přiřazení pouze při inicializaci jen jednoduchá dědičnost tříd nepodporovány - default parametry - const metody ref class C { const int f(int x=1,int y=2);
ref class A { public: property int a; property int b { int get() { return x_; void set( int x) { x_ = x; property int c[int] { int get( int i) { return (x_>>i)&1; void set( int i, int x) { x_ ^= (x&1)<<i; property int default[int] { int get( int i)... private: int x_; A* a =... a->a = a->b; a->c[1] = a[2]; trivial property standard property indexed property default indexed property dědičnost virtual property read/write-only lze nadefinovat jen get/set static property přístup jen k statickým prvkům
Delegate ukazatel na funkci nebo metodu konkrétního objektu Event událost: zpráva odesílatel příjemci ref class MyEventArgs { delegate void MyDelegate( Object^ sender, MyEventArgs^ e); ref class WakeMeUp { void MyHandler( Object^ sender, MyEventArgs^ e) { static void StaticHandler( Object^ sender, MyEventArgs^ e) { deklarace eventu ref class EventSender { event MyDelegate^ Alarm; void OnAlarm( void) { Alarm( this, gcnew MyEventArgs); WakeMeUp^ w = gcnew WakeMeUp(); EventSender^ sender = gcnew EventSender(); sender->alarm += gcnew MyDelegate( w, &WakeMeUp::MyHandler); sender->alarm += gcnew MyDelegate( &WakeMeUp::StaticHandler); sender->onalarm(); sender->alarm -= gcnew MyDelegate( &WakeMeUp::StaticHandler); sender->onalarm(); typ delegáta handlery příjemce zavolání všech funkcí inicializace delegáta objektem a metodou statická metoda odebrání
pole built-in povinně referenční typ, vždy na heapu typované, pevně vícerozměrné, 0-based vícerozměrné indexy [i,j,k] iterace možná i přes iteratory, interior ptr a for each přiřazení reference, Array::Copy kopie array< String^>^ as = gcnew array<string^>(10); array< int, 2>^ a2d = gcnew array<int,2>(2,3); array< int, 2>^ a2d = gcnew array<int,2>(2,3) {{1,2,3,{4,5,6 array< int, 2>^ a2d = gcnew array<int,2> {{1,2,3,{4,5,6 array< int, 2>^ a2d = {{1,2,3,{4,5,6 int x = a2d[1,2]; interior_ptr<string^> is; for( is=&as[0]; is<=&as[9]; ++is)... *is... for each( String^ s in as)... s... IEnumerator^ es = as->getenumerator(); while( es->movenext())... es->current...
parametry součástí typu je typ prvků a dimenze, ne rozměry variabilní počet parametrů bezpečnější než va_arg kovariance pokud existuje konverze z A na B array<a> aa; array<b> bb; bb = aa; nepravidelné pole array<array<int> ^> ^jagged = gcnew array<array<int> ^>(4); for each (array<int> ^% ar in jagged) { int i = 5; ar = gcnew array<int>(++i); proměnný počet argumentů void f( array<int>^ a) {... vždy array^ int sum(... array<int>^ va) { int sum = 0; for each( int a in va) sum += a; return sum; int x = sum( 1, 2, 3, 4);
mnoho metod řada z nich přetížených AsReadOnly<T> BinarySearch Clear Clone ConstrainedCopy ConvertAll<TIn,TOut> Copy CopyTo CreateInstance Equals Exists<T> Find<T> FindAll<T> FindIndex<T> FindLast<T> FindLastIndex<T> ForEach<T> GetEnumerator GetHashCode GetLength GetLowerBound GetType GetUpperBound GetValue IndexOf Initialize LastIndexOf MemberwiseClone Resize<T> Reverse SetValue Sort ToString TrueForAll<T>
gumová pole ArrayList - polymorfní (Object^) absence typové kontroly pomalejší obecnější List - generic obrácené vlastnosti další CLI kontejnery using namespace System::Collections; ArrayList^ al = gcnew ArrayList(); al->add( 1); al->add( "hrusky"); al->add( 3.141); for each( Object^ o in al) Console::WriteLine( o); using namespace System::Collections::Generic; List<String^>^ sl = gcnew List<String^>; sl->add( "hrusky"); sl->add( "jine hrusky"); for each( String^ s in sl) Console::WriteLine( s);
String (CLI)!= string (STD) imutabilní jakákoliv operace vytváří novou instanci mutabilní StringBuilder všechny řetězce Unicode ( wchar_t) přiřazení = přiřazení referencí (mělká kopie) Copy - vytvoření kopie (hluboká kopie) porovnávání == (Equals) - porovnání hodnot ReferenceEquals - porovnání referencí konverze s = gcnew String( "Text"); String^ s = "Text"; s->chars[1] s = t + "."; String^ s = "123"; int x = Int32::Parse( s); int y = Convert::Parse( s); s = x.tostring(); string is = "isostring"; String^ ms = gcnew String( is.c_str()); is = (char*) Runtime::InteropServices::Marshal:: StringToHGlobalAnsi( ms).topointer(); pin_ptr<const wchar_t> ptr = PtrToStringChars( ms); is = static_cast<const wchar_t*>(ptr);
Šablony Templates řeší kompilátor výstup - varianty kódu pro různé typy kompilace jen použitých kombinací konstrukcí a typů Generika Generics řeší runtime výstup - jeden společný kód kompletní kompilace generic <typename T> ref class List { T Add( T t) {... co je tu zajímavého?... List<int> intl; intl.add( 1);
Šablony Generika Klíčové slovo template generic Kód pro každý použitý typ samostatný Vyhodnocování při kompilaci - normální typy společný runtime (JIT kompilace) Kompatibilita STL.Net, CTS, C#, VB Použití netypových parametrů ano ne Instance lišící se pouze typovým parametrem různé typy stejný typ Typové parametry cokoli normální typy nebo generika, nelze šablony Defaultní typový parameter ano ne Parciální a explicitní specializace ano ne Šablonové třídy jako typový parametr template<template<class T1> class T2> kontrola argumentů ne (zatím) constraints ano ne volání metod argumentů ano (kontrola při použití) odpovídající constraints
omezení množiny argumentů generik implementuje rozhraní / odvozená od jiné třídy gcnew constraint (default constr) value / reference type constraint kontrola při kompilaci interface class I { int f(); generic <typename T> where T : I, gcnew(), ref class ref class A { T t; int g() { return t.f(); ref class B : I { // A<int> ai; A<B^> ab; interface constraint implementuje interface reference constraint musí být referenční typ gcnew constraint musí mít default constr kontrola při překladu
System::Collections ArrayList BitArray DictionaryBase Hashtable SortedList Stack System::Collections::Generic Dictionary<TKey, TValue> HashSet<T> LinkedList<T> List<T> Queue<T> SortedDictionary<TKey, TValue> SortedList<TKey, TValue> SortedSet<T> Stack<T> SynchronizedCollection<T> SynchronizedKeyedCollection<K, T> SynchronizedReadOnlyCollection<T> polymorfní generické
metadata popisující vnější vlastnosti na mnoho konstrukcí - assembly, třídy, metody, parametry,... častá vazba na prostředí (.Net) metadata assembly [assembly:assemblytitleattribute("myproject")] [assembly:securitypermission( SecurityAction::RequestMinimum, UnmanagedCode=true)] hlášení kompilátoru [Serializable] ref class A { T t; [Obsolete("Do not use")] int g(); void f([out] String^% s); serializovatelnost do XML serializer->serialize(a) výstupní parametr není nutná inicializace (interoperabilita s C#)
Třída odvozená od System::Attribute přístup k datům pomocí reflexe definice vlastního atributu použití atributu přístup k hodnotám aplikovatelnost [AttributeUsageAttribute(AttributeTargets::Class)] public ref class OwnerAttribute : Attribute { property String^ Developer; [Owner(Developer="Ja Mocny")] ref class MyClass {... OwnerAttribute^ a = Attribute::GetCustomAttribute( MyClass::typeid, OwnerAttribute::typeid);... a->developer...
alternativa RTTI podpora.net jméno typu, vztahy dědičnosti, metody a properties, atributy,... using namespace System::Reflection; Type - přístup k typovým informacím Assembly^ ass = Assembly::LoadFrom("myass.dll"); array<type^>^ types = ass->gettypes(); for each( Type^ t in types) { WriteLine( t->tostring()); array<methodinfo^>^ methods = t->getmethods(); for each( MethodInfo^ m in methods) { WriteLine( "{1 {0", m->name, m->returntype->tostring()); array<parameterinfo^>^ params = t->getparameters();... Type^ t = String::typeid; t = obj->gettype(); t = Type::GetType("System::DateType");
aktivní operace vytváření typů z dll instanciace objektů vyvolávání metod Dynamické vytvoření typu a instance objektu Dynamické vyvolání metody Assembly^ ass = Assembly::LoadFrom("myass.dll"); Type^ t = ass->gettype("nejakytyp"); MethodInfo^ method = t->getmethod("metoda"); Object^ obj = Activator::CreateInstance( t); array<object^>^ paramlist =...; method->invoke( obj, paramlist); neshoda parametrů výjimka
.Net / C# like Console::WriteLine( "Hodnota {0 je {1", "jedna", 1); Console::WriteLine( "Hex {0:x8", 0x123456); String^ s; try { while(( s = Console::ReadLine()))!= nullptr) { int i = Int32::Parse( s); catch( FormatException^ e) {... StreamWriter^ sw = gcnew StreamWriter( "file.txt"); sw->writeline( "whatever"); sw->close();
C++/CLI C++ jak spojovat moduly a volat metody jak sdílet managed a native data nedělejte to - striktní rozhraní když už - speciální šablony C++/CLI C#,... jak spojovat moduly class library / add reference (.dll) jak volat metody class library jak sdílet data CTS
ref class A { int x_; A^ a = gcnew A; // int* y = &a->x_; pin_ptr<y> = &a->x_; *y = 99; interior_ptr<y> = &a->x_; *y = 99; připíchnutý ptr objekt se nehýbe vnitřní ptr do managed struktury hýbe se automaticky s objektem ref class A { int x_; class C { // A^ a; gcroot<a^> a; auto_gcroot<a^> b; int x() { return a->x_; { C c; umožní vkládat managed třídy do nativních nevolá ~A volá ~A při destrukci objektu
Project: C++ CLR Class Library namespace clilib { public ref class A { public: A() : x_(0) { int f() {... clilib.dll Project: C# Console App namespace App { class Program { static void Main(string[] args) { clilib.a a = new clilib.a(); int x = c.f(); Add Reference: clilib.dll
Klady interoperabilita -.Net, C#, CTS téměř plná kompatibilita s C++ managed code Zápory dva jazyky v jednom odlišné typové systémy podivná syntaxe a hlavně sémantika mnoho divných variant definovaných výčtem Use cases velký C++ projekt,.net služby (WPF) úzké rozhraní interoperabilita s C#, VB, F#, *#? first-choice language? zároveň požadavek na výkonnost a těsná integrace s GUI
Gordon Hogenson: C++/CLI - The Visual C++ Language for.net Marcus Heege: Expert C++/CLI -.Net for Visual C++ Programmers Nishant Shivakumar: C++/CLI in Action umět ke zkoušce zhruba se orientovat v problému