【问题标题】:Boost.Python and Boost.FunctionBoost.Python 和 Boost.Function
【发布时间】:2015-05-31 03:37:29
【问题描述】:

我想包装一个boost::function 类成员,以便它可以按以下方式使用:

using namespace boost;
using namespace boost::python;

struct gui_button_t
{
    function<void()> on_pressed;
};

class_<gui_button_t>("GuiButton", init<>())
    .def("on_pressed", &gui_button_t::on_pressed);

然后在 Python 中:

def callback_function():
    print 'button has been pressed'

button = GuiButton()
button.on_pressed = callback_function
button.on_pressed() # function should be callable from C++ or Python

但是,尝试这样做会产生大量关于类模板参数等的错误。

我进行了一些搜索,但未能找到我一直在寻找的答案。下面的文章有点接近,但他们没有直接触及这个主题。

http://bfroehle.com/2011/07/18/boost-python-and-boost-function-ii/

我在这里做错了什么?我需要做什么才能获得此功能所需的界面?

非常感谢。

【问题讨论】:

标签: python c++ function boost


【解决方案1】:

Boost.Python 只接受指向函数的指针和指向成员函数的指针。所以我们需要做的就是将我们的可调用对象转换为函数指针。这里的关键思想是

  1. 可以将没有捕获的 lambda 转换为函数指针(通过 sorcery
  2. 函数指针的解释方式与 Python 中的成员函数相同:第一个参数是 self

所以在你的情况下,我们需要做的是生成这个 lambda:

+[](gui_button_t* self) {
    self->on_pressed();
}

您已经可以将它与 Boost.Python 一起使用,因为这是一个完全正常的函数指针。但是,我们需要一个适用于任何可调用成员的解决方案。当您可以支持任何事情时,为什么支持boost::function

我们将从@Columbo's closure_traits 开始,但另外添加了一种提取参数列表的方法;

template <typename...> struct typelist { };

template <typename C, typename R, typename... Args>                        \
struct closure_traits<R (C::*) (Args... REM_CTOR var) cv>                  \
{                                                                          \
    using arity = std::integral_constant<std::size_t, sizeof...(Args) >;   \
    using is_variadic = std::integral_constant<bool, is_var>;              \
    using is_const    = std::is_const<int cv>;                             \
                                                                           \
    using result_type = R;                                                 \
                                                                           \
    template <std::size_t i>                                               \
    using arg = typename std::tuple_element<i, std::tuple<Args...>>::type; \
                                                                           \
    using args = typelist<Args...>;                                        \
};

然后我们将为任何可调用成员编写一个包装器。由于我们的 lambda 可以不捕获,我们必须将可调用对象作为模板参数:

template <typename CLS, typename F, F CLS::*callable>
class wrap { ... };

我将使用 C++14 的 auto 返回类型推导来节省一些输入。我们创建了一个顶级的make_pointer() 静态成员函数,它只转发给一个额外接受参数的辅助成员函数。完整的wrap 看起来像:

template <typename CLS, typename F, F CLS::*callable>
class wrap {
public:
    static auto make_pointer() {
        return make_pointer_impl(typename closure_traits<F>::args{});
    }

private:
    template <typename... Args>
    static auto make_pointer_impl(typelist<Args...> ) {
        // here is our lambda that takes the CLS as the first argument
        // and then the rest of the callable's arguments,
        // and just calls it
        return +[](CLS* self, Args... args) {
            return (self->*callable)(args...);
        };
    }
};

我们可以用它来包装你的按钮:

void (*f)(gui_button_t*) = wrap<gui_button_t, 
                                decltype(gui_button_t::on_pressed),
                                &gui_button_t::on_pressed
                                >::make_pointer();

这有点冗长和重复,所以让我们做一个宏(叹息):

#define WRAP_MEM(CLS, MEM) wrap<CLS, decltype(CLS::MEM), &CLS::MEM>::make_pointer()

所以我们得到:

void (*f)(gui_button_t*) = WRAP_MEM(gui_button_t, on_pressed);

f(some_button); // calls some_button->on_pressed()

由于这给了我们一个指向函数的指针,我们可以直接将它与普通的 Boost.Python API 一起使用:

class_<gui_button_t>("GuiButton", init<>())
    .def("on_pressed", WRAP_MEM(gui_button_t, on_pressed));

Demo 演示了指向成员 std::function 和带有 operator() 的成员 struct 的函数指针。


以上内容使您能够公开一个可调用对象。如果您还希望能够做作业,即:

button = GuiButton()
button.on_pressed = callback_function
button.on_pressed()

我们需要做点别的。您不能在 Python 中以有意义的方式公开operator=,因此要支持上述功能,您必须改写__setattr__。现在,如果您愿意:

button.set_on_pressed(callback_function)

我们可以扩展上面的wrap 解决方案来添加一个setter,它的实现将是,在上面的脉络中:

static auto set_callable() {
    return make_setter_impl(
        typelist<typename closure_traits<F>::result_type>{},
        typename closure_traits<F>::args{});
}

template <typename R, typename... Args>
static auto make_setter_impl(typelist<R>, typelist<Args...> ) {
    return +[](CLS* self, py::object cb) {
        (self->*callable) = [cb](Args... args) {
            return py::extract<R>(
                cb(args...))();
        };
    };
}

// need a separate overload just for void
template <typename... Args>
static auto make_setter_impl(typelist<void>, typelist<Args...> ) {
    return +[](CLS* self, py::object cb) {
        (self->*callable) = [cb](Args... args) {
            cb(args...);
        };
    };
}

#define SET_MEM(CLS, MEM) wrap<CLS, decltype(CLS::MEM), &CLS::MEM>::set_callable()

然后您可以通过以下方式公开:

.def("set_on_pressed", SET_MEM(button, on_pressed))

但是,如果您坚持支持直接分配,那么您需要另外公开以下内容:

static void setattr(py::object obj, std::string attr, py::object val)
{
     if (attr == "on_pressed") {
         button& b = py::extract<button&>(obj);
         SET_MEM(button, on_pressed)(&b, val);
     }
     else {
         py::str attr_str(attr);
         if (PyObject_GenericSetAttr(obj.ptr(), attr_str.ptr(), val.ptr()) {
             py::throw_error_already_set();
         }
     }
}


.def("__setattr__", &button::setattr);

这可行,但您必须为要设置的每个仿函数添加更多案例。如果每个类只有一个类仿函数对象,可能没什么大不了的,甚至可以编写一个更高阶的函数来为给定的属性名称生成特定的setattr-like 函数。但是如果你有多个,它会比简单的set_on_pressed 解决方案更糟糕。


如果 C++14 不可用,我们将不得不明确指定 make_pointer 的返回类型。我们需要一些方便的类型特征。 concat:

template <typename T1, typename T2>
struct concat;

template <typename T1, typename T2>
using concat_t = typename concat<T1, T2>::type;

template <typename... A1, typename... A2>
struct concat<typelist<A1...>, typelist<A2...>> {
    using type = typelist<A1..., A2...>;
};

然后将返回类型和typelist 转换为函数指针:

template <typename R, typename T>
struct make_fn_ptr;

template <typename R, typename... Args>
struct make_fn_ptr<R, typelist<Args...>> {
    using type = R(*)(Args...);
};

template <typename R, typename T>
using make_fn_ptr_t = typename make_fn_ptr<R, T>::type;

然后在wrap 中,我们可以定义一个结果类型为:

using R = make_fn_ptr_t<
                typename closure_traits<F>::result_type,
                concat_t<
                    typelist<CLS*>,
                    typename closure_traits<F>::args
                    >
                >;

并使用它代替autoC++11 Demo.

【讨论】:

  • 哇,反应快!我现在必须坐飞机去上班,但我会在今天晚些时候回顾一下。 :)
  • 我正在针对 Visual Studio 2013 进行编译,该版本使用 auto 执行返回类型推导的方式更加笨拙。尝试编译发布的代码时出现两个错误:error C3551: expected a trailing return type on static auto make_pointer()static auto make_pointer_impl(typelist&lt;Args...&gt;)
  • @cmbasnett 添加了在 C++11 上运行所需的代码。
  • 谢谢!它似乎可以工作(它至少可以编译),但我今天早上只有大约 5 分钟的时间来试用它,仍然需要看看 C++ 是否可以触发分配的 Python 函数。我回家后肯定会尝试一下!在那之后,我将尝试剖析所有正在发生的神秘模板:P
  • 好的,所以我试了一下,但我认为这个谜题缺少一个关键元素。即,能够将 Python 函数分配给包装的函数对象,以便当 C++ 触发 on_pressed 事件时,它运行我通过调用此定义的 Python 函数:button.on_pressed = callback_function where @987654366 @ 定义为def callback_function(button): print 'yo'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多