comp.lang.ada
 help / color / mirror / Atom feed
* Accessing the underlying rep for enumerated types?
@ 2004-10-12 22:32 Dale Stanbrough
  2004-10-13  0:20 ` Stephen Leake
  2004-10-13  1:26 ` Jeffrey Carter
  0 siblings, 2 replies; 8+ messages in thread
From: Dale Stanbrough @ 2004-10-12 22:32 UTC (permalink / raw)


An enumeration type can have a representation clause applied to it
allowing you to specify a underlying integer representation for
each value.

'Pos tells you the position within the enumeration, not it's
underlying value. Is there any way to get access to this value
other than using Unchecked_Conversion?

Dale

-- 
dstanbro@spam.o.matic.bigpond.net.au



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

* Re: Accessing the underlying rep for enumerated types?
  2004-10-12 22:32 Accessing the underlying rep for enumerated types? Dale Stanbrough
@ 2004-10-13  0:20 ` Stephen Leake
       [not found]   ` <MrNoSpam-2408AC.16521913102004@news-server.bigpond.net.au>
  2004-10-13  1:26 ` Jeffrey Carter
  1 sibling, 1 reply; 8+ messages in thread
From: Stephen Leake @ 2004-10-13  0:20 UTC (permalink / raw)
  To: comp.lang.ada

Dale Stanbrough <MrNoSpam@bigpoop.net.au> writes:

> An enumeration type can have a representation clause applied to it
> allowing you to specify a underlying integer representation for
> each value.
> 
> 'Pos tells you the position within the enumeration, not it's
> underlying value. Is there any way to get access to this value
> other than using Unchecked_Conversion?

No.

This is (unfortunately) a FAQ.

What's wrong with Unchecked_Conversion?

And why do you want to expose the underlying representation?

-- 
-- Stephe




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

* Re: Accessing the underlying rep for enumerated types?
  2004-10-12 22:32 Accessing the underlying rep for enumerated types? Dale Stanbrough
  2004-10-13  0:20 ` Stephen Leake
@ 2004-10-13  1:26 ` Jeffrey Carter
  2004-10-13  6:51   ` Dale Stanbrough
  1 sibling, 1 reply; 8+ messages in thread
From: Jeffrey Carter @ 2004-10-13  1:26 UTC (permalink / raw)


Dale Stanbrough wrote:

> An enumeration type can have a representation clause applied to it
> allowing you to specify a underlying integer representation for
> each value.
> 
> 'Pos tells you the position within the enumeration, not it's
> underlying value. Is there any way to get access to this value
> other than using Unchecked_Conversion?

Sure:

type Enum is (One, Two, Three);
for Enum use (One => 1, Two => 2, Three => 4);

type Rep_List is array (Enum) of Positive;

Get_Rep : constant Rep_List := (One => 1, Two => 2, Three => 4);

E   : Enum     := Enum'First;
Rep : Positive := Get_Rep (E);

If you mean a way built into the language, that's called 
Unchecked_Conversion.

-- 
Jeff Carter
"My legs are gray, my ears are gnarled, my eyes are old and bent."
Monty Python's Life of Brian
81




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

* Re: Accessing the underlying rep for enumerated types?
  2004-10-13  1:26 ` Jeffrey Carter
@ 2004-10-13  6:51   ` Dale Stanbrough
  2004-10-13 21:49     ` Nick Roberts
  2004-10-14  1:25     ` Jeffrey Carter
  0 siblings, 2 replies; 8+ messages in thread
From: Dale Stanbrough @ 2004-10-13  6:51 UTC (permalink / raw)


Jeffrey Carter wrote:

> Dale Stanbrough wrote:
> 
> > An enumeration type can have a representation clause applied to it
> > allowing you to specify a underlying integer representation for
> > each value.
> > 
> > 'Pos tells you the position within the enumeration, not it's
> > underlying value. Is there any way to get access to this value
> > other than using Unchecked_Conversion?
> 
> Sure:
> 
> type Enum is (One, Two, Three);
> for Enum use (One => 1, Two => 2, Three => 4);
> 
> type Rep_List is array (Enum) of Positive;
> 
> Get_Rep : constant Rep_List := (One => 1, Two => 2, Three => 4);
> 
> E   : Enum     := Enum'First;
> Rep : Positive := Get_Rep (E);
> 
> If you mean a way built into the language, that's called 
> Unchecked_Conversion.

<sigh>....

Obviously I have to phrase this much more pedantically.

Is there any feature in the language that allows me to get access
to the underlying enumeration values other than using Unchecked
Conversion?

Dale

-- 
dstanbro@spam.o.matic.bigpond.net.au



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

