-
Notifications
You must be signed in to change notification settings - Fork 1
/
enum.inc
135 lines (126 loc) · 3.69 KB
/
enum.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//============================================================================
// Mini Spin-X Library
//
// Copyright (c) 2024, Jarkko Lempiainen
// All rights reserved.
//============================================================================
//============================================================================
// enumerated value function definitions
//============================================================================
// define the following macros before including this file:
// #define PFC_ENUM_NAMESPACE my_namespace
// #define PFC_ENUM_TYPE e_my_enum_type
// #define PFC_ENUM_PREFIX mytypeprefix_
// #define PFC_ENUM_VALS PFC_ENUM_VAL(val1)\ // = mytypeprefix_val1
// PFC_ENUM_VAL(val2)\ // = mytypeprefix_val2
// PFC_ENUM_VAL_N(val3, "Value 3")
#ifndef PFC_ENUM_PREFIX
#define PFC_ENUM_PREFIX
#endif
#ifdef PFC_ENUM_NAMESPACE
namespace PFC_ENUM_NAMESPACE {
#endif
//----
#define PFC_ENUM_VAL(eval__) PFC_STR(eval__),
#define PFC_ENUM_VAL_N(eval__, name__) PFC_STR(eval__),
const char *const*enum_strings(PFC_ENUM_TYPE)
{
static const char *s_strings[]=
{
"<unknown>",
PFC_ENUM_VALS
0,
};
return s_strings;
}
#undef PFC_ENUM_VAL
#undef PFC_ENUM_VAL_N
//----
#define PFC_ENUM_VAL(eval__) PFC_STR(eval__),
#define PFC_ENUM_VAL_N(eval__, name__) name__,
const char *const*enum_display_strings(PFC_ENUM_TYPE)
{
static const char *s_strings[]=
{
"<unknown>",
PFC_ENUM_VALS
0,
};
return s_strings;
}
#undef PFC_ENUM_VAL
#undef PFC_ENUM_VAL_N
//----
bool enum_dep_value(PFC_ENUM_TYPE &v_, const char *enum_str_)
{
#ifdef PFC_ENUM_DEP_VALS
#define PFC_ENUM_DEP_VAL(dep_eval__, new_eval__) PFC_STR(dep_eval__),
static const char *s_dep_strings[]=
{
PFC_ENUM_DEP_VALS
0
};
#undef PFC_ENUM_DEP_VAL
#define PFC_ENUM_DEP_VAL(dep_eval__, new_eval__) PFC_CAT2(PFC_ENUM_PREFIX, new_eval__),
static PFC_ENUM_TYPE s_dep_vals[]=
{
PFC_ENUM_DEP_VALS
};
#undef PFC_ENUM_DEP_VAL
extern int str_find_substr(const char*, const char*const*);
int vidx=str_find_substr(enum_str_, s_dep_strings);
if(vidx>=0)
{
v_=s_dep_vals[vidx];
return true;
}
#endif
return false;
}
//----
#define PFC_ENUM_VAL(eval__) PFC_CAT2(PFC_ENUM_PREFIX, eval__),
#define PFC_ENUM_VAL_N(eval__, name__) PFC_CAT2(PFC_ENUM_PREFIX, eval__),
const PFC_ENUM_TYPE *enum_values(PFC_ENUM_TYPE)
{
static const PFC_ENUM_TYPE s_vals[]=
{
PFC_ENUM_TYPE(0),
PFC_ENUM_VALS
PFC_ENUM_TYPE(0)
};
return s_vals;
}
#undef PFC_ENUM_VAL
#undef PFC_ENUM_VAL_N
//----
unsigned enum_string_index(PFC_ENUM_TYPE eval_)
{
// setup static enumeration value indices
static unsigned s_counter=0;
#define PFC_ENUM_VAL(eval__) static const unsigned s_index_##eval__=++s_counter;
#define PFC_ENUM_VAL_N(eval__, name__) static const unsigned s_index_##eval__=++s_counter;
PFC_ENUM_VALS
#undef PFC_ENUM_VAL
#undef PFC_ENUM_VAL_N
// return enumeration value index
#define PFC_ENUM_VAL(eval__) case PFC_CAT2(PFC_ENUM_PREFIX, eval__): return s_index_##eval__;
#define PFC_ENUM_VAL_N(eval__, name__) case PFC_CAT2(PFC_ENUM_PREFIX, eval__): return s_index_##eval__;
switch(unsigned(eval_))
{
PFC_ENUM_VALS
}
#undef PFC_ENUM_VAL
#undef PFC_ENUM_VAL_N
return 0;
}
#undef PFC_ENUM_VAL_N
//----------------------------------------------------------------------------
#ifdef PFC_ENUM_NAMESPACE
}
#endif
#undef PFC_ENUM_NAMESPACE
#undef PFC_ENUM_TYPE
#undef PFC_ENUM_PREFIX
#undef PFC_ENUM_VALS
#undef PFC_ENUM_DEP_VALS
//----------------------------------------------------------------------------