Skip to content

Commit

Permalink
Added tests for rclcpp::function_traits and work around expression SF…
Browse files Browse the repository at this point in the history
…INAE
  • Loading branch information
esteve committed Oct 12, 2015
1 parent 8e4cc7c commit 77ccda7
Show file tree
Hide file tree
Showing 5 changed files with 390 additions and 59 deletions.
1 change: 1 addition & 0 deletions rclcpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ if(AMENT_ENABLE_TESTING)

include_directories(include)

ament_add_gtest(test_function_traits test/test_function_traits.cpp)
ament_add_gtest(test_mapped_ring_buffer test/test_mapped_ring_buffer.cpp)
ament_add_gtest(test_intra_process_manager test/test_intra_process_manager.cpp)
if(TARGET test_intra_process_manager)
Expand Down
52 changes: 30 additions & 22 deletions rclcpp/include/rclcpp/any_subscription_callback.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,42 @@ struct AnySubscriptionCallback

AnySubscriptionCallback(const AnySubscriptionCallback &) = default;

template<typename CallbackT,
typename std::enable_if<
function_traits<CallbackT>::arity == 1
>::type * = nullptr,
typename std::enable_if<
std::is_same<
typename function_traits<CallbackT>::template argument_type<0>,
typename std::shared_ptr<MessageT>
>::value
>::type * = nullptr
template<
typename CallbackT,
std::size_t Arity = 1
>
void set(CallbackT callback)
typename std::enable_if<rclcpp::arity_comparator<Arity, CallbackT>::value, void>::type
void set(
CallbackT callback,
typename std::enable_if<
std::is_same<
typename function_traits<CallbackT>::template argument_type<0>,
typename std::shared_ptr<MessageT>
>::value
>::type * = nullptr)
{
shared_ptr_callback = callback;
}

template<typename CallbackT,
typename std::enable_if<
function_traits<CallbackT>::arity == 2
>::type * = nullptr,
typename std::enable_if<
std::is_same<
typename function_traits<CallbackT>::template argument_type<0>,
typename std::shared_ptr<MessageT>
>::value
>::type * = nullptr
template<
typename CallbackT,
std::size_t Arity = 2
>
void set(CallbackT callback)
typename std::enable_if<rclcpp::arity_comparator<Arity, CallbackT>::value, void>::type
void set(
CallbackT callback,
typename std::enable_if<
std::is_same<
typename function_traits<CallbackT>::template argument_type<0>,
typename std::shared_ptr<MessageT>
>::value
>::type * = nullptr,
typename std::enable_if<
std::is_same<
typename function_traits<CallbackT>::template argument_type<1>
rmw_message_info_t
>::value
>::type * = nullptr)
{
shared_ptr_with_info_callback = callback;
}
Expand Down
6 changes: 6 additions & 0 deletions rclcpp/include/rclcpp/function_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ struct function_traits<ReturnTypeT (ClassT::*)(Args ...) const>
: public function_traits<ReturnTypeT(ClassT &, Args ...)>
{};

template<std::size_t Arity, typename FunctorT>
struct arity_comparator
{
static constexpr bool value = (Arity == function_traits<FunctorT>::arity);
};

} /* namespace rclcpp */

#endif /* RCLCPP_RCLCPP_FUNCTION_TRAITS_HPP_ */
62 changes: 25 additions & 37 deletions rclcpp/include/rclcpp/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,17 @@ class Node
template<
typename ServiceT,
typename FunctorT,
typename std::enable_if<
function_traits<FunctorT>::arity == 2
>::type * = nullptr,
std::size_t Arity = 2
>
typename std::enable_if<
rclcpp::arity_comparator<Arity, FunctorT>::value,
typename rclcpp::service::Service<ServiceT>::SharedPtr
>::type
create_service_internal(
std::shared_ptr<rmw_node_t> node_handle,
rmw_service_t * service_handle,
const std::string & service_name,
FunctorT callback,
typename std::enable_if<
std::is_same<
typename function_traits<FunctorT>::template argument_type<0>,
Expand All @@ -264,14 +272,7 @@ class Node
typename function_traits<FunctorT>::template argument_type<1>,
typename std::shared_ptr<typename ServiceT::Response>
>::value
>::type * = nullptr
>
typename rclcpp::service::Service<ServiceT>::SharedPtr
create_service_internal(
std::shared_ptr<rmw_node_t> node_handle,
rmw_service_t * service_handle,
const std::string & service_name,
FunctorT callback)
>::type * = nullptr)
{
typename rclcpp::service::Service<ServiceT>::CallbackType callback_without_header =
callback;
Expand All @@ -282,9 +283,17 @@ class Node
template<
typename ServiceT,
typename FunctorT,
typename std::enable_if<
function_traits<FunctorT>::arity == 3
>::type * = nullptr,
std::size_t Arity = 3
>
typename std::enable_if<
arity_comparator<Arity, FunctorT>::value,
typename rclcpp::service::Service<ServiceT>::SharedPtr
>::type
create_service_internal(
std::shared_ptr<rmw_node_t> node_handle,
rmw_service_t * service_handle,
const std::string & service_name,
FunctorT callback,
typename std::enable_if<
std::is_same<
typename function_traits<FunctorT>::template argument_type<0>,
Expand All @@ -296,35 +305,14 @@ class Node
typename function_traits<FunctorT>::template argument_type<1>,
typename std::shared_ptr<typename ServiceT::Request>
>::value
>::type * = nullptr
/*
TODO(esteve): reenable this block of code when VS2015 gets better support
for SFINAE and remove the static_assert from the body of this method. For
more info about the current support for SFINAE in VS2015 RC:
http://blogs.msdn.com/b/vcblog/archive/2015/04/29/c-11-14-17-features-in-vs-2015-rc.aspx
,
>::type * = nullptr,
typename std::enable_if<
std::is_same<
typename function_traits<FunctorT>::template argument_type<2>,
typename std::shared_ptr<typename ServiceT::Response>
>::value
>::type * = nullptr
*/
>
typename rclcpp::service::Service<ServiceT>::SharedPtr
create_service_internal(
std::shared_ptr<rmw_node_t> node_handle,
rmw_service_t * service_handle,
const std::string & service_name,
FunctorT callback)
>::type * = nullptr)
{
static_assert(
std::is_same<
typename function_traits<FunctorT>::template argument_type<2>,
typename std::shared_ptr<typename ServiceT::Response>
>::value, "Third argument must be of type std::shared_ptr<ServiceT::Response>");

typename rclcpp::service::Service<ServiceT>::CallbackWithHeaderType callback_with_header =
callback;
return service::Service<ServiceT>::make_shared(
Expand Down
Loading

0 comments on commit 77ccda7

Please sign in to comment.