parent
b805d38def
commit
9bbf2ddb5e
@ -0,0 +1,20 @@
|
||||
cpp_src = $(wildcard *.cpp)
|
||||
|
||||
cpp_obj = $(cpp_src:.cpp=.o)
|
||||
c_obj = $(c_src:.c=.o)
|
||||
CC = g++
|
||||
CFLAGS = -Wall -pedantic -li2c
|
||||
LDFLAGS =
|
||||
EXEC = runtest
|
||||
|
||||
|
||||
all : $(EXEC)
|
||||
|
||||
$(EXEC): $(cpp_obj) $(c_obj)
|
||||
$(CC) -o $@ $^ $(LDFLAGS)
|
||||
|
||||
clean:
|
||||
rm -rf $(c_obj) $(cpp_obj) $(EXEC)
|
||||
|
||||
cleanall:
|
||||
rm -rf $(c_obj) $(cpp_obj) $(EXEC)
|
@ -0,0 +1,65 @@
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
|
||||
class Device
|
||||
{
|
||||
public:
|
||||
// cllback type
|
||||
typedef std::function<unsigned int(unsigned int)> cb_t;
|
||||
|
||||
Device(cb_t cb) : cb(cb)//Device(cp_t cb) : cb(cb)
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
void do_something(void)
|
||||
{
|
||||
unsigned int return_value;
|
||||
return_value = cb(1);
|
||||
|
||||
std::cout << return_value << std::endl;
|
||||
};
|
||||
|
||||
private:
|
||||
cb_t cb;
|
||||
};
|
||||
|
||||
|
||||
class Interface
|
||||
{
|
||||
public:
|
||||
|
||||
Interface()
|
||||
{
|
||||
std::cout << "created interface object" << std::endl;
|
||||
};
|
||||
|
||||
unsigned int read_write(unsigned int input)
|
||||
{
|
||||
std:: cout << "calling read_write"<< std::endl;
|
||||
std::cout << input << std::endl;
|
||||
return 5;
|
||||
};
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
int main(void)
|
||||
{
|
||||
//using namespace std::placeholders; // for _1, _2, ...
|
||||
std::cout << "callback_example" << std::endl;
|
||||
|
||||
Interface iface;
|
||||
|
||||
//unsigned int ret = iface.read_write(1);
|
||||
|
||||
//std::cout << ret << std::endl;
|
||||
//auto cb = std::bind(&Interface::read_write, &iface,_1);
|
||||
auto cb = std::bind(&Interface::read_write, &iface, std::placeholders::_1);
|
||||
Device device(cb);
|
||||
|
||||
device.do_something();
|
||||
return 0;
|
||||
}
|
Binary file not shown.
Loading…
Reference in new issue