managed_subscriber.h
Go to the documentation of this file.
00001 /*********************************************************************
00002  *
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2018, University of Luxembourg
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of University of Luxembourg nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  * Author: Maciej Zurad
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   /* TODO - remove if unneccessary
00460   template<class M>
00461   LazyAcquirer makeLazyAcquirer(
00462     const std::string& topic, uint32_t queue_size,
00463     const boost::function<void (const boost::shared_ptr<M const>&)>& callback,
00464     const ros::VoidConstPtr& tracked_object,
00465     const ros::TransportHints& transport_hints)
00466   {
00467     ROS_DEBUG_STREAM("makeLazyAcquirer "
00468       << "const boost::function<void (const boost::shared_ptr<M const>&)>& callback, "
00469       << "form exec");
00470     //MessageCallback<const boost::shared_ptr<M const>&> callback = callback;
00471     return makeLazyAcquirer(topic, queue_size, callback, tracked_object, transport_hints);
00472   }
00473 
00474   template<class M, class C>
00475   LazyAcquirer makeLazyAcquirer(
00476     const std::string& topic, uint32_t queue_size,
00477     const boost::function<void (C)>& callback,
00478     const ros::VoidConstPtr& tracked_object,
00479     const ros::TransportHints& transport_hints)
00480   {
00481     ROS_DEBUG_STREAM("makeLazyAcquirer "
00482       << "const boost::function<void (C)>& callback form exec");
00483     return makeLazyAcquirer(topic, queue_size, callback, tracked_object, transport_hints);
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 }  // namespace resource
00509 }  // namespace robot_activity
00510 
00511 #endif  // ROBOT_ACTIVITY_RESOURCE_MANAGED_SUBSCRIBER_H


robot_activity
Author(s): Maciej ZURAD
autogenerated on Thu Jun 6 2019 18:10:04