Passing context objects to handlers via Register_Handler

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Passing context objects to handlers via Register_Handler

R. Tyler Croy
Before I do something gnarly and add some globals to my application, I'm
curious to learn more about patterns for sharing context with registered
handlers.

I'm using Glade3 and GtkAda (written about a bit here
http://unethicalblogger.com/2017/01/07/updated-glade-gtkada-example.html) and
the Register_Handler procedure for connecting my signals to my callback
procedures, e.g.:


arun.adb

    procedure Main is
      -- snip --
      Register_Handler (Builder      => Builder,
                        Handler_Name => "Main_Quit",
                        Handler      => Arun.Handlers.Quit'Access);
      -- snip --
    end Main;


arun-handlers.adb

   procedure Quit (Object : access Gtkada_Builder_Record'Class) is
      pragma Unreferenced (Object);
   begin
      Ada.Text_IO.Put_Line ("Exiting arun");
      Gtk.Main.Main_Quit;
   end Quit;


The problem I have is that there is shared state which I have in the Main
procedure which I would like to make use of during the handler procedures. My
first attempt was create a new Gtkada_Builder_Record type with a record
extension, but that failed miserably.

Outside of adding a global which I grab across package boundaries, it's not
clear to me how to pass 'context' here :(


Any suggestions or pointers would be most helpful!


Cheers
- R. Tyler Croy

------------------------------------------------------
     Code: <https://github.com/rtyler>
  Chatter: <https://twitter.com/agentdero>
     xmpp: [hidden email]

  % gpg --keyserver keys.gnupg.net --recv-key 1426C7DC3F51E16F
------------------------------------------------------

_______________________________________________
gtkada mailing list
[hidden email]
http://lists.adacore.com/mailman/listinfo/gtkada

signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Passing context objects to handlers via Register_Handler

Nicolas Setton
Hello,

On 09/01/2017 12:41, R. Tyler Croy wrote:
> Before I do something gnarly and add some globals to my application, I'm
> curious to learn more about patterns for sharing context with registered
> handlers.
>
> I'm using Glade3 and GtkAda (written about a bit here
> http://unethicalblogger.com/2017/01/07/updated-glade-gtkada-example.html)

Nice post! I like the concept of "frivolous hacking" - is it the fun
sibling to "deliberate learning"? :-)

(snip)

> The problem I have is that there is shared state which I have in the Main
> procedure which I would like to make use of during the handler procedures. My
> first attempt was create a new Gtkada_Builder_Record type with a record
> extension, but that failed miserably.
>
> Outside of adding a global which I grab across package boundaries, it's not
> clear to me how to pass 'context' here :(
>
>
> Any suggestions or pointers would be most helpful!

Your first idea, to extend Gtkada_Builder_Record, looks like the right
thing to do - what didn't work?

Best regards,

Nicolas




_______________________________________________
gtkada mailing list
[hidden email]
http://lists.adacore.com/mailman/listinfo/gtkada
Reply | Threaded
Open this post in threaded view
|

Re: Passing context objects to handlers via Register_Handler

R. Tyler Croy
(replies inline)

On Mon, 09 Jan 2017, Nicolas Setton wrote:

> On 09/01/2017 12:41, R. Tyler Croy wrote:
> >Before I do something gnarly and add some globals to my application, I'm
> >curious to learn more about patterns for sharing context with registered
> >handlers.
> >
> >The problem I have is that there is shared state which I have in the Main
> >procedure which I would like to make use of during the handler procedures. My
> >first attempt was create a new Gtkada_Builder_Record type with a record
> >extension, but that failed miserably.
> >
> >Outside of adding a global which I grab across package boundaries, it's not
> >clear to me how to pass 'context' here :(
> >
> >
> >Any suggestions or pointers would be most helpful!
>
> Your first idea, to extend Gtkada_Builder_Record, looks like the right thing
> to do - what didn't work?
I *believe* that this isn't working as I would like because
Gtkada_Builder_Record isn't a tagged type. From my understanding,
procedure/function signatures using Gtkada_Builder_Record'Class would gladly
accept my extended (and tagged) record, if the "parent" were to be tagged.

For reference, here's what Gtkada_Builder_Record looks like:

   type Gtkada_Builder_Record is new
     Gtk.Builder.Gtk_Builder_Record with private;
   type Gtkada_Builder is access all Gtkada_Builder_Record'Class;

Here's a sample snippet of what I tried (from arun.adb):

      type Arun_Builder_Record is new Gtkada_Builder_Record with record
         Extended : Boolean;
      end record;
      type Arun_Builder is access all Arun_Builder_Record'Class;

      Builder     : Arun_Builder;
      -- snip
   begin

      Gtk.Main.Init;
      Gtk_New (Builder);
      -- sinp


This results in the predictable error of:

    arun.adb:56:16: expected type "Gtkada_Builder" defined at gtkada-builder.ads:84
    arun.adb:56:16: found type "Arun_Builder" defined at line 40
    gprbuild: *** compilation phase failed


I'm still a relative novice at object-oriented programming with Ada 2012, so I
might not be extending things correctly here. Happy to try whatever alternative
approaches you might recommend here.



Cheers
- R. Tyler Croy

------------------------------------------------------
     Code: <https://github.com/rtyler>
  Chatter: <https://twitter.com/agentdero>
     xmpp: [hidden email]

  % gpg --keyserver keys.gnupg.net --recv-key 1426C7DC3F51E16F
------------------------------------------------------

_______________________________________________
gtkada mailing list
[hidden email]
http://lists.adacore.com/mailman/listinfo/gtkada

signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Passing context objects to handlers via Register_Handler

Emmanuel Briot
> I *believe* that this isn't working as I would like because
> Gtkada_Builder_Record isn't a tagged type. From my understanding,
> procedure/function signatures using Gtkada_Builder_Record'Class would gladly
> accept my extended (and tagged) record, if the "parent" were to be tagged.

It is a tagged type, since it extends a tagged type.

>    arun.adb:56:16: expected type "Gtkada_Builder" defined at gtkada-builder.ads:84
>    arun.adb:56:16: found type "Arun_Builder" defined at line 40
>    gprbuild: *** compilation phase failed


Gtk_New specifically expected a Arun_Builder, so you can’t give it another type.
Instead:


    Builder := new Arun_Builder_Record;
    Gtkada.Builder.Initialize (Builder);

should do the trick.

regards
Emmanuel

_______________________________________________
gtkada mailing list
[hidden email]
http://lists.adacore.com/mailman/listinfo/gtkada
Reply | Threaded
Open this post in threaded view
|

Re: Passing context objects to handlers via Register_Handler

R. Tyler Croy
(replies inline)

On Thu, 12 Jan 2017, Emmanuel Briot wrote:

> > I *believe* that this isn't working as I would like because
> > Gtkada_Builder_Record isn't a tagged type. From my understanding,
> > procedure/function signatures using Gtkada_Builder_Record'Class would gladly
> > accept my extended (and tagged) record, if the "parent" were to be tagged.
>
> It is a tagged type, since it extends a tagged type.
>
> >    arun.adb:56:16: expected type "Gtkada_Builder" defined at gtkada-builder.ads:84
> >    arun.adb:56:16: found type "Arun_Builder" defined at line 40
> >    gprbuild: *** compilation phase failed
>
>
> Gtk_New specifically expected a Arun_Builder, so you can???t give it another type.
> Instead:
>
>
>     Builder := new Arun_Builder_Record;
>     Gtkada.Builder.Initialize (Builder);
>
> should do the trick.

THat did work, thanks. I guess I didn't follow the
Gtk.Builder.Gtk_Builder_Record hierarchy enough up to GObject_Record.


- R. Tyler Croy

------------------------------------------------------
     Code: <https://github.com/rtyler>
  Chatter: <https://twitter.com/agentdero>
     xmpp: [hidden email]

  % gpg --keyserver keys.gnupg.net --recv-key 1426C7DC3F51E16F
------------------------------------------------------

_______________________________________________
gtkada mailing list
[hidden email]
http://lists.adacore.com/mailman/listinfo/gtkada

signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Passing context objects to handlers via Register_Handler

Jacob Sparre Andersen-2
On 12-01-2017 09:45, R. Tyler Croy wrote:

> THat did work, thanks. I guess I didn't follow the
> Gtk.Builder.Gtk_Builder_Record hierarchy enough up to GObject_Record.

No need.  If you can extend a type, it is tagged.

Greetings,

Jacob
--
"we don't have beer yet, it is still warm"

_______________________________________________
gtkada mailing list
[hidden email]
http://lists.adacore.com/mailman/listinfo/gtkada