subscriber.h
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2009, Willow Garage, Inc.
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 #ifndef MESSAGE_FILTERS_SUBSCRIBER_H
00036 #define MESSAGE_FILTERS_SUBSCRIBER_H
00037 
00038 #include <ros/ros.h>
00039 
00040 #include <boost/thread/mutex.hpp>
00041 
00042 #include "connection.h"
00043 #include "simple_filter.h"
00044 
00045 namespace message_filters
00046 {
00047 
00048 class SubscriberBase
00049 {
00050 public:
00051   virtual ~SubscriberBase() {}
00063   virtual void subscribe(ros::NodeHandle& nh, const std::string& topic, uint32_t queue_size, const ros::TransportHints& transport_hints = ros::TransportHints(), ros::CallbackQueueInterface* callback_queue = 0) = 0;
00067   virtual void subscribe() = 0;
00071   virtual void unsubscribe() = 0;
00072 };
00073 typedef boost::shared_ptr<SubscriberBase> SubscriberBasePtr;
00074 
00094 template<class M>
00095 class Subscriber : public SubscriberBase, public SimpleFilter<M>
00096 {
00097 public:
00098   typedef boost::shared_ptr<M const> MConstPtr;
00099   typedef ros::MessageEvent<M const> EventType;
00100 
00112   Subscriber(ros::NodeHandle& nh, const std::string& topic, uint32_t queue_size, const ros::TransportHints& transport_hints = ros::TransportHints(), ros::CallbackQueueInterface* callback_queue = 0)
00113   {
00114     subscribe(nh, topic, queue_size, transport_hints, callback_queue);
00115   }
00116 
00120   Subscriber()
00121   {
00122   }
00123 
00124   ~Subscriber()
00125   {
00126     unsubscribe();
00127   }
00128 
00140   void subscribe(ros::NodeHandle& nh, const std::string& topic, uint32_t queue_size, const ros::TransportHints& transport_hints = ros::TransportHints(), ros::CallbackQueueInterface* callback_queue = 0)
00141   {
00142     unsubscribe();
00143 
00144     if (!topic.empty())
00145     {
00146       ops_.template initByFullCallbackType<const EventType&>(topic, queue_size, boost::bind(&Subscriber<M>::cb, this, _1));
00147       ops_.callback_queue = callback_queue;
00148       ops_.transport_hints = transport_hints;
00149       sub_ = nh.subscribe(ops_);
00150       nh_ = nh;
00151     }
00152   }
00153 
00157   void subscribe()
00158   {
00159     unsubscribe();
00160 
00161     if (!ops_.topic.empty())
00162     {
00163       sub_ = nh_.subscribe(ops_);
00164     }
00165   }
00166 
00170   void unsubscribe()
00171   {
00172     sub_.shutdown();
00173   }
00174 
00175   std::string getTopic() const
00176   {
00177     return ops_.topic;
00178   }
00179 
00183   const ros::Subscriber& getSubscriber() const { return sub_; }
00184 
00188   template<typename F>
00189   void connectInput(F& f)
00190   {
00191     (void)f;
00192   }
00193 
00197   void add(const EventType& e)
00198   {
00199     (void)e;
00200   }
00201 
00202 private:
00203 
00204   void cb(const EventType& e)
00205   {
00206     this->signalMessage(e);
00207   }
00208 
00209   ros::Subscriber sub_;
00210   ros::SubscribeOptions ops_;
00211   ros::NodeHandle nh_;
00212 };
00213 
00214 }
00215 
00216 #endif


message_filters
Author(s): Josh Faust, Vijay Pradeep
autogenerated on Tue Mar 7 2017 04:01:23