comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Private primitive operations available to entire package hierarchy. Can it be done?
Date: Wed, 27 Jul 2005 16:28:01 -0500
Date: 2005-07-27T16:28:01-05:00	[thread overview]
Message-ID: <grqdnRKB5o86ZnrfRVn-pA@megapath.net> (raw)
In-Reply-To: 1122485760.918191.274380@f14g2000cwb.googlegroups.com

"Lucretia" <lucretia9@lycos.co.uk> wrote in message
news:1122485760.918191.274380@f14g2000cwb.googlegroups.com...
...
> In my minimal sample I derive a new Frame_Type and within that I have
> declared a Button_Type instance, i.e.
>
> type Minimal_Frame_Type is new Frame_Type with
>   record
>     ...
>     Button : aliased Button_Type;
>     ...
>   end record;
>
> Now, in the code I do this:
>
> Add(Some_Sizer, Self.Button'Unchecked_Access...);

Is this in the user code, or in the implementation of the interface? If it's
in the implementation of the interface, do whatever you have to do. If it's
in the user code, though, I'd claim that the Add routine is incorrectly
specified -- it should simply pass the Button_Type object and the code
*internal* to the binding should be creating the access types.

> If I remove the aliased, it wont compile because Button is not aliased
> (it is rooted at Object_Type like all other wxAda types).

It will if Button is a parameter, rather than a component.

> If I keep aliased but use 'Access it doesn't compile because it is a
> local object - I have also tried allocating the Minimal_Frame_Type
> using new rather than having a "global" and it still requires the
> aliased.

Right. I've never found a case where I could actually use 'Access on an
object.

> Because of all of this, I am seriously considering using pointers to
> these instances rather than just instantiating them, i.e.:
>
> type Minimal_Frame_Type is new Frame_Type with
>   record
>     ...
>     Button : Button_Access;
>     ...
>   end record;
>
> type Minimal_Frame_Access is access all Minimal_Frame_Type;
>
> and having all primitives take an access parameter as the controlling
> parameter.

That's awful. If you do that, you're requiring your user to do memory
management for you, rather than the other way around. They'll be forced into
subpar syntax (explicit uses of aliased and 'Access), have runtime
accessibility errors, and other nastyness. And they would have no way to use
the containers library do the storage management for them.

My rule of thumb (not shared by all, mind you) is that explicit access types
in the user accessible specifications should be kept to an absolute minimum.
In Claw, we use them only to avoid copying of objects for query functions.
Everything else is passed as an object.

Internal to the library, of course, virtually everything is an access type.
But that sort of grunge is completely hidden from the user. We use the
predefined Finalize routine to insure that dangling pointers don't get left
around - when an object is finalized, it is removed from all internal
objects to which it is linked.

So, at the user level, I'd suggest something like:

    procedure Add (Sizer : Sizer_Type; Button : Button_Type'Class);

And the implementation of Add:

    procedure Add (Sizer : Sizer_Type; Button : Button_Type'Class) is
         My_Button : Any_Button_Access_Type := Button'Unchecked_Access;
         My_Sizer : Any_Sizer_Access_Type := Sizer'Unchecked_Access;
    begin
         -- Call the low level (C) routines:
         wx_Add (My_Sizer, My_Button);
         -- etc.
    end Add;

You might need to do some type conversions on the access types, but that's
OK as it doesn't usually generate any code.

                                Randy.






  reply	other threads:[~2005-07-27 21:28 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-07 16:06 Private primitive operations available to entire package hierarchy. Can it be done? Lucretia
2005-07-07 16:17 ` OT: Joke Adrien Plisson
2005-07-07 16:24   ` Matthew Heaney
2005-07-07 19:10 ` Private primitive operations available to entire package hierarchy. Can it be done? Randy Brukardt
2005-07-13 15:40   ` Lucretia
2005-07-19 23:19     ` Randy Brukardt
2005-07-20 18:14       ` Lucretia
2005-07-21  3:10         ` Randy Brukardt
2005-07-25 18:14           ` Lucretia
2005-07-25 23:58             ` Randy Brukardt
2005-07-27 17:36               ` Lucretia
2005-07-27 21:28                 ` Randy Brukardt [this message]
2005-07-28 10:09                   ` Lucretia
2005-07-29  0:40                     ` Randy Brukardt
2005-08-02 15:55                       ` Lucretia
2005-08-03 18:26                         ` Lucretia
2005-08-03 20:04                           ` Randy Brukardt
2005-08-03 20:03                         ` Randy Brukardt
2005-08-08 18:04                           ` Lucretia
2005-08-08 20:47                             ` Randy Brukardt
replies disabled

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