Passing a typedef method as pointer function

Rinaldi Segecin

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.

Salco

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing typedef function pointer as an argument problem

From Dev

passing a pointer to a class method as a function argument

From Dev

Passing a member function pointer as an argument to a template method

From Dev

Passing a pointer to a typedef'ed array

From

Typedef function pointer?

From Dev

typedef and pointer to function in C

From Dev

C function pointer and typedef

From Dev

Typedef function vs function pointer

From Dev

pointer to function to structure, pointer to function to typedef

From Dev

Passing an array of 'typedef struct' to a function

From Dev

Function pointer IAR ( typedef void )

From Dev

How to typedef template function pointer?

From Dev

Passing an instance method to an API that expects a C function pointer

From Dev

Passing this pointer and arguments of class method to local lambda function at compile time

From Dev

Trouble passing a function pointer to a method from main in C++

From Dev

Passing a pointer to a list, to a function

From Dev

Passing pointer to a function callback

From Dev

Passing pointer or array to function

From Dev

Passing a pointer as a function argument

From Dev

Passing a pointer to a function

From Dev

Passing pointer to function

From Dev

Passing function pointer in Dlang

From Dev

Passing state to function pointer

From Dev

Passing global pointer to function

From Dev

C - Passing pointer to function

From Dev

TypeDef with Function Pointer: Function does not Exist

From Dev

C++; class method pointer; lambda; passing lambda as member function pointer;

From Dev

Passing pointer to an object in BeginThread method

From Dev

Calling a method, and passing in a pointer to class

Related Related

HotTag

Archive