# Во имя Бога Милостивого, Милосердного!!! # language: ru Функциональность: Преобразование в C++ Сценарий: Пустой пакет * Исходник: ``` package test ``` * Результат: ``` void loop() {} void setup() {} ``` Сценарий: Пустая структура - класс * Исходник: ``` package test type device struct {} ``` * Результат: ``` class device { public: }; ``` Сценарий: Интерфейс - это класс с виртуальными методами * Исходник: ``` package test type Device interface { Name() string String() string Some(*Thing) string Add(int) Add2(int,float64) } ``` * Результат: ``` class Device { public: virtual std::string Name() = 0; virtual std::string String() = 0; virtual std::string Some(Thing*) = 0; virtual void Add(int) = 0; virtual void Add2(int,double) = 0; }; ``` Сценарий: Наследование * Исходник: ``` package test type Device interface { } type DeviceClass struct { _baseInterface Device } ``` * Результат: ``` class Device { public: }; class DeviceClass: public Device { public: Device* _baseInterface; }; ``` Сценарий: Класс: свойство типа интерфейс - указатель * Исходник: ``` package test type Device interface { } type DeviceClass struct { } type system struct { d Device dc DeviceClass dcp *DeviceClass } ``` * Результат: ``` class Device { public: }; class DeviceClass { public: }; class system { public: Device* d; DeviceClass* dc; DeviceClass* dcp; }; ``` Сценарий: Структура с полем * Исходник: ``` package test type device struct { a int } ``` * Результат: ``` class device { public: int a; }; ``` Сценарий: Структура с полями одного типа * Исходник: ``` package test type device struct { a,b int } ``` * Результат: ``` class device { public: int a,b; }; ``` Сценарий: Структура с неск полями, строки - std::string * Исходник: ``` package test type device struct { a,b int c,d,e string g [GPIO_count]bool } ``` * Результат: ``` class device { public: int a,b; std::string c,d,e; bool g[GPIO_count]; }; ``` Сценарий: Структура с методом * Исходник: ``` package test type device struct { a int } func (d *device) doSomething(a *int) { } ``` * Результат: ``` class device { public: int a; void doSomething(int* a); }; void device::doSomething(int* a) { } ``` Сценарий: Структура с деструктором Close: вызов memchecker.DeletePointer() меняется на delete this; * Исходник: ``` package test type device struct { } func (d *device) Close() { memchecker.DeletePointer(d) } ``` * Результат: ``` class device { public: void Close(); }; void device::Close() { delete this; } ``` Сценарий: Структура с вызовом метода * Исходник: ``` package test type device struct { a int } func (d *device) doSomething() { d.doSomethingElse() } func (d *device) doSomethingElse() { } ``` * Результат: ``` class device { public: int a; void doSomething(); void doSomethingElse(); }; void device::doSomething() { this->doSomethingElse(); } void device::doSomethingElse() { } ``` Сценарий: Структура с вызовом метода другого объекта * Исходник: ``` package test type device struct { remote Other } func (d *device) doSomething() { d.remote.doSomethingElse() } ``` * Результат: ``` class device { public: Other remote; void doSomething(); }; void device::doSomething() { this->remote->doSomethingElse(); } ``` Сценарий: Вызов вложенных методов динамически созданного объекта * Исходник: ``` package test type device struct { } func (d *device) doSomething() { n := NewOther() n.G.x() n.G.x.x2.x3() } func main() { n := NewOther() n.G.x() n.G.x.x2.x3() } ``` * Результат: ``` class device { public: void doSomething(); }; void main(); void device::doSomething() { auto n=NewOther(); n->G->x(); n->G->x->x2->x3(); } void main() { auto n=NewOther(); n->G->x(); n->G->x->x2->x3(); } ``` Сценарий: Структура с вызовом методов со входящими и выходящими параметрами * Исходник: ``` package test type device struct { a int } func (d *device) doSomething(i int, x string) { d.doSomethingElse(1, 0.5) d.doSomethingElse(i, 0.6) } func (d *device) doSomethingElse(i int, f float64) int { return 1 } func (d *device) doSomethingBool(i int, f float64) bool { return true } ``` * Результат: ``` class device { public: int a; void doSomething(int i,std::string x); int doSomethingElse(int i,double f); bool doSomethingBool(int i,double f); }; void device::doSomething(int i,std::string x) { this->doSomethingElse(1,0.5); this->doSomethingElse(i,0.6); } int device::doSomethingElse(int i,double f) { return 1; } bool device::doSomethingBool(int i,double f) { return true; } ``` Сценарий: Структура с изменением свойства * Исходник: ``` package test type device struct { x int y int } func (d *device) doSomething() { d.x = 1 d.x = d.y } ``` * Результат: ``` class device { public: int x; int y; void doSomething(); }; void device::doSomething() { this->x=1; this->x=this->y; } ``` Сценарий: Структура с вызовом метода и свойства другого объекта * Исходник: ``` package test type device struct { x int } func (d *device) doSomething() { dev2.doSomethingElse() } func (d *device) doSomethingElse() { dev2.x = 1 d.x = dev2.x } ``` * Результат: ``` class device { public: int x; void doSomething(); void doSomethingElse(); }; void device::doSomething() { dev2->doSomethingElse(); } void device::doSomethingElse() { dev2->x=1; this->x=dev2->x; } ``` Сценарий: Структура с объявлением переменных-указателей и их созданием * Исходник: ``` package test var ( dev1, dev2 *device ) type device struct {} func main() { dev1 = &device{} } ``` * Результат: ``` class device { public: }; void main(); device *dev1,*dev2; void main() { dev1=new device(); } ``` Сценарий: Структура с объявлением переменных-указателей и обычным Go конструктором * Исходник: ``` package test var ( dev1, dev2 *Device ) type Device struct {} func NewDevice() *Device { return &Device{} } func main() { dev1 = NewDevice() } ``` * Результат: ``` class Device { public: }; Device* NewDevice(); void main(); Device *dev1,*dev2; Device* NewDevice() { return new Device(); } void main() { dev1=NewDevice(); } ``` Сценарий: Последовательность блоков: инклюды, константы, типы, абстрактные классы/классы, определения функций, переменные, методы классов, функции * Исходник: ``` package test import wifi "some/wifi" func someFunc() {} var a = 1 var b = 1 type device struct {} func (d *device) doSomething() {} type Device interface { Name() string } type Mera int const c1 = 4 ``` * Результат: ``` #include const int c1 = 4; typedef int Mera; class device { public: void doSomething(); }; class Device { public: virtual std::string Name() = 0; }; void someFunc(); int a = 1; int b = 1; void device::doSomething() { } void someFunc() { } ``` Сценарий: Конвертация "while" в "for" * Исходник: ``` package test import wifi "github.com/andygeiss/esp32/api/controller/wifi" func Setup() { serial.Begin(serial.BaudRate115200) wifi.BeginEncrypted("SSID", "PASS") for wifi.Status() != wifi.StatusConnected { serial.Println("Connecting ...") } serial.Println("Connected!") } func Loop() {} ``` * Результат: ``` #include void setup(); void loop(); void setup() { Serial.begin(115200); WiFi.begin("SSID","PASS"); while(WiFi.status()!=WL_CONNECTED) { Serial.println("Connecting ..."); } Serial.println("Connected!"); } void loop() {} ``` Сценарий: Преобразование fmt.Sprint() -> std::to_string() * Исходник: ``` package test type device struct { } func (d *device) doSomething() { a := "" a += fmt.Sprint(5) a += "A " + fmt.Sprint(19) + " A " + fmt.Sprint(a) } func Setup() { a := "" a += fmt.Sprint(5) a += "A " + fmt.Sprint(19) + " A " + fmt.Sprint(a) } ``` * Результат: ``` class device { public: void doSomething(); }; void setup(); void device::doSomething() { std::string a=""; a+=std::to_string(5); a+="A "+std::to_string(19)+" A "+std::to_string(a); } void setup() { std::string a=""; a+=std::to_string(5); a+="A "+std::to_string(19)+" A "+std::to_string(a); } ```