-
Notifications
You must be signed in to change notification settings - Fork 0
/
enumMemberNamePair.h
172 lines (150 loc) · 5.83 KB
/
enumMemberNamePair.h
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Copyright (C) fastman92 <[email protected]>, website: http://fastman92.com
* Licensed under the MIT License, see LICENSE at top level directory.
*
*/
#pragma once
#include <algorithm>
#include <string>
#define PUT_ENUM_MEMBER_FROM_LIST(member, ...) member,
#define PUT_ENUM_MEMBER_NAME_FOR_PAIR(member, ...) { member, #member},
#define DECLARE_LIST_WITH_ENUM_MEMBER_AND_NAME(arrayName, eType, listMacro) \
static const enumMemberNamePair<eType> arrayName[] = \
{ \
listMacro(PUT_ENUM_MEMBER_NAME_FOR_PAIR) \
};
template <typename enumMemberType, typename nameType = const char*> struct enumMemberNamePair
{
typedef enumMemberType tEnumMember;
typedef nameType tName;
enumMemberType enumMember;
nameType name;
/*
bool operator== (const nameType name) const
{
return this -> name == name;
}
bool operator== (const enumMemberType enumMember) const
{
return this -> enumMember == enumMember;
}
*/
};
// Returns enum member-name pair.
// Returns NULL if not found.
template <typename enumMemberNamePairType, size_t _Size, typename searchedValueType> enumMemberNamePairType* getEnumPairFromArrayByMember(enumMemberNamePairType (&enumMemberNameArray)[_Size], searchedValueType searchedEnumMember)
{
const enumMemberNamePairType* pair = std::find_if(
enumMemberNameArray,
enumMemberNameArray + _Size,
[&](enumMemberNamePairType& pair)
{
return pair.enumMember == searchedEnumMember;
}
);
return pair != enumMemberNameArray + _Size ? pair : nullptr;
}
// Returns enum member-name pair.
// Returns NULL if not found.
// template <typename enumMemberNamePairType, size_t _Size, typename searchedValueType> enumMemberNamePairType* getEnumPairFromArrayByName(enumMemberNamePairType (&enumMemberNameArray)[_Size], searchedValueType searchedName);
/*
{
const enumMemberNamePairType* pair = std::find_if(
enumMemberNameArray,
enumMemberNameArray + _Size,
[&](enumMemberNamePairType& pair)
{
return pair.name == searchedName;
}
);
return pair != enumMemberNameArray + _Size ? pair : nullptr;
}
*/
// Returns enum member-name pair.
// Returns NULL if not found.
template <typename enumMemberNamePairType, size_t _Size, typename tSearchedName, typename tCmpFunc> enumMemberNamePairType* _getEnumPairFromArrayByNameHelper(enumMemberNamePairType (&enumMemberNameArray)[_Size], tSearchedName* searchedName, tCmpFunc cmpFunc)
{
const enumMemberNamePairType* pair = std::find_if(
enumMemberNameArray,
enumMemberNameArray + _Size,
[&](enumMemberNamePairType& pair)
{
return pair.name == searchedName || !cmpFunc(pair.name, searchedName);
}
);
return pair != enumMemberNameArray + _Size ? pair : nullptr;
}
// Returns enum member-name pair.
// Returns NULL if not found.
template <typename enumMemberNamePairType, size_t _Size> enumMemberNamePairType* getEnumPairFromArrayByName(enumMemberNamePairType (&enumMemberNameArray)[_Size], const char* searchedName)
{
return _getEnumPairFromArrayByNameHelper(enumMemberNameArray, searchedName, strcmp);
}
// Returns enum member-name pair.
// Returns NULL if not found.
template <typename enumMemberNamePairType, size_t _Size> enumMemberNamePairType* getEnumPairFromArrayByName(enumMemberNamePairType (&enumMemberNameArray)[_Size], const wchar_t* searchedName)
{
return _getEnumPairFromArrayByNameHelper(enumMemberNameArray, searchedName, wcscmp);
}
// Returns name by enum member.
// Returns NULL if not found.
template <typename enumMemberNamePairType, size_t _Size, typename enumMemberType>
typename enumMemberNamePairType::tName getNameByEnumMemberFromArray(enumMemberNamePairType (&enumMemberNameArray)[_Size], enumMemberType searchedEnumMember)
{
const enumMemberNamePairType* pair = getEnumPairFromArrayByMember(enumMemberNameArray, searchedEnumMember);
return pair !=NULL ? pair -> name : NULL;
}
////////////////////////////////
// template tEnumMemberNameManager
// //DECLARE_LIST_WITH_ENUM_MEMBER_AND_NAME(pairArray, enumMemberType, listMacro);
#define GET_NAME_OF_PAIR_ARRAY_FOR_TYPE(managerTypeName) __##managerTypeName##_pairArray
#define DECLARE_ENUM_MEMBER_NAME_MANAGER_TYPE(managerTypeName, enumMemberType, undefinedMember, listMacro) \
DECLARE_LIST_WITH_ENUM_MEMBER_AND_NAME(GET_NAME_OF_PAIR_ARRAY_FOR_TYPE(managerTypeName), enumMemberType, listMacro) \
class managerTypeName \
{ \
public: \
\
/* Finds enum member by name */ \
static enumMemberType GetEnumMemberByName(const char* name) \
{ \
const enumMemberNamePair<enumMemberType>* pair = getEnumPairFromArrayByName(GET_NAME_OF_PAIR_ARRAY_FOR_TYPE(managerTypeName), name); \
\
return pair != NULL ? pair->enumMember : undefinedMember; \
} \
\
/* Finds name by enum member */ \
static const char* GetEnumNameByMember(enumMemberType game) \
{ \
return getNameByEnumMemberFromArray(GET_NAME_OF_PAIR_ARRAY_FOR_TYPE(managerTypeName), game); \
} \
\
};
class CEnumMemberNameManagerBase
{
public:
/* Finds enum member by name */
virtual int GetEnumMemberByName(const char* name) const = 0;
/* Finds name by enum member */
virtual const char* GetEnumNameByMember(int member) const = 0;
};
#define DECLARE_ENUM_MEMBER_NAME_MANAGER_TYPE_CLASS(managerTypeName, enumMemberType, undefinedMember, listMacro) \
DECLARE_LIST_WITH_ENUM_MEMBER_AND_NAME(GET_NAME_OF_PAIR_ARRAY_FOR_TYPE(managerTypeName), enumMemberType, listMacro) \
class managerTypeName : public CEnumMemberNameManagerBase\
{ \
public: \
\
/* Finds enum member by name */ \
virtual int GetEnumMemberByName(const char* name) const \
{ \
const enumMemberNamePair<enumMemberType>* pair = getEnumPairFromArrayByName(GET_NAME_OF_PAIR_ARRAY_FOR_TYPE(managerTypeName), name); \
\
return pair != NULL ? pair->enumMember : undefinedMember; \
} \
\
/* Finds name by enum member */ \
virtual const char* GetEnumNameByMember(int member) const \
{ \
return getNameByEnumMemberFromArray(GET_NAME_OF_PAIR_ARRAY_FOR_TYPE(managerTypeName), member); \
} \
\
};