From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d7ad26fbff0ddd28 X-Google-Attributes: gid103376,public From: matthew_heaney@acm.org (Matthew Heaney) Subject: Re: Generic package exporting a type with a rep spec Date: 1998/05/11 Message-ID: #1/1 X-Deja-AN: 352434416 Content-Transfer-Encoding: 8bit References: <6j7j5j$7i1$1@newsreader.digex.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Organization: Network Intensive Newsgroups: comp.lang.ada Date: 1998-05-11T00:00:00+00:00 List-Id: In article <6j7j5j$7i1$1@newsreader.digex.net>, "Bob Mikkelsen" 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 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 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