comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <rm.dash-bauhaus@futureapps.de>
Subject: Re: Generalized serialization for enumeration types
Date: Wed, 26 Aug 2009 13:17:46 +0200
Date: 2009-08-26T13:17:47+02:00	[thread overview]
Message-ID: <4a9519db$0$32681$9b4e6d93@newsspool2.arcor-online.net> (raw)
In-Reply-To: <249a69e5-8e21-4968-a183-64732618660a@h21g2000yqa.googlegroups.com>

xorque schrieb:
> 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.

Not exactly the same solution, but there doesn't seem to be
a problem with a generic and Seuqential_IO; I might well
have misunderstood.

There is a #Gem at AdaCors's web site explaining how
to use different representations for types derived from
e.g. enum types, if that is any help.

package Enums is

   type Color is (Red, Green, Blue);
   type Smell is (Good, Neutral, Bad);
private
   for Smell use (Good => 14, Neutral => 23, Bad => 100);
   for Smell'size use 32;
   for Color'size use 32;
end Enums;

generic
   type E is (<>);
procedure EG(extension : String);

with Ada.Sequential_IO;
with Interfaces;  use Interfaces;
with Ada.Unchecked_Conversion;

procedure EG(extension : String) is
   package My_IO is new Ada.Sequential_IO(Unsigned_32);
   function To_Unsigned_32 is new Ada.Unchecked_Conversion
      (Source => E, Target => Unsigned_32);
   F : My_IO.File_Type;
begin
   My_IO.Create(F, My_IO.Out_File, "run." & extension);
   for K in E loop
      My_IO.Write(F, Item => To_Unsigned_32 (K));
   end loop;
end EG;

with Enums, EG;
procedure Run is
   use Enums;
   procedure Color_Out is new EG(Color);
   procedure Smell_Out is new EG(Smell);
begin
   Color_Out("rgb");
   Smell_Out("snf");
end Run;



  parent reply	other threads:[~2009-08-26 11:17 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 [this message]
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
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