comp.lang.ada
 help / color / mirror / Atom feed
* Enum or Named Number
@ 2004-04-06 17:30 Jeff
  2004-04-06 17:49 ` Martin Dowie
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Jeff @ 2004-04-06 17:30 UTC (permalink / raw)


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.

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



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Enum or Named Number
  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  9:34 ` Dmitry A. Kazakov
  2004-04-13 13:29 ` Robert I. Eachus
  2 siblings, 1 reply; 6+ messages in thread
From: Martin Dowie @ 2004-04-06 17:49 UTC (permalink / raw)


"Jeff" <jeff.huter@bigfoot.com> wrote in message
news:fe4bb2c2.0404060930.f932668@posting.google.com...
> 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.
>
> Is the named number implementation the way to go, or is there a better
> way?

Depends on a couple of things:

1) are you trying to provide a thick or thin binding?
2) can the "states" really mutually exclusive or can they be 'set' at the
same time?

If the answer to 1) is "thick binding" and 2) is "really mutually exclusive"
then I would take the hit and declare an Ada enumeration and use a lookup
table to get the C representation.

Otherwise, I think I'd just stick with the above.

-- Martin






^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Enum or Named Number
  2004-04-06 17:49 ` Martin Dowie
@ 2004-04-06 19:22   ` Randy Brukardt
  2004-04-07  7:23     ` Martin Dowie
  0 siblings, 1 reply; 6+ messages in thread
From: Randy Brukardt @ 2004-04-06 19:22 UTC (permalink / raw)


"Martin Dowie" <martin.dowie@btopenworld.com> wrote in message
news:c4uqji$b5h$1@titan.btinternet.com...
> "Jeff" <jeff.huter@bigfoot.com> wrote in message
> news:fe4bb2c2.0404060930.f932668@posting.google.com...
> > 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.
> >
> > Is the named number implementation the way to go, or is there a better
> > way?
>
> Depends on a couple of things:
>
> 1) are you trying to provide a thick or thin binding?
> 2) can the "states" really mutually exclusive or can they be 'set' at the
> same time?
>
> If the answer to 1) is "thick binding" and 2) is "really mutually
exclusive"
> then I would take the hit and declare an Ada enumeration and use a lookup
> table to get the C representation.
>
> Otherwise, I think I'd just stick with the above.

I concur with Martin. We faced this issue in Claw, and we decided to use
constants of a private type with combining operations. That way, the
implementation as numbers isn't exposed, but they still work like the bit
operations.

In some of the later packages, we've used enumerations, but only when the
items really are mutually exclusive. And even then, we've had occassional
problems from later changes to the interfaces. (Just because they're
exclusive in version 4.0 doesn't mean that they'll remain exclusive in
version 5.0.)

                        Randy.






^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Enum or Named Number
  2004-04-06 19:22   ` Randy Brukardt
@ 2004-04-07  7:23     ` Martin Dowie
  0 siblings, 0 replies; 6+ messages in thread
From: Martin Dowie @ 2004-04-07  7:23 UTC (permalink / raw)


"Randy Brukardt" <randy@rrsoftware.com> wrote in message
> I concur with Martin. We faced this issue in Claw, and we decided to use

I'm framing that one! ;-)



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Enum or Named Number
  2004-04-06 17:30 Enum or Named Number Jeff
  2004-04-06 17:49 ` Martin Dowie
@ 2004-04-07  9:34 ` Dmitry A. Kazakov
  2004-04-13 13:29 ` Robert I. Eachus
  2 siblings, 0 replies; 6+ messages in thread
From: Dmitry A. Kazakov @ 2004-04-07  9:34 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Enum or Named Number
  2004-04-06 17:30 Enum or Named Number Jeff
  2004-04-06 17:49 ` Martin Dowie
  2004-04-07  9:34 ` Dmitry A. Kazakov
@ 2004-04-13 13:29 ` Robert I. Eachus
  2 siblings, 0 replies; 6+ messages in thread
From: Robert I. Eachus @ 2004-04-13 13:29 UTC (permalink / raw)


Jeff wrote:

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

It may be, but there is a third way that is often right for such types. 
  You define an enumeration type:

type State_Index is (...State3_Index, State2_Index, State1_Index);

(You may not need to reverse the order to match the C code, but it is 
the more common way of defining such types in C.  And in that case it is 
much easier to have exactly 16 or 32--or whatever word size is--values.) 
Now you define your state type:

type State is array (State_Index) of Boolean;

And now you can define any subsets you need:

State3: State := (State3_Index => True, others => False);

This approach is very wordy in the defining package. (But what is cut 
and paste for?)  It does give you an abstraction with the various 
(mathematical) set operations defined but no arithmetic operations.  Of 
course, another way to do that is to make the type private and export 
only the operations you want.


-- 

                                           Robert I. Eachus

"The terrorist enemy holds no territory, defends no population, is 
unconstrained by rules of warfare, and respects no law of morality. Such 
an enemy cannot be deterred, contained, appeased or negotiated with. It 
can only be destroyed--and that, ladies and gentlemen, is the business 
at hand."  -- Dick Cheney




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2004-04-13 13:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2004-04-13 13:29 ` Robert I. Eachus

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