chain.h
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2010, Willow Garage, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Willow Garage nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 #ifndef MESSAGE_FILTERS_CHAIN_H
36 #define MESSAGE_FILTERS_CHAIN_H
37 
38 #include "simple_filter.h"
39 #include "pass_through.h"
40 
41 #include <vector>
42 
43 namespace message_filters
44 {
45 
50 class ChainBase
51 {
52 public:
53  virtual ~ChainBase() {}
54 
62  template<typename F>
63  boost::shared_ptr<F> getFilter(size_t index) const
64  {
66  if (filter)
67  {
68  return boost::static_pointer_cast<F>(filter);
69  }
70 
71  return boost::shared_ptr<F>();
72  }
73 
74 protected:
75  virtual boost::shared_ptr<void> getFilterForIndex(size_t index) const = 0;
76 };
78 
110 template<typename M>
111 class Chain : public ChainBase, public SimpleFilter<M>
112 {
113 public:
116 
120  Chain()
121  {
122  }
123 
127  template<typename F>
128  Chain(F& f)
129  {
130  connectInput(f);
131  }
132 
133  struct NullDeleter
134  {
135  void operator()(void const*) const
136  {
137  }
138  };
139 
143  template<class F>
144  size_t addFilter(F* filter)
145  {
147  return addFilter(ptr);
148  }
149 
153  template<class F>
154  size_t addFilter(const boost::shared_ptr<F>& filter)
155  {
156  FilterInfo info;
157  info.add_func = boost::bind((void(F::*)(const EventType&))&F::add, filter.get(), boost::placeholders::_1);
158  info.filter = filter;
159  info.passthrough = boost::make_shared<PassThrough<M> >();
160 
162  info.passthrough->connectInput(*filter);
163  last_filter_connection_ = info.passthrough->registerCallback(typename SimpleFilter<M>::EventCallback(boost::bind(&Chain::lastFilterCB, this, boost::placeholders::_1)));
164  if (!filters_.empty())
165  {
166  filter->connectInput(*filters_.back().passthrough);
167  }
168 
169  uint32_t count = filters_.size();
170  filters_.push_back(info);
171  return count;
172  }
173 
181  template<typename F>
182  boost::shared_ptr<F> getFilter(size_t index) const
183  {
184  if (index >= filters_.size())
185  {
187  }
188 
189  return boost::static_pointer_cast<F>(filters_[index].filter);
190  }
191 
195  template<class F>
196  void connectInput(F& f)
197  {
199  incoming_connection_ = f.registerCallback(typename SimpleFilter<M>::EventCallback(boost::bind(&Chain::incomingCB, this, boost::placeholders::_1)));
200  }
201 
205  void add(const MConstPtr& msg)
206  {
207  add(EventType(msg));
208  }
209 
210  void add(const EventType& evt)
211  {
212  if (!filters_.empty())
213  {
214  filters_[0].add_func(evt);
215  }
216  }
217 
218 protected:
219  virtual boost::shared_ptr<void> getFilterForIndex(size_t index) const
220  {
221  if (index >= filters_.size())
222  {
223  return boost::shared_ptr<void>();
224  }
225 
226  return filters_[index].filter;
227  }
228 
229 private:
230  void incomingCB(const EventType& evt)
231  {
232  add(evt);
233  }
234 
235  void lastFilterCB(const EventType& evt)
236  {
237  this->signalMessage(evt);
238  }
239 
240  struct FilterInfo
241  {
242  boost::function<void(const EventType&)> add_func;
245  };
246  typedef std::vector<FilterInfo> V_FilterInfo;
247 
249 
252 };
253 }
254 
255 #endif // MESSAGE_FILTERS_CHAIN_H
message_filters::Chain::Chain
Chain()
Default constructor.
Definition: chain.h:152
message_filters::Chain::getFilterForIndex
virtual boost::shared_ptr< void > getFilterForIndex(size_t index) const
Definition: chain.h:251
message_filters::ChainBase::getFilterForIndex
virtual boost::shared_ptr< void > getFilterForIndex(size_t index) const =0
message_filters::Chain::add
void add(const MConstPtr &msg)
Add a message to the start of this chain.
Definition: chain.h:237
message_filters::Connection
Encapsulates a connection from one filter to another (or to a user-specified callback)
Definition: connection.h:80
message_filters::Chain::FilterInfo::filter
boost::shared_ptr< void > filter
Definition: chain.h:275
boost::shared_ptr
message_filters::Chain::last_filter_connection_
Connection last_filter_connection_
Definition: chain.h:283
message_filters::Chain::lastFilterCB
void lastFilterCB(const EventType &evt)
Definition: chain.h:267
message_filters::Chain::connectInput
void connectInput(F &f)
Connect this filter's input to another filter's output.
Definition: chain.h:228
message_filters::Chain::incoming_connection_
Connection incoming_connection_
Definition: chain.h:282
simple_filter.h
message_filters::SimpleFilter::EventCallback
boost::function< void(const EventType &)> EventCallback
Definition: simple_filter.h:130
message_filters::Chain::NullDeleter
Definition: chain.h:165
message_filters::Chain::NullDeleter::operator()
void operator()(void const *) const
Definition: chain.h:167
f
f
message_filters::Chain::incomingCB
void incomingCB(const EventType &evt)
Definition: chain.h:262
message_filters::ChainBase::getFilter
boost::shared_ptr< F > getFilter(size_t index) const
Retrieve a filter from this chain by index. Returns an empty shared_ptr if the index is greater than ...
Definition: chain.h:127
message_filters::ChainBasePtr
boost::shared_ptr< ChainBase > ChainBasePtr
Definition: chain.h:109
message_filters::Chain::getFilter
boost::shared_ptr< F > getFilter(size_t index) const
Retrieve a filter from this chain by index. Returns an empty shared_ptr if the index is greater than ...
Definition: chain.h:214
message_filters::Chain::FilterInfo::add_func
boost::function< void(const EventType &)> add_func
Definition: chain.h:274
message_filters::Chain::MConstPtr
boost::shared_ptr< M const > MConstPtr
Definition: chain.h:146
message_filters::Chain::FilterInfo::passthrough
boost::shared_ptr< PassThrough< M > > passthrough
Definition: chain.h:276
pass_through.h
message_filters
Definition: cache.h:47
message_filters::ChainBase::~ChainBase
virtual ~ChainBase()
Definition: chain.h:117
message_filters::Chain::filters_
V_FilterInfo filters_
Definition: chain.h:280
message_filters::Chain::V_FilterInfo
std::vector< FilterInfo > V_FilterInfo
Definition: chain.h:278
message_filters::SimpleFilter::signalMessage
void signalMessage(const MConstPtr &msg)
Call all registered callbacks, passing them the specified message.
Definition: simple_filter.h:188
message_filters::Chain::addFilter
size_t addFilter(F *filter)
Add a filter to this chain, by bare pointer. Returns the index of that filter in the chain.
Definition: chain.h:176
ros::MessageEvent
message_filters::Chain::EventType
ros::MessageEvent< M const > EventType
Definition: chain.h:147
message_filters::Connection::disconnect
void disconnect()
disconnects this connection
Definition: connection.cpp:84


message_filters
Author(s): Josh Faust, Vijay Pradeep, Dirk Thomas , Jacob Perron
autogenerated on Thu Nov 23 2023 04:01:54