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.

123 lines
2.9 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Pool of all per-level strings. Allocates memory for strings,
  4. // consolodating duplicates. The memory is freed on behalf of clients
  5. // at level transition. Strings are of type string_t.
  6. //
  7. // $NoKeywords: $
  8. //=============================================================================//
  9. #ifndef GAMESTRINGPOOL_H
  10. #define GAMESTRINGPOOL_H
  11. #if defined( _WIN32 )
  12. #pragma once
  13. #endif
  14. #if !defined( GC )
  15. class IGameSystem;
  16. #endif
  17. //-----------------------------------------------------------------------------
  18. // String allocation
  19. //-----------------------------------------------------------------------------
  20. string_t AllocPooledString( const char *pszValue );
  21. #define AllocPooledStringConstant( pszValue ) AllocPooledString( pszValue )
  22. string_t FindPooledString( const char *pszValue );
  23. void RemovePooledString( const char *pszValue );
  24. #define AssertIsValidString( s ) AssertMsg( s == NULL_STRING || s == FindPooledString( STRING(s) ), "Invalid string " #s );
  25. //-----------------------------------------------------------------------------
  26. // String system accessor
  27. //-----------------------------------------------------------------------------
  28. #if !defined( GC )
  29. IGameSystem *GameStringSystem();
  30. #endif
  31. //-----------------------------------------------------------------------------
  32. class CGameString
  33. {
  34. public:
  35. CGameString() :
  36. m_iszString( NULL_STRING ), m_pszString( NULL ), m_iSerial( 0 )
  37. {
  38. }
  39. CGameString( const char *pszString, bool bCopy = false ) :
  40. m_iszString( NULL_STRING ), m_pszString( NULL ), m_iSerial( 0 )
  41. {
  42. Set( pszString, bCopy );
  43. }
  44. ~CGameString()
  45. {
  46. if ( m_bCopy )
  47. free( (void *)m_pszString );
  48. }
  49. void Set( const char *pszString, bool bCopy = false )
  50. {
  51. if ( m_bCopy && m_pszString )
  52. free( (void *)m_pszString );
  53. m_iszString = NULL_STRING;
  54. m_pszString = ( !bCopy ) ? pszString : strdup( pszString );
  55. }
  56. // This is only for temp variables!
  57. // If the global string pool changes this has no way to realloc the string!
  58. void SetFastNoCopy( string_t iszString )
  59. {
  60. m_iszString = iszString;
  61. m_iSerial = gm_iSerialNumber;
  62. }
  63. bool IsSerialNumberOutOfDate( void ) const
  64. {
  65. return m_iSerial != gm_iSerialNumber;
  66. }
  67. string_t Get() const
  68. {
  69. if ( m_iszString == NULL_STRING || IsSerialNumberOutOfDate() )
  70. {
  71. if ( !m_pszString )
  72. return NULL_STRING;
  73. m_iszString = AllocPooledString( m_pszString );
  74. m_iSerial = gm_iSerialNumber;
  75. }
  76. return m_iszString;
  77. }
  78. operator const char *() const
  79. {
  80. return ( m_pszString ) ? m_pszString : "";
  81. }
  82. #ifndef NO_STRING_T
  83. operator string_t() const
  84. {
  85. return Get();
  86. }
  87. #endif
  88. static void IncrementSerialNumber()
  89. {
  90. gm_iSerialNumber++;
  91. }
  92. private:
  93. mutable string_t m_iszString;
  94. const char *m_pszString;
  95. mutable int m_iSerial;
  96. bool m_bCopy;
  97. static int gm_iSerialNumber;
  98. };
  99. #endif // GAMESTRINGPOOL_H