Passing typedef method as a pointer function

I am trying to pass a method as a pointer function, so I created a binding as shown here , but as the method is defined, I cannot pass it as a parameter to the binder. The function I need to pass the method pointer is related to the Regex Lua template library for arduino, found here .

void InterpreterClass::init()
{
    MatchState ms("255.255.255.255");

    bind_regex_member<InterpreterClass, &InterpreterClass::MatchAddressCallback, 0> b(this);
    ms.GlobalMatch("(%d%d?%d?)", b);
}

void InterpreterClass::MatchAddressCallback(const char * match, const unsigned int length, const MatchState & ms)
{
  //do something
}

at the ms.GlobalMatchsecond parameter is the method that I want to execute after interpreting the string, the problem is that the function must obey a certain sequence of parameters, for example, if it has been delegated.

typedef void (*GlobalMatchCallback)   (const char * match,          // matching string (not null-terminated)
                                   const unsigned int length,   // length of matching string
                                   const MatchState & ms);      // MatchState in use (to get captures)

I tried to implement a binder with all declared parameters, and a type name was also declared with it. The bolt follows the binder:

template<class T, void(T::*PTR)(const char *, const unsigned int, const MatchState &), size_t I>
struct bind_regex_member
{
    typedef void(*fn_type)(const char *, const unsigned int, const MatchState &);
    explicit bind_regex_member(const T* _ptr)
    {
        ptr = _ptr;
    }
    static void func(const char * match, const unsigned int length, const MatchState & ms)
    {
        (ptr->*PTR)(match, length, ms);
    }
    operator fn_type()
    {
        return &func;
    }
private:
    static const T*  ptr;
};

template<class T, void(T::*PTR)(const char *, const unsigned int, const MatchState &), size_t I>
const T* bind_regex_member<T, PTR, I>::ptr = NULL;

, :

Error: `Interpreter.cpp:7:80: error: could not convert template argument ‘&InterpreterClass::MatchAddressCallback’ to ‘void (InterpreterClass::*)(const char*, unsigned int, const MatchState&)’`

GlobalMatchCallback . MatchAddressCallback?

: https://github.com/rsegecin/RegexArduino.git.

PS: , .

+4
1

, ptr bind_regex_member const T InterpreterClass:: MatchAddressCallback const. :

InterpreterClass i;
const InterpreterClass* myPtr = &i;
MatchState myMs;
myPtr->MatchAddressCallback("", 0, myMs); // OUCH! myPtr points to const T and MatchAddressCallback is non const member function

ptr bind_regex_member, !

EDIT: Interpreter.h :

class Interpreter
{
public:
    void init();
    GlobalMatchCallback MatchAddressCallback; // <----------- HERE
};

, . "" Interpreter.h :

#ifndef _INTERPRETER_h
#define _INTERPRETER_h

#include <Arduino.h>
#include "Regexp.h"

template<class T, void(T::*PTR)(const char *, const unsigned int, const MatchState &), size_t I>
struct bind_regex_member
{
    typedef void(*fn_type)(const char *, const unsigned int, const MatchState &);
    explicit bind_regex_member(T* _ptr)
    {
        ptr = _ptr;
    }
    static void func(const char * match, const unsigned int length, const MatchState & ms)
    {
        (ptr->*PTR)(match, length, ms);
    }
    operator fn_type()
    {
        return &func;
    }
private:
    static T*  ptr;
};

template<class T,

void(T::*PTR)(const char *, const unsigned int, const MatchState &), size_t  I>
T* bind_regex_member<T, PTR, I>::ptr = NULL;

class InterpreterClass
{
 public:
    void init();

    void MatchAddressCallback(const char * match, const unsigned int length, const MatchState & ms);
};

extern InterpreterClass Interpreter;

#endif
+4

Source: https://habr.com/ru/post/1668071/


All Articles