【问题标题】:How to export complex class with boost.python如何使用 boost.python 导出复杂类
【发布时间】:2014-02-28 21:20:58
【问题描述】:

我有一个事件类,我正在尝试使用 Boost.Python 在 python 中进行访问。这是清单,以及相关的宏和一个示例派生事件类。

class Event
{
  private:
    unsigned __int64 m_TimeStamp;

  public:
    // Empty parameter struct used as base for event parameters
    //  in derived classes.
    struct Params { };

    Event();

    virtual unsigned int Type() const = 0;
    virtual const char* TypeName() const = 0;

    unsigned __int64 TimeStamp() const { return m_TimeStamp; }
};

typedef shared_ptr<Event> EventPtr;

#define DECLARE_EVENTTYPE(typeName, id)                                   \
  enum { TYPE = id };                                                     \
  unsigned int Type() const     { return static_cast<unsigned int>(id); } \
  const char* TypeName() const  { return typeName;          }

// Macro used to declare the start of an event parameters structure
#define START_EVENTPARAMS()     struct Params : public Event::Params {

// Macro used to finish the event parameters structure declaration
#define END_EVENTPARAMS(eventName)                                        \
  } m_Params;                                                             \
  eventName(const Event::Params* pkParams = NULL) : Event()               \
  {                                                                       \
    if (NULL != pkParams)                                                 \
    {                                                                     \
      const eventName::Params* pkEventParams =                            \
        reinterpret_cast<const eventName::Params*>(pkParams);             \
      if (NULL != pkEventParams)                                          \
        m_Params = *pkEventParams;                                        \
    }                                                                     \
  }                                                                       \
  static const eventName::Params& Parameters(const EventPtr pkEvent)      \
  {                                                                       \
    const shared_ptr<eventName> pkErrorEvent(                             \
      dynamic_pointer_cast<eventName>(pkEvent));                          \
    return pkErrorEvent->m_Params;                                        \
  }

// Macro used for events that have no parameters
#define EVENT_NOPARAMS(eventName)                                         \
  eventName(const Event::Params*) : Event() { }

struct ExampleEvent : public Event
{
  START_EVENTPARAMS()
    std::string m_strMsg;
  END_EVENTPARAMS(ExampleEvent)

  DECLARE_EVENTTYPE("ExampleEvent", 1);
};

我的目的是我希望能够从中派生基于 python 的事件类,但是声明和使用事件参数的机制嵌入在宏中。坦率地说,我认为 C++ 中的设置方式无论如何都不会在 python 中运行,因为事件在 python 中的任何参数都可能存储在字典中。

有没有办法使用 boost.python 导出这个功能,或者有更好的方法来设计类以支持在 C++ 和 Python 中很好地工作的泛型参数支持?

【问题讨论】:

    标签: c++ boost-python


    【解决方案1】:

    最终,Python 没有真正的方法来创建新的 C++ 类型,这基本上就是您所要求的。如果您希望能够在 Python 中创建与在 C++ 中创建的新事件类型平等,那么您将需要在 C++ 中更动态的、类似 Python 的结构。

    如果您确实要求这仅适用于 C++ 和 Python(即您不打算将其扩展到其他语言),那么您可以考虑将事件参数存储在 boost::python::dict 中,而不是作为单独的数据成员。您需要在 C++ 中使用 boost::python::extract&lt;&gt; 来访问这些值,否则它是一个非常干净的系统。 (并且您可能可以将提取调用隐藏在模板化访问器方法中。)

    另一种选择是使用 getter 访问事件参数,例如Event::get_param&lt;T&gt;("name")。如果您将此虚拟化,则基于 Python 的事件实现可以提供自己的版本。

    【讨论】:

      猜你喜欢
      • 2014-07-10
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      • 2017-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多