简体   繁体   English

如何使用boost.python导出复杂类

[英]How to export complex class with boost.python

I have an event class I am trying to make accessible in python using Boost.Python. 我有一个事件类,我试图使用Boost.Python在python中访问。 Here is the listing, along with associated macros and an example derived event class. 这是列表,以及相关的宏和示例派生事件类。

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);
};

The intention is I want to be able to derive python based event classes from this one, however the mechanism for declaring and using event parameters is embedded in the macros. 目的是我希望能够从这个派生基于python的事件类,但是声明和使用事件参数的机制嵌入在宏中。 Frankly I don't think the way it is setup in C++ will work in python anyway, as any parameters an event would have in python would likely be stored in a dictionary. 坦率地说,我不认为它在C ++中的设置方式无论如何都会在python中运行,因为事件在python中的任何参数都可能存储在字典中。

Is there a way to export this functionality using boost.python, or is there a better way to design the class for generic parameter support that will work well in C++ and in Python? 有没有办法使用boost.python导出此功能,还是有更好的方法来设计适用于C ++和Python的通用参数支持的类?

Ultimately there's no real way for Python to create new C++ types, which is essentially what you're asking. 最终,Python没有真正的方法来创建新的C ++类型,这实际上就是你所要求的。 If you want to be able to create new event types in Python which are on equal footing with what you can create in C++, then you'll need a more dynamic, Python-like structure in C++. 如果您希望能够在Python中创建与C ++中创建的新事件类型相同的新事件类型,那么您将需要在C ++中使用更加动态的类似Python的结构。

If your requirement really is that this works only in C++ and Python (ie you don't intend to extend this to other languages) then you might consider storing your event parameters in a boost::python::dict instead of as individual data members. 如果您的要求确实只适用于C ++和Python(即您不打算将其扩展到其他语言),那么您可以考虑将您的事件参数存储在boost::python::dict而不是作为单独的数据成员。 You'll need to use boost::python::extract<> in C++ to access the values, but otherwise it's a pretty clean system. 您需要在C ++中使用boost::python::extract<>来访问值,否则它是一个非常干净的系统。 (And you can probably hide the extract call inside a templated accessor method.) (并且您可以在模板化的访问器方法中隐藏提取调用。)

Another option is to access event parameters with a getter, eg Event::get_param<T>("name") . 另一种选择是使用getter访问事件参数,例如Event::get_param<T>("name") If you make this virtual then Python-based implementation of events can provide their own version. 如果你使这个虚拟化,那么基于Python的事件实现可以提供自己的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM