boost python重载operator()

Eon*_*vin 5 binding operator-overloading boost-python

我想使用Boost :: Python绑定operator(),但我真的不知道如何做到这一点.考虑这个例子:

C++:

class Queuer
{ 

public:
void Queuer::operator()(const qfc::Queue & iq, const qfc::Message & im) const;
void Queuer::operator()(const qfc::Agent & ia, const qfc::Message & im) const;
// some other overloaded operator() methods

};
Run Code Online (Sandbox Code Playgroud)

所以在Python脚本中,导入我正在使用的模块(称为qfc)后,我想这样做:

蟒蛇:

>>> queuer = qfc.Queuer()
// instantiating a Message an Agent and a Queue object
>>> queuer(queue,message)
>>> queuer(agent,message)
>>> ...
Run Code Online (Sandbox Code Playgroud)

你对这个怎么办了吗?也许用boost :: python调用<>?

谢谢你,凯文

Tan*_*ury 7

在公开Queuer类时,__call__为每个Queuer::operator()成员函数定义一个方法.Boost.Python将根据类型处理适当的调度.使用指向成员函数的语法引入了唯一的复杂性,因为调用者需要消除歧义&Queuer::operator().

另外,当尝试将Python中的派生类传递给带有Base类参数的C++函数时,需要向Boost.Python公开一些额外的信息:

  • 需要公开基础C++类class_.例如,class_<BaseType>("Base").
  • 派生类在暴露时需要显式列出其基类bases_.例如,class_<DerivedType, bases<BaseType> >("Derived").有了这些信息,Boost.Python可以在调度时进行适当的转换.

这是一个完整的例子:

#include <iostream>

#include <boost/python.hpp>

// Mockup classes.
struct AgentBase   {};
struct MessageBase {};
struct QueueBase   {};
struct SpamBase    {};
struct Agent:   AgentBase   {};
struct Message: MessageBase {};
struct Queue:   QueueBase   {};
struct Spam:    SpamBase    {};

// Class with overloaded operator().
class Queuer
{ 
public:

  void operator()(const AgentBase&, const MessageBase&) const
  {
    std::cout << "Queuer::operator() with Agent." << std::endl;
  }

  void operator()(const QueueBase&, const MessageBase&) const
  {
    std::cout << "Queuer::operator() with Queue." << std::endl;
  }

  void operator()(const SpamBase&, const MessageBase&) const
  {
    std::cout << "Queuer::operator() with Spam." << std::endl;
  }
};

/// Depending on the overlaod signatures, helper types may make the
/// code slightly more readable by reducing pointer-to-member-function syntax.
template <typename A1>
struct queuer_overload
{
  typedef void (Queuer::*type)(const A1&, const MessageBase&) const;
  static type get(type fn) { return fn; }
};

BOOST_PYTHON_MODULE(example)
{
  namespace python = boost::python;
  // Expose only the base class types.  Do not allow the classes to be
  // directly initialized in Python.
  python::class_<AgentBase  >("AgentBase",   python::no_init);
  python::class_<MessageBase>("MessageBase", python::no_init);
  python::class_<QueueBase  >("QueueBase",   python::no_init);
  python::class_<SpamBase   >("SpamBase",    python::no_init);

  // Expose the user types.  These classes inerit from their respective
  // base classes.
  python::class_<Agent,   python::bases<AgentBase>   >("Agent");
  python::class_<Message, python::bases<MessageBase> >("Message");
  python::class_<Queue,   python::bases<QueueBase>   >("Queue");
  python::class_<Spam,    python::bases<SpamBase>    >("Spam");

  // Disambiguate via a varaible.
  queuer_overload<AgentBase>::type queuer_op_agent = &Queuer::operator();

  python::class_<Queuer>("Queuer")
    // Disambiguate via a variable.
    .def("__call__", queuer_op_agent)
    // Disambiguate via a helper type.
    .def("__call__", queuer_overload<QueueBase>::get(&Queuer::operator()))
    // Disambiguate via explicit cast.
    .def("__call__",
         static_cast<void (Queuer::*)(const SpamBase&, 
                                      const MessageBase&) const>(
             &Queuer::operator()))
    ;
}
Run Code Online (Sandbox Code Playgroud)

它的用法:

>>> import example
>>> queuer = example.Queuer()
>>> queuer(example.Agent(), example.Message())
Queuer::operator() with Agent.
>>> queuer(example.Queue(), example.Message())
Queuer::operator() with Queue.
>>> queuer(example.Spam(), example.Message())
Queuer::operator() with Spam.
Run Code Online (Sandbox Code Playgroud)