I've been trying to pass a method as a pointer function so I created a binder like is shown here but as the method is defined I'm unable to pass it as a parameter to the binder. The function that I need to pass the method pointer is from a Regex Lua Pattern 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 ms.GlobalMatch
the second parameter is the method I want to execute after the string is interpreted, the problem is that the function needs to obey a specific sequence of parameters like if it was "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 the binder with all parameters declared and with it's type name declared as well. Bellow 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;
The first error that the compiler shows is:
Error: `Interpreter.cpp:7:80: error: could not convert template argument ‘&InterpreterClass::MatchAddressCallback’ to ‘void (InterpreterClass::*)(const char*, unsigned int, const MatchState&)’`
Making a binder to GlobalMatchCallback
is not working either. What should I do to MatchAddressCallback
be called?
Project's minimal code repo: https://github.com/rsegecin/RegexArduino.git.
PS:I found very difficult to express myself with this type of problem so any feedback is welcomed.