comp.lang.ada
 help / color / mirror / Atom feed
* passing pointer of different type to one procedure
@ 1997-04-16  0:00 Boaz
  1997-04-17  0:00 ` John Harbaugh
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Boaz @ 1997-04-16  0:00 UTC (permalink / raw)



Hi there,

I am trying to write a sort procedure for a data base project.  I have 2
different records.  Instead of having 2 instances for that sort procedure,
I am thinking to use just one that take a pointer of either one of the
records type.

this is my test program but I don't quite sure how to set it up :

with text_io; use text_io;
with ada.float_text_io; use ada.float_text_io;

procedure test is


type record_pointer is access all record;  <-- error

type a is record
	key : float;
	d : integer;
end record;

type b is record
	key : float;
	e : float;
end record;

rp : record_pointer;
d1  : aliased a;
d2	: aliased b;


procedure sort(p : in record_pointer) is
begin
	put(p.key);
end sort;

begin

	d1.key := 1.0;
	d2.key := 5.0;
	rp := d1'access;
	sort(rp);
	rp := d2'access;
	sort(rp);
end test;





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

* Re: passing pointer of different type to one procedure
       [not found]   ` <01bc4ad3$f3702860$6aba0c26@cat>
@ 1997-04-17  0:00     ` Tom Moran
  0 siblings, 0 replies; 6+ messages in thread
From: Tom Moran @ 1997-04-17  0:00 UTC (permalink / raw)



I don't understand.  If you have 10 records of differing structure, you
clearly will need to have 10 different compare procedures.  Is this not
the situation?  It's really the compare's that need to be different.  10
instantiations of a generic sort routine is hardly a significant cost. 
You can make the records the same (for purposes of the compare and sort)
by having a record which contains a key (big enough for any data) and a
pointer to the actual data, or an index into an array or file.  Is that
what you want to do?




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

* Re: passing pointer of different type to one procedure
  1997-04-16  0:00 passing pointer of different type to one procedure Boaz
@ 1997-04-17  0:00 ` John Harbaugh
  1997-04-19  0:00   ` Robert Dewar
       [not found] ` <33555ECA.44A8@bix.com>
  1997-04-18  0:00 ` Mark & Zurima McKinney
  2 siblings, 1 reply; 6+ messages in thread
From: John Harbaugh @ 1997-04-17  0:00 UTC (permalink / raw)



Boaz wrote:
> 
> Hi there,
> 
> I am trying to write a sort procedure for a data base project.  I have 2
> different records.  Instead of having 2 instances for that sort procedure,
> I am thinking to use just one that take a pointer of either one of the
> records type.
> 
> this is my test program but I don't quite sure how to set it up :
> 
> with text_io; use text_io;
> with ada.float_text_io; use ada.float_text_io;
> 
> procedure test is
> 
> type record_pointer is access all record;  <-- error
> 
> type a is record
>         key : float;
>         d : integer;
> end record;
> 
> type b is record
>         key : float;
>         e : float;
> end record;
> 
> rp : record_pointer;
> d1  : aliased a;
> d2      : aliased b;
> 
> procedure sort(p : in record_pointer) is
> begin
>         put(p.key);
> end sort;
> 
> begin
> 
>         d1.key := 1.0;
>         d2.key := 5.0;
>         rp := d1'access;
>         sort(rp);
>         rp := d2'access;
>         sort(rp);
> end test;
Perhaps you could create records as a class hierarchy of tagged types,
then pass a class-wide pointer to your sort routine.

Cheers,

   - John




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

* Re: passing pointer of different type to one procedure
       [not found] ` <33555ECA.44A8@bix.com>
       [not found]   ` <01bc4ad3$f3702860$6aba0c26@cat>
@ 1997-04-17  0:00   ` Robert Dewar
  1 sibling, 0 replies; 6+ messages in thread
From: Robert Dewar @ 1997-04-17  0:00 UTC (permalink / raw)



you could pass the compare procedure, rather than the various,
differently laid out, records.>>

