Counter Strike : Global Offensive Source Code
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.

122 lines
2.5 KiB

  1. //========= Copyright � 2008, Valve Corporation, All rights reserved. ============//
  2. #ifndef __TOKENSET_H
  3. #define __TOKENSET_H
  4. #ifdef _WIN32
  5. #pragma once
  6. #endif
  7. //-----------------------------------------------------------------------------
  8. //
  9. // This class is a handy way to match a series of values to stings and vice
  10. // versa. It should be initalized as static like an array, for example:
  11. //
  12. // const tokenset_t<int> tokens[] = {
  13. // { "token1", 1 },
  14. // { "token2", 2 },
  15. // { "token3", 3 },
  16. // { NULL, -1 }
  17. // };
  18. //
  19. // Then you can call the operators on it by using:
  20. //
  21. // int t = tokens->GetToken( s );
  22. //
  23. // If s is "token1" it returns 1, etc. Invalid string returns the last NULL
  24. // value. GetNameByToken() returns "__UNKNOWN__" when passed a mismatched
  25. // token. GetNameByToken() returns szMismatchResult when passed a mismatched
  26. // token and a string to return in case of mismatch.
  27. //
  28. //-----------------------------------------------------------------------------
  29. template <class _T>
  30. struct tokenset_t
  31. {
  32. const char *name;
  33. _T token;
  34. _T GetToken( const char *s ) const;
  35. _T GetTokenI( const char *s ) const;
  36. const char *GetNameByToken( _T token ) const;
  37. const char *GetNameByToken( _T token, const char *szMismatchResult ) const;
  38. };
  39. template <class _T>
  40. inline _T tokenset_t< _T >::GetToken( const char *s ) const
  41. {
  42. const tokenset_t< _T > *c;
  43. for ( c = this; c->name; ++c )
  44. {
  45. if ( !s )
  46. {
  47. continue; // Loop to the last NULL value
  48. }
  49. if ( Q_strcmp( s, c->name ) == 0 )
  50. {
  51. return c->token;
  52. }
  53. }
  54. return c->token; // c points to the last NULL value
  55. }
  56. template <class _T>
  57. inline _T tokenset_t< _T >::GetTokenI( const char *s ) const
  58. {
  59. const tokenset_t< _T > *c;
  60. for ( c = this; c->name; ++c )
  61. {
  62. if ( !s )
  63. {
  64. continue; // Loop to the last NULL value
  65. }
  66. if ( Q_stricmp( s, c->name ) == 0 )
  67. {
  68. return c->token;
  69. }
  70. }
  71. return c->token; // c points to the last NULL value
  72. }
  73. template <class _T>
  74. inline const char *tokenset_t< _T >::GetNameByToken( _T token ) const
  75. {
  76. static const char *unknown = "__UNKNOWN__";
  77. const tokenset_t< _T > *c;
  78. for ( c = this; c->name; ++c )
  79. {
  80. if ( c->token == token )
  81. {
  82. return c->name;
  83. }
  84. }
  85. return unknown;
  86. }
  87. template <class _T>
  88. inline const char *tokenset_t< _T >::GetNameByToken( _T token, char const *szMismatchResult ) const
  89. {
  90. const tokenset_t< _T > *c;
  91. for ( c = this; c->name; ++c )
  92. {
  93. if ( c->token == token )
  94. {
  95. return c->name;
  96. }
  97. }
  98. return szMismatchResult;
  99. }
  100. #endif //__TOKENSET_H