[Mono-dev] sigc++ signal C# wraper

Jonathan Pryor jonpryor at vt.edu
Mon Oct 29 22:10:13 EDT 2007


On Mon, 2007-10-29 at 20:58 -0300, buhochileno at gmail.com wrote:
> About the sigc++ C# wraper, please be patient with me, I love 
> programming and I really want to learn this kind of wraper (in other 
> situation I write little wraper for common inpout/output function), and 
> you must know that this is a very difcult subject to find 
> documentation...

I used:

http://libsigc.sourceforge.net/libsigc2/docs/reference/html/group__ptr__fun.html
http://libsigc.sourceforge.net/libsigc2/docs/manual/html/index.html
http://www.mono-project.com/Dllimport

> and also recently I learn how manipulate signal/slot in 
> C++ code (I know how to work and define Delegates and Events in C#).
> 
> First I try to write your test code only to see if I can understad and 
> compile the code, so I write the aliendetector.cc class like:
> struct AlienDetector { sigc::signal<void> signal_detected;};
> extern "C" void* CreateAlienDetector (){ return new AlienDetector ();}
> void (*SignalDetectedHandler) ();

As I said, this was untested.  The above *should* be:

	typedef void (*SignalDetectedHandler) ();

Otherwise it declares the variable SignalDetectedHandler instead of
declaring the _type_ SignalDetectedHandler.

> extern "C" void AlienDetector_AddSignalDetected (AlienDetector* 
> detector, SignalDetectedHandler handler)
> {detector->signal_detected.connect (sigc::ptr_fun(handler));
> }
> 
> And I add a little dummy "main" only to see if a traditional compiling 
> command work:
> g++ aliendetector.cc -o aliendetector `pkg-config --cflags --libs 
> sigc++-2.0`
> //I get...
> aliendetector.cc:15: error: ‘SignalDetectedHandler’ is not a type
> aliendetector.cc: In function ‘void 
> AlienDetector_AddSignalDetected(AlienDetector*, int)’:
> aliendetector.cc:17: error: no matching function for call to ‘ptr_fun(int&)’
> The second error may be is related to the firs ‘SignalDetectedHandler’ 
> is not a type, I try diferent stuff, but I don't know exactly what can I 
> change...

The second error is probably related to the first.

> Anyway and only to move forward in the building procees of a shared 
> library, I comment the "void (*SignalDetectedHandler) ();" stuff and the 
> compilation work, but I have some doubts about it:
> 
> I use the following command to get the object code:
> g++ -fpic -c aliendetector.cc -Wall -g `pkg-config --cflags --libs 
> sigc++-2.0` //for the object code without main

When using -c you don't need `pkg-config --libs`, just `pkg-config
--cflags`.

> So later I do a :
> g++ -o libaliendetector.so aliendetector.o -shared -Wl,-Bsymbolic -g 
> -lsigc-2.0

I think you'll need a `pkg-config --libs sigc++-2.0` here, instead of
-lsigc-2.0.

> Also I want to show you a little of my work:
> I write this little test header WebCamOrbit.h:
> #include <sigc++/sigc++.h>
> class WebCamOrbit
> {public:
> WebCamOrbit(); virtual ~WebCamOrbit();
> virtual void SetPanTilt(int, int);
> //signal accessor:
> typedef sigc::signal<void, int, int> type_OnMove;
> type_OnMove OnMove();

OnMove should return `type_OnMove&' instead of `type_OnMove', so that
later calls in "Consumer" classes like wcOrbit.OnMove().connect(...)
don't make an extra copy of m_OnMove and instead modify the variable
directly.

> this work like a charm in C++, and I need to write a wrapper for the 
> signal exposed by the WebCamOrbit class, so I understend that I have to 
> build a shared library with this class right?

Not necessarily, but you should. :-)

> , and then in some how 
> write the C# wrapper to allow to a C# programmer to access to this event 
> in a traditional .NET/mono way (with delegates and events). Do you have 
> any documentation?, sugestion?.

See above URLs.

 - Jon





More information about the Mono-devel-list mailing list