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.

99 lines
1.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #ifndef UTLSTRINGMAP_H
  7. #define UTLSTRINGMAP_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "utlsymbol.h"
  12. template <class T>
  13. class CUtlStringMap
  14. {
  15. public:
  16. CUtlStringMap( bool caseInsensitive = true ) : m_SymbolTable( 0, 32, caseInsensitive )
  17. {
  18. }
  19. // Get data by the string itself:
  20. T& operator[]( const char *pString )
  21. {
  22. CUtlSymbol symbol = m_SymbolTable.AddString( pString );
  23. int index = ( int )( UtlSymId_t )symbol;
  24. if( m_Vector.Count() <= index )
  25. {
  26. m_Vector.EnsureCount( index + 1 );
  27. }
  28. return m_Vector[index];
  29. }
  30. // Get data by the string's symbol table ID - only used to retrieve a pre-existing symbol, not create a new one!
  31. T& operator[]( UtlSymId_t n )
  32. {
  33. Assert( n >=0 && n <= m_Vector.Count() );
  34. return m_Vector[n];
  35. }
  36. const T& operator[]( UtlSymId_t n ) const
  37. {
  38. Assert( n >=0 && n <= m_Vector.Count() );
  39. return m_Vector[n];
  40. }
  41. bool Defined( const char *pString ) const
  42. {
  43. return m_SymbolTable.Find( pString ) != UTL_INVAL_SYMBOL;
  44. }
  45. UtlSymId_t Find( const char *pString ) const
  46. {
  47. return m_SymbolTable.Find( pString );
  48. }
  49. static UtlSymId_t InvalidIndex()
  50. {
  51. return UTL_INVAL_SYMBOL;
  52. }
  53. int GetNumStrings( void ) const
  54. {
  55. return m_SymbolTable.GetNumStrings();
  56. }
  57. const char *String( int n ) const
  58. {
  59. return m_SymbolTable.String( n );
  60. }
  61. // Clear all of the data from the map
  62. void Clear()
  63. {
  64. m_Vector.RemoveAll();
  65. m_SymbolTable.RemoveAll();
  66. }
  67. void Purge()
  68. {
  69. m_Vector.Purge();
  70. m_SymbolTable.RemoveAll();
  71. }
  72. void PurgeAndDeleteElements()
  73. {
  74. m_Vector.PurgeAndDeleteElements();
  75. m_SymbolTable.RemoveAll();
  76. }
  77. private:
  78. CUtlVector<T> m_Vector;
  79. CUtlSymbolTable m_SymbolTable;
  80. };
  81. #endif // UTLSTRINGMAP_H