#include <iostream> #include <string> #include <boost\bind.hpp>
using namespace std; using namespace boost;
int func(int x, int y) { return x + y; }
struct struct_func { int func(int x, int y) { return x*y; } };
int main(int argc, char *argv[]) { auto ref = boost::bind(func, 20, 10)(); cout << "绑定调用: " << ref << endl;
typedef decltype(&func) f_type; f_type ptr_a = func; int x = 100, y = 200; cout << "绑定调用: " << boost::bind(ptr_a, _1, _2)(x, y) << endl; cout << "传入单参数: " << boost::bind(ptr_a, _1, 20)(10) << endl;
struct_func ptr_b; auto struct_ref = boost::bind(&struct_func::func, ptr_b, _1, _2)(10, 10); cout << "绑定调用: " << struct_ref << endl;
getchar(); }
|
function 参数绑定
#include <iostream> #include <string> #include <boost\function.hpp> #include <boost\bind.hpp>
using namespace std;
float MyFunc(int x, int y) { return x + y; }
struct MyStruct { int add(int x, int y) { return x *y; } };
int main(int argc,char *argv[]) { boost::function<float(int, int)> function_ptr;
function_ptr = MyFunc; if (function_ptr) { cout << "调用指针: " << function_ptr(10, 20) << endl; } function_ptr = 0;
boost::function<int(int, int)> struct_ptr; MyStruct sc;
struct_ptr = boost::bind(&MyStruct::add,&sc, _1, _2); cout <<"调用指针: " << struct_ptr(10, 20) << endl; getchar(); }
|
ref库的使用
#include <iostream> #include <string> #include <vector> #include <boost\bind.hpp> #include <boost\function.hpp>
using namespace std;
template<typename T> struct summary { typedef void result_type; T sum;
summary(T v = T()) : sum(v){} void operator()(T const &x) { sum += x; } };
int main(int argc, char *argv[]) { vector<int> vect = { 1, 3, 5, 7, 9 }; summary<int> s;
boost::function<void(int const&)> func(ref(s));
std::for_each(vect.begin(), vect.end(), func); cout << "求和结果: " << s.sum << endl;
getchar(); }
|
使用普通回调函数
#include <iostream> #include <string> #include <vector> #include <boost\bind.hpp> #include <boost\function.hpp>
using namespace std;
void call_back_func(int x) { cout << "执行回调函数(数值翻倍): " << x * 2 << endl; }
class MyClass { private: typedef boost::function<void(int)> func_ptr; func_ptr func; int n;
public: MyClass(int i) :n(i){}
template<typename CallBack> void accept(CallBack call) { func = call; } void run() { func(n); } };
int main(int argc, char *argv[]) { MyClass ptr(10);
ptr.accept(call_back_func); ptr.run();
getchar(); }
|
带状态的回调函数,ref库传递引用
#include <iostream> #include <string> #include <vector> #include <boost\bind.hpp> #include <boost\function.hpp>
using namespace std;
class MyClass { private: typedef boost::function<void(int)> func_ptr; func_ptr func; int n;
public: MyClass(int i) :n(i){}
template<typename CallBack> void accept(CallBack call) { func = call; } void run() { func(n); } };
class call_back_obj { private: int x;
public: call_back_obj(int i) :x(i){} void operator()(int i) { cout << "回调函数: " << i * x << endl; } };
int main(int argc, char *argv[]) { MyClass ptr(10); call_back_obj call_obj(2);
ptr.accept(ref(call_obj));
ptr.run(); ptr.run();
getchar(); }
|
通过类绑定多个callback
#include <iostream> #include <string> #include <vector> #include <boost\bind.hpp> #include <boost\function.hpp>
using namespace std;
class MyClass { private: typedef boost::function<void(int)> func_ptr; func_ptr func; int n;
public: MyClass(int i) :n(i){}
template<typename CallBack> void accept(CallBack call) { func = call; } void run() { func(n); } };
class call_back_factory { public: void call_back_func_a(int x) { cout << "回调函数1: " << x * 2 << endl; }
void call_back_func_b(int x, int y) { cout << "回调函数2: " << x * y << endl; } };
int main(int argc, char *argv[]) { MyClass ptr(10); call_back_factory factory;
ptr.accept(bind(&call_back_factory::call_back_func_a, factory, _1)); ptr.run();
ptr.accept(bind(&call_back_factory::call_back_func_b, factory, _1, 200)); ptr.run(); getchar(); }
|
信号与槽 一个信号关联多个槽,信号发出后,槽函数相应。
#include <iostream> #include <string> #include <boost\signals2.hpp>
using namespace std;
void slots_a() { cout << "slots_a called" << endl; }
void slots_b() { cout << "slots_b called" << endl; }
int main(int argc, char *argv[]) {
boost::signals2::signal<void()> sig;
sig.connect(&slots_a); sig.connect(&slots_b);
sig();
getchar(); }
|
信号的返回值
#include <iostream> #include <string> #include <boost\signals2.hpp>
using namespace std;
template<int T,int C> struct slots { int operator()(int x) { return x + T + C; } };
int main(int argc, char *argv[]) {
boost::signals2::signal<int(int)> sig;
sig.connect(0,slots<10,20>());
int ref = *sig(5); cout << "获取返回值: " << ref << endl;
getchar(); }
|
合并器的使用
#include <iostream> #include <string> #include <numeric> #include <boost\signals2.hpp>
using namespace std;
template<int T, int C> struct slots { int operator()(int x) { return x + T + C; } };
template<typename T> class combiner { T v;
public: typedef std::pair<T, T> result_type; combiner(T t = T()) : v(t){}
template<typename InputIterator> result_type operator()(InputIterator begin, InputIterator end) const { if (begin == end) return result_type();
vector<T> vec(begin, end);
T sum = std::accumulate(vec.begin(), vec.end(), v); T max = *std::max_element(vec.begin(), vec.end());
return result_type(sum, max); } };
int main(int argc, char *argv[]) { boost::signals2::signal<int(int), combiner<int>> sig;
sig.connect(0, slots<10, 20>());
auto x = sig(2); cout << x.first << x.second << endl;; getchar(); }
|