You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
1012 B
75 lines
1012 B
#include <stdint.h>
|
|
#include <unistd.h>
|
|
#include <iostream>
|
|
#include <functional>
|
|
|
|
|
|
class Interface
|
|
{
|
|
public:
|
|
Interface()
|
|
{
|
|
};
|
|
|
|
void write(uint8_t val)
|
|
{
|
|
std::cout << "called Interface::write()" << std::endl;
|
|
};
|
|
private:
|
|
|
|
};
|
|
|
|
class Driver
|
|
{
|
|
public:
|
|
|
|
using cb_t = void (*)(uint8_t);
|
|
//typedef void(*cb_t)(uint8_t);
|
|
|
|
Driver(cb_t cb) : cb(cb)
|
|
{
|
|
|
|
};
|
|
|
|
|
|
void do_something()
|
|
{
|
|
cb(50);
|
|
cb(40);
|
|
};
|
|
|
|
private:
|
|
|
|
cb_t cb;
|
|
};
|
|
|
|
template<class T>
|
|
using CallbackObjectMethod = void(T::*)(uint8_t v);
|
|
|
|
//template<class T>
|
|
//void libFunctionObjectMethod(CallbackObjectMethod cbMethod, T* pFO)
|
|
//{
|
|
// int val = 3;
|
|
// (pFO->*cbMethod)(val);
|
|
//}
|
|
|
|
|
|
int main(void)
|
|
{
|
|
|
|
//using namespace std::placeholders; // for _1, _2, ...
|
|
std::cout << "callback_example" << std::endl;
|
|
|
|
Interface iface;
|
|
|
|
iface.write(1);
|
|
|
|
//Driver driv([](uint8_t a){ std::cout << a << std::endl;});
|
|
|
|
Driver driv([iface.this](uint8_t a) -> void { this->write(a);});
|
|
|
|
driv.do_something();
|
|
|
|
return 0;
|
|
}
|