4

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.

Community
  • 1
  • 1
Rinaldi Segecin
  • 449
  • 8
  • 23

1 Answers1

4

The problem here is that ptr in bind_regex_member points to const T and your method InterpreterClass::MatchAddressCallback is non const. Basicly like this:

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

Remove the const from ptr in bind_regex_member and it should work!

EDIT: There is a second problem in Interpreter.h:

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

You cannot declare a method like this. The "final" Interpreter.h should look like this:

#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
Salco
  • 56
  • 4
  • I did as you told but I'm still getting the same error that btw I said what the problem was but I forgot to post what error the compile spit out. Error: `Interpreter.cpp:7:80: error: could not convert template argument ‘&InterpreterClass::MatchAddressCallback’ to ‘void (InterpreterClass::*)(const char*, unsigned int, const MatchState&)’` – Rinaldi Segecin Jan 28 '17 at 12:17