From 2bee266adf5b35c3c80b1e5b7224c0e3bdd8f2a8 Mon Sep 17 00:00:00 2001 From: Esteve Fernandez Date: Fri, 14 Jun 2019 22:09:47 +0200 Subject: [PATCH] Added support for const member functions (#763) * Added support for const member functions Signed-off-by: Esteve Fernandez * Added signature for Windows Signed-off-by: Esteve Fernandez * Fix style Signed-off-by: Esteve Fernandez * Attempt at fixing function_traits for macOS Signed-off-by: Esteve Fernandez --- rclcpp/include/rclcpp/function_traits.hpp | 18 +++++++++++++++++ rclcpp/test/test_function_traits.cpp | 24 +++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/rclcpp/include/rclcpp/function_traits.hpp b/rclcpp/include/rclcpp/function_traits.hpp index 4ccfc75..3599df5 100644 --- a/rclcpp/include/rclcpp/function_traits.hpp +++ b/rclcpp/include/rclcpp/function_traits.hpp @@ -96,6 +96,24 @@ struct function_traits< : function_traits {}; +// std::bind for object const methods +template +#if defined _LIBCPP_VERSION // libc++ (Clang) +struct function_traits> +#elif defined _GLIBCXX_RELEASE // glibc++ (GNU C++ >= 7.1) +struct function_traits> +#elif defined __GLIBCXX__ // glibc++ (GNU C++) +struct function_traits(FArgs ...)>> +#elif defined _MSC_VER // MS Visual Studio +struct function_traits< + std::_Binder +> +#else +#error "Unsupported C++ compiler / standard library" +#endif + : function_traits +{}; + // std::bind for free functions template #if defined _LIBCPP_VERSION // libc++ (Clang) diff --git a/rclcpp/test/test_function_traits.cpp b/rclcpp/test/test_function_traits.cpp index 4e09b08..d166ed2 100644 --- a/rclcpp/test/test_function_traits.cpp +++ b/rclcpp/test/test_function_traits.cpp @@ -80,6 +80,12 @@ struct ObjectMember return 7; } + int callback_one_bool_const(bool a) const + { + (void)a; + return 7; + } + int callback_two_bools(bool a, bool b) { (void)a; @@ -394,6 +400,16 @@ TEST(TestFunctionTraits, argument_types) { rclcpp::function_traits::function_traits::template argument_type<0> >::value, "Functor accepts a bool as first argument"); + auto bind_one_bool_const = std::bind( + &ObjectMember::callback_one_bool_const, &object_member, std::placeholders::_1); + + static_assert( + std::is_same< + bool, + rclcpp::function_traits::function_traits::template + argument_type<0> + >::value, "Functor accepts a bool as first argument"); + auto bind_two_bools = std::bind( &ObjectMember::callback_two_bools, &object_member, std::placeholders::_1, std::placeholders::_2); @@ -561,6 +577,14 @@ TEST(TestFunctionTraits, check_arguments) { static_assert( rclcpp::function_traits::check_arguments::value, "Functor accepts a single bool as arguments"); + + auto bind_one_bool_const = std::bind( + &ObjectMember::callback_one_bool_const, &object_member, std::placeholders::_1); + + // Test std::bind functions + static_assert( + rclcpp::function_traits::check_arguments::value, + "Functor accepts a single bool as arguments"); } /*