comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: I need a little help - it's been a long time - with enumeration type  and for use representation
Date: Fri, 13 Nov 2009 19:24:08 +0200
Date: 2009-11-13T19:24:08+02:00	[thread overview]
Message-ID: <4afd965b$0$6284$4f793bc4@news.tdc.fi> (raw)
In-Reply-To: <7495d29e-0361-4eba-9e22-a770ae50f113@d5g2000yqm.googlegroups.com>

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:
> 
> <code>
>    type Spell_School_Type is (
>       UNKNOWN,
>       PHYSICAL,
>       HOLY,
>       FIRE,
>     ...
> </code>
> 
> And since the data has a value I use a represetation clause as:
> <code>
>    for Spell_School_Type use
>      (UNKNOWN     => 2#0000_0000#,
>       PHYSICAL      => 2#0000_0001#,
>       HOLY             => 2#0000_0010#,
>       FIRE              => 2#0000_0100#,
>     ...
> </code>
> 
> 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
       .      @       .



  reply	other threads:[~2009-11-13 17:24 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-13 16:03 I need a little help - it's been a long time - with enumeration type and for use representation Harry Tucker
2009-11-13 17:24 ` Niklas Holsti [this message]
2009-11-13 17:24 ` Dmitry A. Kazakov
2009-11-13 18:48   ` Jeffrey R. Carter
2009-11-13 19:13     ` Dmitry A. Kazakov
2009-11-13 20:32       ` Jeffrey R. Carter
2009-11-13 20:53         ` Dmitry A. Kazakov
2009-11-13 21:30           ` Jeffrey R. Carter
2009-11-14  9:24             ` Dmitry A. Kazakov
2009-11-13 18:46 ` Jeffrey R. Carter
replies disabled

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