Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(component_interface_utils): apply message change #1921

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class NodeAdaptor
private:
using CallbackGroup = rclcpp::CallbackGroup::SharedPtr;

template <class SharedPtrT, class InstanceT>
using MessageCallback =
void (InstanceT::*)(const typename SharedPtrT::element_type::SpecType::Message::ConstSharedPtr);

template <class SharedPtrT, class InstanceT>
using ServiceCallback = void (InstanceT::*)(
const typename SharedPtrT::element_type::SpecType::Service::Request::SharedPtr,
Expand Down Expand Up @@ -92,14 +96,25 @@ class NodeAdaptor
srv, [cli, timeout](auto req, auto res) { *res = *cli->call(req, timeout); }, group);
}

/// Create a subscription wrapper.
template <class SharedPtrT, class InstanceT>
void init_sub(
SharedPtrT & sub, InstanceT * instance,
MessageCallback<SharedPtrT, InstanceT> && callback) const
{
using std::placeholders::_1;
init_sub(sub, std::bind(callback, instance, _1));
}

/// Create a service wrapper for logging.
template <class SharedPtrT, class InstanceT>
void init_srv(
SharedPtrT & srv, InstanceT * instance, ServiceCallback<SharedPtrT, InstanceT> callback,
SharedPtrT & srv, InstanceT * instance, ServiceCallback<SharedPtrT, InstanceT> && callback,
CallbackGroup group = nullptr) const
{
init_srv(
srv, [instance, callback](auto req, auto res) { (instance->*callback)(req, res); }, group);
using std::placeholders::_1;
using std::placeholders::_2;
init_srv(srv, std::bind(callback, instance, _1, _2), group);
}

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class ServiceException : public std::runtime_error
{
code_ = code;
}

template <class T>
void set(T & status) const
{
status.success = false;
status.code = code_;
status.message = what();
}

ResponseStatus status() const
{
ResponseStatus status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Service
try {
callback(request, response);
} catch (const ServiceException & error) {
response->status = error.status();
error.set(response->status);
}
}
RCLCPP_INFO_STREAM(logger, "service exit: " << SpecT::name << "\n" << to_yaml(*response));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2022 TIER IV, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef COMPONENT_INTERFACE_UTILS__STATUS_HPP_
#define COMPONENT_INTERFACE_UTILS__STATUS_HPP_

namespace component_interface_utils::status
{

template <class T1, class T2>
void copy(const T1 & src, T2 & dst) // NOLINT(build/include_what_you_use): cpplint false positive
{
dst->status.success = src->status.success;
dst->status.code = src->status.code;
dst->status.message = src->status.message;
}

} // namespace component_interface_utils::status

#endif // COMPONENT_INTERFACE_UTILS__STATUS_HPP_