[C++-sig] Fwd: Help - Callbacks

Richard M Norton rmn201 at ecs.soton.ac.uk
Fri Apr 30 15:25:44 CEST 2004


Hello, I’m having difficulties concerning using callbacks that have been 
extended in python. I’m using the windows video API DirectShow to play video 
to a screen through a transform method. The problem occurs when the 
Transform method is called from an object extended in python as a callback, 
not when called explicitly. How do I represent this functionality and 
prevent the python interpreter from crashing? I do not think the problem in 
this case is from the live times of the objects, but I may be wrong. Is 
using boost’s function and bind features a solution, and if so how?

Many Thanks,
Richard

// Cut down version of the C++ code

class Callback
{
	virtual void Transform() = 0;
}

class Player
{
	Player(Callback *);
	void Play();	// Play the video to the screen, returns allowing 
video to continue play
}

class Callback_Wrap : public Callback
{
	Callback_Wrap(PyObject *obj) : self(obj) {}
	virtual Transform() {call_method<void>(self, “Transform”);}
	PyObject *self;
}

BOOST_PYTHON_MODULE(MyVideo)
{
	class_<Callback, boost::noncopyable, 
boost::shared_ptr<Callback_Wrap> >(“Callback”)
		.def(“Transform”, &Callback::Transform)
		;

	class_<Player>(“Player”, init<Calback *>())
		.def(“Play”, &Player::Play)
		;
}


# Example python use

from MyVideo import *

class My_Callback(Callback):
	def Transform(self):
		print “Hello, My_Callback here!”

a = My_Callback()
b = Player(a)
a.Play()








More information about the Cplusplus-sig mailing list