comp.lang.ada
 help / color / mirror / Atom feed
* class-wide objects
@ 1998-12-07  0:00 Francois Godme
  1998-12-07  0:00 ` Tucker Taft
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Francois Godme @ 1998-12-07  0:00 UTC (permalink / raw)


Sometimes, pointers are used in an application only because the
programming language does not offer good support for dynamically sized
objects.

Ada has very good support for dynamically sized objects. Global objects
and stack objects can be sized as late as the elaboration of their
declaration. For global objects, it means size is fixed during
elaboration of the program,  for stack objects, it means size is fixed
during entry into the procedure or function which declares them. Stack
allocation has lots of advantages: it's fast and it's not prone to
memory fragmentation and memory leak because it works on an all or
nothing scheme.

In the following examples, one can see that Ada supports well dynamic
objects because array attributes and
reading of record discriminant allow to gain information on the actual
parameter. With this information, first, some subtypes are defined, then
objects or types can be built on top of them.

procedure P1 (S : in String;
              N : in Natural) is

   Local : String (S'Range); -- anonymous subtype

   subtype T_Local_String is String (S'Range);

   Local_Array : array (1 .. N) of T_Local_String;

begin
   ...
end P1;

procedure P2 (S : in Square; -- Square is defined Page 3-35 RM Ada83
              N : in Natural) is

   Local : Square (S.Side); -- Side is the discriminant of type Square

   subtype T_Local_Square is Square (S.Side);

   Local_Array : array (1 .. N) of T_Local_Square;

begin
   ...
end P2;

Ada95 has introduced tagged objects and class-wide objects. Class-wide
object types are here to achieve polymorphism. They serve as formal
parameters or to define access types. A class-wide object is a
dynamically sized object. It has the size of the actual tagged object
parameter passed to the formal parameter.

Since the introduction of class-wide objects, support for dynamically
sized objects, as far as I know and understand Ada95, has dropped down.
Correct me if I'm wrong. Ada95 support for dynamically sized objects is
now incomplete to allow pointer-free code because Ada95 doesn't provide
a mechanism to gain information on the actual tagged object suitable to
define a subtype.

Ada95 allows a local variable to be constrained by the actual tagged
parameter but in an incomplete way:

procedure P3 (S : in Point'Class;
              N : in Natural) is

   Local : Point'Class := S; -- But what if type point is limited ?

begin
   ...
end P3;

An attribute to get the subtype of the actual tagged parameter would
solve both problems. May be we could combine
the two attributes 'Class and 'Tag together:

procedure P4 (S : in Point'Class;
              N : in Natural) is

   Local : Point'Class (S'Tag); -- allocates an object with the tag of S

   subtype T_Local_Point is Point'Class (S'Tag);

   Local_Array : array (1 .. N) of T_Local_Point;

begin
   ...
end P4;

What do you think ? Am I right or am I missing something ? Could the
GNAT team provide an implementation defined attribute for this ?

Francois Godme






^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~1998-12-12  0:00 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-12-07  0:00 class-wide objects Francois Godme
1998-12-07  0:00 ` Tucker Taft
1998-12-07  0:00   ` Francois Godme
1998-12-07  0:00 ` Matthew Heaney
1998-12-06  0:00   ` David Botton
1998-12-09  0:00   ` Francois Godme
1998-12-10  0:00     ` Matthew Heaney
1998-12-10  0:00       ` Francois Godme
1998-12-11  0:00       ` Stephen Leake
1998-12-12  0:00         ` Francois Godme
1998-12-07  0:00 ` Tom Moran

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