Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Makes enum-to-string and string-to-enum easier to declare
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #ifndef GCENUMUTILS_H
  8. #define GCENUMUTILS_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. namespace GCSDK
  13. {
  14. struct EnumString_s
  15. {
  16. int nValue;
  17. const char *pszString;
  18. };
  19. }
  20. // starts defining a enum value string map
  21. #define ENUMSTRINGS_START( etype ) static const GCSDK::EnumString_s s_##etype[] = {
  22. // ends defining a enum value string map. generates PchNameFromEnumName()
  23. #define ENUMSTRINGS_END( etype ) }; const char* PchNameFrom##etype( etype nValue ) \
  24. { for( uint i=0; i<Q_ARRAYSIZE(s_##etype); i++ ) { if ( s_##etype[i].nValue == nValue ) return s_##etype[i].pszString; } \
  25. AssertMsg2( false, "Missing String for %s (%d)", #etype, nValue ); return "Unknown"; } \
  26. const char* PchNameFrom##etype##Unsafe( etype nValue ) \
  27. { for( uint i=0; i<Q_ARRAYSIZE(s_##etype); i++ ) { if ( s_##etype[i].nValue == nValue ) return s_##etype[i].pszString; } \
  28. return NULL; }
  29. // ends defining a enum value string map. generates PchNameFromEnum() and EnumFromNam(). Invalid element must be first in array
  30. #define ENUMSTRINGS_REVERSE( etype, default ) }; const char* PchNameFrom##etype(etype nValue ) \
  31. { for( uint i=0; i<Q_ARRAYSIZE(s_##etype); i++ ) { if ( s_##etype[i].nValue == nValue ) return s_##etype[i].pszString; } \
  32. AssertMsg2( false, "Missing String for %s (%d)", #etype, nValue ); return "Unknown"; }; \
  33. etype etype##FromName( const char *pchName ) \
  34. { for( uint i=0; i<Q_ARRAYSIZE(s_##etype); i++ ) { if ( !Q_stricmp( s_##etype[i].pszString, pchName ) ) return (etype)(s_##etype[i].nValue); } \
  35. return default; }
  36. #endif // GCENUMUTILS_H