Using std or Boost library to bind class member in MQL 4

 
Hello,

I read that it should be possible with std::bind or the c++ Boost library to bind a function pointer to a class method. Unfortunately I couldn't find any examples of this and I can't do it without help.

Suppose I have two classes and I want to bind a callback from class A to an instance of class b. 
How could I achieve this with std::bind or Boost in MQL 4?

Any help is greatly appreciated, best regards 
holger

typedef void (*TCallback)(string & words[]); 

class A 
{
        private:

                TCallback _callback;

        public:

                void SetCallback(TCallback callback) { _callback = callback; }

                void TestCallback()
                {
                        string words[];
                        ArrayResize(words, 2);
                        words[0] = "hello";
                        words[1] = "world";

                        _callback(words);
                }       
};

class B
{
        private:

                A * _a;

        public:

                B()
                {
                        _a = new A();
                        _a.SetCallback(<< How to bind here to this.Callback ? >>);
                }

                void Callback(string & symbols[])
                {
                        Print("Callback called");
                }

                void TestCallback()
                {
                        _a.TestCallback();
                }
};


(new B()).TestCallback();
 
Holger David Julian Krause:
Hello,

I read that it should be possible with std::bind or the c++ Boost library to bind a function pointer to a class method. Unfortunately I couldn't find any examples of this and I can't do it without help.

Suppose I have two classes and I want to bind a callback from class A to an instance of class b. 
How could I achieve this with std::bind or Boost in MQL 4?

Any help is greatly appreciated, best regards 
holger

You cannot do it like you showed. - std::bind is C/C++ and not available in MQL4/5. - You cannot use Boost library, because macros in MQL are more limited than in C/C++

Function pointers in MQL4/5 can only hold pointers to functions which are not members of a class, struct or union. - You need to define a "normal" function and assign it to your f-pointer variable. - Only known way in MQL4/5 up to now.

EDIT:

For what you have shown and are trying to achieve, I suggest using inheritance instead of callback-pointers. - If you could describe more precisely what you are trying to achieve, there might actually be a better way to do it.

 
Dominik Egert #:

You cannot do it like you showed. - std::bind is C/C++ and not available in MQL4/5. - You cannot use Boost library, because macros in MQL are more limited than in C/C++

Function pointers in MQL4/5 can only hold pointers to functions which are not members of a class, struct or union. - You need to define a "normal" function and assign it to your f-pointer variable. - Only known way in MQL4/5 up to now.

EDIT:

For what you have shown and are trying to achieve, I suggest using inheritance instead of callback-pointers. - If you could describe more precisely what you are trying to achieve, there might actually be a better way to do it.

Hello,

thank you very much for the explanation. I would like to try to use MQL more Event-Driven, but the possibilities seem to be very limited.
For example, I would like to have a class that monitors the market watch and informs subscribers via callback when the selected symbols change.
I had also tried an interface-based approach, but since classes in MQL can only implement a single interface (or class), that's also inappropriate in my opinion.

The last thing I could think of was a message broker where class instances could subscribe to specific topics... ?

I'm more from the C# corner and MQL still gives me a lot of headaches :)

 
Holger David Julian Krause #:

Hello,

thank you very much for the explanation. I would like to try to use MQL more Event-Driven, but the possibilities seem to be very limited.
For example, I would like to have a class that monitors the market watch and informs subscribers via callback when the selected symbols change.
I had also tried an interface-based approach, but since classes in MQL can only implement a single interface (or class), that's also inappropriate in my opinion.

The last thing I could think of was a message broker where class instances could subscribe to specific topics... ?

I'm more from the C# corner and MQL still gives me a lot of headaches :)

OK, got it.

I would go as following:


class __base_subscriber;

class __registration
{
        protected:
        __base_subscriber    subscribers[];

        __registration() {};

        const bool broadcast_message(const string filter_detail)
        { 
                for(int cnt; ) 
                { subscribers[cnt].broadcast_update(filter_detail); }
        };      

        const bool register_subscriber(__base_subscriber* p_new)
        { 
                //... [some array managing implementation] ...
                subscribers[ptr] = p_new;
        };
};

class market_watcher : protected __registration
{
        public:
        market_watcher()
        { };

        void _OnTickUpdate()
        {
                // ... [ some condition evaluation] ...
                const bool result = broadcast_message(_Symbol);
        };
};

class __base_subscriber
{
        protected:
        __base_subscriber()
        { };

        virtual void broadcast_update(const string filter_detail) = 0;
};

class my_receiving_client : protected __base_subscriber
{
        public: 
        my_receiving_client()
        { };

        virtual void broadcast_update(const string filter_detail)
        {
                // ... [ process the new details] ...

                return;
        };
};
I hope you get the idea.
 
Holger David Julian Krause #:

Hello,

thank you very much for the explanation. I would like to try to use MQL more Event-Driven, but the possibilities seem to be very limited.
For example, I would like to have a class that monitors the market watch and informs subscribers via callback when the selected symbols change.
I had also tried an interface-based approach, but since classes in MQL can only implement a single interface (or class), that's also inappropriate in my opinion.

The last thing I could think of was a message broker where class instances could subscribe to specific topics... ?

I'm more from the C# corner and MQL still gives me a lot of headaches :)

You need to use the Observer pattern, no need for callback function. There are tons of example on Internet, and Dominik gives you an example.
 
Dominik Egert #:

OK, got it.

I would go as following:


I hope you get the idea.
Yeah, thank you!