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.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,e5dc20115bb61aec X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!postnews.google.com!v36g2000yqv.googlegroups.com!not-for-mail From: sjw Newsgroups: comp.lang.ada Subject: Re: Generalized serialization for enumeration types Date: Wed, 26 Aug 2009 22:05:24 -0700 (PDT) Organization: http://groups.google.com Message-ID: <4bc2562f-cfa3-48ca-a6b5-2502669b233e@v36g2000yqv.googlegroups.com> References: <249a69e5-8e21-4968-a183-64732618660a@h21g2000yqa.googlegroups.com> NNTP-Posting-Host: 82.30.110.254 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1251349524 13289 127.0.0.1 (27 Aug 2009 05:05:24 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 27 Aug 2009 05:05:24 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: v36g2000yqv.googlegroups.com; posting-host=82.30.110.254; posting-account=_RXWmAoAAADQS3ojtLFDmTNJCT0N2R4U User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-us) AppleWebKit/531.9 (KHTML, like Gecko) Version/4.0.3 Safari/531.9,gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:8012 Date: 2009-08-26T22:05:24-07:00 List-Id: On Aug 26, 11:22=A0am, xorque wrote: > Hello. > > I'm designing a package that uses a lot of similar but distinct > enumeration types. > > At some point, those types need to be encoded to be sent over > the wire. The encoding rules are simple: > > =A0 The enumeration values are converted to unsigned 32 bit > =A0 integers with the first value as 0 and increasing sequentially > =A0 with each new value. The 32 bit value is packed into big-endian > =A0 byte order. > > The problem is: With so many enumeration types, I now have about 300 > lines of almost identical procedures (to be used as stream attributes) > that just call a procedure that packs Unsigned_32 values into > Storage_Element arrays. > > Is there some clever way I can just write ONE 'Write attribute > procedure > and ONE 'Read attribute procedure and have all the defined enumeration > types use those? > > Freezing rules prevented me from writing a generic > Packed_Enumeration_IO > package ("representation item appears too late"). This _compiles_: with Ada.Streams; generic type E is (<>); procedure Xorque_Writer_G (Stream : access Ada.Streams.Root_Stream_Type'Class; V : E); procedure Xorque_Writer_G (Stream : access Ada.Streams.Root_Stream_Type'Class; V : E) is begin null; end Xorque_Writer_G; with Xorque_Writer_G; package Xorque is type E_Base is (A, B, C); procedure E_Base_Writer is new Xorque_Writer_G (E_Base); type E is new E_Base; for E'Write use E_Base_Writer; end Xorque;