31 #if defined _MSC_VER // Microsoft compiler ... 32 # define RAW_CPP_TYPENAME(t) t.raw_name() 33 #else // i.e. especially GCC and clang ... 34 # define RAW_CPP_TYPENAME(t) t.name() 37 #define RAW_CPP_TYPENAME_OF(type) RAW_CPP_TYPENAME(typeid(type)) 39 #define GIG_DECLARE_ENUM(type, ...) \ 40 enum type { __VA_ARGS__ }; \ 42 struct type##InfoRegistrator { \ 43 type##InfoRegistrator() { \ 44 const char* typeName = RAW_CPP_TYPENAME_OF(type); \ 45 g_enumsByRawTypeName[typeName] = _parseEnumBody( #__VA_ARGS__ ); \ 49 static type##InfoRegistrator g_##type##InfoRegistrator 51 struct EnumDeclaration {
52 std::map<size_t, std::string> nameByValue;
53 std::map<std::string, size_t> valueByName;
56 EnumDeclaration() : allKeys(NULL) {}
58 const size_t countKeys()
const {
return valueByName.size(); }
61 const size_t n = valueByName.size();
62 allKeys =
new char*[n + 1];
64 for (std::map<std::string, size_t>::const_iterator it = valueByName.begin();
65 it != valueByName.end(); ++it, ++i)
67 allKeys[i] = strdup(it->first.c_str());
76 bool isValid()
const {
return !name.empty(); }
79 static std::map<std::string, EnumDeclaration> g_enumsByRawTypeName;
80 static std::map<std::string, size_t> g_allEnumValuesByKey;
87 static inline bool isWhiteSpace(
const char& c) {
88 return c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\r';
91 static size_t decodeEnumValue(
const std::string& encodedValue) {
92 return strtoll(encodedValue.c_str(), NULL, 0 );
95 static EnumKeyVal _parseEnumKeyVal(
const char* start,
const char* end,
size_t value) {
101 for (; isWhiteSpace(*start); ++start)
108 for (
const char* p = start;
true; ++p) {
109 if (p >= end || isWhiteSpace(*p) || *p ==
'=') {
110 const size_t sz = p - start;
111 keyval.name.resize(sz);
112 memcpy(&keyval.name[0], start, sz);
113 keyval.value = value;
122 for (; isWhiteSpace(*start) || *start ==
'='; ++start)
128 std::string encodedValue;
130 for (
const char* p = start;
true; ++p) {
131 if (p >= end || isWhiteSpace(*p)) {
132 const size_t sz = p - start;
133 encodedValue.resize(sz);
134 memcpy(&encodedValue[0], start, sz);
139 if (encodedValue.empty())
142 keyval.value = decodeEnumValue(encodedValue);
147 static EnumDeclaration _parseEnumBody(
const char* body) {
148 EnumDeclaration decl;
150 for (
const char* a = body, *b = body;
true; ++b) {
151 if (*b == 0 || *b ==
',') {
152 const EnumKeyVal keyval = _parseEnumKeyVal(a, b, value);
153 if (!keyval.isValid())
break;
154 decl.nameByValue[keyval.value] = keyval.name;
155 decl.valueByName[keyval.name] = keyval.value;
156 g_allEnumValuesByKey[keyval.name] = keyval.value;
157 value = keyval.value + 1;
174 static leverage_ctrl_t::type_tInfoRegistrator g_leverageCtrlTTypeT;
191 if (!g_enumsByRawTypeName.count(typeName))
193 return g_enumsByRawTypeName[typeName].countKeys();
207 return enumCount(RAW_CPP_TYPENAME(type));
220 if (!g_allEnumValuesByKey.count(key))
222 return g_allEnumValuesByKey[key];
240 if (!g_enumsByRawTypeName.count(typeName))
242 return g_enumsByRawTypeName[typeName].valueByName.count(key);
257 bool enumKey(
const std::type_info& type, String key) {
258 return enumKey(RAW_CPP_TYPENAME(type), key);
280 const char*
enumKey(String typeName,
size_t value) {
281 if (!g_enumsByRawTypeName.count(typeName))
283 if (!g_enumsByRawTypeName[typeName].nameByValue.count(value))
285 return g_enumsByRawTypeName[typeName].nameByValue[value].c_str();
305 const char*
enumKey(
const std::type_info& type,
size_t value) {
306 return enumKey(RAW_CPP_TYPENAME(type), value);
324 if (!g_enumsByRawTypeName.count(typeName))
326 if (!g_enumsByRawTypeName[typeName].allKeys)
327 g_enumsByRawTypeName[typeName].loadAllKeys();
328 return (
const char**) g_enumsByRawTypeName[typeName].allKeys;
342 const char**
enumKeys(
const std::type_info& type) {
343 return enumKeys(RAW_CPP_TYPENAME(type));
const char * enumKey(String typeName, size_t value)
Enum constant name of numeric value.
size_t enumCount(String typeName)
Amount of elements in given enum type.
size_t enumValue(String key)
Numeric value of enum constant.
Gigasampler/GigaStudio specific classes and definitions.
const char ** enumKeys(String typeName)
All element names of enum type.