From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
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 18:24:36 +0100
Date: 2009-11-13T18:24:31+01:00 [thread overview]
Message-ID: <uj01w89h0yfz$.49he8yac790e$.dlg@40tude.net> (raw)
In-Reply-To: 7495d29e-0361-4eba-9e22-a770ae50f113@d5g2000yqm.googlegroups.com
On Fri, 13 Nov 2009 08:03:23 -0800 (PST), Harry Tucker wrote:
> 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>
For bit fields modular types are better:
type Spell_School_Type is mod 2**3;
-- 3 is the number of independent bits
Unknown : constant Spell_School_Type := 0;
Physical : constant Spell_School_Type := 2**0;
Holy : constant Spell_School_Type := 2**1;
Fire : constant Spell_School_Type := 2**2;
Any : constant Spell_School_Type := Spell_School_Type'Last;
> 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.
You parse "0x", then you do a hexadecimal number following it, then you
pass that number (x) to:
Spell_School_Type'Val (x)
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
next prev parent 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
2009-11-13 17:24 ` Dmitry A. Kazakov [this message]
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