Конверсия управляемых языков в неуправляемые

21
Dmitri Nesteruk JetBrains

Upload: platonov-sergey

Post on 21-Aug-2015

373 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Конверсия управляемых языков в неуправляемые

Dmitri NesterukJetBrains

Page 2: Конверсия управляемых языков в неуправляемые

Managed language = C#, Java, D etc.GC (garbage collector), Unicode, …

Unmanaged languageSomething that compiles to native codeMore control, less safetyC/C++, VHDL, etc.

Page 3: Конверсия управляемых языков в неуправляемые

PerformanceStop-the-world GCLow-level control (e.g., SIMD)Collections (containers), multithreading

Defense against reverse-engineeringC#, Java are easy to decompile

Hardware supportCUDA, Xeon Phi

Portability (surprise!)Sudden desire to write everything in С++

Page 4: Конверсия управляемых языков в неуправляемые

Lex the source codeBuild ASTResolve symbolsTraverse the tree, translating it to a different languagePerfect translation is (of course) impossible

Page 5: Конверсия управляемых языков в неуправляемые

Translate syntax with fidelityFill the gaps in what’s missing

Replace equivalent constructsList<> vector<>

Handle cases where 1-to-1 mapping is impossible

Page 6: Конверсия управляемых языков в неуправляемые

Integral typesUse types from <cstdint>int int32_t, byte uint8_t, etc.

float/double as-isExtended precision types (System.Decimal)

Need external libs

Slightly different init rules

Page 7: Конверсия управляемых языков в неуправляемые

Unicode Most LoB apps do not absolutely require Unicode (nice to have)

Relatively safe option: string std::wstringchar wchar_tSlightly more careless option: string/char

A completely safe solution has to allow strings to be nullptrI.e., you either use char* or option<string>Expensive, tedious, just use myString.empty()

Unicode in source (identifiers, literals, …) won’t work

Page 8: Конверсия управляемых языков в неуправляемые

class Person{string name;int age;

}

class Person { std::string name;int32_t age;

public:Person();

};

Page 9: Конверсия управляемых языков в неуправляемые

Managed languagesGive types default valuesAllow initialization right in the declaration

In both cases, initialization happens in the (generated) ctorIn C++, types don’t have default values. So we are forced to make a constructor ourselves

Page 10: Конверсия управляемых языков в неуправляемые

Property = field+getter+setterCan be read or write-onlyCan be automatic

Name of field not specified explicitlyOffers no benefit over a field in C++

Can have an inline initializerMore ctor abuse

Two options hereGetXxx/SetXxx (safe, tedious)__declspec(property)(not C++ standard, but nicer)

int age;public int Age{get { return age; }set { age = value; }

}

// automaticpublic int Age{ get; set; }

Page 11: Конверсия управляемых языков в неуправляемые

__declspec(property(get = GetAge, put = PutAge)) int Age;

int GetAge() const { return age; }

void PutAge(int value) { … }

// laterPerson p;p.Age = 123; // calls PutAge()

Page 12: Конверсия управляемых языков в неуправляемые

Used in many places, e.g.:Handling UI interactionsProperty change notifications

C++ doesn’t have themBoost.SignalsTricky since event subscription is done with +=

And unsubscription with −=

Page 13: Конверсия управляемых языков в неуправляемые

template <typename T> class INotifyPropertyChanged{public:

signal<void(const T*, string)> PropertyChanged;

};

Page 14: Конверсия управляемых языков в неуправляемые

class Player : public INotifyPropertyChanged<Player>{int age;

public:__declspec(property(get = GetAge, put = PutAge)) int Age;int GetAge() const { return age; }void PutAge(int value){if (value == age) return;PropertyChanged(this, "Age"); // oops, a string! (macro?)age = value;

}

Page 15: Конверсия управляемых языков в неуправляемые

“The big problem”GC non-GC translationStore everything as shared_ptr

Replace every `new` with make_shared

Detecting use-cases for make_unique very difficultCircular references (weak_ptr<>?)

Page 16: Конверсия управляемых языков в неуправляемые

References require #includesNeed a list of typeheader for STL etc.

Circular references may need forward declarationsHeader groupings (at namespace or folder level)

Very tricky internal consistency (topological sort)Also a good idea for IDEs

Page 17: Конверсия управляемых языков в неуправляемые

Type translation is (relatively) easyint int32_tList<T> vector<T>

Function call translation is harderList.Add() vector.emplace_back(), but…Value vs ref. stuff

Page 18: Конверсия управляемых языков в неуправляемые

We need two convertersOne for .H filesAnother for .CPP files (implementation)

The option of doing everything inline isn’t considered

There are situations where 100% inlining doesn’t work

Page 19: Конверсия управляемых языков в неуправляемые
Page 20: Конверсия управляемых языков в неуправляемые

Naming conventionClean Unicode handlingAutomated API mapping

Page 21: Конверсия управляемых языков в неуправляемые

[email protected]@dnesterukCome to the JB booth! (R#C++, CLion)