Andreas Schwarz
Andreas Schwarz

Reputation: 1798

What is the best way to know if a value is defined in an enum?

I read that it is difficult to find out if an element is in an enumeration. So what would be the best way ?

For example, the following code comes from the Linux kernel 2.6.32:

enum v4l2_colorfx {
    V4L2_COLORFX_NONE    = 0,
    V4L2_COLORFX_BW      = 1,
    V4L2_COLORFX_SEPIA   = 2,
};

And this one from the 2.6.38 version:

enum v4l2_colorfx {
    V4L2_COLORFX_NONE        = 0,
    V4L2_COLORFX_BW          = 1,
    V4L2_COLORFX_SEPIA       = 2,
    V4L2_COLORFX_NEGATIVE    = 3,
    V4L2_COLORFX_EMBOSS      = 4,
    V4L2_COLORFX_SKETCH      = 5,
    V4L2_COLORFX_SKY_BLUE    = 6,
    V4L2_COLORFX_GRASS_GREEN = 7,
    V4L2_COLORFX_SKIN_WHITEN = 8,
    V4L2_COLORFX_VIVID       = 9,
};

How would you check if V4L2_COLORFX_NEGATIVE is defined ? Would #ifndef V4L2_COLORFX_NEGATIVE be okay ?

Upvotes: 1

Views: 1321

Answers (2)

crazyjul
crazyjul

Reputation: 2539

Check the version of linux in /usr/include/linux/version.h ( you need to install kernel headers though )

it contains something like :

#define LINUX_VERSION_CODE 132640
#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))

So you can use this :

#if LINUX_VERSION_CODE >= KERNEL_VERSION( 2, 6, 38 )

Upvotes: 1

Joe
Joe

Reputation: 47609

You would have to look at a compiler macro in the wider context (for example the version of linux, I don't know what's available) or some other piece of information at compile time. ifndef is for checking if compiler macros are defined, not symbols in code.

Upvotes: 3

Related Questions