|
|
|
#include "callbackmanager.h" |
|
|
|
|
|
// https://community.element14.com/products/devtools/software/f/forum/54719/c-callbacks-and-templates |
|
|
|
|
|
#include <cstdio> |
|
#include "callbackmanager.h" |
|
#include <string> |
|
|
|
class MyClass { |
|
public: |
|
inline int handler(const int& num1, const int& num2) const { |
|
Serial.println("class member function callback "); |
|
return num1 + num2; |
|
} |
|
static inline int staticHandler(const int& num1, const int& num2) { |
|
Serial.println("static class member function callback "); |
|
return num1 + num2; |
|
} |
|
}; |
|
|
|
int functionHandler(const int& num1, const int& num2) { |
|
Serial.println("classic C function callback "); |
|
return num1 + num2; |
|
} |
|
|
|
void setup() { |
|
|
|
char msg[50]; |
|
|
|
Serial.begin(9600); |
|
delay(4000); // enough time to open the Serial Monitor :) |
|
Serial.println("starting up"); |
|
|
|
int a = 4; |
|
int b = 5; |
|
|
|
{ // scenario: call object method |
|
MyClass myClass; |
|
|
|
Callback<int, const int&, const int&> cb; |
|
// Use a lambda to capture myClass and call the object method |
|
cb.set([&myClass](const int& num1, const int& num2) -> int { |
|
return myClass.handler(num1, num2); |
|
}); |
|
|
|
int o = cb.call(a, b); |
|
sprintf(msg, "Value: %i\n", o); |
|
Serial.print(msg); |
|
} |
|
|
|
{ // scenario: call static method |
|
Callback<int, const int&, const int&> cb; |
|
// Use a lambda to call the static method |
|
cb.set([](const int& num1, const int& num2) -> int { |
|
return MyClass::staticHandler(num1, num2); |
|
}); |
|
|
|
int o = cb.call(a, b); |
|
sprintf(msg, "Value: %i\n", o); |
|
Serial.print(msg); |
|
} |
|
|
|
{ // scenario: call C function |
|
Callback<int, const int&, const int&> cb; |
|
// Use a lambda to call the classic C function |
|
cb.set([](const int& num1, const int& num2) -> int { |
|
return functionHandler(num1, num2); |
|
}); |
|
|
|
int o = cb.call(a, b); |
|
sprintf(msg, "Value: %i\n", o); |
|
Serial.print(msg); |
|
} |
|
|
|
{ // scenario: call pure lambda |
|
Callback<int, const int&, const int&> cb; |
|
// Use a lambda to execute anonymous C code |
|
cb.set([](const int& num1, const int& num2) -> int { |
|
Serial.println("lambda int function callback "); |
|
return num1 + num2; |
|
}); |
|
|
|
int o = cb.call(a, b); |
|
sprintf(msg, "Value: %i\n", o); |
|
Serial.print(msg); |
|
} |
|
|
|
{ // scenario: return a bool |
|
Callback<bool, const int&, const int&> cb; |
|
// Use a lambda to execute anonymous C code |
|
cb.set([&msg](const int& num1, const int& num2) -> bool { |
|
Serial.println("lambda bool function callback "); |
|
return num1 == num2; |
|
}); |
|
sprintf(msg, "Value: %s\n", cb.call(a, b) ? "true" : "false"); |
|
Serial.print(msg); |
|
} |
|
|
|
{ // scenario: use void |
|
Callback<void, const int&, const int&> cb; |
|
// Use a lambda to execute anonymous C code |
|
cb.set([&msg](const int& num1, const int& num2) { |
|
Serial.println("lambda void function callback "); |
|
sprintf(msg, "Value a: %i, value b: %i\n", num1, num2); |
|
Serial.print(msg); |
|
return; |
|
}); |
|
cb.call(a, b); |
|
} |
|
|
|
{ // scenario: use void, and no attributes |
|
Callback<void> cb; |
|
// Use a lambda to execute anonymous C code |
|
cb.set([]() { |
|
Serial.println("lambda void function with no parameters callback "); |
|
Serial.println("hello "); |
|
return; |
|
}); |
|
cb.call(); |
|
} |
|
|
|
/* |
|
{ // scenario: use an unsupported (non-fundamental) type for return value R |
|
// this will generate a compile error |
|
Callback<std::string, const int&, const int&> cb; |
|
} |
|
*/ |
|
|
|
} |
|
|
|
#include <limits.h> |
|
void loop() { |
|
delay(ULONG_MAX); |
|
} |