Or you could use pointers to procedures -- see files

g-hesora.ads/adb
g-hesorg.ads/adb

in the GNAT distribution for a general purpose sort program using 
generics (hesorg) or access types (hesora).





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

* Re: passing pointer of different type to one procedure
  1997-04-16  0:00 passing pointer of different type to one procedure Boaz
  1997-04-17  0:00 ` John Harbaugh
       [not found] ` <33555ECA.44A8@bix.com>
@ 1997-04-18  0:00 ` Mark & Zurima McKinney
  2 siblings, 0 replies; 6+ messages in thread
From: Mark & Zurima McKinney @ 1997-04-18  0:00 UTC (permalink / raw)
  To: Boaz


Boaz wrote:
> 
> Hi there,
> 
> I am trying to write a sort procedure for a data base project.  I have 2
> different records.  Instead of having 2 instances for that sort procedure,
> I am thinking to use just one that take a pointer of either one of the
> records type.
> 
> this is my test program but I don't quite sure how to set it up :
> 
> with text_io; use text_io;
> with ada.float_text_io; use ada.float_text_io;
> 
> procedure test is
> 
> type record_pointer is access all record;  <-- error
> 
> type a is record
>         key : float;
>         d : integer;
> end record;
> 
> type b is record
>         key : float;
>         e : float;
> end record;
> 

--You can make then the same type with a discriminant record ...
--Link so ...

type record_types is (A, B);

type Keyed_Record(Record_Type : Record_Types) is 
   record
      Key : Float;
      case Record_Type is
         when A =>
            I : Integer;
         when B =>
            F : Float;
      end case;
   end record;    

--Now the ponter will bee of one type and can point to 2 or more
different types of 
--discriminant records.

type Keyed_Record_Pointer is access Keyed_Record;

--It is likely that you would want to sort an array particularly if
there 
--will be more than two items to sort.

type Keyed_Record_Pointer_Arrays is array(natural range <>) of
Keyed_Record_Pointer;

--this might also be useful
function "<"(Left, Right : in Keyed_Record) return Boolean is
begin
    return Left.Key < Right.Key;
end;

In Ada95 you can also use inheritance.

type keyed_class is tagged 
   record
      Key : Float; 
   end record;

type key_record_pointer is access all keyed_Class;

function "<"(Left, Right : Keyed_Class'Class) return boolean is ......


--perhaps in a child package
type integer_keyed_class is new keyed_class with 
   record
      I : integer;
   end record;

--perhaps in a another child package
type float_keyed_class is new keyed_class with 
   record
      F : float;
   end record;


Enough said this should point you in the right direction.

MArk McKinney




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

* Re: passing pointer of different type to one procedure
  1997-04-17  0:00 ` John Harbaugh
@ 1997-04-19  0:00   ` Robert Dewar
  0 siblings, 0 replies; 6+ messages in thread
From: Robert Dewar @ 1997-04-19  0:00 UTC (permalink / raw)



John said

<<Perhaps you could create records as a class hierarchy of tagged types,
then pass a class-wide pointer to your sort routine.>>

There have been several suggestions which involve changing the data
(using variant records, using tagged types ...)

but surely the desire is to sort different unrelated data with the same
shared program.

And it seems to be that the approach with access procedures that is
in g-hesora in the GNAT library seems by far the most straight
forward approach. We use this in the compiler itself for sorting all
kinds of different data, with shared common code.





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

end of thread, other threads:[~1997-04-19  0:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-04-16  0:00 passing pointer of different type to one procedure Boaz
1997-04-17  0:00 ` John Harbaugh
1997-04-19  0:00   ` Robert Dewar
     [not found] ` <33555ECA.44A8@bix.com>
     [not found]   ` <01bc4ad3$f3702860$6aba0c26@cat>
1997-04-17  0:00     ` Tom Moran
1997-04-17  0:00   ` Robert Dewar
1997-04-18  0:00 ` Mark & Zurima McKinney

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