comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Visibility issue
Date: Mon, 14 Sep 2020 16:42:21 +0200	[thread overview]
Message-ID: <rjnvgb$1gtv$1@gioia.aioe.org> (raw)
In-Reply-To: 27269975-58eb-407b-98ca-344bee6894d2n@googlegroups.com

On 14/09/2020 13:33, Daniel wrote:
> Thank you for your answer Dmitry. Let me focus and change my example to easier go to the problem. I will write some question to your answer at the end of the example.
> 
> I didn't write it in the example for simplicity, but the user has to set a callback function.
> This callback function is triggered in the internal implementation hierarchy packages. Let me show you the code within questions inside comments:
> 
> --- API SIDE: -----
> package API.Callbacks
>     type Handle is tagged private
>     type pointer_to_Procedure is access procedure (Self : API.CAllbacks.Handle );
>     procedure Set_Callback (Self: in out API_HandleCallBack; cp: CallbackProcedure  );
> private
>     type Handle is tagged record
>        Hidden : Natural;
>     end record;
>    procedure Hidden_constructor (S: in out Handle'Class ; Hidden_content :Natural);

You need a constructing function for your Handle

    package API.Callbacks
       type Handle is tagged private;
       function Create (Parameter : Natural) return Handle;

or else a factory object:

    type Factory is new
       abstract Ada.Finalization.Limited_Controlled with null record;
    function Create (Creator : in out Factory; ...)
       return Handle'Class is abstract;

> task body Caller is
> 
>   MyHandle : API.Handle;
>   cp: CallbackProcedure := Get_from_pool_of_callbacks;
> begin
>    Hidden_Constructor (MyHandle, 6);
>     --Cannot access to private part for API.Handle from here :(
>    -- compiler says that Hidden_Constructor is not a visible entity of" Callbacks"
>    -- I just wanted to build it to later call to the procedure:
>     cp (MyHandle);
> end Caller;

    task body Caller is
       My_Handle : Handle := Create (6);
    ...

As for callbacks, in Ada you could have a tagged object like 
Event_Handler. The callback will be a primitive operation of Event_Handler:

    type Event_Handler is
       abstract Ada.Finalization.Limited_Controlled with null record;
    procedure On_Event
              (  Handler : in out Event_Handler;
                 Object  : in out Handle
              )  is abstract;
    procedure Register
              (  Object  : in out Handle;
                 Handler : in out Event_Handler'Class
              );

The advantage: you derive from Event_Handler and can add whatever data 
you want.

Another pattern is a generic package or a generic subroutine. It is 
useful when you want to pass a closure, e.g. local variables to the 
callback without much fuss. E.g. a one-shot callbacks, visitors could be 
done this way.

    generic
       with function Visitor (Object : in out Handle'Class)
         return Boolean;
    procedure For_Each (Object : in out Handle'Class);

Both are capable to nicely breach privacy if you put them into child 
packages. Both do not suffer the accessibility level issues named access 
to subprogram types have.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

  reply	other threads:[~2020-09-14 14:42 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-11 10:37 Visibility issue Daniel
2020-09-11 11:51 ` Maxim Reznik
2020-09-11 20:11   ` Daniel
2020-09-11 14:23 ` Jeffrey R. Carter
2020-09-11 20:17   ` Daniel
2020-09-11 22:36     ` Jeffrey R. Carter
2020-09-14 10:47       ` Daniel
2020-09-14 16:10         ` Jeffrey R. Carter
2020-09-15 19:11           ` Daniel
2020-09-15 20:03             ` Jeffrey R. Carter
2020-09-11 21:05 ` Dmitry A. Kazakov
2020-09-14 11:33   ` Daniel
2020-09-14 14:42     ` Dmitry A. Kazakov [this message]
2020-09-15 19:35       ` Daniel
2020-09-16  7:14         ` Dmitry A. Kazakov
2020-09-16 10:23           ` Daniel
2020-09-16 10:58             ` Dmitry A. Kazakov
2020-09-16 14:35               ` Daniel
2020-09-16 14:49                 ` Jeffrey R. Carter
2020-09-16 15:05                 ` Dmitry A. Kazakov
2020-09-16 20:09                   ` Daniel
2020-09-16 21:48                     ` Simon Wright
2020-09-17 13:31                       ` Daniel
2020-09-17 15:00                         ` Dmitry A. Kazakov
2020-09-17 15:32                           ` Daniel
2020-09-17 16:47                             ` Dmitry A. Kazakov
2020-09-18  8:05                         ` Simon Wright
2020-09-14 16:18 ` Simon Wright
2020-09-17 15:58 ` Jere
2020-09-17 16:10 ` Jere
2020-09-18  8:08   ` Simon Wright
2020-09-17 21:47 ` Shark8
replies disabled

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