subscription_callback_helper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009, Willow Garage, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef ROSCPP_SUBSCRIPTION_CALLBACK_HELPER_H
29 #define ROSCPP_SUBSCRIPTION_CALLBACK_HELPER_H
30 
31 #include <typeinfo>
32 
33 #include "common.h"
34 #include "ros/forwards.h"
35 #include "ros/parameter_adapter.h"
36 #include "ros/message_traits.h"
38 #include "ros/serialization.h"
39 #include "ros/message_event.h"
40 #include <ros/static_assert.h>
41 
42 #include <boost/type_traits/add_const.hpp>
43 #include <boost/type_traits/remove_const.hpp>
44 #include <boost/type_traits/remove_reference.hpp>
45 #include <boost/type_traits/is_base_of.hpp>
46 #include <boost/utility/enable_if.hpp>
47 #include <boost/make_shared.hpp>
48 
49 namespace ros
50 {
51 
53 {
54  uint8_t* buffer;
55  uint32_t length;
57 };
58 
60 {
62 };
63 
69 class ROSCPP_DECL SubscriptionCallbackHelper
70 {
71 public:
74  virtual void call(SubscriptionCallbackHelperCallParams& params) = 0;
75  virtual const std::type_info& getTypeInfo() = 0;
76  virtual bool isConst() = 0;
77  virtual bool hasHeader() = 0;
78 };
80 
86 template<typename P, typename Enabled = void>
88 {
89 public:
93  typedef typename boost::add_const<NonConstType>::type ConstType;
96 
97  static const bool is_const = ParameterAdapter<P>::is_const;
98 
99  typedef boost::function<void(typename Adapter::Parameter)> Callback;
100  typedef boost::function<NonConstTypePtr()> CreateFunction;
101 
102  SubscriptionCallbackHelperT(const Callback& callback,
103  const CreateFunction& create = DefaultMessageCreator<NonConstType>())
104  : callback_(callback)
105  , create_(create)
106  { }
107 
108  void setCreateFunction(const CreateFunction& create)
109  {
110  create_ = create;
111  }
112 
113  virtual bool hasHeader()
114  {
115  return message_traits::hasHeader<typename ParameterAdapter<P>::Message>();
116  }
117 
119  {
120  namespace ser = serialization;
121 
122  NonConstTypePtr msg = create_();
123 
124  if (!msg)
125  {
126  ROS_DEBUG("Allocation failed for message of type [%s]", getTypeInfo().name());
127  return VoidConstPtr();
128  }
129 
130  ser::PreDeserializeParams<NonConstType> predes_params;
131  predes_params.message = msg;
132  predes_params.connection_header = params.connection_header;
133  ser::PreDeserialize<NonConstType>::notify(predes_params);
134 
135  ser::IStream stream(params.buffer, params.length);
136  ser::deserialize(stream, *msg);
137 
138  return VoidConstPtr(msg);
139  }
140 
142  {
143  Event event(params.event, create_);
144  callback_(ParameterAdapter<P>::getParameter(event));
145  }
146 
147  virtual const std::type_info& getTypeInfo()
148  {
149  return typeid(NonConstType);
150  }
151 
152  virtual bool isConst()
153  {
155  }
156 
157 private:
158  Callback callback_;
159  CreateFunction create_;
160 };
161 
162 }
163 
164 #endif // ROSCPP_SUBSCRIPTION_CALLBACK_HELPER_H
boost::add_const< NonConstType >::type ConstType
boost::shared_ptr< void const > VoidConstPtr
Definition: forwards.h:52
SubscriptionCallbackHelperT(const Callback &callback, const CreateFunction &create=DefaultMessageCreator< NonConstType >())
virtual void call(SubscriptionCallbackHelperCallParams &params)
Abstract base class used by subscriptions to deal with concrete message types through a common interf...
bool hasHeader()
bool call(const std::string &service_name, MReq &req, MRes &res)
Invoke an RPC service.
Definition: service.h:65
virtual const std::type_info & getTypeInfo()
Concrete generic implementation of SubscriptionCallbackHelper for any normal message type...
virtual VoidConstPtr deserialize(const SubscriptionCallbackHelperDeserializeParams &params)
boost::shared_ptr< SubscriptionCallbackHelper > SubscriptionCallbackHelperPtr
boost::remove_reference< typename boost::remove_const< M >::type >::type Message
ParameterAdapter< P >::Message NonConstType
boost::function< NonConstTypePtr()> CreateFunction
boost::function< void(typename Adapter::Parameter)> Callback
Generally not for outside use. Adapts a function parameter type into the message type, event type and parameter. Allows you to retrieve a parameter type from an event type.
boost::shared_ptr< NonConstType > NonConstTypePtr
boost::shared_ptr< ConstType > ConstTypePtr
void deserialize(Stream &stream, T &t)
void setCreateFunction(const CreateFunction &create)
#define ROS_DEBUG(...)


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas
autogenerated on Mon Feb 28 2022 23:33:27