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.

106 lines
2.9 KiB

  1. //========= Copyright (c) 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef STRINGPOOL_H
  8. #define STRINGPOOL_H
  9. #if defined( _WIN32 )
  10. #pragma once
  11. #endif
  12. #include "utlrbtree.h"
  13. #include "utlvector.h"
  14. #include "utlbuffer.h"
  15. //-----------------------------------------------------------------------------
  16. // Purpose: Allocates memory for strings, checking for duplicates first,
  17. // reusing exising strings if duplicate found.
  18. //-----------------------------------------------------------------------------
  19. enum StringPoolCase_t
  20. {
  21. StringPoolCaseInsensitive,
  22. StringPoolCaseSensitive
  23. };
  24. class CStringPool
  25. {
  26. public:
  27. CStringPool( StringPoolCase_t caseSensitivity = StringPoolCaseInsensitive );
  28. ~CStringPool();
  29. unsigned int Count() const;
  30. const char * Allocate( const char *pszValue );
  31. // This feature is deliberately not supported because it's pretty dangerous
  32. // given current uses of CStringPool, which assume they can copy string pointers without
  33. // any refcounts.
  34. //void Free( const char *pszValue );
  35. void FreeAll();
  36. // searches for a string already in the pool
  37. const char * Find( const char *pszValue );
  38. protected:
  39. typedef CUtlRBTree<const char *, unsigned short> CStrSet;
  40. CStrSet m_Strings;
  41. };
  42. //-----------------------------------------------------------------------------
  43. // Purpose: A reference counted string pool.
  44. //
  45. // Elements are stored more efficiently than in the conventional string pool,
  46. // quicker to look up, and storage is tracked via reference counts.
  47. //
  48. // At some point this should replace CStringPool
  49. //-----------------------------------------------------------------------------
  50. class CCountedStringPool
  51. {
  52. public: // HACK, hash_item_t structure should not be public.
  53. struct hash_item_t
  54. {
  55. char* pString;
  56. unsigned short nNextElement;
  57. unsigned char nReferenceCount;
  58. unsigned char pad;
  59. };
  60. enum
  61. {
  62. INVALID_ELEMENT = 0,
  63. MAX_REFERENCE = 0xFF,
  64. HASH_TABLE_SIZE = 1024
  65. };
  66. CUtlVector<unsigned short> m_HashTable; // Points to each element
  67. CUtlVector<hash_item_t> m_Elements;
  68. unsigned short m_FreeListStart;
  69. StringPoolCase_t m_caseSensitivity;
  70. public:
  71. CCountedStringPool( StringPoolCase_t caseSensitivity = StringPoolCaseInsensitive );
  72. virtual ~CCountedStringPool();
  73. void FreeAll();
  74. char *FindString( const char* pIntrinsic );
  75. char *ReferenceString( const char* pIntrinsic );
  76. void DereferenceString( const char* pIntrinsic );
  77. // These are only reliable if there are less than 64k strings in your string pool
  78. unsigned short FindStringHandle( const char* pIntrinsic );
  79. unsigned short ReferenceStringHandle( const char* pIntrinsic );
  80. char *HandleToString( unsigned short handle );
  81. void SpewStrings();
  82. unsigned Hash( const char *pszKey );
  83. bool SaveToBuffer( CUtlBuffer &buffer );
  84. bool RestoreFromBuffer( CUtlBuffer &buffer );};
  85. #endif // STRINGPOOL_H