【发布时间】: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