By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,294 Members | 2,687 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,294 IT Pros & Developers. It's quick & easy.

Check if value is defined in enum

P: n/a
Dear Group,

I'd like to check if a value is defined in an enum.
Example:

------------------------------------------------------

typedef enum
{
A_VALUE = 1,
SOME_OTHER_VALUE = 8,
ANOTHER_VALUE = 21,
/* a few hundred more values... */
YET_ANOTHER_VALUE = 45770
} VALUES;

VALUES test;

test = 1; /* OK, because 1 is defined in VALUES (as SOME_VALUE) */
test = 2; /* NOT OK, because 2 is not defined in VALUES */

------------------------------------------------------

My compiler checks if an assigned value is defined in an enum at
compile-time, but I'd like to check it at run-time.
It might be possible using a switch, and check each (maybe a hundred) value
in the enum, but this is difficult to maintain, especially if the enum
changes, I'd have to update each validate switch.

I'd like to know if there's a clever Macro or programming method which
validates a value in an enum?

Thanks in advance,
Stanley

Nov 14 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
DJTB wrote:
Dear Group,

I'd like to check if a value is defined in an enum.
Example:

------------------------------------------------------

typedef enum
{
A_VALUE = 1,
SOME_OTHER_VALUE = 8,
ANOTHER_VALUE = 21,
/* a few hundred more values... */
YET_ANOTHER_VALUE = 45770
} VALUES;

VALUES test;

test = 1; /* OK, because 1 is defined in VALUES (as SOME_VALUE) */
test = 2; /* NOT OK, because 2 is not defined in VALUES */

------------------------------------------------------

My compiler checks if an assigned value is defined in an enum at
compile-time, but I'd like to check it at run-time.
It might be possible using a switch, and check each (maybe a hundred) value
in the enum, but this is difficult to maintain, especially if the enum
changes, I'd have to update each validate switch.

I'd like to know if there's a clever Macro or programming method which
validates a value in an enum?

Thanks in advance,
Stanley


There are no facilities in the language to detect the
the number of enumerations or the values of each one.

There are no macro tricks either.

Every idea I came up with, I found a way to break that
idea.

Perhaps a better idea is to encapsulate the enum into a function
or structure. That way you could control things such as
quantity and incrementing values.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #2

P: n/a
On Fri, 27 Feb 2004 15:18:13 +0100, DJTB <us****@terabytemusic.cjb.net>
wrote:
Dear Group,

I'd like to check if a value is defined in an enum.
Example:

------------------------------------------------------

typedef enum
{
A_VALUE = 1,
SOME_OTHER_VALUE = 8,
ANOTHER_VALUE = 21,
/* a few hundred more values... */
YET_ANOTHER_VALUE = 45770
} VALUES;

VALUES test;

test = 1; /* OK, because 1 is defined in VALUES (as SOME_VALUE) */
test = 2; /* NOT OK, because 2 is not defined in VALUES */

------------------------------------------------------

My compiler checks if an assigned value is defined in an enum at
compile-time, but I'd like to check it at run-time.
It might be possible using a switch, and check each (maybe a hundred) value
in the enum, but this is difficult to maintain, especially if the enum
changes, I'd have to update each validate switch.

I'd like to know if there's a clever Macro or programming method which
validates a value in an enum?

Thanks in advance,
Stanley


As Thomas says, there's no built-in facility for that. If the largest value
is bounded, you might consider a bit-map of some type (array of char for
simplicity, actual bit-twiddling for space-efficiency...hey, an actual use
for vector<bool>! Oh darn, this is C...) where you can check a value in
constant time before using it. If the range of values it too great to make
that feasible, you may just be stuck with approaches involving b-searching,
hashing, etc., to validate values.
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #3

P: n/a
DJTB wrote:
Dear Group,

I'd like to check if a value is defined in an enum.
Example:

------------------------------------------------------

typedef enum
{
A_VALUE = 1,
SOME_OTHER_VALUE = 8,
ANOTHER_VALUE = 21,
/* a few hundred more values... */
YET_ANOTHER_VALUE = 45770
} VALUES;

VALUES test;

test = 1; /* OK, because 1 is defined in VALUES (as SOME_VALUE) */
test = 2; /* NOT OK, because 2 is not defined in VALUES */


It is far better to avoid this problem by not mixing enumerated values with
integer values. Use either one or the other set. Using enumerated values helps
make your code more readable; otherwise, why did you define them in the first
place? BTW, there is nothing wrong with

int test = ANOTHER_VALUE;

as long as you don't overflow test.

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Nov 14 '05 #4

P: n/a
DJTB wrote:
Dear Group,

I'd like to check if a value is defined in an enum.
Example:

------------------------------------------------------

typedef enum
{
A_VALUE = 1,
SOME_OTHER_VALUE = 8,
ANOTHER_VALUE = 21,
/* a few hundred more values... */
YET_ANOTHER_VALUE = 45770
} VALUES;

VALUES test;

test = 1; /* OK, because 1 is defined in VALUES (as SOME_VALUE) */
test = 2; /* NOT OK, because 2 is not defined in VALUES */

------------------------------------------------------

My compiler checks if an assigned value is defined in an enum at
compile-time, but I'd like to check it at run-time.
It might be possible using a switch, and check each (maybe a hundred) value
in the enum, but this is difficult to maintain, especially if the enum
changes, I'd have to update each validate switch.


This is probably the best way. If you're using gcc and you turn up your
warnings, it will even warn you about unhandled enum values in the switch.

[...]

--
Pull out a splinter to reply.
Nov 14 '05 #5

P: n/a
David Rubin wrote:

test = 1; /* OK, because 1 is defined in VALUES (as SOME_VALUE) */
test = 2; /* NOT OK, because 2 is not defined in VALUES */


It is far better to avoid this problem by not mixing enumerated values
with integer values. [..] here is nothing wrong with

int test = ANOTHER_VALUE;


Hi David,
It was just an example to describe the problem. I don't actually assign an
integer value in my code.

The value of the 'test' variable will be read from a socket, I've to
validate the value on the socket (== check if value is defined in enum)
before assigning it to a variable of type VALUES.

Stanley
Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.