comp.lang.ada
 help / color / mirror / Atom feed
* Re: Rep Clause Vaues??
  1997-01-22  0:00 Rep Clause Vaues?? Robert B. Love 
  1997-01-22  0:00 ` David C. Hoos, Sr.
@ 1997-01-22  0:00 ` Samuel Tardieu
  1997-01-25  0:00 ` Robert Dewar
  2 siblings, 0 replies; 7+ messages in thread
From: Samuel Tardieu @ 1997-01-22  0:00 UTC (permalink / raw)
  To: Robert B. Love 


>>>>> "Robert" == Robert B Love <rlove@neosoft.com> writes:

Robert> What method can I use to get back the bit pattern or integer
Robert> value represented by the enumerated type?  All my attempts at
Robert> type conversion and pos/val attributes have failed.

Wanna be dirty? :) The untested piece of code below should do the job
using the Get_Value function:

###
   type Int_Mechs is mod 2 ** Mechs'Size;
   for Int_Mechs'Size use Mechs'Size;

   function Get_Value is new Ada.Unchecked_Conversion (Mechs, Int_Mechs);
###

  Sam
-- 
Samuel Tardieu -- sam@ada.eu.org




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

* Re: Rep Clause Vaues??
  1997-01-22  0:00 Rep Clause Vaues?? Robert B. Love 
@ 1997-01-22  0:00 ` David C. Hoos, Sr.
  1997-01-24  0:00   ` Robert A Duff
  1997-01-22  0:00 ` Samuel Tardieu
  1997-01-25  0:00 ` Robert Dewar
  2 siblings, 1 reply; 7+ messages in thread
From: David C. Hoos, Sr. @ 1997-01-22  0:00 UTC (permalink / raw)



Hi Robert,
The main problem in your thinking is that 'Val and 'Pos only relate the
enumeration VALUE and its POSITION in the declaration.  See Ada95 LRM
13.4(11).
The only way to obtain the representation is with unchecked conversion. 
The following code shows how it can be done.  Note carefully the comments
in the code.

with Unchecked_Conversion;
procedure Love is
type mechs is (bolt,latch);
for mechs use(bolt => 12, latch => 29);
-- You should specify a size for this type here.  
-- E.g., for Mechs'Size use Integer'Size.
-- In my experience, when specific representations are needed, e.g., to
match
-- some hardware or software interface, the size should be specified to
insure
-- portability, even if your current Ada compiler generates the correct
size
-- by default.

mechanism : mechs := latch;

-- Here we specify an integer tyoe which has exactly the same size as the
-- mechs type.
type Mechs_Bit_Pattern_Type is new Integer range 0 .. 2 ** Mechs'Size -1;
for Mechs_Bit_Pattern_Type'Size use Mechs'Size;

-- And, of, cource, we declare the object to be of our new type.
bit_pattern : Mechs_Bit_Pattern_Type;

-- here we declare two instances of Unchecked_Conversion to convert from
the
-- integer type to the enumerated type, and vice versa.

function To_Mechs is new Unchecked_Conversion (
    Source => Mechs_Bit_Pattern_Type,
    Target => Mechs
    );

function To_Mechs_Bit_Pattern_Type is new Unchecked_Conversion (
    Source => Mechs,
    Target => Mechs_Bit_Pattern_Type
    );

begin
   --....
   --....
   -- And the assignment now becomes
   bit_pattern := To_Mechs_Bit_Pattern_Type (mechanism);
   --....
end Love;

-- 
David C. Hoos, Sr.,
http://www.dbhwww.com
http://www.ada95.com

Robert B. Love  <rlove@neosoft.com> wrote in article
<5c3oh5$7oh@uuneo.neosoft.com>...
> 
> Because of design aspects beyond my control I find myself wanting
> to know how to return the bit pattern of an enumerated type that
> has a specific rep spec.  A simple example fragment is in order--
> 
> type mechs is (bolt,latch);
> for mechs use(bolt => 12, latch => 29);
> 
> mechanism : mechs := latch;
> bit_pattern : integer;
> 
> begin
>    ....
>    ....
>    bit_pattern := Integer(mechs'val(mechanism));
>    ....
> end
> 
> What method can I use to get back the bit pattern or integer
> value represented by the enumerated type?  All my attempts at
> type conversion and pos/val attributes have failed.
> 
> All help appreciated.
> ----------------------------------------------------------------
> Bob Love, rlove@neosoft.com (local)        MIME & NeXT Mail OK
> rlove@raptor.rmnug.org  (permanent)        PGP key available
> ----------------------------------------------------------------
> 
> 




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

* Rep Clause Vaues??
@ 1997-01-22  0:00 Robert B. Love 
  1997-01-22  0:00 ` David C. Hoos, Sr.
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Robert B. Love  @ 1997-01-22  0:00 UTC (permalink / raw)




