I have been trying to sort this one for a day now - but can't quite get there...
Part1: passing a function to a "function caller" (functor ?) - ok
Part2: calling a class member function by pointer - ok
Part3: passing a class member function to a "function caller" similar to Part1 - dismal fail.
class System {
public:
typedef void(System::*testFunction1) (void) ;
typedef void(System::*testFunction2) (uint8_t L_value) ;
typedef bool(System::*testFunction3) (void) ;
typedef bool(System::*testFunction4) (uint8_t L_value) ;
virtual void testFuncA() {
Serial.print(__FUNCTION__) ; Serial.println(" was called") ;
}
virtual void testFuncB(uint8_t L_value) {
Serial.print(__FUNCTION__) ; Serial.println(" was called") ;
Serial.print("I received:") ; Serial.println(L_value) ;
}
virtual bool testFuncC() {
Serial.print(__FUNCTION__) ; Serial.println(" was called") ;
return true ;
}
virtual bool testFuncD(uint8_t L_value) {
Serial.print(__FUNCTION__) ; Serial.println(" was called") ;
Serial.print("I received:") ; Serial.println(L_value) ;
return true ;
}
System() : _member(1)
{
Serial.print("Class instatiated:"); Serial.println(__FUNCTION__) ;
}
private:
uint8_t _member ;
} ;
// note: function is overloaded.
void function() {
Serial.print(__FUNCTION__) ; Serial.println(" was called") ;
}
void function(uint8_t L_value) {
Serial.print(__FUNCTION__) ; Serial.println(" was called") ;
Serial.print("I received:") ; Serial.println(L_value) ;
}
// note: functionCaller is overloaded.
void functionCaller( void(*P_function) (void) ) {
Serial.print(__FUNCTION__) ; Serial.println(" was called") ;
Serial.println("I am going to call a function") ;
(*P_function) () ;
}
void functionCaller( void(*P_function) (uint8_t), uint8_t L_value ) {
Serial.print(__FUNCTION__) ; Serial.println(" was called") ;
Serial.println("I am going to call a function") ;
(*P_function) (L_value) ;
}
void classFunctionCaller( System(*P_function) (void) ) { // <--- compiles - but not sure if correct.
Serial.print(__FUNCTION__) ; Serial.println(" was called") ;
Serial.println("I am going to call a function") ;
(*P_function) () ;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//*********************** Basic Function Caller ***********************************
Serial.print("\n\n\n\n");
Serial.println("*** Using functionCaller ***") ;
functionCaller(function) ;
functionCaller(function, 10) ;
//*********************** Calling class members ***********************************
Serial.print("\n\n\n\n");
Serial.println("*** Using Class ***") ;
System* P_system = new System() ;
System::testFunction1 P_function1 = &System::testFuncA ;
System::testFunction2 P_function2 = &System::testFuncB ;
System::testFunction3 P_function3 = &System::testFuncC ;
System::testFunction4 P_function4 = &System::testFuncD ;
(P_system->*P_function1) () ;
(P_system->*P_function2) (2) ;
bool returnValue ;
returnValue = (P_system->*P_function3) () ;
Serial.print("and I returned:"); Serial.println(returnValue) ;
returnValue = (P_system->*P_function4) (2) ;
Serial.print("and I returned:"); Serial.println(returnValue);
//*********************************************************************************
Serial.print("\n\n\n\n");
/*
classFunctionCaller(P_function1 () ) ; <--- not even close to correct!
*/
}
void loop() {
}
Output:
11:57:33.439 ->
11:57:33.439 ->
11:57:33.439 ->
11:57:33.439 ->
11:57:33.439 -> *** Using functionCaller ***
11:57:33.439 -> functionCaller was called
11:57:33.439 -> I am going to call a function
11:57:33.439 -> function was called
11:57:33.439 -> functionCaller was called
11:57:33.439 -> I am going to call a function
11:57:33.473 -> function was called
11:57:33.473 -> I received:10
11:57:33.473 ->
11:57:33.473 ->
11:57:33.473 ->
11:57:33.473 ->
11:57:33.473 -> *** Using Class ***
11:57:33.473 -> Class instatiated:System
11:57:33.473 -> testFuncA was called
11:57:33.473 -> testFuncB was called
11:57:33.473 -> I received:2
11:57:33.473 -> testFuncC was called
11:57:33.473 -> and I returned:1
11:57:33.473 -> testFuncD was called
11:57:33.473 -> I received:2
11:57:33.473 -> and I returned:1
11:57:33.473 ->
11:57:33.473 ->
11:57:33.473 ->
11:57:33.473 ->