1 /*
2  * virenum.h: enum value conversion helpers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library.  If not, see
16  * <http://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 #include "internal.h"
22 
23 int
24 virEnumFromString(const char * const *types,
25                   unsigned int ntypes,
26                   const char *type);
27 
28 const char *
29 virEnumToString(const char * const *types,
30                 unsigned int ntypes,
31                 int type);
32 
33 #define VIR_ENUM_IMPL(name, lastVal, ...) \
34     static const char *const name ## TypeList[] = { __VA_ARGS__ }; \
35     const char *name ## TypeToString(int type) { \
36         return virEnumToString(name ## TypeList, \
37                                G_N_ELEMENTS(name ## TypeList), \
38                                type); \
39     } \
40     int name ## TypeFromString(const char *type) { \
41         return virEnumFromString(name ## TypeList, \
42                                  G_N_ELEMENTS(name ## TypeList), \
43                                  type); \
44     } \
45     G_STATIC_ASSERT(G_N_ELEMENTS(name ## TypeList) == lastVal)
46 
47 #define VIR_ENUM_DECL(name) \
48     const char *name ## TypeToString(int type); \
49     int name ## TypeFromString(const char*type)
50 
51 typedef enum {
52     VIR_TRISTATE_BOOL_ABSENT = 0,
53     VIR_TRISTATE_BOOL_YES,
54     VIR_TRISTATE_BOOL_NO,
55 
56     VIR_TRISTATE_BOOL_LAST
57 } virTristateBool;
58 
59 typedef enum {
60     VIR_TRISTATE_SWITCH_ABSENT = 0,
61     VIR_TRISTATE_SWITCH_ON,
62     VIR_TRISTATE_SWITCH_OFF,
63 
64     VIR_TRISTATE_SWITCH_LAST
65 } virTristateSwitch;
66 
67 VIR_ENUM_DECL(virTristateBool);
68 VIR_ENUM_DECL(virTristateSwitch);
69 
70 virTristateBool virTristateBoolFromBool(bool val);
71 virTristateSwitch virTristateSwitchFromBool(bool val);
72 
73 /* the two enums must be in sync to be able to use helpers interchangeably in
74  * some special cases */
75 G_STATIC_ASSERT((int)VIR_TRISTATE_BOOL_YES == (int)VIR_TRISTATE_SWITCH_ON);
76 G_STATIC_ASSERT((int)VIR_TRISTATE_BOOL_NO == (int)VIR_TRISTATE_SWITCH_OFF);
77 G_STATIC_ASSERT((int)VIR_TRISTATE_BOOL_ABSENT == (int)VIR_TRISTATE_SWITCH_ABSENT);
78