Because of design aspects beyond my control I find myself wanting
to know how to return the bit pattern of an enumerated type that
has a specific rep spec.  A simple example fragment is in order--

type mechs is (bolt,latch);
for mechs use(bolt => 12, latch => 29);

mechanism : mechs := latch;
bit_pattern : integer;

begin
   ....
   ....
   bit_pattern := Integer(mechs'val(mechanism));
   ....
end

What method can I use to get back the bit pattern or integer
value represented by the enumerated type?  All my attempts at
type conversion and pos/val attributes have failed.

All help appreciated.
----------------------------------------------------------------
Bob Love, rlove@neosoft.com (local)        MIME & NeXT Mail OK
rlove@raptor.rmnug.org  (permanent)        PGP key available
----------------------------------------------------------------





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

* Re: Rep Clause Vaues??
  1997-01-22  0:00 ` David C. Hoos, Sr.
@ 1997-01-24  0:00   ` Robert A Duff
  0 siblings, 0 replies; 7+ messages in thread
From: Robert A Duff @ 1997-01-24  0:00 UTC (permalink / raw)



In article <01bc0862$77b689c0$018c71a5@dhoossr.iquest.com>,
David C. Hoos, Sr. <david.c.hoos.sr@ada95.com> wrote:
>-- E.g., for Mechs'Size use Integer'Size.
...
>type Mechs_Bit_Pattern_Type is new Integer range 0 .. 2 ** Mechs'Size -1;
>for Mechs_Bit_Pattern_Type'Size use Mechs'Size;

If Mechs'Size = Integer'Size, then Integer'Last is probably
2**(Mechs'Size-1)-1, so the above will overflow.

I suspect you meant:

    type Mechs_Bit_Pattern_Type is range 0 .. 2 ** Mechs'Size -1;

which will work fine, IF the implementation supports integer types
bigger than Integer.

If you want an unsigned type that is the same size as the biggest
supported signed integer, then you have to use modular types, which is
unfortunate, because you lose overflow checking.  (It's unfortunate if
you *want* overflow checking, that is.)

- Bob




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

* Re: Rep Clause Vaues??
  1997-01-22  0:00 Rep Clause Vaues?? Robert B. Love 
  1997-01-22  0:00 ` David C. Hoos, Sr.
  1997-01-22  0:00 ` Samuel Tardieu
@ 1997-01-25  0:00 ` Robert Dewar
  1997-01-26  0:00   ` Matthew Heaney
  2 siblings, 1 reply; 7+ messages in thread
From: Robert Dewar @ 1997-01-25  0:00 UTC (permalink / raw)



Bob Love asks

"What method can I use to get back the bit pattern or integer
value represented by the enumerated type?  All my attempts at
type conversion and pos/val attributes have failed."


1) use unchecked conversion
2) if you are using GNAT, check out the 'Enum_Rep attribute, which does
	exactly what you want.





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

* Re: Rep Clause Vaues??
  1997-01-26  0:00   ` Matthew Heaney
@ 1997-01-26  0:00     ` Robert Dewar
  0 siblings, 0 replies; 7+ messages in thread
From: Robert Dewar @ 1997-01-26  0:00 UTC (permalink / raw)



Matthew says

I'm curious, why didn't you name the attribute just 'Rep, rather than 'Enum_Rep?

Robert replies

because it applies only to enumeration types





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

* Re: Rep Clause Vaues??
  1997-01-25  0:00 ` Robert Dewar
@ 1997-01-26  0:00   ` Matthew Heaney
  1997-01-26  0:00     ` Robert Dewar
  0 siblings, 1 reply; 7+ messages in thread
From: Matthew Heaney @ 1997-01-26  0:00 UTC (permalink / raw)



In article <dewar.854213652@merv>, dewar@merv.cs.nyu.edu (Robert Dewar) wrote:


>"What method can I use to get back the bit pattern or integer
>value represented by the enumerated type?  All my attempts at
>type conversion and pos/val attributes have failed."
>
>2) if you are using GNAT, check out the 'Enum_Rep attribute, which does
>        exactly what you want.

I'm curious, why didn't you name the attribute just 'Rep, rather than 'Enum_Rep?

--------------------------------------------------------------------
Matthew Heaney
Software Development Consultant
<mailto:matthew_heaney@acm.org>
(818) 985-1271




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

end of thread, other threads:[~1997-01-26  0:00 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-01-22  0:00 Rep Clause Vaues?? Robert B. Love 
1997-01-22  0:00 ` David C. Hoos, Sr.
1997-01-24  0:00   ` Robert A Duff
1997-01-22  0:00 ` Samuel Tardieu
1997-01-25  0:00 ` Robert Dewar
1997-01-26  0:00   ` Matthew Heaney
1997-01-26  0:00     ` Robert Dewar

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