From: "Robert I. Eachus" <rieachus@comcast.net>
Subject: Re: Heterogenous array
Date: Mon, 01 Mar 2004 11:48:15 -0500
Date: 2004-03-01T11:48:15-05:00 [thread overview]
Message-ID: <iJKdnRosbr_N9N7dRVn-jg@comcast.com> (raw)
In-Reply-To: <pIw0c.84623$dP1.233768@newsc.telia.net>
Bj�rn Persson wrote:
> I need an array type that can hold elements of different types. My
> current attempt (somewhat simplified) is below. My question is, is there
> a way to do this without pointers?
Sure, try:
with Ada.Strings.Bounded; with Ada.Strings.Unbounded;
package Multirecord is
package A_Bounded_String is new
Ada.Strings.Bounded.Generic_Bounded_Length(80);
subtype A_Bounded_String_Type is A_Bounded_String.Bounded_String;
type T_Param_Type is (Int, Str);
type Parameter_Definition(Param_Type : T_Param_Type := Int) is record
Name : A_Bounded_String_Type;
case Param_Type is
when Int =>
Min : Integer;
Max : Integer;
Int_Value : Integer;
when Str =>
Str_Value : Ada.Strings.Unbounded.Unbounded_String;
end case;
end record;
type Parameter_Wrapper is record
Content: Parameter_Definition;
end record;
type Parameter_Array is array(Positive range <>) of
Parameter_Wrapper;
end Multirecord;
It should compile without problems and do exactly what you want. The
type Parameter_Wrapper is needed so that the element type of
Parameter_Array is constrained. The rule that requires this may seem
silly but it is not. Objects of type Parameter_Definition can be
different sizes, objects of type Parameter_Wrapper are not. In effect,
the reason for having the Parameter_Wrapper type is for the compiler to
have a hook to do the mutable type thing. (Which in Ada is only a
property of records with (defaulted) discriminants but never of array
elements.)
Whether the implementation accomplishes this by making Parameter_Wrapper
the size of the largest possible Parameter_Definition or uses "hidden"
pointers is, of course, up to the implementation. Some compilers alway
"allocate the max," others only do that if the maximum size is below
some threshold. I am not sure whether there are any compilers that
always use hidden pointers.
--
Robert I. Eachus
"The only thing necessary for the triumph of evil is for good men to do
nothing." --Edmund Burke
next prev parent reply other threads:[~2004-03-01 16:48 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-03-01 1:53 Heterogenous array Björn Persson
2004-03-01 2:22 ` Steve
2004-03-01 16:48 ` Robert I. Eachus [this message]
2004-03-01 22:01 ` Björn Persson
2004-03-01 23:29 ` Randy Brukardt
2004-03-02 2:56 ` Robert I. Eachus
2004-03-02 3:13 ` Steve
2004-03-02 22:29 ` Björn Persson
2004-03-03 9:43 ` Jean-Pierre Rosen
2004-03-03 14:44 ` Björn Persson
2004-03-03 17:52 ` Jean-Pierre Rosen
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox