Skip to content

Commit c4aa105

Browse files
isamu-takagiboyali
authored andcommitted
feat(component_interface_utils): apply message change (autowarefoundation#1921)
* feat(component_interface_utils): apply message change Signed-off-by: Takagi, Isamu <isamu.takagi@tier4.jp> * feat: add error category Signed-off-by: Takagi, Isamu <isamu.takagi@tier4.jp> Signed-off-by: Takagi, Isamu <isamu.takagi@tier4.jp>
1 parent 1783f31 commit c4aa105

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

common/component_interface_utils/include/component_interface_utils/rclcpp.hpp

+18-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class NodeAdaptor
3232
private:
3333
using CallbackGroup = rclcpp::CallbackGroup::SharedPtr;
3434

35+
template <class SharedPtrT, class InstanceT>
36+
using MessageCallback =
37+
void (InstanceT::*)(const typename SharedPtrT::element_type::SpecType::Message::ConstSharedPtr);
38+
3539
template <class SharedPtrT, class InstanceT>
3640
using ServiceCallback = void (InstanceT::*)(
3741
const typename SharedPtrT::element_type::SpecType::Service::Request::SharedPtr,
@@ -92,14 +96,25 @@ class NodeAdaptor
9296
srv, [cli, timeout](auto req, auto res) { *res = *cli->call(req, timeout); }, group);
9397
}
9498

99+
/// Create a subscription wrapper.
100+
template <class SharedPtrT, class InstanceT>
101+
void init_sub(
102+
SharedPtrT & sub, InstanceT * instance,
103+
MessageCallback<SharedPtrT, InstanceT> && callback) const
104+
{
105+
using std::placeholders::_1;
106+
init_sub(sub, std::bind(callback, instance, _1));
107+
}
108+
95109
/// Create a service wrapper for logging.
96110
template <class SharedPtrT, class InstanceT>
97111
void init_srv(
98-
SharedPtrT & srv, InstanceT * instance, ServiceCallback<SharedPtrT, InstanceT> callback,
112+
SharedPtrT & srv, InstanceT * instance, ServiceCallback<SharedPtrT, InstanceT> && callback,
99113
CallbackGroup group = nullptr) const
100114
{
101-
init_srv(
102-
srv, [instance, callback](auto req, auto res) { (instance->*callback)(req, res); }, group);
115+
using std::placeholders::_1;
116+
using std::placeholders::_2;
117+
init_srv(srv, std::bind(callback, instance, _1, _2), group);
103118
}
104119

105120
private:

common/component_interface_utils/include/component_interface_utils/rclcpp/exceptions.hpp

+9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ class ServiceException : public std::runtime_error
3434
{
3535
code_ = code;
3636
}
37+
38+
template <class T>
39+
void set(T & status) const
40+
{
41+
status.success = false;
42+
status.code = code_;
43+
status.message = what();
44+
}
45+
3746
ResponseStatus status() const
3847
{
3948
ResponseStatus status;

common/component_interface_utils/include/component_interface_utils/rclcpp/service_server.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class Service
7070
try {
7171
callback(request, response);
7272
} catch (const ServiceException & error) {
73-
response->status = error.status();
73+
error.set(response->status);
7474
}
7575
}
7676
RCLCPP_INFO_STREAM(logger, "service exit: " << SpecT::name << "\n" << to_yaml(*response));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2022 TIER IV, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef COMPONENT_INTERFACE_UTILS__STATUS_HPP_
16+
#define COMPONENT_INTERFACE_UTILS__STATUS_HPP_
17+
18+
namespace component_interface_utils::status
19+
{
20+
21+
template <class T1, class T2>
22+
void copy(const T1 & src, T2 & dst) // NOLINT(build/include_what_you_use): cpplint false positive
23+
{
24+
dst->status.success = src->status.success;
25+
dst->status.code = src->status.code;
26+
dst->status.message = src->status.message;
27+
}
28+
29+
} // namespace component_interface_utils::status
30+
31+
#endif // COMPONENT_INTERFACE_UTILS__STATUS_HPP_

0 commit comments

Comments
 (0)