comp.lang.ada
 help / color / mirror / Atom feed
* hash tables and class-wide types as fields in a record
@ 2000-02-02  0:00 Anna Esparcia
  2000-02-02  0:00 ` Matthew Heaney
  0 siblings, 1 reply; 2+ messages in thread
From: Anna Esparcia @ 2000-02-02  0:00 UTC (permalink / raw)


I'm trying to build a (generic) hash table that can store data belonging to
various subclasses of a base class - which subclass won't be known till run
time.
The code is as follows:

generic
   type DATA_TYPE is tagged private;
   type IDENT_TYPE is private;
   TABLE_SIZE : POSITIVE;
---
   type ELEMENT_TYPE;
   type INDEX_TYPE is access all ELEMENT_TYPE;
   type ELEMENT_TYPE is
      record
         DATA  : DATA_TYPE;
         IDENT : IDENT_TYPE;
         NEXT  : INDEX_TYPE:= null;
      end record;

   HASH_TABLE : array(0..TABLE_SIZE-1) of INDEX_TYPE:= (others => null);

What i would like to do is this:
 DATA  : DATA_TYPE'Class;
so that i could store objects of type DATA_TYPE or any of its subclasses,
but the compiler doesn't like it 'cos it's an "unconstrained subtype in
component declaration"
Now, I could declare a type pointer as follows:

type DATA_POINTER is access all DATA_TYPE'Class;

and an object of this type could point to any object of type DATA_TYPE or
any of its subclasses.
So, in my record above i can declare:
 DATA  : DATA_POINTER;
and the compiler accepts it, but when i add objects to the table the
pointers end up pointing to nothing (when the actual objects disappear).

Now, i could declare the record as originally (with DATA  : DATA_TYPE, i.e.
an object and not a pointer) but do the argument passing in the methods with
pointers e.g. i would have a method
ADD(p: DATA_POINTER)
which would copy the object pointed to by p into the field DATA of the
record. But the compiler doesn't let me do this either.

Any ideas? Any other way of implementing the hash table?
BTW the hash tables will be in shared memory and once they've been created
they'll stay there forever, so that they can be accessible to anybody. Can i
copy the objects pointed to onto global objects in this shared memory so
that they don't vanish into thin air? Will this defeat the whole point of
having a hash table?

Anna






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

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

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-02-02  0:00 hash tables and class-wide types as fields in a record Anna Esparcia
2000-02-02  0:00 ` Matthew Heaney

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