Created
May 21, 2016 09:43
-
-
Save Aegean-Homines/0d090538a7b6ebc2eb404071da741afb to your computer and use it in GitHub Desktop.
C++ convert enum to string (taken from http://www.codeproject.com/Articles/10500/Converting-C-enums-to-strings)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void MyFunction( Enum_Name_Here value ) | |
{ | |
// NOTE = also define another header, include all EnumName.h files to generate all enums on compile time | |
return GetStringEnum_Name_Here(value); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if ( !defined(ENUM_NAME_H) || defined(GENERATE_ENUM_STRINGS) ) | |
#if (!defined(GENERATE_ENUM_STRINGS)) | |
#define ENUM_NAME_H | |
#endif | |
#include "EnumToStringHelper.h" | |
/////////////////////////////// | |
// The enum declaration | |
/////////////////////////////// | |
BEGIN_ENUM(Enum_Name_Here) | |
{ | |
DECL_ENUM_ELEMENT(Enum_Elem_0), | |
DECL_ENUM_ELEMENT(Enum_Elem_1), | |
DECL_ENUM_ELEMENT(Enum_Elem_2) | |
} | |
END_ENUM(Enum_Name_Here) | |
#endif // (!defined(ENUM_NAME_H) || defined(GENERATE_ENUM_STRINGS)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#undef DECL_ENUM_ELEMENT | |
#undef BEGIN_ENUM | |
#undef END_ENUM | |
#ifndef GENERATE_ENUM_STRINGS | |
#define DECL_ENUM_ELEMENT( element ) element | |
#define BEGIN_ENUM( ENUM_NAME ) typedef enum tag##ENUM_NAME | |
#define END_ENUM( ENUM_NAME ) ENUM_NAME; \ | |
char* GetString##ENUM_NAME(enum tag##ENUM_NAME index); | |
#else | |
#define DECL_ENUM_ELEMENT( element ) #element | |
#define BEGIN_ENUM( ENUM_NAME ) char* gs_##ENUM_NAME [] = | |
#define END_ENUM( ENUM_NAME ) ; char* GetString##ENUM_NAME(enum \ | |
tag##ENUM_NAME index){ return gs_##ENUM_NAME [index]; } | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment