00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00046 #ifndef ROBOT_ACTIVITY_RESOURCE_MANAGED_SUBSCRIBER_H
00047 #define ROBOT_ACTIVITY_RESOURCE_MANAGED_SUBSCRIBER_H
00048
00049 #include <ros/ros.h>
00050 #include <ros/console.h>
00051
00052 #include <robot_activity/resource/managed_resource.h>
00053
00054 #include <string>
00055
00056 namespace robot_activity
00057 {
00058 namespace resource
00059 {
00060
00071 class ManagedSubscriber : public Managed<ManagedSubscriber, ros::Subscriber>
00072 {
00073 public:
00089 using Managed<ManagedSubscriber, ros::Subscriber>::Managed;
00090
00103 void subscribe(const ros::NodeHandlePtr& node_handle)
00104 {
00105 return acquire(node_handle);
00106 }
00107
00114 void shutdown()
00115 {
00116 return release();
00117 }
00118
00124 template <class Message>
00125 using MessageCallback = boost::function<void(Message)>;
00126
00144 template<class Message>
00145 LazyAcquirer makeLazyAcquirer(
00146 const std::string& topic, uint32_t queue_size,
00147 const MessageCallback<Message>& callback,
00148 const ros::VoidConstPtr& tracked_object = ros::VoidConstPtr(),
00149 const ros::TransportHints& transport_hints = ros::TransportHints()) const
00150 {
00151 ROS_DEBUG("makeLazyAcquirer MessageCallback<Message>& callback form exec");
00152 return [ = ](const ros::NodeHandlePtr & nh) -> ros::Subscriber
00153 {
00154 ROS_DEBUG("Subscribing...");
00155 return nh->subscribe<Message>(
00156 topic,
00157 queue_size,
00158 static_cast<MessageCallback<Message>>(wrapMessageCallback(callback)),
00159 tracked_object,
00160 transport_hints);
00161 };
00162 }
00163
00180 template<class M, class T>
00181 LazyAcquirer makeLazyAcquirer(
00182 const std::string& topic, uint32_t queue_size, void(T::*fp)(M), T* obj,
00183 const ros::TransportHints& transport_hints = ros::TransportHints()) const
00184 {
00185 ROS_DEBUG("makeLazyAcquirer void(T::*fp)(M), T* obj, form exec");
00186 MessageCallback<M> callback = boost::bind(fp, obj, _1);
00187 return makeLazyAcquirer(topic, queue_size, callback, ros::VoidConstPtr(), transport_hints);
00188 }
00189
00206 template<class M, class T>
00207 LazyAcquirer makeLazyAcquirer(
00208 const std::string& topic, uint32_t queue_size, void(T::*fp)(M) const, T* obj,
00209 const ros::TransportHints& transport_hints = ros::TransportHints()) const
00210 {
00211 ROS_DEBUG("makeLazyAcquirer void(T::*fp)(M) const, T* obj, form exec");
00212 MessageCallback<M> callback = boost::bind(fp, obj, _1);
00213 return makeLazyAcquirer(topic, queue_size, callback, ros::VoidConstPtr(), transport_hints);
00214 }
00215
00233 template<class M, class T>
00234 LazyAcquirer makeLazyAcquirer(
00235 const std::string& topic, uint32_t queue_size,
00236 void(T::*fp)(const boost::shared_ptr<M const>&), T* obj,
00237 const ros::TransportHints& transport_hints = ros::TransportHints()) const
00238 {
00239 ROS_DEBUG_STREAM("makeLazyAcquirer "
00240 << "void(T::*fp)(const boost::shared_ptr<M const>&), "
00241 << "T* obj, form exec");
00242 MessageCallback<M> callback = boost::bind(fp, obj, _1);
00243 return makeLazyAcquirer(topic, queue_size, callback, ros::VoidConstPtr(), transport_hints);
00244 }
00245
00263 template<class M, class T>
00264 LazyAcquirer makeLazyAcquirer(
00265 const std::string& topic, uint32_t queue_size,
00266 void(T::*fp)(const boost::shared_ptr<M const>&) const, T* obj,
00267 const ros::TransportHints& transport_hints = ros::TransportHints()) const
00268 {
00269 ROS_DEBUG_STREAM("makeLazyAcquirer "
00270 << "void(T::*fp)(const boost::shared_ptr<M const>&) const, "
00271 << "T* obj, form exec");
00272 MessageCallback<M> callback = boost::bind(fp, obj, _1);
00273 return makeLazyAcquirer(topic, queue_size, callback, ros::VoidConstPtr(), transport_hints);
00274 }
00275
00295 template<class M, class T>
00296 LazyAcquirer makeLazyAcquirer(
00297 const std::string& topic, uint32_t queue_size,
00298 void(T::*fp)(M), const boost::shared_ptr<T>& obj,
00299 const ros::TransportHints& transport_hints = ros::TransportHints()) const
00300 {
00301 ROS_DEBUG_STREAM("makeLazyAcquirer "
00302 << "void(T::*fp)(M), "
00303 << "const boost::shared_ptr<T>& obj, form exec");
00304 MessageCallback<M> callback = boost::bind(fp, obj.get(), _1);
00305 return makeLazyAcquirer(topic, queue_size, callback, obj, transport_hints);
00306 }
00307
00327 template<class M, class T>
00328 LazyAcquirer makeLazyAcquirer(
00329 const std::string& topic, uint32_t queue_size,
00330 void(T::*fp)(M) const, const boost::shared_ptr<T>& obj,
00331 const ros::TransportHints& transport_hints = ros::TransportHints()) const
00332 {
00333 ROS_DEBUG_STREAM("makeLazyAcquirer "
00334 << "void(T::*fp)(M) const, "
00335 << "const boost::shared_ptr<T>& obj, form exec");
00336 MessageCallback<M> callback = boost::bind(fp, obj.get(), _1);
00337 return makeLazyAcquirer(topic, queue_size, callback, obj, transport_hints);
00338 }
00339
00359 template<class M, class T>
00360 LazyAcquirer makeLazyAcquirer(
00361 const std::string& topic, uint32_t queue_size,
00362 void(T::*fp)(const boost::shared_ptr<M const>&),
00363 const boost::shared_ptr<T>& obj,
00364 const ros::TransportHints& transport_hints = ros::TransportHints()) const
00365 {
00366 ROS_DEBUG_STREAM("makeLazyAcquirer "
00367 << "void(T::*fp)(const boost::shared_ptr<M const>&), "
00368 << "const boost::shared_ptr<T>& obj, form exec");
00369 MessageCallback<M> callback = boost::bind(fp, obj.get(), _1);
00370 return makeLazyAcquirer(topic, queue_size, callback, obj, transport_hints);
00371 }
00372
00392 template<class M, class T>
00393 LazyAcquirer makeLazyAcquirer(
00394 const std::string& topic, uint32_t queue_size,
00395 void(T::*fp)(const boost::shared_ptr<M const>&) const,
00396 const boost::shared_ptr<T>& obj,
00397 const ros::TransportHints& transport_hints = ros::TransportHints()) const
00398 {
00399 ROS_DEBUG_STREAM("makeLazyAcquirer "
00400 << "void(T::*fp)(const boost::shared_ptr<M const>&) const, "
00401 << "const boost::shared_ptr<T>& obj, form exec");
00402 MessageCallback<M> callback = boost::bind(fp, obj.get(), _1);
00403 return makeLazyAcquirer(topic, queue_size, callback, obj, transport_hints);
00404 }
00405
00421 template<class M>
00422 LazyAcquirer makeLazyAcquirer(
00423 const std::string& topic, uint32_t queue_size, void(*fp)(M),
00424 const ros::TransportHints& transport_hints = ros::TransportHints()) const
00425 {
00426 ROS_DEBUG_STREAM("makeLazyAcquirer "
00427 << "void(*fp)(M) form exec");
00428 MessageCallback<M> callback = boost::bind(fp);
00429 return makeLazyAcquirer(topic, queue_size, callback, ros::VoidConstPtr(), transport_hints);
00430 }
00431
00448 template<class M>
00449 LazyAcquirer makeLazyAcquirer(
00450 const std::string& topic, uint32_t queue_size, void(*fp)(const boost::shared_ptr<M const>&),
00451 const ros::TransportHints& transport_hints = ros::TransportHints()) const
00452 {
00453 ROS_DEBUG_STREAM("makeLazyAcquirer "
00454 << "void(*fp)(const boost::shared_ptr<M const>&) form exec");
00455 MessageCallback<M> callback = boost::bind(fp);
00456 return makeLazyAcquirer(topic, queue_size, callback, ros::VoidConstPtr(), transport_hints);
00457 }
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486 private:
00495 template<class Message>
00496 MessageCallback<Message> wrapMessageCallback(const MessageCallback<Message>& callback) const
00497 {
00498 return [this, &callback](Message message)
00499 {
00500 if (!paused_)
00501 callback(message);
00502 else
00503 ROS_DEBUG("callback is paused!");
00504 };
00505 }
00506 };
00507
00508 }
00509 }
00510
00511 #endif // ROBOT_ACTIVITY_RESOURCE_MANAGED_SUBSCRIBER_H