comp.lang.ada
 help / color / mirror / Atom feed
From: sjw <simon.j.wright@mac.com>
Subject: Re: Generalized serialization for enumeration types
Date: Wed, 26 Aug 2009 22:05:24 -0700 (PDT)
Date: 2009-08-26T22:05:24-07:00	[thread overview]
Message-ID: <4bc2562f-cfa3-48ca-a6b5-2502669b233e@v36g2000yqv.googlegroups.com> (raw)
In-Reply-To: 249a69e5-8e21-4968-a183-64732618660a@h21g2000yqa.googlegroups.com

On Aug 26, 11:22 am, xorque <xorquew...@googlemail.com> 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:
>
>   The enumeration values are converted to unsigned 32 bit
>   integers with the first value as 0 and increasing sequentially
>   with each new value. The 32 bit value is packed into big-endian
>   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;




  parent reply	other threads:[~2009-08-27  5:05 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-26 10:22 Generalized serialization for enumeration types xorque
2009-08-26 11:00 ` okellogg
2009-08-26 11:33   ` xorque
2009-08-26 12:03     ` okellogg
2009-08-26 11:17 ` Georg Bauhaus
2009-08-26 11:35   ` xorque
2009-08-26 12:05 ` Dmitry A. Kazakov
2009-08-26 19:44 ` Jeffrey R. Carter
2009-08-27  5:05 ` sjw [this message]
2009-08-28 10:47   ` xorque
replies disabled

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