* Re: Accessing the underlying rep for enumerated types?
       [not found]   ` <MrNoSpam-2408AC.16521913102004@news-server.bigpond.net.au>
@ 2004-10-13  7:23     ` Marius Amado Alves
  2004-10-13 12:29     ` Stephen Leake
  1 sibling, 0 replies; 8+ messages in thread
From: Marius Amado Alves @ 2004-10-13  7:23 UTC (permalink / raw)
  To: comp.lang.ada

>>This is (unfortunately) a FAQ.

Yes. The thread "accesing internal codes used for an enumeration", 
initiated by myself on 2001, contains full explanation of this subject. 
My last message in that thread is a recap.




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

* Re: Accessing the underlying rep for enumerated types?
       [not found]   ` <MrNoSpam-2408AC.16521913102004@news-server.bigpond.net.au>
  2004-10-13  7:23     ` Marius Amado Alves
@ 2004-10-13 12:29     ` Stephen Leake
  1 sibling, 0 replies; 8+ messages in thread
From: Stephen Leake @ 2004-10-13 12:29 UTC (permalink / raw)
  To: comp.lang.ada

Dale Stanbrough <MrNoSpam@bigpoop.net.au> writes:

> Stephen Leake wrote:
> 
> > Dale Stanbrough <MrNoSpam@bigpoop.net.au> writes:
> > 
> > > An enumeration type can have a representation clause applied to it
> > > allowing you to specify a underlying integer representation for
> > > each value.
> > > 
> > > 'Pos tells you the position within the enumeration, not it's
> > > underlying value. Is there any way to get access to this value
> > > other than using Unchecked_Conversion?
> > 
> > No.
> > 
> > This is (unfortunately) a FAQ.
> > 
> > What's wrong with Unchecked_Conversion?
> 
> There's nothing wrong with it.

Then why ask this question?
 
> > And why do you want to expose the underlying representation?
> 
> So I can show people how holey enumeration types can be expensive.

Ah, I forgot you are a teacher (or do you prefer 'professor'?). 

So use Unchecked_Conversion!

-- 
-- Stephe




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

* Re: Accessing the underlying rep for enumerated types?
  2004-10-13  6:51   ` Dale Stanbrough
@ 2004-10-13 21:49     ` Nick Roberts
  2004-10-14  1:25     ` Jeffrey Carter
  1 sibling, 0 replies; 8+ messages in thread
From: Nick Roberts @ 2004-10-13 21:49 UTC (permalink / raw)


Dale Stanbrough wrote:

> <sigh>....

Yes, me too. The answers to Dale's question seem to have been a little
less than courteous and sympathetic.

> Obviously I have to phrase this much more pedantically.
> 
> Is there any feature in the language that allows me to get access
> to the underlying enumeration values other than using Unchecked
> Conversion?

I think the answer is "No", there is no such feature in the language.

Obviously any use of Unchecked_Conversion is theoretically non-portable,
and so, indeed, is any attempt to specify the representation of an
enumerated type.

On the other hand, I suspect that using Unchecked_Conversion to convert
to a modular type of the right size is likely to work on most
implementations (perhaps all that don't reject it). I've tested this
with GNAT 3.15p (it works). Writing out a value of the enumerated type
to a stream, and then reading it back into an object of a modular type
(of the right size) might also work.

Some implementations might provide an attribute which returns the
underlying value. I admit, I'm not aware of any.

-- 
Nick Roberts



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

* Re: Accessing the underlying rep for enumerated types?
  2004-10-13  6:51   ` Dale Stanbrough
  2004-10-13 21:49     ` Nick Roberts
@ 2004-10-14  1:25     ` Jeffrey Carter
  1 sibling, 0 replies; 8+ messages in thread
From: Jeffrey Carter @ 2004-10-14  1:25 UTC (permalink / raw)


Dale Stanbrough wrote:

> Jeffrey Carter wrote:
> 
>>If you mean a way built into the language, that's called 
>>Unchecked_Conversion.
> 
> Obviously I have to phrase this much more pedantically.
> 
> Is there any feature in the language that allows me to get access
> to the underlying enumeration values other than using Unchecked
> Conversion?

As I pointed out, attempting to keep tongue in cheek, the only feature 
in the language that provides this is Unchecked_Conversion.

I believe GNAT has a non-portable attribute to do this. Yes, 'Enum_Rep.

-- 
Jeff Carter
"Mr. President, we must not allow a mine-shaft gap!"
Dr. Strangelove
33




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

end of thread, other threads:[~2004-10-14  1:25 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-10-12 22:32 Accessing the underlying rep for enumerated types? Dale Stanbrough
2004-10-13  0:20 ` Stephen Leake
     [not found]   ` <MrNoSpam-2408AC.16521913102004@news-server.bigpond.net.au>
2004-10-13  7:23     ` Marius Amado Alves
2004-10-13 12:29     ` Stephen Leake
2004-10-13  1:26 ` Jeffrey Carter
2004-10-13  6:51   ` Dale Stanbrough
2004-10-13 21:49     ` Nick Roberts
2004-10-14  1:25     ` Jeffrey Carter

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