comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Enum or Named Number
Date: Wed, 07 Apr 2004 11:34:36 +0200
Date: 2004-04-07T11:34:36+02:00	[thread overview]
Message-ID: <c50hvr$2o2aj9$2@ID-77047.news.uni-berlin.de> (raw)
In-Reply-To: fe4bb2c2.0404060930.f932668@posting.google.com

Jeff wrote:

> Looking for implementation advise.  I'm writing a binding to some C
> code that has several defined number macros (e.g. #define Viewable 1)
> for states and flags.  I would like to bitwise-or these numbers in Ada
> which leads me to believe I should define these as named mod numbers
> such as below:
> 
> type State is mod 2**Interfaces.C.Int'Size
> State1 : constant State := 2#0001#
> State2 : constant State := 2#0010#
> State3 : constant State := 2#0100#
> 
> Any reason to define these as an Enum with a representation clause?
> Seems like there would be more plumbing code.  For example, I believe
> that I would need to write my own bitwise-or function for the Enum.

Formally enum is inappropriate here. An enum defines a set of values, the
whole set. So it would be

{State1, State2, State3}

What you have is a very different thing. It is a product set:

{State1, not State1} x {State2, not State2} x {State3, not State3}

Those are not equivalent. The first has 3 elements, the second has 2**3=8.
An enum equivalent of what you need [without multiple inheritance] looks
like:

type Base1 is new Boolean; -- The state 1 on/off
type Base2 is new Boolean; -- The state 2 on/off
type Base3 is new Boolean; -- The state 3 on/off
type State is record
   First  : Base1;
   Second : Base2;
   Third  : Base3;
end record;
+ representation clauses to pack State into one word
+ function "or" on State

Another variant is a boolean array, which is very close to a modular
integer.

> Is the named number implementation the way to go, or is there a better
> way?

type State is mod 2**3;
State1 : constant State := 2#0001#;
State2 : constant State := 2#0010#;
State3 : constant State := 2#0100#;
+ optionally making +,-,... abstract to prevent their use.

-- 
Regards,
Dmitry A. Kazakov
www.dmitry-kazakov.de



  parent reply	other threads:[~2004-04-07  9:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-04-06 17:30 Enum or Named Number Jeff
2004-04-06 17:49 ` Martin Dowie
2004-04-06 19:22   ` Randy Brukardt
2004-04-07  7:23     ` Martin Dowie
2004-04-07  9:34 ` Dmitry A. Kazakov [this message]
2004-04-13 13:29 ` Robert I. Eachus
replies disabled

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