make IMC/IMV pairs independent of libcharon
[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
17 /**
18 * @defgroup enum enum
19 * @{ @ingroup libstrongswan
20 */
21
22 #ifndef ENUM_H_
23 #define ENUM_H_
24
25 #include "printf_hook.h"
26
27 typedef struct enum_name_t enum_name_t;
28
29 /**
30 * Struct to store names for enums.
31 *
32 * To print the string representation of enumeration values, the strings
33 * are stored in these structures. Every enum_name contains a range
34 * of strings, multiple ranges are linked together.
35 * Use the convenience macros to define these linked ranges.
36 *
37 * For a single range, use:
38 * @code
39 ENUM(name, first, last, string1, string2, ...)
40 @endcode
41 * For multiple linked ranges, use:
42 * @code
43 ENUM_BEGIN(name, first, last, string1, string2, ...)
44 ENUM_NEXT(name, first, last, last_from_previous, string3, ...)
45 ENUM_NEXT(name, first, last, last_from_previous, string4, ...)
46 ENUM_END(name, last_from_previous)
47 @endcode
48 * The ENUM and the ENUM_END define a enum_name_t pointer with the name supplied
49 * in "name".
50 *
51 * Resolving of enum names is done using a printf hook. A printf fromat
52 * character %N is replaced by the enum string. Printf needs two arguments to
53 * resolve a %N, the enum_name_t* (the defined name in ENUM_BEGIN) followed
54 * by the numerical enum value.
55 */
56 struct enum_name_t {
57 /** value of the first enum string */
58 int first;
59 /** value of the last enum string */
60 int last;
61 /** next enum_name_t in list */
62 enum_name_t *next;
63 /** array of strings containing names from first to last */
64 char *names[];
65 };
66
67 /**
68 * Begin a new enum_name list.
69 *
70 * @param name name of the enum_name list
71 * @param first enum value of the first enum string
72 * @param last enum value of the last enum string
73 * @param ... a list of strings
74 */
75 #define ENUM_BEGIN(name, first, last, ...) static enum_name_t name##last = {first, last, NULL, { __VA_ARGS__ }}
76
77 /**
78 * Continue a enum name list startetd with ENUM_BEGIN.
79 *
80 * @param name name of the enum_name list
81 * @param first enum value of the first enum string
82 * @param last enum value of the last enum string
83 * @param prev enum value of the "last" defined in ENUM_BEGIN/previous ENUM_NEXT
84 * @param ... a list of strings
85 */
86 #define ENUM_NEXT(name, first, last, prev, ...) static enum_name_t name##last = {first, last, &name##prev, { __VA_ARGS__ }}
87
88 /**
89 * Complete enum name list started with ENUM_BEGIN.
90 *
91 * @param name name of the enum_name list
92 * @param prev enum value of the "last" defined in ENUM_BEGIN/previous ENUM_NEXT
93 */
94 #define ENUM_END(name, prev) enum_name_t *name = &name##prev;
95
96 /**
97 * Define a enum name with only one range.
98 *
99 * This is a convenience macro to use when a enum_name list contains only
100 * one range, and is equal as defining ENUM_BEGIN followed by ENUM_END.
101 *
102 * @param name name of the enum_name list
103 * @param first enum value of the first enum string
104 * @param last enum value of the last enum string
105 * @param ... a list of strings
106 */
107 #define ENUM(name, first, last, ...) ENUM_BEGIN(name, first, last, __VA_ARGS__); ENUM_END(name, last)
108
109 /**
110 * Convert a enum value to its string representation.
111 *
112 * @param e enum names for this enum value
113 * @param val enum value to get string for
114 * @return string for enum, NULL if not found
115 */
116 char *enum_to_name(enum_name_t *e, int val);
117
118 /**
119 * Convert a enum string back to its enum value.
120 *
121 * @param e enum names for this enum value
122 * @param name name to get enum value for
123 * @return enum value, -1 if not found
124 */
125 int enum_from_name(enum_name_t *e, char *name);
126
127 /**
128 * printf hook function for enum_names_t.
129 *
130 * Arguments are:
131 * enum_names_t *names, int value
132 */
133 int enum_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
134 const void *const *args);
135
136 #endif /** ENUM_H_ @}*/