P99

◆ P99_DECLARE_ENUM

#define P99_DECLARE_ENUM (   T,
  ... 
)
Value:
\
enum T { __VA_ARGS__ , \ \
P99_PASTE2(T, _amount), \ \
P99_PASTE2(T, _max) = ((size_t)(P99_PASTE2(T, _amount)) - 1u), \ \
P99_PASTE2(T, _min) = 0 \
}; \ \ \
typedef enum T T; \
P99_DECLARE_ENUM_GETNAME(T, __VA_ARGS__); \
P99_DECLARE_ENUM_PARSE(T, __VA_ARGS__)

Declare a simple enumeration type.

This macro only works for enumeration types that have no assignments to the constants. To define an enumeration type color use the macro in something like

P99_DECLARE_ENUM(color, red, green, blue);

As additional benefits you obtain:

  • constants color_min and color_max that in the example here would correspond to red and blue, respectively
  • constant color_amount that in the example here would correspond to 3
  • a function color_getname that returns a string containing the name of its argument.
  • a function color_parse that returns the color with the longest prefix in a string.

To have this work, you have to put a line

in one of your object files.

Unfortunately you can't use doxygen like comments inside the argument list to document the enumeration constants. You have to document the constants separately. For the example above you would do

/// the most reddish color of all colors

Observe the bizarre naming convention, here. Although in C (and C++) ‘red’ is declared in the same scope as ‘enum color’ you have to prefix it with ‘color’ such that the documentation ends up inside the one for ‘color’.

Remarks
argument 0 must be an identifier

Definition at line 139 of file p99_enum.h.

P99_PASTE2
#define P99_PASTE2(_1, _2)
Paste two token sequences at their junction.
Definition: p99_paste.h:82
P99_DEFINE_ENUM
#define P99_DEFINE_ENUM(T)
Define the necessary symbols for a simple enumeration type.
Definition: p99_enum.h:178
P99_DECLARE_ENUM
#define P99_DECLARE_ENUM(T,...)
Declare a simple enumeration type.
Definition: p99_enum.h:139