ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

ros::SubscribeOptions::create doesn't take object T* Obj pointer.

asked 2019-01-25 01:03:09 -0500

cascais gravatar image

updated 2019-01-25 02:48:37 -0500

gvdhoorn gravatar image


SubscribeOptions::create function doesn't support setting class member function as callback function? I cannot see the argument option in its signature.

template<class M >
static SubscribeOptions     create (const std::string &topic, uint32_t queue_size, const boost::function< void(const boost::shared_ptr< M const > &)> &callback, const VoidConstPtr &tracked_object, CallbackQueueInterface *queue)

Like subscribe does below. Is it possible to add a class member function as callback function to the SubscribeOptions?

 Subscriber subscribe(const std::string& topic, uint32_t queue_size, 
                         void(T::*fp)(const boost::shared_ptr<M const>&) const, T* obj, 
                          const TransportHints& transport_hints = TransportHints())
edit retag flag offensive close merge delete


Can you show us how you've tried to use this?

Using member functions should be/is definitely supported, so perhaps you're not providing the correct arguments to the function.

gvdhoorn gravatar image gvdhoorn  ( 2019-01-25 02:49:19 -0500 )edit

@gvdhoorn, I have not tried to use the function yet but from the interface to create SubscribeOptions like pasted in question, I cannot see the possibility to give a pointer to the instance. That's why I assumed it is not supported. Shall I use boost::bind?

cascais gravatar image cascais  ( 2019-01-25 04:56:03 -0500 )edit

Ah, wait. I'd missed you were trying to use SubscribeOptions. The create(..) function does seem to only take the "simple" type of callbacks, but perhaps the initByFullCallbackType(..) function can help there.

I've not used this myself, so I don't know whether it will work, but ..

gvdhoorn gravatar image gvdhoorn  ( 2019-01-25 05:31:58 -0500 )edit

.. the docs say (from here):

Templated initialization, templated on callback parameter type. Supports any callback parameters supported by the SubscriptionCallbackAdapter.

gvdhoorn gravatar image gvdhoorn  ( 2019-01-25 05:32:34 -0500 )edit

@gvdhoorn. Hmm I cannot find the definition of SubscriptionCallbackAdapter..

cascais gravatar image cascais  ( 2019-01-25 05:55:29 -0500 )edit

It's in subscription_callback_helper.h. Docs here. But again: I don't know whether this will work.

gvdhoorn gravatar image gvdhoorn  ( 2019-01-25 05:57:25 -0500 )edit

Did someone solve this?

Marcel Usai gravatar image Marcel Usai  ( 2020-03-25 13:34:21 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2020-04-20 09:13:31 -0500

FreitagS gravatar image

updated 2020-04-20 09:18:02 -0500

This is, what I did:

  1. Defined a Function to get me some ros::SubscribeOptions with class member function and object:
template <class M, class T>
ros::SubscribeOptions getSubscribeOptions(
    const std::string &topic, uint32_t queue_size,
    void (T::*fp)(const boost::shared_ptr<M const> &),
    const boost::shared_ptr<T> &obj,
    const ros::TransportHints &transport_hints = ros::TransportHints()) {
  ros::SubscribeOptions ops;
  ops.template init<M>(topic, queue_size, boost::bind(fp, obj.get(), _1));
  ops.tracked_object = obj;
  ops.transport_hints = transport_hints;
  return ops;
  1. Use it.
ros::SubscribeOptions ops = getSubscribeOptions( "chatter", 1000, &WebClient::callback, WebClientObject);
ros::Subscriber Sub = n.subscribe(ops);

That seems to work right now. I hope this answers the question.

edit flag offensive delete link more

Question Tools


Asked: 2019-01-25 01:03:09 -0500

Seen: 687 times

Last updated: Apr 20 '20