service_callback_helper.h
Go to the documentation of this file.
1 #include "sick_scan/sick_scan_base.h" /* Base definitions included in all header files, added by add_sick_scan_base_header.py. Do not edit this line. */
2 /*
3  * Copyright (C) 2009, Willow Garage, Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef ROSCPP_SERVICE_MESSAGE_HELPER_H
30 #define ROSCPP_SERVICE_MESSAGE_HELPER_H
31 
32 #include "ros/forwards.h"
33 #include "ros/common.h"
34 #include "ros/message.h"
35 #include "ros/message_traits.h"
36 #include "ros/service_traits.h"
37 #include "ros/serialization.h"
38 
39 //#include <boost/type_traits/is_base_of.hpp>
40 //#include <boost/utility/enable_if.hpp>
41 
42 namespace roswrap
43 {
45 {
48  std::shared_ptr<M_string> connection_header;
49 };
50 
51 template<typename M>
52 inline std::shared_ptr<M> defaultServiceCreateFunction()
53 {
54  return std::make_shared<M>();
55 }
56 
57 template<typename MReq, typename MRes>
59 {
60  std::shared_ptr<MReq> request;
61  std::shared_ptr<MRes> response;
62  std::shared_ptr<M_string> connection_header;
63 };
64 
70 template<typename MReq, typename MRes>
72 {
73 public:
74  typedef MReq RequestType;
75  typedef MRes ResponseType;
76  typedef std::shared_ptr<RequestType> RequestPtr;
77  typedef std::shared_ptr<ResponseType> ResponsePtr;
79 
81  {
83  return cb(event);
84  }
85 
86  ServiceEvent(const std::shared_ptr<MReq const>& req, const std::shared_ptr<MRes>& res, const std::shared_ptr<M_string>& connection_header)
87  : request_(req)
88  , response_(res)
89  , connection_header_(connection_header)
90  {}
91 
95  const RequestType& getRequest() const { return *request_; }
99  ResponseType& getResponse() const { return *response_; }
104 
108  const std::string& getCallerName() const { return (*connection_header_)["callerid"]; }
109 private:
110  std::shared_ptr<RequestType const> request_;
111  std::shared_ptr<ResponseType> response_;
112  std::shared_ptr<M_string> connection_header_;
113 };
114 
115 template<typename MReq, typename MRes>
117 {
118  typedef MReq RequestType;
119  typedef MRes ResponseType;
120  typedef std::shared_ptr<RequestType> RequestPtr;
121  typedef std::shared_ptr<ResponseType> ResponsePtr;
122  typedef std::function<bool(RequestType&, ResponseType&)> CallbackType;
123 
125  {
126  return cb(*params.request, *params.response);
127  }
128 };
129 
136 {
137 public:
139  virtual bool call(ServiceCallbackHelperCallParams& params) = 0;
140 };
141 typedef std::shared_ptr<ServiceCallbackHelper> ServiceCallbackHelperPtr;
142 
146 template<typename Spec>
148 {
149 public:
150  typedef typename Spec::RequestType RequestType;
151  typedef typename Spec::ResponseType ResponseType;
152  typedef typename Spec::RequestPtr RequestPtr;
153  typedef typename Spec::ResponsePtr ResponsePtr;
154  typedef typename Spec::CallbackType Callback;
155  typedef std::function<RequestPtr()> ReqCreateFunction;
156  typedef std::function<ResponsePtr()> ResCreateFunction;
157 
159  const ReqCreateFunction& create_req =
160  // these static casts are legally unnecessary, but
161  // here to keep clang 2.8 from getting confused
162  static_cast<RequestPtr(*)()>(defaultServiceCreateFunction<RequestType>),
163  const ResCreateFunction& create_res =
164  static_cast<ResponsePtr(*)()>(defaultServiceCreateFunction<ResponseType>))
166  , create_req_(create_req)
167  , create_res_(create_res)
168  {
169  }
170 
171  virtual bool call(ServiceCallbackHelperCallParams& params)
172  {
173  namespace ser = serialization;
174  RequestPtr req(create_req_());
175  ResponsePtr res(create_res_());
176 
177  ser::deserializeMessage(params.request, *req);
178 
180  call_params.request = req;
181  call_params.response = res;
182  call_params.connection_header = params.connection_header;
183  bool ok = Spec::call(callback_, call_params);
184  params.response = ser::serializeServiceResponse(ok, *res);
185  return ok;
186  }
187 
188 private:
192 };
193 
194 }
195 
196 #endif // ROSCPP_SERVICE_MESSAGE_HELPER_H
roswrap::ServiceCallbackHelperT
Concrete generic implementation of ServiceCallbackHelper for any normal service type.
Definition: service_callback_helper.h:147
roswrap::ServiceSpecCallParams::response
std::shared_ptr< MRes > response
Definition: service_callback_helper.h:61
roswrap::ServiceSpec::RequestPtr
std::shared_ptr< RequestType > RequestPtr
Definition: service_callback_helper.h:120
roswrap::ServiceCallbackHelperT::ResponsePtr
Spec::ResponsePtr ResponsePtr
Definition: service_callback_helper.h:153
roswrap::ServiceEvent::RequestPtr
std::shared_ptr< RequestType > RequestPtr
Definition: service_callback_helper.h:76
roswrap::defaultServiceCreateFunction
std::shared_ptr< M > defaultServiceCreateFunction()
Definition: service_callback_helper.h:52
roswrap::ServiceCallbackHelperCallParams::connection_header
std::shared_ptr< M_string > connection_header
Definition: service_callback_helper.h:48
roswrap::ServiceEvent::getRequest
const RequestType & getRequest() const
Returns a const-reference to the request.
Definition: service_callback_helper.h:95
roswrap::ServiceSpec
Definition: service_callback_helper.h:116
roswrap::M_string
std::map< std::string, std::string > M_string
Definition: datatypes.h:46
roswrap::ServiceCallbackHelperT::create_req_
ReqCreateFunction create_req_
Definition: service_callback_helper.h:190
roswrap::ServiceCallbackHelperCallParams::request
SerializedMessage request
Definition: service_callback_helper.h:46
roswrap::ServiceSpecCallParams
Definition: service_callback_helper.h:58
roswrap::ServiceEvent::getCallerName
const std::string & getCallerName() const
Returns the name of the node which called this service.
Definition: service_callback_helper.h:108
roswrap::ServiceEvent::connection_header_
std::shared_ptr< M_string > connection_header_
Definition: service_callback_helper.h:112
roswrap::ServiceCallbackHelper
Abstract base class used by service servers to deal with concrete message types through a common inte...
Definition: service_callback_helper.h:135
roswrap::ServiceEvent
Event type for services, ros::ServiceEvent<MReq, MRes>& can be used in your callback instead of MReq&...
Definition: service_callback_helper.h:71
roswrap::ServiceEvent::call
static bool call(const CallbackType &cb, ServiceSpecCallParams< RequestType, ResponseType > &params)
Definition: service_callback_helper.h:80
roswrap::ServiceEvent::CallbackType
std::function< bool(ServiceEvent< RequestType, ResponseType > &)> CallbackType
Definition: service_callback_helper.h:78
roswrap::SerializedMessage
Definition: serialized_message.h:40
roswrap::ServiceCallbackHelperCallParams
Definition: service_callback_helper.h:44
roswrap::ServiceSpec::CallbackType
std::function< bool(RequestType &, ResponseType &)> CallbackType
Definition: service_callback_helper.h:122
roswrap::ServiceCallbackHelperT::ResponseType
Spec::ResponseType ResponseType
Definition: service_callback_helper.h:151
roswrap::ServiceSpecCallParams::request
std::shared_ptr< MReq > request
Definition: service_callback_helper.h:60
roswrap::ServiceSpec::ResponsePtr
std::shared_ptr< ResponseType > ResponsePtr
Definition: service_callback_helper.h:121
roswrap::ServiceCallbackHelperT::RequestType
Spec::RequestType RequestType
Definition: service_callback_helper.h:150
roswrap
Definition: param_modi.cpp:41
roswrap::ServiceSpec::call
static bool call(const CallbackType &cb, ServiceSpecCallParams< RequestType, ResponseType > &params)
Definition: service_callback_helper.h:124
roswrap::ServiceEvent::ResponsePtr
std::shared_ptr< ResponseType > ResponsePtr
Definition: service_callback_helper.h:77
roswrap::ServiceEvent::request_
std::shared_ptr< RequestType const > request_
Definition: service_callback_helper.h:110
roswrap::ServiceEvent::ResponseType
MRes ResponseType
Definition: service_callback_helper.h:75
roswrap::ServiceEvent::getResponse
ResponseType & getResponse() const
Returns a non-const reference to the response.
Definition: service_callback_helper.h:99
roswrap::ServiceEvent::ServiceEvent
ServiceEvent(const std::shared_ptr< MReq const > &req, const std::shared_ptr< MRes > &res, const std::shared_ptr< M_string > &connection_header)
Definition: service_callback_helper.h:86
roswrap::ServiceCallbackHelperCallParams::response
SerializedMessage response
Definition: service_callback_helper.h:47
roswrap::ServiceCallbackHelperT::ResCreateFunction
std::function< ResponsePtr()> ResCreateFunction
Definition: service_callback_helper.h:156
roswrap::ServiceCallbackHelper::~ServiceCallbackHelper
virtual ~ServiceCallbackHelper()
Definition: service_callback_helper.h:138
roswrap::ServiceCallbackHelperPtr
std::shared_ptr< ServiceCallbackHelper > ServiceCallbackHelperPtr
Definition: service_callback_helper.h:141
roswrap::ServiceSpecCallParams::connection_header
std::shared_ptr< M_string > connection_header
Definition: service_callback_helper.h:62
roswrap::ServiceCallbackHelperT::RequestPtr
Spec::RequestPtr RequestPtr
Definition: service_callback_helper.h:152
roswrap::ServiceCallbackHelperT::ServiceCallbackHelperT
ServiceCallbackHelperT(const Callback &callback, const ReqCreateFunction &create_req=static_cast< RequestPtr(*)()>(defaultServiceCreateFunction< RequestType >), const ResCreateFunction &create_res=static_cast< ResponsePtr(*)()>(defaultServiceCreateFunction< ResponseType >))
Definition: service_callback_helper.h:158
roswrap::ServiceEvent::getConnectionHeader
M_string & getConnectionHeader() const
Returns a reference to the connection header.
Definition: service_callback_helper.h:103
sick_scan_base.h
roswrap::ServiceSpec::RequestType
MReq RequestType
Definition: service_callback_helper.h:118
roswrap::ServiceSpec::ResponseType
MRes ResponseType
Definition: service_callback_helper.h:119
roswrap::ServiceCallbackHelperT::create_res_
ResCreateFunction create_res_
Definition: service_callback_helper.h:191
roswrap::ServiceEvent::RequestType
MReq RequestType
Definition: service_callback_helper.h:74
roswrap::ServiceCallbackHelperT::Callback
Spec::CallbackType Callback
Definition: service_callback_helper.h:154
ROSCPP_DECL
#define ROSCPP_DECL
Definition: roswrap/src/cfgsimu/sick_scan/ros/common.h:63
roswrap::ok
ROSCPP_DECL bool ok()
Check whether it's time to exit.
Definition: rossimu.cpp:273
roswrap::service::call
bool call(const std::string &service_name, MReq &req, MRes &res)
Invoke an RPC service.
Definition: service.h:66
roswrap::ServiceCallbackHelperT::callback_
Callback callback_
Definition: service_callback_helper.h:189
roswrap::ServiceCallbackHelperT::call
virtual bool call(ServiceCallbackHelperCallParams &params)
Definition: service_callback_helper.h:171
roswrap::ServiceEvent::response_
std::shared_ptr< ResponseType > response_
Definition: service_callback_helper.h:111
callback
void callback(const sick_scan_xd::RadarScan::ConstPtr &oa)
Definition: radar_object_marker.cpp:157
roswrap::ServiceCallbackHelperT::ReqCreateFunction
std::function< RequestPtr()> ReqCreateFunction
Definition: service_callback_helper.h:155


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:10