Using g_signal_connect in class

View: New views
9 Messages — Rating Filter:   Alert me  

Using g_signal_connect in class

by Marco Rocco :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello, this is my first post on this mailing list, now i do my request:
can i use g_signal_connect  in a method of my class, using as c_handler
a private function of class? ...and if i can, how i can do ?

_______________________________________________
gtk-list mailing list
gtk-list@...
http://mail.gnome.org/mailman/listinfo/gtk-list

Re: Using g_signal_connect in class

by Murray Cumming :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, 2008-07-15 at 12:04 +0200, Marco Rocco wrote:
> Hello, this is my first post on this mailing list, now i do my request:
> can i use g_signal_connect  in a method of my class, using as c_handler
> a private function of class? ...and if i can, how i can do ?

You are talking about C++, I believe.

The answer is No, not unless your method is a static method.

gtkmm makes this possible, using libsigc++ slots.

--
murrayc@...
www.murrayc.com
www.openismus.com

_______________________________________________
gtk-list mailing list
gtk-list@...
http://mail.gnome.org/mailman/listinfo/gtk-list

RE: Using g_signal_connect in class

by Vallone, Anthony :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Only if the member function is static.  Calling a non-static member
function requires two addresses: the instance address and the function
address.  Whether its public or private doesn't matter because access to
function pointers is not checked by compilers.  I frequently do
something like this:

class SomeClass
{
  ...
private:
  ...
  static void staticCb(..., gpointer inInstance) {
    SomeClass* instance = (SomeClass*)inInstance;
    instance->callback(...);
  }
  void callback(...) {
    ...
  }
  ...
  void someFunc() {
    ...
    g_signal_connect(..., G_CALLBACK(&staticCb), this);
    ...
  }
  ...
};


-Anthony Vallone


-----Original Message-----
From: gtk-list-bounces@... [mailto:gtk-list-bounces@...] On
Behalf Of Marco Rocco
Sent: Tuesday, July 15, 2008 6:04 AM
To: gtk-list@...
Subject: Using g_signal_connect in class

Hello, this is my first post on this mailing list, now i do my request:
can i use g_signal_connect  in a method of my class, using as c_handler
a private function of class? ...and if i can, how i can do ?

_______________________________________________
gtk-list mailing list
gtk-list@...
http://mail.gnome.org/mailman/listinfo/gtk-list
_______________________________________________
gtk-list mailing list
gtk-list@...
http://mail.gnome.org/mailman/listinfo/gtk-list

Re: Using g_signal_connect in class

by Chris Vine :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, 15 Jul 2008 11:34:41 -0400
"Vallone, Anthony" <anthony.vallone@...> wrote:

> Only if the member function is static.  Calling a non-static member
> function requires two addresses: the instance address and the function
> address.  Whether its public or private doesn't matter because access
> to function pointers is not checked by compilers.  I frequently do
> something like this:
>
> class SomeClass
> {
>   ...
> private:
>   ...
>   static void staticCb(..., gpointer inInstance) {
>     SomeClass* instance = (SomeClass*)inInstance;
>     instance->callback(...);
>   }
>   void callback(...) {
>     ...
>   }
>   ...
>   void someFunc() {
>     ...
>     g_signal_connect(..., G_CALLBACK(&staticCb), this);
>     ...
>   }
>   ...
> };

This will work with gcc/g++ but it is not standard conforming, as
static class functions do not have C linkage, and the C++ standard
states that functions otherwise identical with C++ and C linkage are
different types.

To be standard conforming you should use a friend function (if it needs
access to private data) declared extern "C".

Chris

_______________________________________________
gtk-list mailing list
gtk-list@...
http://mail.gnome.org/mailman/listinfo/gtk-list

Re: Using g_signal_connect in class

by Jean Bréfort :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Le samedi 19 juillet 2008 à 12:29 +0100, Chris Vine a écrit :

> On Tue, 15 Jul 2008 11:34:41 -0400
> "Vallone, Anthony" <anthony.vallone@...> wrote:
>
> > Only if the member function is static.  Calling a non-static member
> > function requires two addresses: the instance address and the function
> > address.  Whether its public or private doesn't matter because access
> > to function pointers is not checked by compilers.  I frequently do
> > something like this:
> >
> > class SomeClass
> > {
> >   ...
> > private:
> >   ...
> >   static void staticCb(..., gpointer inInstance) {
> >     SomeClass* instance = (SomeClass*)inInstance;
> >     instance->callback(...);
> >   }
> >   void callback(...) {
> >     ...
> >   }
> >   ...
> >   void someFunc() {
> >     ...
> >     g_signal_connect(..., G_CALLBACK(&staticCb), this);
> >     ...
> >   }
> >   ...
> > };
>
> This will work with gcc/g++ but it is not standard conforming, as
> static class functions do not have C linkage, and the C++ standard
> states that functions otherwise identical with C++ and C linkage are
> different types.
>
> To be standard conforming you should use a friend function (if it needs
> access to private data) declared extern "C".
>
> Chris

The extern "C" declaration is not needed.

Regards,
Jean

_______________________________________________
gtk-list mailing list
gtk-list@...
http://mail.gnome.org/mailman/listinfo/gtk-list

Re: Using g_signal_connect in class

