comp.lang.ada
 help / color / mirror / Atom feed
* Retrieving Rep Spec'ed Enumeration Values
@ 1993-06-28 20:47 cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland.
  0 siblings, 0 replies; 6+ messages in thread
From: cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland. @ 1993-06-28 20:47 UTC (permalink / raw)


Help!

Suppose you had a type like so:

  type Color is ( Red, Green, Blue );
  for Color use
    ( Red   => 4,
      Green => 8,
      Blue  => 16 );

I cannot seem to find a portable way to retrieve the values assigned
in the rep-spec.  Color'Pos is explicitly defined to be unaffected by
the rep spec (LRM 13.3 paragraph 6).
        /s
--
Alexander Erskine Wise /\/\/\/\/\/\/\/\/\/\/\/\ Software Development Laboratory
/\/\/\/\/\/\/\/\/\/\/\/\/\/\ WISE@CS.UMASS.EDU /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\ This situation calls for large amounts of unadulterated CHOCOLATE! /\/\/\

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

* Re: Retrieving Rep Spec'ed Enumeration Values
@ 1993-06-28 20:58 Mark A Biggar
  0 siblings, 0 replies; 6+ messages in thread
From: Mark A Biggar @ 1993-06-28 20:58 UTC (permalink / raw)


In article <SANDY.93Jun28134749@beeker.cs.umass.edu> wise@cs.umass.edu writes:
>Suppose you had a type like so:
>  type Color is ( Red, Green, Blue );
>  for Color use ( Red   => 4, Green => 8, Blue  => 16 );
>I cannot seem to find a portable way to retrieve the values assigned
>in the rep-spec.  Color'Pos is explicitly defined to be unaffected by
>the rep spec (LRM 13.3 paragraph 6).

The only protable way is to use Unchecked_conversion.

But, why does your program even need to get at the rep values?  If your program
needs to work with anything other then the enumeration identifiers, then it
very likely has a very badly broken abstraction.  I have never seen an Ada
program that required emuneration rep specs, that really required the
internal representation to become visible in the program.

But, if you really really need it just declare an anonymous array to be
used for conversions:

internal_colors: array (Color) of integer := (4,8,16);

--
Mark Biggar
mab@wdl.loral.com

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

* Re: Retrieving Rep Spec'ed Enumeration Values
@ 1993-06-29 16:39 agate!howland.reston.ans.net!noc.near.net!nic.umass.edu!ymir.cs.umass.edu
  0 siblings, 0 replies; 6+ messages in thread
From: agate!howland.reston.ans.net!noc.near.net!nic.umass.edu!ymir.cs.umass.edu @ 1993-06-29 16:39 UTC (permalink / raw)


In article <1993Jun28.205806.11601@wdl.loral.com> mab@dst17.wdl.loral.com (Mark
 A Biggar) writes:
   In article I write:
   >Suppose you had a type like so:
   >  type Color is ( Red, Green, Blue );
   >  for Color use ( Red   => 4, Green => 8, Blue  => 16 );
   >I cannot seem to find a portable way to retrieve the values assigned
   >in the rep-spec.  Color'Pos is explicitly defined to be unaffected by
   >the rep spec (LRM 13.3 paragraph 6).

   The only protable way is to use Unchecked_conversion.

Yech!  And not very portable.  Makes assumptions about the size
and representation of enumeration types.

   But, why does your program even need to get at the rep values?  If
   your program needs to work with anything other then the enumeration
   identifiers, then it very likely has a very badly broken
   abstraction.

No it just needs to communicate with a very broken language... (C)

I need to be able to communicate enumeration types over a network
link.  Transmitting them a string rep ('image) is expensive at both
ends of the link -- so the rational thing to do seemed to be set the
numeric representation (they are called ENUMERATION types, you know
:-) and send that!

   I have never seen an Ada program that required
   emuneration rep specs, that really required the internal
   representation to become visible in the program.

They don't need to become visible in the program but are needed in the
external data representation code...

   But, if you really really need it just declare an anonymous array to be
   used for conversions:

   internal_colors: array (Color) of integer := (4,8,16);

Ah ha!  A portable solution!  I knew you had it in you... :-)
        /s
