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 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-7-bit Path: g2news2.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Date: Wed, 26 Aug 2009 13:17:46 +0200 From: Georg Bauhaus User-Agent: Thunderbird 2.0.0.23 (Macintosh/20090812) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Generalized serialization for enumeration types References: <249a69e5-8e21-4968-a183-64732618660a@h21g2000yqa.googlegroups.com> In-Reply-To: <249a69e5-8e21-4968-a183-64732618660a@h21g2000yqa.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Message-ID: <4a9519db$0$32681$9b4e6d93@newsspool2.arcor-online.net> Organization: Arcor NNTP-Posting-Date: 26 Aug 2009 13:17:47 CEST NNTP-Posting-Host: 7333d193.newsspool2.arcor-online.net X-Trace: DXC=CNG=TLn1XAheoCI^f\Y]EaA9EHlD;3Ycb4Fo<]lROoRa^YC2XCjHcbi>EHjJZ1ZB_b;9OJDO8_SKfNSZ1n^B98ij?]XglFVfB\h X-Complaints-To: usenet-abuse@arcor.de Xref: g2news2.google.com comp.lang.ada:7986 Date: 2009-08-26T13:17:47+02:00 List-Id: 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;