Hi
being an enthusiastic user of boost python, I often missed the possibility to 
use function objects, see
http://www.boost.org/doc/libs/1_55_0/libs/python/todo.html#wrapping-function-objects.

Now I succeeded in a workaround to do so. The code below allows to use function 
objects, lambdas and std::function<> with arbitrary signature. It should work 
for c++11 compliant compilers (declspec must be implemented), I tested it with 
Visual Studio 2012 and gcc4.8.1. For the latter the code must be included prior 
to #include <boost/python.hpp>.

Up to now it didn't break my existing code and the definitions with lambdas 
work as expected.

Regards
Martin
------------------------------------------------------------------------------------------

#ifndef BOOST_NO_CXX11_DECLTYPE

#  include <boost/mpl/erase.hpp>
#  include <boost/mpl/next.hpp>
#  include <boost/utility.hpp>
#  include <boost/function_types/is_callable_builtin.hpp>
#  include <boost/mpl/size.hpp>
#  include <boost/mpl/greater.hpp>
#  include <boost/mpl/size_t.hpp>

namespace boost { namespace python { namespace detail {
template<typename T, typename Enable = void>
struct fun_sign
{
  typedef T type;
};

template<typename T>
struct fun_sign<
  T,
  typename enable_if<
    mpl::greater<
      mpl::size<T>,
      mpl::size_t<1>
    >
  >::type
>
{
  typedef typename mpl::erase<
    T,
    typename mpl::next<
      typename mpl::begin<
        T
      >::type
    >::type
  >::type type;
};

template<typename T>
typename disable_if<typename function_types::is_callable_builtin<T>, typename 
fun_sign<typename 
function_types::components<decltype(&T::operator())>::types>::type>::type
get_signature(T, void* = 0)
{
  return typename fun_sign<typename 
function_types::components<decltype(&T::operator())>::types>::type();
}

}}}
//#endif
#ifndef BOOST_NO_CXX11_DECLTYPE

#  include <boost/mpl/erase.hpp>
#  include <boost/mpl/next.hpp>
#  include <boost/utility.hpp>
#  include <boost/function_types/is_callable_builtin.hpp>
#  include <boost/mpl/size.hpp>
#  include <boost/mpl/greater.hpp>
#  include <boost/mpl/size_t.hpp>

namespace boost { namespace python { namespace detail {
template<typename T, typename Enable = void>
struct fun_sign
{
  typedef T type;
};

template<typename T>
struct fun_sign<
  T,
  typename enable_if<
    mpl::greater<
      mpl::size<T>,
      mpl::size_t<1>
    >
  >::type
>
{
  typedef typename mpl::erase<
    T,
    typename mpl::next<
      typename mpl::begin<
        T
      >::type
    >::type
  >::type type;
};

template<typename T>
typename disable_if<typename function_types::is_callable_builtin<T>, typename 
fun_sign<typename 
function_types::components<decltype(&T::operator())>::types>::type>::type
get_signature(T, void* = 0)
{
  return typename fun_sign<typename 
function_types::components<decltype(&T::operator())>::types>::type();
}

}}}
//#endif
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
https://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to