[C++-sig] extending with C++ and typeinfo question

John ripsnorta_1 at hotmail.com
Mon Jan 5 00:45:51 CET 2004


Hi,

(I'm not sure this worked the first time. My apologies if it did.)

I am attempting to implement a scripting language for my application by
embedding the python interpreter and exposing an API to python. So far so
good.

My application is heavily event driven, I have developed a publish/subscribe
style event manager to which classes subscribe to published events. Frex:

class i_subscriber
{
    void on_event(event* e, event_type* et) = 0;
};

class x : public i_subscriber
{
    x()
    {
        SUBSCRIBE(this, event_startup)
    }

    void on_event(event* e, event_type* et)
    {
        if (IS_EVENT(event_startup, et)) { .... }
    }
}

when a event_startup event is generated, the event manager scoots through
all the subscribers for that particular event and calls the on_event handler
which determines the event type and handles it appropriately. The system
depends heavily on rtti to work.

Now what I want to do is to expose this system to my Python scripts. I'd
like to have a Python class subscribe to an event, and when that event is
published, for the script to be notified and be able to handle it. Frex:

class y(i_subscriber):    # the subscriber interface has been exposed by
boost.python
    def __init__(self):
        subscribe(self, event_startup) # subscribe is an exposed API

    def __del__(self):
        unsubscribe(self) # unsubscribe from all events

    def on_event(evt, evt_type):
        # handle the event here, but how do I determine what the
        # type is?

y_inst = y()

These are the areas I am having trouble getting my head around at the
moment.

The main application needs to register the Python class and know that it
is_a subscriber. This means I need to transmit type information for both the
subscriber and the event through the interpreter.

When an event is published it's not known if the subscriber is c++ or
python. If I call on_event on the subscriber interface for a python class is
there some marshalling that I need to do?

How do I transmit the type of a published event through the interpreter so
that my on_event handler knows what event is being processed?

I've been looking through the Python and Boost.Python documentation and so
far I haven't seen what I need to do to get this working. I'm probably just
missing something simple. Any pointers would be helpful. Of course I might
be blowing against the wind here. That would be good to know too. :-)

Has anyone here attempted to do this before? Are there any other web sites
out there where this may have been attempted?

Cheers

John








More information about the Cplusplus-sig mailing list