managed_subscriber.h
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2018, University of Luxembourg
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of University of Luxembourg nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * Author: Maciej Zurad
36  *********************************************************************/
46 #ifndef ROBOT_ACTIVITY_RESOURCE_MANAGED_SUBSCRIBER_H
47 #define ROBOT_ACTIVITY_RESOURCE_MANAGED_SUBSCRIBER_H
48 
49 #include <ros/ros.h>
50 #include <ros/console.h>
51 
53 
54 #include <string>
55 
56 namespace robot_activity
57 {
58 namespace resource
59 {
60 
71 class ManagedSubscriber : public Managed<ManagedSubscriber, ros::Subscriber>
72 {
73 public:
90 
103  void subscribe(const ros::NodeHandlePtr& node_handle)
104  {
105  return acquire(node_handle);
106  }
107 
114  void shutdown()
115  {
116  return release();
117  }
118 
124  template <class Message>
125  using MessageCallback = boost::function<void(Message)>;
126 
144  template<class Message>
146  const std::string& topic, uint32_t queue_size,
147  const MessageCallback<Message>& callback,
148  const ros::VoidConstPtr& tracked_object = ros::VoidConstPtr(),
149  const ros::TransportHints& transport_hints = ros::TransportHints()) const
150  {
151  ROS_DEBUG("makeLazyAcquirer MessageCallback<Message>& callback form exec");
152  return [ = ](const ros::NodeHandlePtr & nh) -> ros::Subscriber
153  {
154  ROS_DEBUG("Subscribing...");
155  return nh->subscribe<Message>(
156  topic,
157  queue_size,
158  static_cast<MessageCallback<Message>>(wrapMessageCallback(callback)),
159  tracked_object,
160  transport_hints);
161  };
162  }
163 
180  template<class M, class T>
182  const std::string& topic, uint32_t queue_size, void(T::*fp)(M), T* obj,
183  const ros::TransportHints& transport_hints = ros::TransportHints()) const
184  {
185  ROS_DEBUG("makeLazyAcquirer void(T::*fp)(M), T* obj, form exec");
186  MessageCallback<M> callback = boost::bind(fp, obj, _1);
187  return makeLazyAcquirer(topic, queue_size, callback, ros::VoidConstPtr(), transport_hints);
188  }
189 
206  template<class M, class T>
208  const std::string& topic, uint32_t queue_size, void(T::*fp)(M) const, T* obj,
209  const ros::TransportHints& transport_hints = ros::TransportHints()) const
210  {
211  ROS_DEBUG("makeLazyAcquirer void(T::*fp)(M) const, T* obj, form exec");
212  MessageCallback<M> callback = boost::bind(fp, obj, _1);
213  return makeLazyAcquirer(topic, queue_size, callback, ros::VoidConstPtr(), transport_hints);
214  }
215 
233  template<class M, class T>
235  const std::string& topic, uint32_t queue_size,
236  void(T::*fp)(const boost::shared_ptr<M const>&), T* obj,
237  const ros::TransportHints& transport_hints = ros::TransportHints()) const
238  {
239  ROS_DEBUG_STREAM("makeLazyAcquirer "
240  << "void(T::*fp)(const boost::shared_ptr<M const>&), "
241  << "T* obj, form exec");
242  MessageCallback<M> callback = boost::bind(fp, obj, _1);
243  return makeLazyAcquirer(topic, queue_size, callback, ros::VoidConstPtr(), transport_hints);
244  }
245 
263  template<class M, class T>
265  const std::string& topic, uint32_t queue_size,
266  void(T::*fp)(const boost::shared_ptr<M const>&) const, T* obj,
267  const ros::TransportHints& transport_hints = ros::TransportHints()) const
268  {
269  ROS_DEBUG_STREAM("makeLazyAcquirer "
270  << "void(T::*fp)(const boost::shared_ptr<M const>&) const, "
271  << "T* obj, form exec");
272  MessageCallback<M> callback = boost::bind(fp, obj, _1);
273  return makeLazyAcquirer(topic, queue_size, callback, ros::VoidConstPtr(), transport_hints);
274  }
275 
295  template<class M, class T>
297  const std::string& topic, uint32_t queue_size,
298  void(T::*fp)(M), const boost::shared_ptr<T>& obj,
299  const ros::TransportHints& transport_hints = ros::TransportHints()) const
300  {
301  ROS_DEBUG_STREAM("makeLazyAcquirer "
302  << "void(T::*fp)(M), "
303  << "const boost::shared_ptr<T>& obj, form exec");
304  MessageCallback<M> callback = boost::bind(fp, obj.get(), _1);
305  return makeLazyAcquirer(topic, queue_size, callback, obj, transport_hints);
306  }
307 
327  template<class M, class T>
329  const std::string& topic, uint32_t queue_size,
330  void(T::*fp)(M) const, const boost::shared_ptr<T>& obj,
331  const ros::TransportHints& transport_hints = ros::TransportHints()) const
332  {
333  ROS_DEBUG_STREAM("makeLazyAcquirer "
334  << "void(T::*fp)(M) const, "
335  << "const boost::shared_ptr<T>& obj, form exec");
336  MessageCallback<M> callback = boost::bind(fp, obj.get(), _1);
337  return makeLazyAcquirer(topic, queue_size, callback, obj, transport_hints);
338  }
339 
359  template<class M, class T>
361  const std::string& topic, uint32_t queue_size,
362  void(T::*fp)(const boost::shared_ptr<M const>&),
363  const boost::shared_ptr<T>& obj,
364  const ros::TransportHints& transport_hints = ros::TransportHints()) const
365  {
366  ROS_DEBUG_STREAM("makeLazyAcquirer "
367  << "void(T::*fp)(const boost::shared_ptr<M const>&), "
368  << "const boost::shared_ptr<T>& obj, form exec");
369  MessageCallback<M> callback = boost::bind(fp, obj.get(), _1);
370  return makeLazyAcquirer(topic, queue_size, callback, obj, transport_hints);
371  }
372 
392  template<class M, class T>
394  const std::string& topic, uint32_t queue_size,
395  void(T::*fp)(const boost::shared_ptr<M const>&) const,
396  const boost::shared_ptr<T>& obj,
397  const ros::TransportHints& transport_hints = ros::TransportHints()) const
398  {
399  ROS_DEBUG_STREAM("makeLazyAcquirer "
400  << "void(T::*fp)(const boost::shared_ptr<M const>&) const, "
401  << "const boost::shared_ptr<T>& obj, form exec");
402  MessageCallback<M> callback = boost::bind(fp, obj.get(), _1);
403  return makeLazyAcquirer(topic, queue_size, callback, obj, transport_hints);
404  }
405 
421  template<class M>
423  const std::string& topic, uint32_t queue_size, void(*fp)(M),
424  const ros::TransportHints& transport_hints = ros::TransportHints()) const
425  {
426  ROS_DEBUG_STREAM("makeLazyAcquirer "
427  << "void(*fp)(M) form exec");
428  MessageCallback<M> callback = boost::bind(fp);
429  return makeLazyAcquirer(topic, queue_size, callback, ros::VoidConstPtr(), transport_hints);
430  }
431 
448  template<class M>
450  const std::string& topic, uint32_t queue_size, void(*fp)(const boost::shared_ptr<M const>&),
451  const ros::TransportHints& transport_hints = ros::TransportHints()) const
452  {
453  ROS_DEBUG_STREAM("makeLazyAcquirer "
454  << "void(*fp)(const boost::shared_ptr<M const>&) form exec");
455  MessageCallback<M> callback = boost::bind(fp);
456  return makeLazyAcquirer(topic, queue_size, callback, ros::VoidConstPtr(), transport_hints);
457  }
458 
459  /* TODO - remove if unneccessary
460  template<class M>
461  LazyAcquirer makeLazyAcquirer(
462  const std::string& topic, uint32_t queue_size,
463  const boost::function<void (const boost::shared_ptr<M const>&)>& callback,
464  const ros::VoidConstPtr& tracked_object,
465  const ros::TransportHints& transport_hints)
466  {
467  ROS_DEBUG_STREAM("makeLazyAcquirer "
468  << "const boost::function<void (const boost::shared_ptr<M const>&)>& callback, "
469  << "form exec");
470  //MessageCallback<const boost::shared_ptr<M const>&> callback = callback;
471  return makeLazyAcquirer(topic, queue_size, callback, tracked_object, transport_hints);
472  }
473 
474  template<class M, class C>
475  LazyAcquirer makeLazyAcquirer(
476  const std::string& topic, uint32_t queue_size,
477  const boost::function<void (C)>& callback,
478  const ros::VoidConstPtr& tracked_object,
479  const ros::TransportHints& transport_hints)
480  {
481  ROS_DEBUG_STREAM("makeLazyAcquirer "
482  << "const boost::function<void (C)>& callback form exec");
483  return makeLazyAcquirer(topic, queue_size, callback, tracked_object, transport_hints);
484  }
485  */
486 private:
495  template<class Message>
497  {
498  return [this, &callback](Message message)
499  {
500  if (!paused_)
501  callback(message);
502  else
503  ROS_DEBUG("callback is paused!");
504  };
505  }
506 };
507 
508 } // namespace resource
509 } // namespace robot_activity
510 
511 #endif // ROBOT_ACTIVITY_RESOURCE_MANAGED_SUBSCRIBER_H
void shutdown()
Shutdowns the ROS subscriber.
LazyAcquirer makeLazyAcquirer(const std::string &topic, uint32_t queue_size, void(*fp)(M), const ros::TransportHints &transport_hints=ros::TransportHints()) const
Creates a function that when called subscribes to a ROS topic.
LazyAcquirer makeLazyAcquirer(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()) const
Creates a function that when called subscribes to a ROS topic.
std::atomic< bool > paused_
Atomic flag specifing whether the resource is paused or not.
LazyAcquirer makeLazyAcquirer(const std::string &topic, uint32_t queue_size, void(T::*fp)(M) const, const boost::shared_ptr< T > &obj, const ros::TransportHints &transport_hints=ros::TransportHints()) const
Creates a function that when called subscribes to a ROS topic.
boost::function< void(Message)> MessageCallback
Typedef for ROS subscriber callbacks.
LazyAcquirer makeLazyAcquirer(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), const boost::shared_ptr< T > &obj, const ros::TransportHints &transport_hints=ros::TransportHints()) const
Creates a function that when called subscribes to a ROS topic.
std::function< ros::Subscriber(const ros::NodeHandlePtr &)> LazyAcquirer
MessageCallback< Message > wrapMessageCallback(const MessageCallback< Message > &callback) const
Wraps the message callback.
Wrapper around ROS resources, such as ros::Subscriber and ros::ServiceServer.
LazyAcquirer makeLazyAcquirer(const std::string &topic, uint32_t queue_size, void(T::*fp)(const boost::shared_ptr< M const > &) const, T *obj, const ros::TransportHints &transport_hints=ros::TransportHints()) const
Creates a function that when called subscribes to a ROS topic.
void release()
Releases the resource if it&#39;s already acquired. shutdown() method in case of ros::Subscriber and ros:...
LazyAcquirer makeLazyAcquirer(const std::string &topic, uint32_t queue_size, const MessageCallback< Message > &callback, const ros::VoidConstPtr &tracked_object=ros::VoidConstPtr(), const ros::TransportHints &transport_hints=ros::TransportHints()) const
Creates a function that when called subscribes to a ROS topic.
LazyAcquirer makeLazyAcquirer(const std::string &topic, uint32_t queue_size, void(T::*fp)(M) const, T *obj, const ros::TransportHints &transport_hints=ros::TransportHints()) const
Creates a function that when called subscribes to a ROS topic.
LazyAcquirer makeLazyAcquirer(const std::string &topic, uint32_t queue_size, void(*fp)(const boost::shared_ptr< M const > &), const ros::TransportHints &transport_hints=ros::TransportHints()) const
Creates a function that when called subscribes to a ROS topic.
#define ROS_DEBUG_STREAM(args)
void subscribe(const ros::NodeHandlePtr &node_handle)
Subscribes to a ROS topic given a node handle.
Implementation of Managed class for ros::Subscriber.
LazyAcquirer makeLazyAcquirer(const std::string &topic, uint32_t queue_size, void(T::*fp)(const boost::shared_ptr< M const > &), T *obj, const ros::TransportHints &transport_hints=ros::TransportHints()) const
Creates a function that when called subscribes to a ROS topic.
LazyAcquirer makeLazyAcquirer(const std::string &topic, uint32_t queue_size, void(T::*fp)(const boost::shared_ptr< M const > &) const, const boost::shared_ptr< T > &obj, const ros::TransportHints &transport_hints=ros::TransportHints()) const
Creates a function that when called subscribes to a ROS topic.
void acquire(const ros::NodeHandlePtr &node_handle)
Acquires the resource if it&#39;s not already acquired.
LazyAcquirer makeLazyAcquirer(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const ros::TransportHints &transport_hints=ros::TransportHints()) const
Creates a function that when called subscribes to a ROS topic.
Managed<Derived,R> class implements a base class which manages ROS resources, such as ros::Subscriber...
#define ROS_DEBUG(...)


robot_activity
Author(s): Maciej ZURAD
autogenerated on Thu Jun 6 2019 19:32:17