[C++-sig] boost.python Handling python objects in c++ efficiently

Furkan Kuru furkankuru at gmail.com
Fri Apr 18 17:24:18 CEST 2008


Hello,

I have a message queue and message handling system that is fed from python
side and consumed by c++ side.
The messages are not independent so they have to be handled in order.
I go over the list every frame and handle the message objects one by one.
The messages inherit from a common class and all message classes are exposed
to python.
There are two problems. The first problem is when I add a new message I have
to change my message manager code adding a few more lines to check type.
(which seems ugly)
The second problem is that in order to assure the order of messages I go
over the python list twice and I take objects using [] operator which is
slow I think. ( I do this every frame and even if most of the time message
queue is empty it is annoying)
Any help is appreciated.

the code looks like this (not same):

boost::python::list actList ;

actList=boost::python::extract<boost::python::list> (actListObj);

int len=boost::python::len(actList);

for(int i=0;i<len;i++)

{

boost::python::object cur = actList[i];

boost::python::extract<Message1&> m1 (cur);

if(m1.check()) {

activeHandlers.push_back(new Message1Handler(m1(), params);

continue;

}

boost::python::extract<Message2&> m2 (cur);

if (m2.check()) {

activeHandlers.push_back(new Message2Handler(m1(), params);

continue;

}

}

// finally clear the list

for(int i=0;i<len;i++)

{

actList.pop();

}


-- 
Furkan Kuru
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20080418/efaed084/attachment.htm>


More information about the Cplusplus-sig mailing list