How to export a complex class with boost.python

I have an event class that I am trying to make available in python using Boost.Python. Here is a list, as well as related macros and an example 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 goal is that I want to be able to derive python-based event classes from this, however the mechanism for declaring and using event parameters is built into macros. Honestly, I do not think that the way it is configured in C ++ will work in python anyway, since any parameters that the event would have in python are likely to be stored in a dictionary.

Is there a way to export this function using boost.python or is there a better way to create a class to support common parameters that will work well in C ++ and Python?

+3
1

Python ++, , , . Python, , ++, , Python ++.

, ++ Python (.. ), boost::python::dict . boost::python::extract<> ++ , . ( , , .)

, . Event::get_param<T>("name"). , Python .

0

All Articles