comp.lang.ada
 help / color / mirror / Atom feed
* Constants instead of enum?
@ 2002-12-18 15:03 Wojtek Narczynski
  2002-12-18 16:36 ` John McCabe
  2002-12-18 17:52 ` Robert A Duff
  0 siblings, 2 replies; 3+ messages in thread
From: Wojtek Narczynski @ 2002-12-18 15:03 UTC (permalink / raw)


Hello,

I have the following situation: I read request record from network, an
Unsigned_8 (byte) denotes its type. Valid types are 1..10. Response
record has the same structure as request record. I also want to reuse
the memory allocated for request as the response. Now problem: for
every unimplemented request type (outside 1..10 range) I am supposed
to send a response record with type 11 back.

I am trying to use enumeration with representation clause for this,
but it just doesn't work. I read an Unsigned_8 from network, and all
its values are valid and should be handled gracefully, so I cannot use
this enum in the request structure. Because of this I have to convert
from Unsigned_8 to enumeration, but only sometimes. Then I have to use
unchecked conversion to get the enumeration's underlying Unsigned_8 to
store it in the response record (same structure and memory) as
request, so it has to be Unsigned_8.

If I defined types as constants for 1..11 range the amount of code
necessary and conversions will reduce.

So my question is - would this be fine to use a group of consts in
this specific case instead of an enum? I could encapsulate them into a
nested package.


Thanks,
Wojtek



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

* Re: Constants instead of enum?
  2002-12-18 15:03 Constants instead of enum? Wojtek Narczynski
@ 2002-12-18 16:36 ` John McCabe
  2002-12-18 17:52 ` Robert A Duff
  1 sibling, 0 replies; 3+ messages in thread
From: John McCabe @ 2002-12-18 16:36 UTC (permalink / raw)


On 18 Dec 2002 07:03:21 -0800, wojtek@power.com.pl (Wojtek Narczynski)
wrote:

>Hello,
>
>I have the following situation: I read request record from network, an
>Unsigned_8 (byte) denotes its type. Valid types are 1..10. Response
>record has the same structure as request record. I also want to reuse
>the memory allocated for request as the response. Now problem: for
>every unimplemented request type (outside 1..10 range) I am supposed
>to send a response record with type 11 back.
>
>I am trying to use enumeration with representation clause for this,
>but it just doesn't work. I read an Unsigned_8 from network, and all
>its values are valid and should be handled gracefully, so I cannot use
>this enum in the request structure. Because of this I have to convert
>from Unsigned_8 to enumeration, but only sometimes. Then I have to use
>unchecked conversion to get the enumeration's underlying Unsigned_8 to
>store it in the response record (same structure and memory) as
>request, so it has to be Unsigned_8.

How is the record defined? You may be able to read in any value from
the network and store it in the enumeration variable, then use the
'Valid attribute to determine whether or not the value is a valid one.


If you use 11 to denote invalid, then you have the option of either:

----------
type My_Enum_Type is (Msg_0,
                      Msg_1,
                      :
                      :
                      Msg_Invalid);
for My_Enum_Type use (Msg_0 => 0,
                      Msg_1 => 1,
                      :
                      :
                      Msg_Invalid => 11);
for My_Enum_Type'size use 8;

Enum_In_Variable : My_Enum_Type;

-- read value into Enum_In_Variable
if (Enum_In_Variable'Valid and then Enum_In_Variable /= Msg_Invalid) 
then
   blah blah
else
   Enum_Out_Variable = Msg_Invalid;
end if;
----------

or something like:

----------
type My_Enum_Type is (Msg_0,
                      Msg_1,
                      :
                      :
                      Msg_10);
for My_Enum_Type use (Msg_0 => 0,
                      Msg_1 => 1,
                      :
                      :
                      Msg_10 => 10);
for My_Enum_Type'size use 8;

Msg_Invalid : constant Integer := 11;
function To_My_Enum_Type is
   new Ada.Unchecked_Conversion (Integer, My_Enum_Type);
Enum_In_Variable : My_Enum_Type;

-- read value into Enum_In_Variable
if (Enum_In_Variable'Valid) then
   blah blah
else
   Enum_Out_Variable = To_My_Enum_Type (Msg_Invalid);
end if;
----------


Or something like that. I haven't used Ada for a while to be honest,
but that's probably how I'd do it. Someone else might shoot me down in
flames though for making such a suggestion!

>If I defined types as constants for 1..11 range the amount of code
>necessary and conversions will reduce.

>So my question is - would this be fine to use a group of consts in
>this specific case instead of an enum? I could encapsulate them into a
>nested package.

Well you could do that I suppose.

Best Regards
John McCabe

To reply by email replace 'nospam' with 'assen'



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

* Re: Constants instead of enum?
  2002-12-18 15:03 Constants instead of enum? Wojtek Narczynski
  2002-12-18 16:36 ` John McCabe
@ 2002-12-18 17:52 ` Robert A Duff
  1 sibling, 0 replies; 3+ messages in thread
From: Robert A Duff @ 2002-12-18 17:52 UTC (permalink / raw)


wojtek@power.com.pl (Wojtek Narczynski) writes:

> Hello,
> 
> I have the following situation: I read request record from network, an
> Unsigned_8 (byte) denotes its type. Valid types are 1..10. Response
> record has the same structure as request record. I also want to reuse
> the memory allocated for request as the response. Now problem: for
> every unimplemented request type (outside 1..10 range) I am supposed
> to send a response record with type 11 back.
> 
> I am trying to use enumeration with representation clause for this,
> but it just doesn't work.

Correct -- it doesn't work.

>... I read an Unsigned_8 from network, and all
> its values are valid and should be handled gracefully, so I cannot use
> this enum in the request structure. Because of this I have to convert
> from Unsigned_8 to enumeration, but only sometimes. Then I have to use
> unchecked conversion to get the enumeration's underlying Unsigned_8 to
> store it in the response record (same structure and memory) as
> request, so it has to be Unsigned_8.
> 
> If I defined types as constants for 1..11 range the amount of code
> necessary and conversions will reduce.
> 
> So my question is - would this be fine to use a group of consts in
> this specific case instead of an enum? I could encapsulate them into a
> nested package.

This would be fine.  In fact, it is superior to the enum-with-rep-clause
solution, for the reasons you stated.

In fact, I would go so far as to say enum rep clauses are rarely useful,
and should not be in the language.  Use enums when you don't care about
the representation.  If you care about representation, use types that
exactly match the hardware or whatever you're interfacing to, and use
constants (perhaps deferred constants) to define enum-like values.

- Bob



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

end of thread, other threads:[~2002-12-18 17:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-18 15:03 Constants instead of enum? Wojtek Narczynski
2002-12-18 16:36 ` John McCabe
2002-12-18 17:52 ` Robert A Duff

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