From b22fca578024ec5e09750209806a9b75e80932b7 Mon Sep 17 00:00:00 2001 From: Esteve Fernandez Date: Fri, 11 Dec 2015 12:55:41 -0800 Subject: [PATCH] Add support for std::bind --- rclcpp/include/rclcpp/function_traits.hpp | 15 ++++++++++++ rclcpp/test/test_function_traits.cpp | 29 +++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/rclcpp/include/rclcpp/function_traits.hpp b/rclcpp/include/rclcpp/function_traits.hpp index 5e3b498..910c353 100644 --- a/rclcpp/include/rclcpp/function_traits.hpp +++ b/rclcpp/include/rclcpp/function_traits.hpp @@ -15,6 +15,7 @@ #ifndef RCLCPP__FUNCTION_TRAITS_HPP_ #define RCLCPP__FUNCTION_TRAITS_HPP_ +#include #include #include @@ -77,6 +78,20 @@ template struct function_traits: function_traits {}; +// std::bind +template +#if defined _LIBCPP_VERSION // libc++ (Clang) +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 +> +#endif + : function_traits +{}; + // Lambdas template struct function_traits diff --git a/rclcpp/test/test_function_traits.cpp b/rclcpp/test/test_function_traits.cpp index 363e9d1..aaaf53f 100644 --- a/rclcpp/test/test_function_traits.cpp +++ b/rclcpp/test/test_function_traits.cpp @@ -14,6 +14,7 @@ #include +#include #include #include "rclcpp/function_traits.hpp" @@ -71,6 +72,15 @@ struct FunctionObjectOneIntOneChar } }; +struct ObjectMember +{ + int callback(bool a) + { + (void)a; + return 7; + } +}; + template< typename FunctorT, std::size_t Arity = 0, @@ -358,6 +368,16 @@ TEST(TestFunctionTraits, argument_types) { FunctionObjectOneIntOneChar >::template argument_type<1> >::value, "Functor accepts a char as second argument"); + + ObjectMember object_member; + + auto bind_one_bool = std::bind(&ObjectMember::callback, &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"); } /* @@ -439,6 +459,15 @@ TEST(TestFunctionTraits, check_arguments) { static_assert( rclcpp::function_traits::check_arguments::value, "Functor accepts an int and a char as arguments"); + + ObjectMember object_member; + + auto bind_one_bool = std::bind(&ObjectMember::callback, &object_member, std::placeholders::_1); + + // Test std::bind functions + static_assert( + rclcpp::function_traits::check_arguments::value, + "Functor accepts a single bool as arguments"); } /*