d585a1b0c2d4429d881a9fcc3768b8a99b20d3a1
[strongswan.git] / src / libstrongswan / enum.h
1 /*
2 * Copyright (C) 2009 Tobias Brunner
3 * Copyright (C) 2006-2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * $Id$
17 */
18
19 /**
20 * @defgroup enum enum
21 * @{ @ingroup libstrongswan
22 */
23
24 #ifndef ENUM_H_
25 #define ENUM_H_
26
27 #include <printf_hook.h>
28
29 typedef struct enum_name_t enum_name_t;
30
31 /**
32 * Struct to store names for enums.
33 *
34 * To print the string representation of enumeration values, the strings
35 * are stored in these structures. Every enum_name contains a range
36 * of strings, multiple ranges are linked together.
37 * Use the convenience macros to define these linked ranges.
38 *
39 * For a single range, use:
40 * @code
41 ENUM(name, first, last, string1, string2, ...)
42 @endcode
43 * For multiple linked ranges, use:
44 * @code
45 ENUM_BEGIN(name, first, last, string1, string2, ...)
46 ENUM_NEXT(name, first, last, last_from_previous, string3, ...)
47 ENUM_NEXT(name, first, last, last_from_previous, string4, ...)
48 ENUM_END(name, last_from_previous)
49 @endcode
50 * The ENUM and the ENUM_END define a enum_name_t pointer with the name supplied
51 * in "name".
52 *
53 * Resolving of enum names is done using a printf hook. A printf fromat
54 * character %N is replaced by the enum string. Printf needs two arguments to
55 * resolve a %N, the enum_name_t* (the defined name in ENUM_BEGIN) followed
56 * by the numerical enum value.
57 */
58 struct enum_name_t {
59 /** value of the first enum string */
60 int first;
61 /** value of the last enum string */
62 int last;
63 /** next enum_name_t in list */
64 enum_name_t *next;
65 /** array of strings containing names from first to last */
66 char *names[];
67 };
68
69 /**
70 * Begin a new enum_name list.
71 *
72 * @param name name of the enum_name list
73 * @param first enum value of the first enum string
74 * @param last enum value of the last enum string
75 * @param ... a list of strings
76 */
77 #define ENUM_BEGIN(name, first, last, ...) static enum_name_t name##last = {first, last, NULL, { __VA_ARGS__ }}
78
79 /**
80 * Continue a enum name list startetd with ENUM_BEGIN.
81 *
82 * @param name name of the enum_name list
83 * @param first enum value of the first enum string
84 * @param last enum value of the last enum string
85 * @param prev enum value of the "last" defined in ENUM_BEGIN/previous ENUM_NEXT
86 * @param ... a list of strings
87 */
88 #define ENUM_NEXT(name, first, last, prev, ...) static enum_name_t name##last = {first, last, &name##prev, { __VA_ARGS__ }}
89
90 /**
91 * Complete enum name list started with ENUM_BEGIN.
92 *
93 * @param name name of the enum_name list
94 * @param prev enum value of the "last" defined in ENUM_BEGIN/previous ENUM_NEXT
95 */
96 #define ENUM_END(name, prev) enum_name_t *name = &name##prev;
97
98 /**
99 * Define a enum name with only one range.
100 *
101 * This is a convenience macro to use when a enum_name list contains only
102 * one range, and is equal as defining ENUM_BEGIN followed by ENUM_END.
103 *
104 * @param name name of the enum_name list
105 * @param first enum value of the first enum string
106 * @param last enum value of the last enum string
107 * @param ... a list of strings
108 */
109 #define ENUM(name, first, last, ...) ENUM_BEGIN(name, first, last, __VA_ARGS__); ENUM_END(name, last)
110
111 /**
112 * printf hook function for enum_names_t.
113 *
114 * Arguments are:
115 * enum_names_t *names, int value
116 */
117 int enum_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
118 const void *const *args);
119
120 #endif /* ENUM_H_ @}*/