From: "Matthew Heaney" <mheaney@on2.com>
Subject: Re: Idiom for a class and an object in Ada
Date: Wed, 20 Oct 2004 11:23:12 -0400
Date: 2004-10-20T15:23:13+00:00 [thread overview]
Message-ID: <417682e0$0$91007$39cecf19@news.twtelecom.net> (raw)
In-Reply-To: UUsdd.3499$ta5.1160@newsread3.news.atl.earthlink.net
"Marin David Condic" <nobody@noplace.com> wrote in message
news:UUsdd.3499$ta5.1160@newsread3.news.atl.earthlink.net...
>
> So now you want to say "I have this 'class' called a Blivet and I might
> want to have a "Blue_Blivet" and a "Shiny_Blue_Blivet" and a
> "Big_Shiny_Blue_Blivet" so the natural thing to do is make a 'class' from
> a package and a tagged type and a bunch of subprograms.
You still haven't explained why TYPE Blivet is tagged and nonlimited.
> Having done that, you find you have a constraint: For reasons having
> nothing special to do with Ada, you want all the data for the 'objects' of
> that class to be in static storage and not involve access types and all
> that. No generics because of code bloat and other issues. Any other issues
> such as private vs limited private are all just sidebars having nothing
> special to do with the question at hand. You can get what I describe with
> a simple declaraion of "My_Blivet : Blivet;" put somewhere at the Library
> level. To repeat my question:
>
> What is the preferred Ada idiom (when talking about Object Oriented
> methodology) for declaring those objects?
To repeat my answer: declare the TYPE as limited and indefinite, and declare
selector functions that return references to the statically declared
instances. Just like Text_IO.
Limited types are passed by reference, so one way to do sans access types is
like this:
package Blivets is
type Blivet (<>) is limited private;
procedure Op (B : Blivet);
function My_Blivet return Blivet;
private
type Blivet is limited record .. end record;
end;
package body Blivets is
...
My_Blivet_Object : Blivet;
function My_Blivet return Blivet is
begin
return My_Blivet_Object;
end;
end Blivets;
Limited types are passed by reference, so neither allocation nor access
types are necessary.
Note that using access types doesn't imply allocation, so it's not clear to
me what you have against access types. Another possibility is to implement
the type as an access type (so direct pointer manipulation isn't necessary):
package Blivets is
type Blivet (<>) is limited private;
procedure Op (B : Blivet);
function My_Blivet return Blivet;
private
type Rep_Type is <whatever>;
type Blivet is access all Rep_Type;
for Blivet'Storage_Size use 0;
end Blivets;
package body Blivets is
...
My_Blivet_Object : aliased Rep_Type;
function My_Blivet return Blivet is
begin
return My_Blivet_Object'Access;
end;
end Blivets;
This has the benefit that objects are allocated statically, and all uses of
pointers are hidden from the user.
Yet another possibility is to expose the access type, but pass the object as
an access parameter:
package Blivets is
type Blivet (<>) is limited private;
procedure Op (B : access Blivet);
type Blivet_Access is access all Blivet;
for Blivet_Access'Storage_Size use 0;
My_Blivet : constant Blivet_Access;
private
type Blivet is limited record ... end record;
My_Blivet_Object : aliased Blivet;
My_Blivet : constant Blivet_Access := My_Blivet_Object'Access;
end Blivets;
So take your pick! In all cases, the objects are declared statically, which
satisfies your primary constraint.
next prev parent reply other threads:[~2004-10-20 15:23 UTC|newest]
Thread overview: 55+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-10-18 11:47 Idiom for a class and an object in Ada Marin David Condic
2004-10-18 12:14 ` Martin Krischik
2004-10-18 19:40 ` Matthew Heaney
2004-10-19 12:59 ` Marin David Condic
2004-10-19 14:46 ` Martin Dowie
2004-10-19 15:55 ` Matthew Heaney
2004-10-19 18:31 ` Martin Dowie
2004-10-19 15:52 ` Matthew Heaney
2004-10-18 12:26 ` Marius Amado Alves
2004-10-19 2:09 ` Jeffrey Carter
2004-10-19 3:28 ` Matthew Heaney
2004-10-19 12:53 ` Marin David Condic
2004-10-19 14:44 ` Matthew Heaney
2004-10-19 15:01 ` Dmitry A. Kazakov
2004-10-19 15:40 ` Matthew Heaney
2004-10-20 7:58 ` Dmitry A. Kazakov
2004-10-20 12:31 ` Marin David Condic
2004-10-20 13:53 ` Dmitry A. Kazakov
2004-10-20 15:23 ` Matthew Heaney [this message]
2004-10-21 12:24 ` Marin David Condic
2004-10-21 17:15 ` Matthew Heaney
2004-10-20 5:39 ` Simon Wright
2004-10-20 7:24 ` Matthew Heaney
2004-10-20 8:39 ` Dmitry A. Kazakov
2004-10-21 1:36 ` Jeffrey Carter
2004-10-21 1:46 ` Matthew Heaney
2004-10-21 7:51 ` Dmitry A. Kazakov
2004-10-21 12:45 ` Matthew Heaney
2004-10-21 14:11 ` Dmitry A. Kazakov
2004-10-22 1:04 ` Jeffrey Carter
2004-10-22 1:36 ` Matthew Heaney
2004-10-21 19:31 ` Kevin Cline
2004-10-21 22:02 ` Matthew Heaney
2004-10-22 0:10 ` Matthew Heaney
2004-10-21 8:25 ` Martin Dowie
2004-10-20 17:04 ` Matthew Heaney
2004-10-20 19:37 ` Simon Wright
2004-10-20 20:04 ` Matthew Heaney
2004-10-22 5:37 ` Simon Wright
2004-10-20 1:10 ` Jeffrey Carter
2004-10-20 7:04 ` Matthew Heaney
2004-10-20 12:42 ` Marin David Condic
2004-10-20 12:55 ` Matthew Heaney
2004-10-20 15:27 ` Matthew Heaney
2004-10-21 1:36 ` Matthew Heaney
2004-10-19 12:38 ` Marin David Condic
2004-10-18 16:59 ` Matthew Heaney
2004-10-18 18:02 ` Martin Dowie
2004-10-19 13:06 ` Marin David Condic
2004-10-19 14:51 ` Martin Dowie
2004-10-20 16:20 ` Michael Paus
2004-10-20 17:15 ` Matthew Heaney
2004-10-20 17:55 ` Michael Paus
2004-10-21 12:33 ` Marin David Condic
-- strict thread matches above, loose matches on Subject: below --
2004-10-21 13:59 Stephen Leake
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox