From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.5-pre1 Path: eternal-september.org!reader02.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED.2uCIJahv+a4XEBqttj5Vkw.user.gioia.aioe.org!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Visibility issue Date: Mon, 14 Sep 2020 16:42:21 +0200 Organization: Aioe.org NNTP Server Message-ID: References: <27269975-58eb-407b-98ca-344bee6894d2n@googlegroups.com> NNTP-Posting-Host: 2uCIJahv+a4XEBqttj5Vkw.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 Content-Language: en-US X-Notice: Filtered by postfilter v. 0.9.2 Xref: reader02.eternal-september.org comp.lang.ada:60138 List-Id: 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