by Chris Vine :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, 19 Jul 2008 13:42:58 +0200
Jean Bréfort <jean.brefort@...> wrote:

> Le samedi 19 juillet 2008 à 12:29 +0100, Chris Vine a écrit :
> > This will work with gcc/g++ but it is not standard conforming, as
> > static class functions do not have C linkage, and the C++ standard
> > states that functions otherwise identical with C++ and C linkage are
> > different types.
> >
> > To be standard conforming you should use a friend function (if it
> > needs access to private data) declared extern "C".
>
> The extern "C" declaration is not needed.

Are you saying there is a misprint in the C++ standard, or that
with the particular compiler you use it is not necessary?  If the
latter, what part of my post do you disagree with?

Chris

_______________________________________________
gtk-list mailing list
gtk-list@...
http://mail.gnome.org/mailman/listinfo/gtk-list

Re: Using g_signal_connect in class

by milosz derezynski :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I think he meant that in order to use the function with g_signal_connect(), a C linkage is not needed (and i think this is actually right, too).

2008/7/19 Chris Vine <chris@...>:
On Sat, 19 Jul 2008 13:42:58 +0200
Jean Bréfort <jean.brefort@...> wrote:
> Le samedi 19 juillet 2008 à 12:29 +0100, Chris Vine a écrit :
> > This will work with gcc/g++ but it is not standard conforming, as
> > static class functions do not have C linkage, and the C++ standard
> > states that functions otherwise identical with C++ and C linkage are
> > different types.
> >
> > To be standard conforming you should use a friend function (if it
> > needs access to private data) declared extern "C".
>
> The extern "C" declaration is not needed.

Are you saying there is a misprint in the C++ standard, or that
with the particular compiler you use it is not necessary?  If the
latter, what part of my post do you disagree with?

Chris

_______________________________________________
gtk-list mailing list
gtk-list@...
http://mail.gnome.org/mailman/listinfo/gtk-list



--
------------
Please note that according to the German law on data retention,
information on every electronic information exchange with me is
retained for a period of six months.
[Bitte beachten Sie, dass dem Gesetz zur Vorratsdatenspeicherung zufolge
jeder elektronische Kontakt mit mir sechs Monate lang gespeichert wird.]

_______________________________________________
gtk-list mailing list
gtk-list@...
http://mail.gnome.org/mailman/listinfo/gtk-list

Re: Using g_signal_connect in class

by Chris Vine :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, 19 Jul 2008 16:40:17 +0200
"Milosz Derezynski" <internalerror@...> wrote:

> I think he meant that in order to use the function with
> g_signal_connect(), a C linkage is not needed (and i think this is
> actually right, too).

No, g_signal_connect() takes a function pointer and in C++ function
pointers have a linkage type.  For GObject/GTK functions they have C
linkage type.

By default, when compiled in a C++ environment non-member functions and
non-member function pointers have C++ linkage type, so that amongst
other things functions can be overloaded, but some compilers may also
keep arguments on the stack for function calls with C linkage
differently from functions calls with C++ linkage.  The way to give them
C linkage type is to declare them extern "C".

Passing functions with C++ linkage type as GSignal callbacks works with
gcc/g++, but it is not guaranteed by the standard.

For more discussion you might want to read section 9.2.5 of TC++PL.

Chris

_______________________________________________
gtk-list mailing list
gtk-list@...
http://mail.gnome.org/mailman/listinfo/gtk-list

RE: Using g_signal_connect in class

by Vallone, Anthony :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks for the linkage info Chris.  I've been using static class members
successfully with the Sun compiler on Solaris, but I'll start declaring
my callbacks with c linkage to be standard conforming.  Sun's
documentation gives the same advice:
https://developers.sun.com/solaris/articles/mixing.html
   
-Anthony Vallone

-----Original Message-----
From: gtk-list-bounces@... [mailto:gtk-list-bounces@...] On
Behalf Of Chris Vine
Sent: Saturday, July 19, 2008 4:51 PM
To: Milosz Derezynski
Cc: gtk-list@...; Vallone, Anthony
Subject: Re: Using g_signal_connect in class

On Sat, 19 Jul 2008 16:40:17 +0200
"Milosz Derezynski" <internalerror@...> wrote:

> I think he meant that in order to use the function with
> g_signal_connect(), a C linkage is not needed (and i think this is
> actually right, too).

No, g_signal_connect() takes a function pointer and in C++ function
pointers have a linkage type.  For GObject/GTK functions they have C
linkage type.

By default, when compiled in a C++ environment non-member functions and
non-member function pointers have C++ linkage type, so that amongst
other things functions can be overloaded, but some compilers may also
keep arguments on the stack for function calls with C linkage
differently from functions calls with C++ linkage.  The way to give them
C linkage type is to declare them extern "C".

Passing functions with C++ linkage type as GSignal callbacks works with
gcc/g++, but it is not guaranteed by the standard.

For more discussion you might want to read section 9.2.5 of TC++PL.

Chris

_______________________________________________
gtk-list mailing list
gtk-list@...
http://mail.gnome.org/mailman/listinfo/gtk-list
_______________________________________________
gtk-list mailing list
gtk-list@...
http://mail.gnome.org/mailman/listinfo/gtk-list
LightInTheBox - Buy quality products at wholesale price