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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,68038c52a7413447 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2004-04-07 02:34:54 PST Path: archiver1.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!dialin-145-254-036-136.arcor-ip.NET!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Enum or Named Number Date: Wed, 07 Apr 2004 11:34:36 +0200 Organization: At home Message-ID: References: Reply-To: mailbox@dmitry-kazakov.de NNTP-Posting-Host: dialin-145-254-036-136.arcor-ip.net (145.254.36.136) Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de 1081330492 92351081 I 145.254.36.136 ([77047]) User-Agent: KNode/0.7.2 Xref: archiver1.google.com comp.lang.ada:6802 Date: 2004-04-07T11:34:36+02:00 List-Id: 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