/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include <stdio.h>
using namespace std;
class CallbackInterface
{
public:
// The prefix "cbi" is to prevent naming clashes.
virtual int cbiCallbackFunction(int) = 0;
};
//------------------------------------------------------------------------
// "Caller" allows a callback to be connected. It will call that callback.
class SignalDetectorClass
{
public:
// Clients can connect their callback with this
void connectCallback(CallbackInterface *cb)
{
m_cb = cb;
}
// Test the callback to make sure it works.
void test()
{
printf("SignalDetectorClass::test() calling callback...\n");
int i = m_cb -> cbiCallbackFunction(10);
printf("Result (20): %d\n", i);
}
private:
// The callback provided by the client via connectCallback().
CallbackInterface *m_cb;
};
//------------------------------------------------------------------------
// "Callee" can provide a callback to Caller.
class Callee : public CallbackInterface
{
public:
// The callback function that Caller will call.
int cbiCallbackFunction(int i)
{
printf(" Callee::cbiCallbackFunction() inside callback\n");
return 2 * i;
}
};
//------------------------------------------------------------------------
// SIGNALduino.ino
int main()
{
SignalDetectorClass musterDec;
Callee callee;
// Connect the callback
musterDec.connectCallback(&callee);
// Test the callback
musterDec.test();
return 0;
}