comp.lang.ada
 help / color / mirror / Atom feed
* Pointer to Array
@ 2007-10-16 13:40 shaunpatterson
  2007-10-16 14:09 ` Niklas Holsti
  2007-10-16 14:16 ` Robert A Duff
  0 siblings, 2 replies; 3+ messages in thread
From: shaunpatterson @ 2007-10-16 13:40 UTC (permalink / raw)


I'm attempting to make a pointer to an array... or in a sense
"rename" an array.

I'm combining code from two very similar systems.  Lots of code.

I have thousands of lines of code that's exactly the same, using
(for just an example) an array of Integers.

There is a store file in each system that declares:

-- system 1
    type INT_ARRAY is new Integer (1..SYSTEM_1_SIZE);

    Int_Array_Var : INT_ARRAY;

-- system 2
    type INT_ARRAY is new Integer (1..SYSTEM_2_SIZE);
    Int_Array_Var : INT_ARRAY;

---------

so in other words, the variable names are the same, types are the same
only the size of the array is different..

then I have code in both systems that use that array like:

For I in Int_Array_Var'Range loop
       -- Do lots of stuff with Int_Array_Var
end loop

------
I want to keep that code untouched if possible.  I'm hoping there is a
way
in Ada to do something like

    type INT_ARRAY is array (Positive range <>) of Integer;
    Int_Array_Var_System1 : INT_ARRAY (1..System_1_Size);
    Int_Array_Var_System2 : INT_ARRAY (1..System_2_Size);


And I'd like to so something at run time that is like:

    if usingSystem1 then
        Int_array_Var = "pointer to Int_Array_Var_System1"
    elsif usingSystem2 then
        Int_array_Var = "pointer to Int_Array_Var_System2"

by changing the pointer I'm hoping to keep most of existing code the
same.

Is there any method in ada to do this?

-
Shaun




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

* Re: Pointer to Array
  2007-10-16 13:40 Pointer to Array shaunpatterson
@ 2007-10-16 14:09 ` Niklas Holsti
  2007-10-16 14:16 ` Robert A Duff
  1 sibling, 0 replies; 3+ messages in thread
From: Niklas Holsti @ 2007-10-16 14:09 UTC (permalink / raw)


shaunpatterson@gmail.com wrote:
> I'm attempting to make a pointer to an array... or in a sense
> "rename" an array.
> 
> I'm combining code from two very similar systems.  Lots of code.
> 
> I have thousands of lines of code that's exactly the same, using
> (for just an example) an array of Integers.
> 
> There is a store file in each system that declares:
> 
> -- system 1
>     type INT_ARRAY is new Integer (1..SYSTEM_1_SIZE);

That declaration of INT_ARRAY is not correct -- you must have made 
a typo of some sort in writing your post. What is the real declaration?

> I want to keep that code untouched if possible.  I'm hoping there is a
> way
> in Ada to do something like
> 
>     type INT_ARRAY is array (Positive range <>) of Integer;
>     Int_Array_Var_System1 : INT_ARRAY (1..System_1_Size);
>     Int_Array_Var_System2 : INT_ARRAY (1..System_2_Size);
> 
> 
> And I'd like to so something at run time that is like:
> 
>     if usingSystem1 then
>         Int_array_Var = "pointer to Int_Array_Var_System1"
>     elsif usingSystem2 then
>         Int_array_Var = "pointer to Int_Array_Var_System2"
> 
> by changing the pointer I'm hoping to keep most of existing code the
> same.
> 
> Is there any method in ada to do this?

Better method: Make the the code into a procedure that takes the 
parameter as an array, then call the procedure either with 
Int_Array_Var_System1 or Int_Array_Var_System2 as the actual 
parameter. But this can be difficult if you have several such pairs 
of arrays, and the condition for choosing system1/2 is not the same 
for all pairs -- you can get a large number of combinations.

Second method, using an access variable (the Text_IO and 
Command_Line are there just for testing):

with Ada.Text_IO;
with Ada.Command_Line;

procedure IntArr
is

     type INT_ARRAY is array (Positive range <>) of Integer;

     System_1_Size : constant := 55;
     System_2_Size : constant := 77;

     Int_Array_Var_System1 : aliased INT_ARRAY :=
        (1..System_1_Size => 0);
     Int_Array_Var_System2 : aliased INT_ARRAY :=
        (1..System_2_Size => 0);

     type INT_ARRAY_REF is access all INT_ARRAY;

     Int_Array_Var : INT_ARRAY_REF;

begin

    if Ada.Command_Line.Argument_Count > 0 then

       Int_Array_Var := Int_Array_Var_System1'Access;

    else

       Int_Array_Var := Int_Array_Var_System2'Access;

    end if;

    Ada.Text_IO.Put_Line (
       "Length" & Natural'Image (Int_Array_Var'Length));

end IntArr;


HTH...

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



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

* Re: Pointer to Array
  2007-10-16 13:40 Pointer to Array shaunpatterson
  2007-10-16 14:09 ` Niklas Holsti
@ 2007-10-16 14:16 ` Robert A Duff
  1 sibling, 0 replies; 3+ messages in thread
From: Robert A Duff @ 2007-10-16 14:16 UTC (permalink / raw)


shaunpatterson@gmail.com writes:

> -- system 1
>     type INT_ARRAY is new Integer (1..SYSTEM_1_SIZE);
>
>     Int_Array_Var : INT_ARRAY;

The above doesn't seem right -- not sure what you really meant.

>     if usingSystem1 then
>         Int_array_Var = "pointer to Int_Array_Var_System1"

You can do something like:

    type Int_Array_Ref is access all INT_ARRAY;
    Int_Array_Var : Int_Array_Ref;
    ...
    if .,. then
        Int_Array_Var := Int_Array_Var_System1'Access;
        -- Make Int_Array_Var point to Int_Array_Var_System1.
    ...

You need to make the variables "aliased" -- otherwise,
'Access is illegal.  You might need to add ".all" in
a few places, but in most contexts (including A'Range,
and A(I)), the ".all" is implicit.

But it might be cleaner to pass the arrays around as parameters.

- Bob



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

end of thread, other threads:[~2007-10-16 14:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-10-16 13:40 Pointer to Array shaunpatterson
2007-10-16 14:09 ` Niklas Holsti
2007-10-16 14:16 ` Robert A Duff

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