2017-01-28 2 views
4

포인터 함수로 메서드를 전달하려고 했으므로 바인더를 만들었습니다. here이 표시되지만 메서드가 정의되면이 메서드를 매개 변수로 전달할 수 없습니다. 접합재. 메서드 포인터를 전달하는 데 필요한 함수는 arduino의 Regex Lua Pattern 라이브러리에서 찾았습니다 (here). ms.GlobalMatch 두번째 파라미터에포인터 함수로 typedef 메서드 전달

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 
} 

는 I 문자열 해석 된 후 수행 할 방법이 문제는 함수가 「위임」인 경우와 같은 파라미터들의 특정 순서를 따라야 할 필요가 있다는 것이다.

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) 

모든 매개 변수를 선언하고 형식 이름을 선언하여 바인더를 구현하려고했습니다.

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; 

컴파일러 도시 상기 제 1 에러는 :

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.

추신 : 나는 이런 종류의 문제로 자신을 표현하기가 매우 어려웠으므로 어떤 피드백도 환영합니다.

답변

4

여기서 문제는 bind_regex_member의 ptr이 const T를 가리키며 InterpreterClass :: MatchAddressCallback 메서드가 const가 아닌 것입니다. 이 같은 Basicly :

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 

bind_regex_member에 PTR에서 CONST를 제거하고 작업을해야합니다!

편집 : 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 
+0

당신이 말한하지만 난 여전히 BTW 나는 문제가 무엇인지 말했다 같은 오류 받고 있어요하지만 난 컴파일 침을 오류 무엇을 게시하는 것을 잊었다으로 내가 한 아웃. 오류 :'Interpreter.cpp : 7 : 80 : 오류 : 템플릿 인자 '& InterpreterClass :: MatchAddressCallback'을 'void (InterpreterClass :: *) (const char *, unsigned int, const MatchState &)'로 변환 할 수 없습니다 ' –

+1

대답! – Salco

+0

대단히 감사합니다 Salco. –