comp.lang.ada
 help / color / mirror / Atom feed
From: ake.ragnar.dahlgren@gmail.com
Subject: Re: Access type to member procedure of instance (Object Oriented programming in Ada)
Date: Mon, 19 Nov 2012 12:22:21 -0800 (PST)
Date: 2012-11-19T12:22:21-08:00	[thread overview]
Message-ID: <8906e1e3-3b18-4b37-ac46-3857db47cea4@googlegroups.com> (raw)
In-Reply-To: <9b0bcb37-8ae3-440f-af4f-a796702e4250@googlegroups.com>

On Monday, November 19, 2012 10:59:42 AM UTC+1, ake.ragna...@gmail.com wrote:
> Not sure if this has already been discussed but: Is it possible to define a pointer to a member procedure of an instance?
> 
> 
> 
> This problem has it's origin in the implementation of the MVC pattern for a GtkAda application. Consider the following:
> 
> 
> 
> package Some_Package is
> 
> 
> 
>    type Message_Type is tagged null record;
> 
>    
> 
>    procedure Print(Message : Message_Type);
> 
> 
> 
> end Some_Package;
> 
> 
> 
> 
> 
> 
> 
> package body Some_Package is
> 
> 
> 
>    procedure Print (Message : Message_Type) is
> 
>    begin
> 
>       null;
> 
>    end Print;
> 
> 
> 
> end Some_Package;
> 
> 
> 
> 
> 
> Then it is possible to create the following Main application that compiles just fine:
> 
> 
> 
> with Some_Package;
> 
> 
> 
> procedure Main is
> 
>    Message : Some_Package.Message_Type;
> 
> begin
> 
>    Message.Print;
> 
> end Main;
> 
> 
> 
> Is it possible to create a parameterless access type to the Message.Print procedure? The following application refuses to compile:
> 
> 
> 
> with Some_Package;
> 
> 
> 
> procedure Main is
> 
>    Message : Some_Package.Message_Type;
> 
>    
> 
>    Method_Ref : access procedure := Message.Print'Access;
> 
> begin
> 
>    Method_Ref;
> 
> end Main;
> 
> 
> 
> The error message is: no selector "Print" for type "Message_Type" defined at some_package.ads:3
> 
> 
> 
> 
> 
> It is possible to create a workaround:
> 
> 
> 
> with Some_Package;
> 
> 
> 
> procedure Main is
> 
>    Message : Some_Package.Message_Type;
> 
>    
> 
>    procedure Method_Ref is
> 
>    begin
> 
>       Message.Print;
> 
>    end Method_Ref;
> 
>    
> 
> begin
> 
>    Method_Ref;
> 
> end Main;
> 
> 
> 
> Is there a better way? Or is this the optimal way?
> 
> 
> 
> Regards,
> 
> Åke Ragnar Dahlgren

Thanks everybody for posting. Adam you answered my question perfectly since my question was a pure Ada question whether or not it is possible to define pointers to member methods of class instances. And the answer is no. To add this as a feature to Ada might be useful. But of course this needs careful investigation.

I do not mean to make this a Gtkada issue but I have received requests for clarifying the origin of the question.

The Gtk application uses Glade and contains the following procedure call:

Gtkada.Builder.Register_Handler
   (Builder      => Builder,
    Handler_Name => "File_Quit",
    Handler      => On_Quit'Access);

where the On_Quit procedure is declared as follows:

procedure On_Quit (Object : access Gtkada.Builder.Gtkada_Builder_Record'Class) is
begin
  -- Code is omitted
end On_Quit;

When On_Quit is defined as above everything compiles just fine. The problem is if one would like the On_Quit method to be a member method of an instance. In this case there is a Controller instance whose method should be called. It is declared as:

procedure On_Quit (This   : Controller_Type;
                   Object : access Gtkada.Builder.Gtkada_Builder_Record'Class);

Using the Controller's On_Quit procedure one would like to write:

Gtkada.Builder.Register_Handler
   (Builder      => Builder,
    Handler_Name => "File_Quit",
    Handler      => Controller.On_Quit'Access);

This did not compile and thereof the question.

Additional information if needed: The spec. of the Register_Handler procedure in the Gtkada.Builder package is declared as:

procedure Register_Handler
  (Builder      : access Gtkada_Builder_Record'Class;
   Handler_Name : String;
   Handler      : Builder_Handler);

The Builder_Handler is a pointer to a method:

type Builder_Handler is access procedure
  (Builder : access Gtkada_Builder_Record'Class);

Best regards,
Åke Ragnar Dahlgren



  parent reply	other threads:[~2012-11-19 20:22 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-19  9:59 Access type to member procedure of instance (Object Oriented programming in Ada) ake.ragnar.dahlgren
2012-11-19 11:13 ` Georg Bauhaus
2012-11-19 11:39 ` Brian Drummond
2012-11-20 11:43   ` Brian Drummond
2012-11-20 21:57     ` Randy Brukardt
2012-11-19 13:03 ` sbelmont700
2012-11-19 16:18 ` Adam Beneschan
2012-11-19 17:02   ` Peter C. Chapin
2012-11-19 18:23     ` Adam Beneschan
2012-11-19 20:57       ` Peter C. Chapin
2012-11-19 21:26       ` Dmitry A. Kazakov
2012-11-19 22:19         ` Adam Beneschan
2012-11-20 10:12           ` Dmitry A. Kazakov
2012-11-20 21:51             ` Randy Brukardt
2012-11-21  8:24               ` Dmitry A. Kazakov
2012-11-21 22:19                 ` Randy Brukardt
2012-11-20 10:59     ` Brian Drummond
2012-11-19 20:22 ` ake.ragnar.dahlgren [this message]
2012-11-20 11:16   ` Brian Drummond
2012-11-19 20:52 ` ake.ragnar.dahlgren
2012-11-19 21:56   ` Dmitry A. Kazakov
2012-11-22  9:49     ` ake.ragnar.dahlgren
2012-11-19 22:13   ` sbelmont700
2012-11-19 23:59 ` Randy Brukardt
2012-11-20  0:05   ` Randy Brukardt
2012-11-20  1:00     ` Adam Beneschan
2012-11-20 21:38       ` Randy Brukardt
2012-11-20 23:43         ` Adam Beneschan
2012-11-21 22:12           ` Randy Brukardt
2012-11-22  1:59             ` Adam Beneschan
2012-11-29  2:43               ` Randy Brukardt
2012-11-20  0:52   ` Adam Beneschan
2012-11-20 21:34     ` Randy Brukardt
2012-11-20 11:28   ` Brian Drummond
2012-11-20 14:27     ` Georg Bauhaus
2012-11-20 15:52     ` Adam Beneschan
2012-11-22  9:47 ` ake.ragnar.dahlgren
2012-11-22 10:25   ` Dmitry A. Kazakov
2012-12-02 20:42     ` ake.ragnar.dahlgren
2012-12-03 11:21       ` Dmitry A. Kazakov
2012-12-03 20:21         ` ake.ragnar.dahlgren
2012-12-03 22:15           ` Dmitry A. Kazakov
2012-12-25 21:51           ` Gustaf Thorslund
2012-12-26 18:11             ` ake.ragnar.dahlgren
2012-11-22 12:13   ` Brian Drummond
2012-12-03 16:17     ` ake.ragnar.dahlgren
2012-12-03 21:56       ` Brian Drummond
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox