00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, 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_SIGNAL1_H 00036 #define MESSAGE_FILTERS_SIGNAL1_H 00037 00038 #include <boost/noncopyable.hpp> 00039 00040 #include "connection.h" 00041 #include <ros/message_event.h> 00042 #include <ros/parameter_adapter.h> 00043 00044 #include <boost/bind.hpp> 00045 #include <boost/thread/mutex.hpp> 00046 00047 namespace message_filters 00048 { 00049 template<class M> 00050 class CallbackHelper1 00051 { 00052 public: 00053 virtual ~CallbackHelper1() {} 00054 00055 virtual void call(const ros::MessageEvent<M const>& event, bool nonconst_need_copy) = 0; 00056 00057 typedef boost::shared_ptr<CallbackHelper1<M> > Ptr; 00058 }; 00059 00060 template<typename P, typename M> 00061 class CallbackHelper1T : public CallbackHelper1<M> 00062 { 00063 public: 00064 typedef ros::ParameterAdapter<P> Adapter; 00065 typedef boost::function<void(typename Adapter::Parameter)> Callback; 00066 typedef typename Adapter::Event Event; 00067 00068 CallbackHelper1T(const Callback& cb) 00069 : callback_(cb) 00070 { 00071 } 00072 00073 virtual void call(const ros::MessageEvent<M const>& event, bool nonconst_force_copy) 00074 { 00075 Event my_event(event, nonconst_force_copy || event.nonConstWillCopy()); 00076 callback_(Adapter::getParameter(my_event)); 00077 } 00078 00079 private: 00080 Callback callback_; 00081 }; 00082 00083 template<class M> 00084 class Signal1 00085 { 00086 typedef boost::shared_ptr<CallbackHelper1<M> > CallbackHelper1Ptr; 00087 typedef std::vector<CallbackHelper1Ptr> V_CallbackHelper1; 00088 00089 public: 00090 template<typename P> 00091 CallbackHelper1Ptr addCallback(const boost::function<void(P)>& callback) 00092 { 00093 CallbackHelper1T<P, M>* helper = new CallbackHelper1T<P, M>(callback); 00094 00095 boost::mutex::scoped_lock lock(mutex_); 00096 callbacks_.push_back(CallbackHelper1Ptr(helper)); 00097 return callbacks_.back(); 00098 } 00099 00100 void removeCallback(const CallbackHelper1Ptr& helper) 00101 { 00102 boost::mutex::scoped_lock lock(mutex_); 00103 typename V_CallbackHelper1::iterator it = std::find(callbacks_.begin(), callbacks_.end(), helper); 00104 if (it != callbacks_.end()) 00105 { 00106 callbacks_.erase(it); 00107 } 00108 } 00109 00110 void call(const ros::MessageEvent<M const>& event) 00111 { 00112 boost::mutex::scoped_lock lock(mutex_); 00113 bool nonconst_force_copy = callbacks_.size() > 1; 00114 typename V_CallbackHelper1::iterator it = callbacks_.begin(); 00115 typename V_CallbackHelper1::iterator end = callbacks_.end(); 00116 for (; it != end; ++it) 00117 { 00118 const CallbackHelper1Ptr& helper = *it; 00119 helper->call(event, nonconst_force_copy); 00120 } 00121 } 00122 00123 private: 00124 boost::mutex mutex_; 00125 V_CallbackHelper1 callbacks_; 00126 }; 00127 00128 } // message_filters 00129 00130 #endif // MESSAGE_FILTERS_SIGNAL1_H 00131 00132