lundi 25 février 2019

Using a boost::bind result as a parameter

I have the following code in which I hook callback functions within ROS to do the // Do my stuff thing in retSelf():

template <typename T>
const typename T::ConstPtr retSelf(const typename T::ConstPtr self, size_t id)
{
    // Do my stuff
    return self;
}

template<typename CallbackType, typename ClassType>
void subscribe(void (ClassType::*cb)(typename CallbackType::ConstPtr const),
               ClassType *thisPtr)
{
    auto id = generateId(...);
    auto selfP = &retSelf<CallbackType>;
    auto returnSelf = boost::bind(selfP, _1, id);
    auto callback = boost::bind(cb, thisPtr, returnSelf);

   // Register callback
}

Now, this works fine for calls like :

void MyClass::MyCallback(sensor_msgs::Image::ConstPtr img){}

subscribe<sensor_msgs::Image>(&MyClass::MyCallback, this);

However, I have other cases where I want to do something like this:

void MyClass::AnotherCallback(sensor_msgs::Image::ConstPtr img, int idx){}

subscribe<sensor_msgs::Image>(boost::bind(&MyClass::AnotherCallback, this, _1, 42));

That is, I wish to also specify an index parameter that the client software knows about but the template doesn't, and I end up in AnotherCallback() with the 42 value set and my code in retSelf() executed.

Note I have to use boost::bind and not the standard library as ROS only works with the first kind of binding.

Aucun commentaire:

Enregistrer un commentaire