Questions on OOP in MQL5 - page 61

 
Alexey Navoykov:
On the other hand, for serialization, it is probably better to explicitly specify text field names, so that you can then freely rename them in code without fear that the stored files will stop working.

I'm asking about the workaround structure, the implementation itself is not a big deal ))))

it's not a problem to name the fields, the problem is to think, what to do afterwards - will json be just a method in each class, or a static class or generally duplicate class fields in the form of text....

;)


ZS:

thought of some well-established bravado - "yes I have code well in general in C++ from MQL ported without change here and there!!!! "

of course this is needed, but in terms of time spent on development of such a code structure, it is quite expensive, and code support will be needed with new wishes

only advantages of the connection C++ (+++ MQL ) are not clear, syntax constructs are 90% same, why duplicated? imho interesting other languages with extended syntax and compiler possibilities of control in IDE while writing the project - it is at least fast and comfortable, not to mention the ready developments in the public domain, the same C# and Python

 
Igor Makanu:

only the advantages of the C++ bundle (+++ MQL) are not clear

C++ has much more possibilities. Plus a billion different libraries.

 
Koldun Zloy:

C++ has many more features. Plus a billion different libraries.

indisputably

but no matter how hard you try, you won't be able to open any C header file in MQL and use a ready-made C++ solution to connect a million of libraries

I googled it earlier this year and tried to connect TensorFlow to MQL having C++ API.

 
Igor Makanu:

undoubtedly

But no matter how hard you try, you can't take any C header file, open it in MQL and use a ready-made solution from C++ to connect a million libraries

I googled it earlier this year and tried to connect TensorFlow to MQL having C++ API - you have to write a viper

You asked about the C++ and MQL pairing. I understood that C++ is used in DLL. How else could it be?

 
Koldun Zloy:

You asked about the link between C++ and MQL. I understood that C++ is used in the DLL. How else could it be?

I askedhttps://www.mql5.com/ru/forum/307970/page10#comment_11652222

otherwise, well .... a trick that allows you to open the header file c_api.h (I have it attached) in MQL and then use the TensorFlow docs

now you open the header file in VS, then wrap the calls, model, internal states... ...into your .dll and then you can use it in MQL


all this is time consuming


Igor Makanu:

that's why I'm asking about the workaround structure, the implementation itself is an afterthought )))

maybe it's realistic to serialize simple structures in json using MQL toolshttps://habr.com/ru/post/311262/

at least it's reassuring to know that the material is available on the web

Files:
c_api.zip  17 kb
 
Igor Makanu:

Uh-huh... I'll try asking again:

HELP ME AND NEED AN EXAMPLE! Please!

It's just something I wrote on my knees, just to think about it.

#define  COMMA (text==NULL?NULL:",\n")
#define  PUSH(dVal) Push(#dVal,dVal)

class CJSon{
   string text;
public:
   CJSon():text(NULL){}
   CJSon* Push(string key,string value) {text+=COMMA+"\""+key+"\":\""+value+"\""; return &this;}
   CJSon* Push(string key,long value)   {text+=COMMA+"\""+key+"\":"+(string)value; return &this;}
   CJSon* Push(string key,double value) {text+=COMMA+"\""+key+"\":"+(string)value; return &this;}
   string Finish()   {return text="{\n"+text+"\n}";}
};

#undef  COMMA

void OnStart(){
   int a=56;
   double b=0.369;
   string text="Example";
   string json=(CJSon()).PUSH(a).PUSH(b).PUSH(text).Finish();
   Print(json);
 } 
 
Vladimir Simakov:

Written on my knees, just to think about it.

Thanks, but it's not working (((

