comp.lang.ada
 help / color / mirror / Atom feed
From: "Robert I. Eachus" <rieachus@comcast.net>
Subject: Re: array of strings in a function
Date: Thu, 16 Oct 2003 14:30:51 GMT
Date: 2003-10-16T14:30:51+00:00	[thread overview]
Message-ID: <3F8EAB8F.8040901@comcast.net> (raw)
In-Reply-To: bmjc7h$per$1@mercurio.cica.es

Antonio Martï¿œnez ï¿œlvarez wrote:
> Hello again. I'm the original poster.
> 
> What I want to do is a procedure (sorry, not a function) to do something 
> like this:
> 
> my_AND("Entrada_1", "P2", "Output");
> 
> and with this argument, this function have to write this:
> 
> entity my_AND is port(
>   Entrada_1     : in std_logic;
>   P2        : in std_logic;
>   Output    : out std:logic;
> );
> 
> (This is VHDL code, very similar to Ada95).

Now it becomes clear.  You need a way to "wrap-up" names passed as 
strings and unwrap them into the original strings inside the procedure. 
  The idea of declaring a procedure with a lot of String parameters all 
with default values denoting that the parameter is not used can work. 
But it is conceptually ugly.  A better way is to define a type:

type Parameter_List is private;

the operations to create a parameter list:

function "+"(Left, Right: String) return Parameter_List;
function "+"(Left: Parameter_List; Right: String) return Parameter_List;

and the operations to access the contents of objects of the type:

function Length(P: Parameter_List) return Integer;
function Contents(P: Parameter_List; Row: Integer) return String;

Now any of the techniques suggested for implementing the Parameter_List 
type will work. Let me, in case it isn't obvious say how you should use 
the ADT:

    procedure my_AND(P: Parameter_List);
begin
    my_AND("Entrada_1" + "P2" + "Output");

This call will create Parameter_List with three entries that can be 
"unwrapped" inside my_AND.

So how to implement the ADT?  You could make Parameter_List an array of 
Unbounded_String.  Or you could choose a String with markers:

type Parameter_List is String;

function "+"(Left, Right: String) return Parameter_List is
   Temp: String(1..Left'Length) := Left -- slide Left if necessary;
begin return Temp & '+' & Right; end "+";

function "+"(Left: Parameter_List; Right: String) return Parameter_List;
begin return Left & '+' & Right; end "+";

function Length(P: Parameter_List) return Integer is
   Count: Integer := 0;
begin
   for I in P'Range loop
     if P(I) = '+' then Count := Count + 1;
   end loop;
   return Count;
end Length;

function Contents(P: Parameter_List; Row: Integer) is
   First: Integer := P'First;
   Last: Integer := P'Last;
   Count: Integer := 0;
begin
   for I in P'Range loop
     if P(I) = '+'
     then
       Count := Count + 1;
       if Count = Row - 1
       then First := I+1;
       elsif Count = Row
       then Last := I-1;
         return P(First..Last);
       end if;
     end if;
   end loop;
   if Count = Row - 1
   then return P(First..Last); -- last entry not followed by "+"
   else raise Constraint_Error; -- Row number incorrect
   end if;
end Contents;

Of course, this would mean that users couldn't have "+" in their name 
strings.  So you might want to use a marker such as 
Ada.Characters.Latin_1.Reserved_128. That can't accidently appear in a 
string literal. Or Ada.Characters.Latin1.No_Break_Space. ;-)
-- 
                                                     Robert I. Eachus

"Quality is the Buddha. Quality is scientific reality. Quality is the 
goal of Art. It remains to work these concepts into a practical, 
down-to-earth context, and for this there is nothing more practical or 
down-to-earth than what I have been talking about all along...the repair 
of an old motorcycle."  -- from Zen and the Art of Motorcycle 
Maintenance by Robert Pirsig




  parent reply	other threads:[~2003-10-16 14:30 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-14 20:54 array of strings in a function Antonio Martínez
2003-10-15  2:55 ` Robert I. Eachus
2003-10-15  3:29   ` Jeff C,
2003-10-15  3:08 ` Jeffrey Carter
2003-10-16  6:40   ` tmoran
2003-10-16  9:31     ` Craig Carey
2003-10-16 18:13       ` Craig Carey
2003-10-16 21:44         ` Marius Amado Alves
2003-10-17 19:48           ` Craig Carey
2003-10-18 10:05             ` Marius Amado Alves
2003-10-18 20:05               ` Craig Carey
2003-10-30  9:42                 ` Craig Carey
2003-10-16 17:58     ` Jeffrey Carter
2003-10-16 20:00       ` tmoran
2003-10-17  0:51         ` Jeffrey Carter
2003-10-15 11:49 ` Antonio Martínez Álvarez
2003-10-15 12:29   ` Preben Randhol
2003-10-15 14:19   ` Ole-Hjalmar Kristensen
2003-10-16 14:30   ` Robert I. Eachus [this message]
2003-10-16 17:53     ` Jeffrey Carter
2003-10-17  0:48       ` Robert I. Eachus
2003-10-17 18:41         ` Jeffrey Carter
replies disabled

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