C++?ROS與boost:bind()使用詳解
1. boost::bind
boost::bind是標(biāo)準(zhǔn)庫(kù)函數(shù)std::bind1st和std::bind2nd的一種泛化形式。其可以支持函數(shù)對(duì)象、函數(shù)、函數(shù)指針、成員函數(shù)指針,并且綁定任意參數(shù)到某個(gè)指定值上或者將輸入?yún)?shù)傳入任意位置。
1.1 通過(guò)functions和function pointers使用bind
給定如下函數(shù):
int f(int a, int b)
{
return a + b;
}
int g(int a, int b, int c)
{
return a + b + c;
}- 可以綁定所有參數(shù),如:
bind(f, 1, 2)等價(jià)于f(1, 2); bind(g, 1, 2, 3)等價(jià)于g(1, 2, 3);
- 也可以選擇性地綁定參數(shù),如:
bind(f, _1, 5)(x)等價(jià)于f(x, 5),其中_1是一個(gè)占位符,表示用第一個(gè)參數(shù)來(lái)替換;
bind(f, _2, _1)(x, y)等價(jià)于f(y, x);
bind(g, _1, 9, _1)(x)等價(jià)于g(x, 9, x);
bind(g, _3, _3, _3)(x, y, z)等價(jià)于g(z, z, z);
說(shuō)明:
傳入bind函數(shù)的參數(shù)一般為變量的copy,如:
int i = 5; bind(f, i, _1);
比如:
void func(int a, int b, int c); boost::bind(func, 7, _1, _2);
boost::bind()會(huì)返回一個(gè)函數(shù)對(duì)象,所以boost::bind(func, 7, _1, _2)得到一個(gè)函數(shù)對(duì)象ob,即ob = boost::bind(func, 7, _1, _2)。當(dāng)我們調(diào)用ob(3,4)時(shí),相當(dāng)于調(diào)用func(7,3,4),如果連著一起寫(xiě)那就是boost::bind(func, 7, _1, _2)(3, 4);
需要注意的一點(diǎn)是,boost::bind()里的參數(shù)個(gè)數(shù)一定要與被bind包括的函數(shù)的形參個(gè)數(shù)相同,否則這個(gè)bind函數(shù)對(duì)象ob就無(wú)法生成并報(bào)錯(cuò)。
如果想傳入變量的引用,可以使用boost::ref和boost::cref,如:
int i = 5; bind(f, ref(i), _1); bind(f, cref(i), _1);
1.2 通過(guò)function objects使用bind
struct F
{
int operator()(int a, int b) { return a – b; }
bool operator()(long a, long b) { return a == b; }
};
F f;
int x = 100;
bind<int>(f, _1, _1)(x); // f(x, x)可能某些編譯器不支持上述的bind語(yǔ)法,可以用下列方式代替:
boost::bind(boost::type<int>(), f, _1, _1)(x);
默認(rèn)情況下,bind擁有的是函數(shù)對(duì)象的副本,但是也可以使用boost::ref和boost::cref來(lái)傳入函數(shù)對(duì)象的引用,尤其是當(dāng)該function object是non-copyable或者expensive to copy。
1.3 通過(guò)pointers to members使用bind
bind將傳入的成員(數(shù)據(jù)成員和成員函數(shù))指針作為第一個(gè)參數(shù),其行為如同使用boost::mem_fn將成員指針轉(zhuǎn)換為一個(gè)函數(shù)對(duì)象,即:
bind(&X::f, args); 等價(jià)于bind<R>(mem_fn(&X::f), args),其中R為X::f的返回類(lèi)型(成員函數(shù))或類(lèi)型(數(shù)據(jù)成員)。
struct X
{
bool f(int a);
};
X x;
shared_ptr<X> p(new X);
int i = 5;
bind(&X::f, ref(x), _1)(i); // x.f(i)
bind(&X::f, &x, _1)(i); // (&x)->f(i)
bind(&X::f, x, _1)(i); // x.f(i)
bind(&X::f, p, _1)(i); // p->f(i)如:
cb = boost::bind(&NodeExample::configCallback, node_example, _1, _2);
其中的 node_example是指針變量NodeExample *node_example = new NodeExample();
因此boost::bind(&NodeExample::configCallback, node_example, _1, _2)的意思就是 node_example -> configCallback(x, y);
又如:
boost::bind(&MyNode::doneCb, this, _1, _2);
意思就是this -> doneCb(x, y) 這里的x,y 分別為第一個(gè)和第二個(gè)輸入?yún)?shù)。
2. ROS與bind()
2.1
當(dāng)我們訂閱一個(gè)消息時(shí)候,會(huì)調(diào)用一個(gè)返回函數(shù)。如:
ros::Subscriber topic_sub=n.subscribe<std_msgs::Int8>("/topic", 10, Callback);這樣Callback函數(shù)應(yīng)該包含一個(gè)參數(shù),即:
void Callback(const std_msgs::Int8::ConstPtr& msg){}但是,如果我們想要多參數(shù)傳入的話,就需要使用boost庫(kù)中的bind函數(shù)。例如,當(dāng)我們的回調(diào)函數(shù)是這樣的:
void Callback(const std_msgs::Int8::ConstPtr& msg, int& x, int& y){}2.2 示例
#include <ros/ros.h>
#include <std_msgs/Int8.h>
int index1 = 1;
int index2 = 2;
void Callback(const std_msgs::Int8::ConstPtr &msg, int &x, int &y) {
printf("%d", *msg);
printf("%d \r\n", x);
printf("%d \r\n", y);
}
void Callback(const std_msgs::Int8::ConstPtr &msg) { printf("%d \r\n", *msg); }
int main(int argc, char **argv) {
ros::init(argc, argv, "multi_callback");
ros::NodeHandle n;
ros::NodeHandle private_nh("~");
int rate;
private_nh.param("rate", rate, 40);
// ros::Subscriber scan_sub=n.subscribe<std_msgs::Int8>("/topic", 10,
//boost::bind(&Callback, _1, index1, index2));//①
ros::Subscriber scan_sub = n.subscribe<std_msgs::Int8>("topic", 10, Callback);//②
ros::Rate r(rate);
while (n.ok()) {
ros::spinOnce();
r.sleep();
}
return 0;
}
當(dāng)使用①函數(shù)時(shí):
ros::Subscriber scan_sub=n.subscribe<std_msgs::Int8>("/topic", 10, \
boost::bind(&Callback, _1, index1, index2));//①返回函數(shù)調(diào)用的是:
void Callback(const std_msgs::Int8::ConstPtr &msg, int &x, int &y) {
printf("%d", *msg);
printf("%d \r\n", x);
printf("%d \r\n", y);
}當(dāng)使用②函數(shù)時(shí):
ros::Subscriber scan_sub = n.subscribe<std_msgs::Int8>("topic", 10, Callback);//②返回函數(shù)調(diào)用的是:
void Callback(const std_msgs::Int8::ConstPtr &msg) { printf("%d \r\n", *msg); }參考資料
到此這篇關(guān)于C++ ROS與boost:bind()詳解的文章就介紹到這了,更多相關(guān)C++ ROS與boost:bind()內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實(shí)現(xiàn)單例模式的自動(dòng)釋放
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)單例模式的自動(dòng)釋放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
C++如何計(jì)算結(jié)構(gòu)體與對(duì)象的大小
這篇文章主要給大家介紹了關(guān)于C++如何計(jì)算結(jié)構(gòu)體與對(duì)象大小的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05

