I have an event class I am trying to make accessible in python using Boost.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. 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.
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?
Ultimately there's no real way for Python to create new C++ types, which is essentially what you're asking. 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++.
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. You'll need to use boost::python::extract<>
in C++ to access the values, but otherwise it's a pretty clean system. (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")
. If you make this virtual then Python-based implementation of events can provide their own version.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.