comp.lang.ada
 help / color / mirror / Atom feed
* Generic package exporting a type with a rep spec
@ 1998-05-11  0:00 Bob Mikkelsen
  1998-05-11  0:00 ` Matthew Heaney
  1998-05-12  0:00 ` david.c.hoos.sr
  0 siblings, 2 replies; 4+ messages in thread
From: Bob Mikkelsen @ 1998-05-11  0:00 UTC (permalink / raw)



Is there any good way to construct a generic package which exports a data
structure whose rep spec may vary on separate instantiations?

We are trying to solve a problem where we have data coming in from various
sources.  All the data requires similar processing when it gets to us, but
when it is imported or exported to the sources, it may have slightly
different forms: sometimes 7 bytes, sometimes 11.

We are trying to generalize the representation of the data in the generic by
bringing the data size into the generic with a generic formal parameter, but
when we try to use this value in the rep spec, the complier gets very fussy
(a static value is required).

Any solution not requiring C will be welcomed.






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

* Re: Generic package exporting a type with a rep spec
  1998-05-11  0:00 Generic package exporting a type with a rep spec Bob Mikkelsen
@ 1998-05-11  0:00 ` Matthew Heaney
  1998-05-12  0:00 ` david.c.hoos.sr
  1 sibling, 0 replies; 4+ messages in thread
From: Matthew Heaney @ 1998-05-11  0:00 UTC (permalink / raw)



In article <6j7j5j$7i1$1@newsreader.digex.net>, "Bob Mikkelsen"
<bobmik@access.digex.net> wrote:

(start of quote)
Is there any good way to construct a generic package which exports a data
structure whose rep spec may vary on separate instantiations?

We are trying to solve a problem where we have data coming in from various
sources.  All the data requires similar processing when it gets to us, but
when it is imported or exported to the sources, it may have slightly
different forms: sometimes 7 bytes, sometimes 11.

We are trying to generalize the representation of the data in the generic by
bringing the data size into the generic with a generic formal parameter, but
when we try to use this value in the rep spec, the complier gets very fussy
(a static value is required).
(end of quote)

Here are a couple of ideas.  The concept is to specific the representation
statically, outside of the generic, and then have the generic import that
type.

1) Import the type as derived from base type, as in

package P is

   type T is 
      record 
         I : Integer;
         C : Character;
      end record;

   generic
      type NT is new T;
   package GQ is 

      <ops to process NT>

   end GQ;

end P;

This works if you have a raw type, an unencapsulated record or something,
whose structure (though not specific representation) is known to GQ.  To
instantiate this package, we're going to take advantage of Ada's "change of
representation" facility:

with P;
package R is
  
   type NT is new P.T;

   for NT use
      record
         I at 0 range 0 .. 31;
         C at 7 range 0 .. 7;
      end record;

   package Q is new P.GQ (NT);

end R;

Vwah-lah: You've now got a set of ops (in R.Q) that operate on type NT,
which is just like T, but with a different representation.  Let's try it
again, with a different representation:

with P;
package S is
   
   type NT is new P.T;
   
   for NT use
      record
         C at 0 range 0 .. 7;
         I at 4 range 0 .. 31;
      end record;

   package Q is new P.GQ (NT);

end S;

S.NT has a different representation from R.NT, but you still get to use the
ops in GQ.

2)  If the exact structure of the type to operate on isn't known (say, it's
not a record with different representations), then import the type as a
generic formal type, with operations to operate on the data, ie

generic
   type T is private;
   with function Get_Integer (O : T) return Integer is <>;
   with function Get_Char (O : T) return Character is <>;
package GQ is 

   <ops to operate of objects of type T>

end GQ;

T can be any format you need.  Let's try a byte stream:

package S is

   subtype T is Stream_Element_Array;

   function Read_Integer (O : T) return Integer;

   function Read_Character (O : T) return Character;

   package Q is new GQ (T, Read_Integer, Read_Character);

end S;  


Is this what you had in mind?

Hope this helps,
Matt




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

* Re: Generic package exporting a type with a rep spec
  1998-05-11  0:00 Generic package exporting a type with a rep spec Bob Mikkelsen
  1998-05-11  0:00 ` Matthew Heaney
@ 1998-05-12  0:00 ` david.c.hoos.sr
  1998-05-14  0:00   ` Niklas Holsti
  1 sibling, 1 reply; 4+ messages in thread
From: david.c.hoos.sr @ 1998-05-12  0:00 UTC (permalink / raw)



In article <6j7j5j$7i1$1@newsreader.digex.net>,
  "Bob Mikkelsen" <bobmik@access.digex.net> wrote:
>
> Is there any good way to construct a generic package which exports a data
> structure whose rep spec may vary on separate instantiations?
>
> We are trying to solve a problem where we have data coming in from various
> sources.  All the data requires similar processing when it gets to us, but
> when it is imported or exported to the sources, it may have slightly
> different forms: sometimes 7 bytes, sometimes 11.
>
> We are trying to generalize the representation of the data in the generic by
> bringing the data size into the generic with a generic formal parameter, but
> when we try to use this value in the rep spec, the complier gets very fussy
> (a static value is required).
>
> Any solution not requiring C will be welcomed.
>
Well, I'm not sure I have fully understood your problem, but it sounds like
a job for tagged types.

Assuming that either the source of the message or something in its content
defines of which type it is, tagged types are the ticket, I believe.

I have posted a tagged_types example on my FTP site at:
ftp://ftp.ada95.com/pub/tagged_types

If this is not going in the right direction to solve your problem, please
send more details (preferably by E-Mail)

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading




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

* Re: Generic package exporting a type with a rep spec
  1998-05-12  0:00 ` david.c.hoos.sr
@ 1998-05-14  0:00   ` Niklas Holsti
  0 siblings, 0 replies; 4+ messages in thread
From: Niklas Holsti @ 1998-05-14  0:00 UTC (permalink / raw)



"Bob Mikkelsen" <bobmik@access.digex.net> wrote:
>
> Is there any good way to construct a generic package which exports a data
> structure whose rep spec may vary on separate instantiations?
>
> We are trying to solve a problem where we have data coming in from various
> sources.  All the data requires similar processing when it gets to us, but
> when it is imported or exported to the sources, it may have slightly
> different forms: sometimes 7 bytes, sometimes 11.
>
> We are trying to generalize the representation of the data in the generic by
> bringing the data size into the generic with a generic formal parameter, but
> when we try to use this value in the rep spec, the complier gets very fussy
> (a static value is required).
>
> Any solution not requiring C will be welcomed.

Perhaps you could replace the "size" formal parameter with two generic
formal subprogram parameters, one to convert data from the external
form to the internal form, and the other to convert from internal form
to external form.

The generic would define the internal form without a rep spec; the user
who instantiates the generic would define an external form with the
particular rep spec needed, and provide the internal/external
converters.

I'm assuming that the structure of the type "exported" by the generic is
public, so that the user can write the conversion subprograms.




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

end of thread, other threads:[~1998-05-14  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-05-11  0:00 Generic package exporting a type with a rep spec Bob Mikkelsen
1998-05-11  0:00 ` Matthew Heaney
1998-05-12  0:00 ` david.c.hoos.sr
1998-05-14  0:00   ` Niklas Holsti

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