--
Alexander Erskine Wise /\/\/\/\/\/\/\/\/\/\/\/\ Software Development Laboratory
/\/\/\/\/\/\/\/\/\/\/\/\/\/\ WISE@CS.UMASS.EDU /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\ This situation calls for large amounts of unadulterated CHOCOLATE! /\/\/\

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

* Re: Retrieving Rep Spec'ed Enumeration Values
@ 1993-06-29 17:44 Dave Bashford
  0 siblings, 0 replies; 6+ messages in thread
From: Dave Bashford @ 1993-06-29 17:44 UTC (permalink / raw)


In article ... wise@cs.umass.edu writes:
>In article ... mab@dst17.wdl.loral.com (Mark A Biggar) writes:
>   In article I write:
>   >Suppose you had a type like so:
>   >  type Color is ( Red, Green, Blue );
>   >  for Color use ( Red   => 4, Green => 8, Blue  => 16 );
>   >I cannot seem to find a portable way to retrieve the values assigned
>   >in the rep-spec.  Color'Pos is explicitly defined to be unaffected by
>   >the rep spec (LRM 13.3 paragraph 6).
>
>   [suggestion for using unchecked_conversion, ...]
>
>I need to be able to communicate enumeration types over a network
>link.  Transmitting them a string rep ('image) is expensive at both
>ends of the link -- so the rational thing to do seemed to be set the
>numeric representation (they are called ENUMERATION types, you know
>:-) and send that!
>
>   But, if you really really need it just declare an anonymous array to be
>   used for conversions:
>
>   internal_colors: array (Color) of integer := (4,8,16);
>
Am I missing something here ?  Given the following procedures and the
above declarations, and ignoring the storage size problem, both
procedure calls will send exactly the same thing (4), won't they ?

	procedure Send( Value: in Color );
	procedure Send( Value: in integer );
	...
	Send(Red);
	Send(internal_colors(Red));

The reason that there isn't an explicit attribute for retrieving the
value of an enumeration-type object is the same reason that there isn't
one for retrieving the value of an integer-type object - it isn't
necessary.
-- 

db
bashford@srs.loral.com (Dave Bashford, Sunnyvale, CA)

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

* Re: Retrieving Rep Spec'ed Enumeration Values
@ 1993-06-29 20:25 Gene Ouye
  0 siblings, 0 replies; 6+ messages in thread
From: Gene Ouye @ 1993-06-29 20:25 UTC (permalink / raw)


Sandy Wise (sandy@beeker.cs.umass.edu) wrote:
: In article <1993Jun28.205806.11601@wdl.loral.com> mab@dst17.wdl.loral.com (Ma
rk A Biggar) writes:
:    In article I write:
:    >Suppose you had a type like so:
:    >  type Color is ( Red, Green, Blue );
:    >  for Color use ( Red   => 4, Green => 8, Blue  => 16 );
:    >I cannot seem to find a portable way to retrieve the values assigned
:    >in the rep-spec.  Color'Pos is explicitly defined to be unaffected by
:    >the rep spec (LRM 13.3 paragraph 6).

:    The only protable way is to use Unchecked_conversion.

: Yech!  And not very portable.  Makes assumptions about the size
: and representation of enumeration types.

Dave's previous post is correct about this, you don't need some special
attribute to get the value assigned in the rep-clause.  Assuming that you
used a 'SIZE attribute to make the enumeration values take the same space
as an integer, if you looked at 

	My_Color   : Color   := Red;
	My_Integer : Integer := 4;

Both storage locations would appear identical in memory.  Of course the
fact that you are using the predefined type Integer makes your code not
portable (because Integer'Size can vary from one platform to another).

:...

: No it just needs to communicate with a very broken language... (C)

: I need to be able to communicate enumeration types over a network
: link.  Transmitting them a string rep ('image) is expensive at both
: ends of the link -- so the rational thing to do seemed to be set the
: numeric representation (they are called ENUMERATION types, you know
: :-) and send that!

If you are talking over a heterogeneoust network to C programs, will
all of your C programs view storage the same way?  I'm not a C person,
so I don't know if C "int"s (or whatever you're mapping your Ada Color
type to in C) are the same in all environments.  If they're not, you
have some work to do.  If they are the same, the use the 'SIZE attribute
(see LRM 13.7.2(5)).

:    I have never seen an Ada program that required
:    emuneration rep specs, that really required the internal
:    representation to become visible in the program.

I have, for exactly the following reason

: They don't need to become visible in the program but are needed in the
: external data representation code...

:    But, if you really really need it just declare an anonymous array to be
:    used for conversions:

:    internal_colors: array (Color) of integer := (4,8,16);

: Ah ha!  A portable solution!  I knew you had it in you... :-)

As mentioned above, this is not really portable.

Gene Ouye (geneo@rational.com)

(301) 897-4014

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

* Re: Retrieving Rep Spec'ed Enumeration Values
@ 1993-06-29 21:05 David Emery
  0 siblings, 0 replies; 6+ messages in thread
From: David Emery @ 1993-06-29 21:05 UTC (permalink / raw)


A much better way to handle this particular problem over the net is to
use the 'POS/'VAL attributes, which are portable for all
representations of the same enumeration type:

	shared code:
	
package DEFINES_ENUM_TYPE is

  type COLOR is (RED, GREEN, BLUE):

end DEFINES_ENUM_TYPE;   

package INTEGERS_OVER_NETWORK is

  procedure SEND (I : in INTEGER);
  procedure RECEIVE (I : out INTEGER);

end INTEGERS_OVER_NETWORK;
		
	server/sender code:

with INTEGERS_OVER_NETWORK;
with DEFINES_ENUM_TYPE;
procedure SEND_ENUM_VALUE (E : DEFINES_ENUM_TYPE.COLOR)
is
begin
  INTEGERS_OVER_NETWORK.SEND (DEFINES_ENUM_TYPE.COLOR'POS(E));
	-- send the (integer) position (first, second, third enum value);
end SEND_ENUM_VALUE;

	client/receiver code: 

with INTEGERS_OVER_NETWORK;
with DEFINES_ENUM_TYPE;
procedure RECEIVE_ENUM_VALUE (E : out DEFINES_ENUM_TYPE.COLOR)
is
  I : INTEGER;
begin
  INTEGERS_OVER_NETWORK.RECEIVE (I);
  if I in DEFINES_ENUM_TYPE.COLOR'POS (DEFINES_ENUM_TYPE.COLOR'FIRST))
	  .. 	DEFINES_ENUM_TYPE.COLOR'POS (DEFINES_ENUM_TYPE.COLOR'LAST))
  then
    E := DEFINES_ENUM_TYPE.COLOR'VAL (I);
  else
    -- appropriate action for out of range value, e.g. raise
    -- exception, return default value, etc.
  end if;
end RECEIVE_ENUM_VALUE;

We have used this technique for sending enumeration types across
Remote Procedure Calls.  The SEND and RECEIVE operations were
implemented using the ONC XDR xdr_int() operations (for which we have
a simple Ada binding.)

				dave

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

end of thread, other threads:[~1993-06-29 21:05 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1993-06-29 16:39 Retrieving Rep Spec'ed Enumeration Values agate!howland.reston.ans.net!noc.near.net!nic.umass.edu!ymir.cs.umass.edu
  -- strict thread matches above, loose matches on Subject: below --
1993-06-29 21:05 David Emery
1993-06-29 20:25 Gene Ouye
1993-06-29 17:44 Dave Bashford
1993-06-28 20:58 Mark A Biggar
1993-06-28 20:47 cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland.

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