日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++?ROS與boost:bind()使用詳解_C 語言

作者:Amelie_xiao ? 更新時間: 2023-02-26 編程語言

1. boost::bind

boost::bind是標準庫函數std::bind1ststd::bind2nd的一種泛化形式。其可以支持函數對象、函數、函數指針、成員函數指針,并且綁定任意參數到某個指定值上或者將輸入參數傳入任意位置。

1.1 通過functions和function pointers使用bind

給定如下函數:

int f(int a, int b)
{
    return a + b;
}
 
int g(int a, int b, int c)
{
    return a + b + c;
}
  • 可以綁定所有參數,如:

bind(f, 1, 2)等價于f(1, 2); bind(g, 1, 2, 3)等價于g(1, 2, 3);

  • 也可以選擇性地綁定參數,如:

bind(f, _1, 5)(x)等價于f(x, 5),其中_1是一個占位符,表示用第一個參數來替換;

bind(f, _2, _1)(x, y)等價于f(y, x);

bind(g, _1, 9, _1)(x)等價于g(x, 9, x);

bind(g, _3, _3, _3)(x, y, z)等價于g(z, z, z);

說明

傳入bind函數的參數一般為變量的copy,如:

int i = 5;
bind(f, i, _1);

比如:

void func(int a, int b, int c);
boost::bind(func, 7, _1, _2);

boost::bind()會返回一個函數對象,所以boost::bind(func, 7, _1, _2)得到一個函數對象ob,即ob = boost::bind(func, 7, _1, _2)。當我們調用ob(3,4)時,相當于調用func(7,3,4),如果連著一起寫那就是boost::bind(func, 7, _1, _2)(3, 4);

需要注意的一點是,boost::bind()里的參數個數一定要與被bind包括的函數的形參個數相同,否則這個bind函數對象ob就無法生成并報錯

如果想傳入變量的引用,可以使用boost::refboost::cref,如:

int i = 5;
bind(f, ref(i), _1);
bind(f, cref(i), _1);

1.2 通過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語法,可以用下列方式代替:

boost::bind(boost::type<int>(), f, _1, _1)(x);

默認情況下,bind擁有的是函數對象的副本,但是也可以使用boost::ref和boost::cref來傳入函數對象的引用,尤其是當該function object是non-copyable或者expensive to copy。

1.3 通過pointers to members使用bind

bind將傳入的成員(數據成員和成員函數)指針作為第一個參數,其行為如同使用boost::mem_fn將成員指針轉換為一個函數對象,即:

bind(&X::f, args); 等價于bind<R>(mem_fn(&X::f), args),其中R為X::f的返回類型(成員函數)或類型(數據成員)。

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 分別為第一個和第二個輸入參數。

2. ROS與bind()

2.1

當我們訂閱一個消息時候,會調用一個返回函數。如:

ros::Subscriber topic_sub=n.subscribe<std_msgs::Int8>("/topic", 10, Callback);

這樣Callback函數應該包含一個參數,即:

void Callback(const std_msgs::Int8::ConstPtr& msg){}

但是,如果我們想要多參數傳入的話,就需要使用boost庫中的bind函數。例如,當我們的回調函數是這樣的:

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;
}

當使用①函數時:

ros::Subscriber scan_sub=n.subscribe<std_msgs::Int8>("/topic", 10, \
        boost::bind(&Callback, _1, index1, index2));//①

返回函數調用的是:

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);
}

當使用②函數時:

ros::Subscriber scan_sub = n.subscribe<std_msgs::Int8>("topic", 10, Callback);//②

返回函數調用的是:

void Callback(const std_msgs::Int8::ConstPtr &msg) { printf("%d \r\n", *msg); }

參考資料

boost::bind()詳解

ROS與boost:bind()簡單解析

原文鏈接:https://blog.csdn.net/lemonxiaoxiao/article/details/128613919

欄目分類
最近更新