Check if value is defined in enum

D

DJTB

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
 
T

Thomas Matthews

DJTB said:
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
 
L

Leor Zolman

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
(e-mail address removed)
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
 
D

David Rubin

DJTB said:
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
 
P

Peter Ammon

DJTB said:
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.

[...]
 
D

DJTB

David said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top