Skip to content

Commit 760411d

Browse files
committed
Added enum class support to EnumToString
1 parent 26c998a commit 760411d

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

Shared/sdk/SharedUtil.Misc.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,11 +1288,12 @@ namespace SharedUtil
12881288
//
12891289
enum eDummy { };
12901290

1291+
template<class T>
12911292
struct CEnumInfo
12921293
{
12931294
struct SEnumItem
12941295
{
1295-
int iValue;
1296+
T iValue;
12961297
const char* szName;
12971298
};
12981299

@@ -1346,16 +1347,17 @@ namespace SharedUtil
13461347
};
13471348

13481349

1349-
#define DECLARE_ENUM( T ) \
1350-
CEnumInfo* GetEnumInfo ( const T& ); \
1350+
#define DECLARE_ENUM2(T, U) \
1351+
CEnumInfo<U>* GetEnumInfo ( const T& ); \
13511352
inline const SString& EnumToString ( const T& value ) { return GetEnumInfo ( *(T*)0 )->FindName ( (eDummy)value ); }\
13521353
inline bool StringToEnum ( const SString& strName, T& outResult ) { return GetEnumInfo ( *(T*)0 )->FindValue ( strName, (eDummy&)outResult ); }\
13531354
inline const SString& GetEnumTypeName ( const T& ) { return GetEnumInfo ( *(T*)0 )->GetTypeName (); }\
13541355
inline bool EnumValueValid ( const T& value ) { return GetEnumInfo ( *(T*)0 )->ValueValid ( (eDummy)value ); }\
13551356

1356-
#define IMPLEMENT_ENUM_BEGIN(cls) \
1357-
CEnumInfo* GetEnumInfo( const cls& ) \
1357+
#define IMPLEMENT_ENUM_BEGIN2(T, U) \
1358+
CEnumInfo<U>* GetEnumInfo( const T& ) \
13581359
{ \
1360+
using CEnumInfo = CEnumInfo<U>; \
13591361
static const CEnumInfo::SEnumItem items[] = {
13601362

13611363
#define IMPLEMENT_ENUM_END(name) \
@@ -1370,6 +1372,16 @@ namespace SharedUtil
13701372
#define ADD_ENUM(value,name) {value, name},
13711373
#define ADD_ENUM1(value) {value, #value},
13721374

1375+
// enum
1376+
#define DECLARE_ENUM(T) DECLARE_ENUM2(T, int)
1377+
#define IMPLEMENT_ENUM_BEGIN(T) IMPLEMENT_ENUM_BEGIN2(T, int)
1378+
1379+
// enum class
1380+
#define DECLARE_ENUM_CLASS(T) DECLARE_ENUM2(T, T)
1381+
#define IMPLEMENT_ENUM_CLASS_BEGIN(T) IMPLEMENT_ENUM_BEGIN2(T, T)
1382+
#define IMPLEMENT_ENUM_CLASS_END(name) IMPLEMENT_ENUM_END(name)
1383+
#define IMPLEMENT_ENUM_CLASS_END_DEFAULTS(name,defvalue,defname) IMPLEMENT_ENUM_END_DEFAULTS(name,defvalue,defname)
1384+
13731385

13741386
//
13751387
// Fast wildcard matching

0 commit comments

Comments
 (0)