Merge pull request #183 from ros2/std_bind_function_traits

Added support for std::bind (redux)
This commit is contained in:
Esteve Fernandez 2015-12-15 17:32:29 -08:00
commit 534ae69ed5
2 changed files with 44 additions and 0 deletions

View file

@ -15,6 +15,7 @@
#ifndef RCLCPP__FUNCTION_TRAITS_HPP_
#define RCLCPP__FUNCTION_TRAITS_HPP_
#include <functional>
#include <memory>
#include <tuple>
@ -77,6 +78,20 @@ template<typename ReturnTypeT, typename ... Args>
struct function_traits<ReturnTypeT (*)(Args ...)>: function_traits<ReturnTypeT(Args ...)>
{};
// std::bind
template<typename ClassT, typename ReturnTypeT, typename ... Args, typename ... FArgs>
#if defined _LIBCPP_VERSION // libc++ (Clang)
struct function_traits<std::__1::__bind<ReturnTypeT (ClassT::*)(Args ...), FArgs ...>>
#elif defined __GLIBCXX__ // glibc++ (GNU C++)
struct function_traits<std::_Bind<std::_Mem_fn<ReturnTypeT (ClassT::*)(Args ...)>(FArgs ...)>>
#elif defined _MSC_VER // MS Visual Studio
struct function_traits<
std::_Binder<std::_Unforced, ReturnTypeT(__cdecl ClassT::*)(Args ...), FArgs ...>
>
#endif
: function_traits<ReturnTypeT(Args ...)>
{};
// Lambdas
template<typename ClassT, typename ReturnTypeT, typename ... Args>
struct function_traits<ReturnTypeT (ClassT::*)(Args ...) const>

View file

@ -14,6 +14,7 @@
#include <gtest/gtest.h>
#include <functional>
#include <string>
#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<decltype(bind_one_bool)>::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<FunctionObjectOneIntOneChar, int, char>::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<decltype(bind_one_bool), bool>::value,
"Functor accepts a single bool as arguments");
}
/*