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 
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  {
146  boost::shared_ptr<F> ptr(filter, NullDeleter());
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(), _1);
158  info.filter = filter;
159  info.passthrough = boost::make_shared<PassThrough<M> >();
160 
161  last_filter_connection_.disconnect();
162  info.passthrough->connectInput(*filter);
163  last_filter_connection_ = info.passthrough->registerCallback(typename SimpleFilter<M>::EventCallback(boost::bind(&Chain::lastFilterCB, this, _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  {
186  return boost::shared_ptr<F>();
187  }
188 
189  return boost::static_pointer_cast<F>(filters_[index].filter);
190  }
191 
195  template<class F>
196  void connectInput(F& f)
197  {
198  incoming_connection_.disconnect();
199  incoming_connection_ = f.registerCallback(typename SimpleFilter<M>::EventCallback(boost::bind(&Chain::incomingCB, this, _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 
248  V_FilterInfo filters_;
249 
252 };
253 }
254 
255 #endif // MESSAGE_FILTERS_CHAIN_H
boost::shared_ptr< ChainBase > ChainBasePtr
Definition: chain.h:77
boost::shared_ptr< M const > MConstPtr
Definition: chain.h:114
void connectInput(F &f)
Connect this filter&#39;s input to another filter&#39;s output.
Definition: chain.h:196
virtual ~ChainBase()
Definition: chain.h:53
void incomingCB(const EventType &evt)
Definition: chain.h:230
void add(const MConstPtr &msg)
Add a message to the start of this chain.
Definition: chain.h:205
void add(const EventType &evt)
Definition: chain.h:210
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:182
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:63
Connection incoming_connection_
Definition: chain.h:250
boost::function< void(const EventType &)> add_func
Definition: chain.h:242
Connection last_filter_connection_
Definition: chain.h:251
void operator()(void const *) const
Definition: chain.h:135
ros::MessageEvent< M const > EventType
Definition: chain.h:115
virtual boost::shared_ptr< void > getFilterForIndex(size_t index) const =0
Chain()
Default constructor.
Definition: chain.h:120
Chains a dynamic number of simple filters together. Allows retrieval of filters by index after they a...
Definition: chain.h:111
boost::shared_ptr< PassThrough< M > > passthrough
Definition: chain.h:244
Chain(F &f)
Constructor with filter. Calls connectInput(f)
Definition: chain.h:128
Convenience base-class for simple filters which output a single message.
Definition: simple_filter.h:60
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:144
boost::function< void(const EventType &)> EventCallback
Definition: simple_filter.h:66
void lastFilterCB(const EventType &evt)
Definition: chain.h:235
std::vector< FilterInfo > V_FilterInfo
Definition: chain.h:246
Base class for Chain, allows you to store multiple chains in the same container. Provides filter retr...
Definition: chain.h:50
Encapsulates a connection from one filter to another (or to a user-specified callback) ...
Definition: connection.h:48
boost::shared_ptr< void > filter
Definition: chain.h:243
V_FilterInfo filters_
Definition: chain.h:248
size_t addFilter(const boost::shared_ptr< F > &filter)
Add a filter to this chain, by shared_ptr. Returns the index of that filter in the chain...
Definition: chain.h:154
virtual boost::shared_ptr< void > getFilterForIndex(size_t index) const
Definition: chain.h:219


message_filters
Author(s): Josh Faust, Vijay Pradeep
autogenerated on Fri Jun 8 2018 02:54:48