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,b85c1b84f840dc22 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!news-out1.kabelfoon.nl!newsfeed.kabelfoon.nl!xindi.nntp.kabelfoon.nl!fi.sn.net!newsfeed2.fi.sn.net!news.song.fi!not-for-mail Date: Fri, 13 Nov 2009 19:24:08 +0200 From: Niklas Holsti Organization: Tidorum Ltd User-Agent: Mozilla-Thunderbird 2.0.0.22 (X11/20090706) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: I need a little help - it's been a long time - with enumeration type and for use representation References: <7495d29e-0361-4eba-9e22-a770ae50f113@d5g2000yqm.googlegroups.com> In-Reply-To: <7495d29e-0361-4eba-9e22-a770ae50f113@d5g2000yqm.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <4afd965b$0$6284$4f793bc4@news.tdc.fi> NNTP-Posting-Host: 81.17.205.61 X-Trace: 1258133084 news.tdc.fi 6284 81.17.205.61:57316 X-Complaints-To: abuse@tdcnet.fi Xref: g2news1.google.com comp.lang.ada:8088 Date: 2009-11-13T19:24:08+02:00 List-Id: Harry Tucker wrote: > I haven't played with Ada for many years. I've read John Barnes' book, > Programming in Ada 2005, and picked through the ARM on my problem. > > I have a piece of data which represents a enumeration as a hexadecimal > value. It is a bit field but I see the data most usefull as an > enumeration. for example in the CSV file the data is represented as > '0x1'. So I defined the enum as: > > > type Spell_School_Type is ( > UNKNOWN, > PHYSICAL, > HOLY, > FIRE, > ... > > > And since the data has a value I use a represetation clause as: > > for Spell_School_Type use > (UNKNOWN => 2#0000_0000#, > PHYSICAL => 2#0000_0001#, > HOLY => 2#0000_0010#, > FIRE => 2#0000_0100#, > ... > > > Is the best way to parse the raw info (i.e. 0x1) into the enumeration > (i.e PHYSICAL) to use a if/elsif and assign the enum position or is > there an attribute which works best. I would use an enumeration representation clause only in very limited circumstances: when the input and output is binary (not text), and the enumerated type is not used as an array index or case-statement index. In Harry's case, the input seems to be text (CSV) so I would not use a representation clause. I would use an if/elsif sequence if the number of possible values is small (say not more than 5). Otherwise, given an input such as Str(1..3) = "0x1", I would: - define an integer type, say Code_Type, to represent the hex value, - check and remove the "0x" prefix: Str(1..2) = "0x" - enclose the rest in the Ada hex brackets "16#" and "#", - use the 'Value attribute to get the hex value: Code : Code_Type := Code_Type'Value ("16#" & Str(3..Str'Last) & '#'); - define a constant array to map the hex value to the enumeration value: To_Spell_School : constant array (Code_Type) of Spell_School_Type := ( 16#0# => UNKNOWN, 16#1# => PHYSICAL, 16#2# => HOLY, 16#4# => FIRE, .. others => INVALID_CODE); where INVALID_CODE is added to Spell_School_Type. If you use an enumeration representation clause on Spell_School_Type, to read textual input you have to get the Code value as above (using 'Value), then use Unchecked_Conversion from Code_Type to Spell_School_Type (taking care that the sizes match), and then check the validity of the result using the 'Valid attribute. I prefer the above solution, using a mapping array, because it avoids Unchecked_Conversion. HTH, -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .