Debian Codegraph @

texk/gregorio/gregorio-src/src/enum_generator.h

raw
1 /*
2 * Copyright (C) 2006-2025 The Gregorio Project (see CONTRIBUTORS.md)
3 *
4 * This file is part of Gregorio.
5 *
6 * Gregorio is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Gregorio is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Gregorio. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /* These macros are used for generating enum code. This is based on the
21 * technique described in http://stackoverflow.com/a/202511
22 *
23 * Two of these macros are meant for use outside this set of macros:
24 *
25 * - ENUM declares the enum itself and a prototype for the function that
26 * returns the string value of the enum's numeric value. This is meant to be
27 * used from a header file.
28 * - ENUM_TO_STRING defines the function whose prototype was declared by ENUM.
29 * This is meant to be used from a code file.
30 *
31 * These two macros both take the following two arguments:
32 *
33 * - TYPE is the name to use for the enum.
34 * - DEF is the name of a macro that generates the bodies of the enum and the
35 * to-string function. DEF should take four arguments, in the following
36 * order (with suggested names):
37 * - A is a macro that generates an enum value with a specifiec numeric
38 * value. It takes two arguments, the name and the numeric value.
39 * - E is a macro that generates an enum value that has no specific numeric
40 * value. It takes one argument, the name.
41 * - X is a macro that generates the last enum value when it has a specific
42 * numeric value. It takes the same two arguments as A.
43 * - L is a macro that generates the last enum value when it has no specific
44 * numeric value. It takes the same argument as E.
45 *
46 * Note: If we were supporting C11, we could take empty macro arguments and
47 * would be able to get away with only two arguments to DEF.
48 */
49
50 #ifndef ENUM_GENERATOR_H
51 #define ENUM_GENERATOR_H
52
53 /* for enum values */
54 #define ENUM_VALUE(NAME,VALUE) NAME = VALUE,
55 #define ENUM_ENTRY(NAME) NAME,
56 #define ENUM_LAST_VALUE(NAME,VALUE) NAME = VALUE
57 #define ENUM_LAST_ENTRY(NAME) NAME
58
59 /* for enum case */
60 #define ENUM_VALUE_CASE(NAME,VALUE) case NAME: return #NAME;
61 #define ENUM_ENTRY_CASE(NAME) case NAME: return #NAME;
62
63 /* enum declaration */
64 #define ENUM(TYPE,DEF) \
65 typedef enum TYPE { \
66 DEF(ENUM_VALUE, ENUM_ENTRY, ENUM_LAST_VALUE, ENUM_LAST_ENTRY) \
67 } TYPE; \
68 const char *TYPE##_to_string(TYPE value)
69
70 /* enum *_to_string function defintiion */
71 #define ENUM_TO_STRING(TYPE,DEF) \
72 const char *TYPE##_to_string(TYPE value) \
73 { \
74 switch(value) { \
75 DEF(ENUM_VALUE_CASE, ENUM_ENTRY_CASE, ENUM_VALUE_CASE, ENUM_ENTRY_CASE) \
76 default: return gregorio_unknown(value); \
77 } \
78 }
79
80 #endif