//+------------------------------------------------------------------+
static string _json;
template<typename T>class CJSon{
#define  COMMA (_json==NULL?NULL:",\n")
#define  PUSH(dVal) Push(#dVal,dVal)
public:
   CJSon() {_json = "";}
   CJSon* Push(string key,string value) {_json+=COMMA+"\""+key+"\":\""+value+"\""; return &this;}
   CJSon* Push(string key,long value)   {_json+=COMMA+"\""+key+"\":"+(string)value; return &this;}
   CJSon* Push(string key,double value) {_json+=COMMA+"\""+key+"\":"+(string)value; return &this;}
   static string Finish()   {return _json="{\n"+_json+"\n}";}
};
//+------------------------------------------------------------------+
class A
{
   public:
   static long a1;
};
long A::a1;
//+------------------------------------------------------------------+
void OnStart()
{
   CJSon<A> a;
   A::a1 = 10;
   
   a.PUSH(A::a1);
   string s = a.Finish() ;
   
   PRINT(s);
}
//+------------------------------------------------------------------+

2020.05.20 23:51:43.932 tst____ (EURUSD,H1) s = {

2020.05.20 23:51:43.932 tst____ (EURUSD,H1) ,

2020.05.20 23:51:43.932 tst____ (EURUSD,H1) "A::a1":10

2020.05.20 23:51:43.932 tst____ (EURUSD,H1) }

on top of that, it only wants to work with statics

although it's not so hard to collect a string in one method with defines, in general, you need to think about it

 
Igor Makanu:

here's askinghttps://www.mql5.com/ru/forum/307970/page10#comment_11652222

otherwise, well .... some trick that allows to open header file c_api.h in MQL and then use TensorFlow docs

now you open the header file in VS, then wrap the calls, model, internal states... ...into your .dll and then you can use it in MQL


all in all it's time consuming

No tricks allowing to compile C++ source with MQL compiler.

TensorFlow library exports a lot of functions. You probably only need a small fraction of them.

Write prototypes of the functions you are using. It won't take long.


 
Vladimir Simakov:

Written on my knees, just to think about it.

Finally, something very interesting. Is this the "Steam Train" pattern?

 
Igor Makanu:

Thanks, but it didn't stick (((

2020.05.20 23:51:43.932 tst____ (EURUSD,H1) s = {

2020.05.20 23:51:43.932 tst____ (EURUSD,H1) ,

2020.05.20 23:51:43.932 tst____ (EURUSD,H1) "A::a1":10

2020.05.20 23:51:43.932 tst____ (EURUSD,H1) }

on top of that, it only wants to work with statics

Although it's not so hard to collect a string in one method with defines, you need to think about it

Somehow you're doing it wrong)

#define  COMMA (text==NULL?"{\n":",\n")
#define  VALUE(dVal) Push(#dVal,dVal)
#define  STRUCT(dVal) PushStruct(#dVal,dVal.JSon())

class CJSon{
   string text;
public:
   CJSon():text(NULL){}
   CJSon* Push(string key,string value) {text+=COMMA+"\""+key+"\":\""+value+"\""; return &this;}
   CJSon* PushStruct(string key,string value) {text+=COMMA+"\""+key+"\":"+value; return &this;}
   CJSon* Push(string key,long value)   {text+=COMMA+"\""+key+"\":"+(string)value; return &this;}
   CJSon* Push(string key,double value) {text+=COMMA+"\""+key+"\":"+(string)value; return &this;}
   string Finish()   {return text+="\n}";}
};

#undef  COMMA

class _CTest{
   int a;
   double b;
   string t;
public:
   _CTest(int _a,double _b,string _t):a(_a),b(_b),t(_t){}
   string JSon()  {return (CJSon()).VALUE(a).VALUE(b).VALUE(t).Finish();}
};

class CTest{
   _CTest test;
   int a;
   long b;
public:
   CTest():test(10,0.369,"Hi"),a(555),b(LONG_MIN){}
   string JSon() {return (CJSon()).STRUCT(test).VALUE(a).VALUE(b).Finish();}
};

void OnStart(){
   CTest test;
   Print(test.JSon());
 } 
PS Corrected
Reason: