[Mono-dev] libsigc++ signal wrapper

Jonathan Pryor jonpryor at vt.edu
Sun Oct 28 22:05:05 EDT 2007

On Sun, 2007-10-28 at 22:39 -0300, Mauricio Henriquez wrote:
>   Currently, I need to write a wrapper to allow a C# program to connect 
> to a signal exposed in a class that use the libsigc++ library, and I 
> want to ask if you have any idea, suggestion or documentation related to 
> this kind of stuff??

Events are classes of type sigc::signal, and new events are added via
sigc::signal::connect(const sigc::slot_type&).  So you need to a way to
get a sigc::slot_type.

Conveniently enough, libsigc++ provides sigc::ptr_fun() which takes a
pointer to a function and creates a sigc::slot_type for it.

So something like this (untested) code should suffice:

	// C++
	struct AlienDetector {
	  sigc::signal<void> signal_detected;

	extern "C" void* CreateAlienDetector ()
	    return new AlienDetector ();

	void (*SignalDetectedHandler) ();

	extern "C" void AlienDetector_AddSignalDetected (
		AlienDetector* detector,
		SignalDetectedHandler handler)
	    detector->signal_detected.connect (sigc::ptr_fun(handler));

	// C#
	using System.Runtime.InteropServices;
	delegate void SignalDetectedHandler ();
	class Demo {
	    [DllImport ("...")] static extern IntPtr
	        CreateAlienDetector ();
	    [DllImport ("...")] static extern void
	        AlienDetector_AddSignalDetected (
	            IntPtr detector, SignalDetectedHandler h);

	    static void MyHandler ()
	        Console.WriteLine ("Managed Code Invoked!");

	    static void Main ()
	        IntPtr d = CreateAlienDetector ();
	        AlienDetector_AddSignalDetected (d, MyHandler);

 - Jon

More information about the Mono-devel-list mailing list