From 26d52d949ec63eabe365b088e77a51f1290b51bd Mon Sep 17 00:00:00 2001 From: Esteve Fernandez Date: Wed, 18 Nov 2015 17:09:49 -0800 Subject: [PATCH] Move function_traits templates to a separate namespace --- .../include/rclcpp/any_service_callback.hpp | 4 +- .../rclcpp/any_subscription_callback.hpp | 12 +- rclcpp/include/rclcpp/client.hpp | 4 +- rclcpp/include/rclcpp/function_traits.hpp | 6 + rclcpp/test/test_function_traits.cpp | 124 +++++++++++------- 5 files changed, 89 insertions(+), 61 deletions(-) diff --git a/rclcpp/include/rclcpp/any_service_callback.hpp b/rclcpp/include/rclcpp/any_service_callback.hpp index 3913672..076cc5a 100644 --- a/rclcpp/include/rclcpp/any_service_callback.hpp +++ b/rclcpp/include/rclcpp/any_service_callback.hpp @@ -57,7 +57,7 @@ public: template< typename CallbackT, typename std::enable_if< - rclcpp::same_arguments< + rclcpp::function_traits::same_arguments< CallbackT, SharedPtrCallback >::value @@ -71,7 +71,7 @@ public: template< typename CallbackT, typename std::enable_if< - rclcpp::same_arguments< + rclcpp::function_traits::same_arguments< CallbackT, SharedPtrWithRequestHeaderCallback >::value diff --git a/rclcpp/include/rclcpp/any_subscription_callback.hpp b/rclcpp/include/rclcpp/any_subscription_callback.hpp index a52e8d9..0e657ee 100644 --- a/rclcpp/include/rclcpp/any_subscription_callback.hpp +++ b/rclcpp/include/rclcpp/any_subscription_callback.hpp @@ -72,7 +72,7 @@ public: template< typename CallbackT, typename std::enable_if< - rclcpp::same_arguments< + rclcpp::function_traits::same_arguments< CallbackT, SharedPtrCallback >::value @@ -86,7 +86,7 @@ public: template< typename CallbackT, typename std::enable_if< - rclcpp::same_arguments< + rclcpp::function_traits::same_arguments< CallbackT, SharedPtrWithInfoCallback >::value @@ -100,7 +100,7 @@ public: template< typename CallbackT, typename std::enable_if< - rclcpp::same_arguments< + rclcpp::function_traits::same_arguments< CallbackT, ConstSharedPtrCallback >::value @@ -114,7 +114,7 @@ public: template< typename CallbackT, typename std::enable_if< - rclcpp::same_arguments< + rclcpp::function_traits::same_arguments< CallbackT, ConstSharedPtrWithInfoCallback >::value @@ -128,7 +128,7 @@ public: template< typename CallbackT, typename std::enable_if< - rclcpp::same_arguments< + rclcpp::function_traits::same_arguments< CallbackT, UniquePtrCallback >::value @@ -142,7 +142,7 @@ public: template< typename CallbackT, typename std::enable_if< - rclcpp::same_arguments< + rclcpp::function_traits::same_arguments< CallbackT, UniquePtrWithInfoCallback >::value diff --git a/rclcpp/include/rclcpp/client.hpp b/rclcpp/include/rclcpp/client.hpp index 589719f..2eaed00 100644 --- a/rclcpp/include/rclcpp/client.hpp +++ b/rclcpp/include/rclcpp/client.hpp @@ -135,7 +135,7 @@ public: template< typename CallbackT, typename std::enable_if< - rclcpp::same_arguments< + rclcpp::function_traits::same_arguments< CallbackT, CallbackType >::value @@ -161,7 +161,7 @@ public: template< typename CallbackT, typename std::enable_if< - rclcpp::same_arguments< + rclcpp::function_traits::same_arguments< CallbackT, CallbackWithRequestType >::value diff --git a/rclcpp/include/rclcpp/function_traits.hpp b/rclcpp/include/rclcpp/function_traits.hpp index 5c1ce9a..8f051a0 100644 --- a/rclcpp/include/rclcpp/function_traits.hpp +++ b/rclcpp/include/rclcpp/function_traits.hpp @@ -21,6 +21,10 @@ namespace rclcpp { +namespace function_traits + +{ + /* NOTE(esteve): * We support service callbacks that can optionally take the request id, * which should be possible with two overloaded create_service methods, @@ -106,6 +110,8 @@ struct same_arguments : std::is_same< > {}; +} // namespace function_traits + } // namespace rclcpp #endif // RCLCPP__FUNCTION_TRAITS_HPP_ diff --git a/rclcpp/test/test_function_traits.cpp b/rclcpp/test/test_function_traits.cpp index e92420e..5475060 100644 --- a/rclcpp/test/test_function_traits.cpp +++ b/rclcpp/test/test_function_traits.cpp @@ -72,7 +72,8 @@ struct FunctionObjectOneIntOneChar template< typename FunctorT, std::size_t Arity = 0, - typename std::enable_if::value>::type * = nullptr + typename std::enable_if< + rclcpp::function_traits::arity_comparator::value>::type * = nullptr > int func_accept_callback(FunctorT callback) { @@ -82,7 +83,7 @@ int func_accept_callback(FunctorT callback) template< typename FunctorT, typename std::enable_if< - rclcpp::check_arguments::value + rclcpp::function_traits::check_arguments::value >::type * = nullptr > int func_accept_callback(FunctorT callback) @@ -94,7 +95,7 @@ int func_accept_callback(FunctorT callback) template< typename FunctorT, typename std::enable_if< - rclcpp::check_arguments::value + rclcpp::function_traits::check_arguments::value >::type * = nullptr > int func_accept_callback(FunctorT callback) @@ -107,7 +108,7 @@ int func_accept_callback(FunctorT callback) template< typename FunctorT, typename std::enable_if< - rclcpp::check_arguments::value + rclcpp::function_traits::check_arguments::value >::type * = nullptr > int func_accept_callback(FunctorT callback) @@ -123,19 +124,19 @@ int func_accept_callback(FunctorT callback) TEST(TestFunctionTraits, arity) { // Test regular functions static_assert( - rclcpp::function_traits::arity == 0, + rclcpp::function_traits::function_traits::arity == 0, "Functor does not accept arguments"); static_assert( - rclcpp::function_traits::arity == 1, + rclcpp::function_traits::function_traits::arity == 1, "Functor only accepts one argument"); static_assert( - rclcpp::function_traits::arity == 2, + rclcpp::function_traits::function_traits::arity == 2, "Functor only accepts two arguments"); static_assert( - rclcpp::function_traits::arity == 2, + rclcpp::function_traits::function_traits::arity == 2, "Functor only accepts two arguments"); // Test lambdas @@ -161,36 +162,36 @@ TEST(TestFunctionTraits, arity) { }; static_assert( - rclcpp::function_traits::arity == 0, + rclcpp::function_traits::function_traits::arity == 0, "Functor does not accept arguments"); static_assert( - rclcpp::function_traits::arity == 1, + rclcpp::function_traits::function_traits::arity == 1, "Functor only accepts one argument"); static_assert( - rclcpp::function_traits::arity == 2, + rclcpp::function_traits::function_traits::arity == 2, "Functor only accepts two arguments"); static_assert( - rclcpp::function_traits::arity == 2, + rclcpp::function_traits::function_traits::arity == 2, "Functor only accepts two arguments"); // Test objects that have a call operator static_assert( - rclcpp::function_traits::arity == 0, + rclcpp::function_traits::function_traits::arity == 0, "Functor does not accept arguments"); static_assert( - rclcpp::function_traits::arity == 1, + rclcpp::function_traits::function_traits::arity == 1, "Functor only accepts one argument"); static_assert( - rclcpp::function_traits::arity == 2, + rclcpp::function_traits::function_traits::arity == 2, "Functor only accepts two arguments"); static_assert( - rclcpp::function_traits::arity == 2, + rclcpp::function_traits::function_traits::arity == 2, "Functor only accepts two arguments"); } @@ -202,31 +203,35 @@ TEST(TestFunctionTraits, argument_types) { static_assert( std::is_same< int, - rclcpp::function_traits::template argument_type<0> + rclcpp::function_traits::function_traits::template argument_type<0> >::value, "Functor accepts an int as first argument"); static_assert( std::is_same< int, - rclcpp::function_traits::template argument_type<0> + rclcpp::function_traits::function_traits::template argument_type<0> >::value, "Functor accepts an int as first argument"); static_assert( std::is_same< int, - rclcpp::function_traits::template argument_type<1> + rclcpp::function_traits::function_traits::template argument_type<1> >::value, "Functor accepts an int as second argument"); static_assert( std::is_same< int, - rclcpp::function_traits::template argument_type<0> + rclcpp::function_traits::function_traits< + decltype(func_one_int_one_char) + >::template argument_type<0> >::value, "Functor accepts an int as first argument"); static_assert( std::is_same< char, - rclcpp::function_traits::template argument_type<1> + rclcpp::function_traits::function_traits< + decltype(func_one_int_one_char) + >::template argument_type<1> >::value, "Functor accepts a char as second argument"); // Test lambdas @@ -250,62 +255,70 @@ TEST(TestFunctionTraits, argument_types) { static_assert( std::is_same< int, - rclcpp::function_traits::template argument_type<0> + rclcpp::function_traits::function_traits::template argument_type<0> >::value, "Functor accepts an int as first argument"); static_assert( std::is_same< int, - rclcpp::function_traits::template argument_type<0> + rclcpp::function_traits::function_traits::template argument_type<0> >::value, "Functor accepts an int as first argument"); static_assert( std::is_same< int, - rclcpp::function_traits::template argument_type<1> + rclcpp::function_traits::function_traits::template argument_type<1> >::value, "Functor accepts an int as second argument"); static_assert( std::is_same< int, - rclcpp::function_traits::template argument_type<0> + rclcpp::function_traits::function_traits< + decltype(lambda_one_int_one_char) + >::template argument_type<0> >::value, "Functor accepts an int as first argument"); static_assert( std::is_same< char, - rclcpp::function_traits::template argument_type<1> + rclcpp::function_traits::function_traits< + decltype(lambda_one_int_one_char) + >::template argument_type<1> >::value, "Functor accepts a char as second argument"); // Test objects that have a call operator static_assert( std::is_same< int, - rclcpp::function_traits::template argument_type<0> + rclcpp::function_traits::function_traits::template argument_type<0> >::value, "Functor accepts an int as first argument"); static_assert( std::is_same< int, - rclcpp::function_traits::template argument_type<0> + rclcpp::function_traits::function_traits::template argument_type<0> >::value, "Functor accepts an int as first argument"); static_assert( std::is_same< int, - rclcpp::function_traits::template argument_type<1> + rclcpp::function_traits::function_traits::template argument_type<1> >::value, "Functor accepts an int as second argument"); static_assert( std::is_same< int, - rclcpp::function_traits::template argument_type<0> + rclcpp::function_traits::function_traits< + FunctionObjectOneIntOneChar + >::template argument_type<0> >::value, "Functor accepts an int as first argument"); static_assert( std::is_same< char, - rclcpp::function_traits::template argument_type<1> + rclcpp::function_traits::function_traits< + FunctionObjectOneIntOneChar + >::template argument_type<1> >::value, "Functor accepts a char as second argument"); } @@ -315,35 +328,35 @@ TEST(TestFunctionTraits, argument_types) { TEST(TestFunctionTraits, check_arguments) { // Test regular functions static_assert( - rclcpp::check_arguments::value, + rclcpp::function_traits::check_arguments::value, "Functor accepts a single int as arguments"); static_assert( - !rclcpp::check_arguments::value, + !rclcpp::function_traits::check_arguments::value, "Functor does not accept a char as argument"); static_assert( - !rclcpp::check_arguments::value, + !rclcpp::function_traits::check_arguments::value, "Functor does not accept two arguments"); static_assert( - !rclcpp::check_arguments::value, + !rclcpp::function_traits::check_arguments::value, "Functor does not accept a single int as argument, requires two ints"); static_assert( - rclcpp::check_arguments::value, + rclcpp::function_traits::check_arguments::value, "Functor accepts two ints as arguments"); static_assert( - !rclcpp::check_arguments::value, + !rclcpp::function_traits::check_arguments::value, "Functor does not accept a bool and an int as arguments, requires two ints"); static_assert( - !rclcpp::check_arguments::value, + !rclcpp::function_traits::check_arguments::value, "Functor does not accept an int and a char as arguments, requires two ints"); static_assert( - rclcpp::check_arguments::value, + rclcpp::function_traits::check_arguments::value, "Functor accepts an int and a char as arguments"); // Test lambdas @@ -365,28 +378,28 @@ TEST(TestFunctionTraits, check_arguments) { }; static_assert( - rclcpp::check_arguments::value, + rclcpp::function_traits::check_arguments::value, "Functor accepts an int as the only argument"); static_assert( - rclcpp::check_arguments::value, + rclcpp::function_traits::check_arguments::value, "Functor accepts two ints as arguments"); static_assert( - rclcpp::check_arguments::value, + rclcpp::function_traits::check_arguments::value, "Functor accepts an int and a char as arguments"); // Test objects that have a call operator static_assert( - rclcpp::check_arguments::value, + rclcpp::function_traits::check_arguments::value, "Functor accepts an int as the only argument"); static_assert( - rclcpp::check_arguments::value, + rclcpp::function_traits::check_arguments::value, "Functor accepts two ints as arguments"); static_assert( - rclcpp::check_arguments::value, + rclcpp::function_traits::check_arguments::value, "Functor accepts an int and a char as arguments"); } @@ -406,27 +419,36 @@ TEST(TestFunctionTraits, same_arguments) { }; static_assert( - rclcpp::same_arguments::value, + rclcpp::function_traits::same_arguments< + decltype(lambda_one_int), decltype(func_one_int) + >::value, "Lambda and function have the same arguments"); static_assert( - !rclcpp::same_arguments::value, + !rclcpp::function_traits::same_arguments< + decltype(lambda_two_ints), decltype(func_one_int) + >::value, "Lambda and function have different arguments"); static_assert( - !rclcpp::same_arguments::value, + !rclcpp::function_traits::same_arguments< + decltype(func_one_int_one_char), decltype(func_two_ints) + >::value, "Functions have different arguments"); static_assert( - !rclcpp::same_arguments::value, + !rclcpp::function_traits::same_arguments< + decltype(lambda_one_int), decltype(lambda_two_ints) + >::value, "Lambdas have different arguments"); static_assert( - rclcpp::same_arguments::value, + rclcpp::function_traits::same_arguments::value, "Functor and function have the same arguments"); static_assert( - rclcpp::same_arguments::value, + rclcpp::function_traits::same_arguments< + FunctionObjectTwoInts, decltype(lambda_two_ints)>::value, "Functor and lambda have the same arguments"); }