hstring.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ////////////////////////////////////////////////////////////////////////////////////////
  2. // RAVEN STANDARD USEFUL FUNCTION LIBRARY
  3. // (c) 2002 Activision
  4. //
  5. //
  6. // Handle String
  7. // -------------
  8. // Handle strings are allocated once in a static buffer (with a hash index), and are
  9. // never cleared out. You should use these for very common string names which are
  10. // redundant or intended to last a long time.
  11. //
  12. // Handle strings are also good for comparison and storage because they compare only
  13. // the handles, which are simple unique integers.
  14. //
  15. ////////////////////////////////////////////////////////////////////////////////////////
  16. ////////////////////////////////////////////////////////////////////////////////////////
  17. // Includes
  18. ////////////////////////////////////////////////////////////////////////////////////////
  19. #include "hstring.h"
  20. #include <string.h>
  21. #include "..\Ratl\hash_pool_vs.h"
  22. ////////////////////////////////////////////////////////////////////////////////////////
  23. // Defines
  24. ////////////////////////////////////////////////////////////////////////////////////////
  25. #define MAX_HASH 16384 // How Many Hash
  26. #define BLOCK_SIZE 65536 // Size of a string storage block in bytes.
  27. ////////////////////////////////////////////////////////////////////////////////////////
  28. // The Hash Pool
  29. ////////////////////////////////////////////////////////////////////////////////////////
  30. typedef ratl::hash_pool<BLOCK_SIZE, MAX_HASH> TStrPool;
  31. TStrPool& Pool()
  32. {
  33. static TStrPool TSP;
  34. return TSP;
  35. }
  36. #ifdef _XBOX
  37. // Utility for clearing the pool between levels
  38. void ClearHStringPool( void )
  39. {
  40. Pool().clear();
  41. }
  42. namespace dllNamespace
  43. {
  44. #endif
  45. ////////////////////////////////////////////////////////////////////////////////////////
  46. // Constructor
  47. ////////////////////////////////////////////////////////////////////////////////////////
  48. hstring::hstring()
  49. {
  50. mHandle = 0;
  51. #ifdef _DEBUG
  52. mStr = 0;
  53. #endif
  54. }
  55. ////////////////////////////////////////////////////////////////////////////////////////
  56. // Constructor
  57. ////////////////////////////////////////////////////////////////////////////////////////
  58. hstring::hstring(const char *str)
  59. {
  60. init(str);
  61. }
  62. ////////////////////////////////////////////////////////////////////////////////////////
  63. // Constructor
  64. ////////////////////////////////////////////////////////////////////////////////////////
  65. hstring::hstring(const hstring &str)
  66. {
  67. mHandle = str.mHandle;
  68. #ifdef _DEBUG
  69. mStr = str.mStr;
  70. #endif
  71. }
  72. ////////////////////////////////////////////////////////////////////////////////////////
  73. // Assignment
  74. ////////////////////////////////////////////////////////////////////////////////////////
  75. hstring& hstring::operator= (const char *str)
  76. {
  77. init(str);
  78. return *this;
  79. }
  80. ////////////////////////////////////////////////////////////////////////////////////////
  81. // Constructor
  82. ////////////////////////////////////////////////////////////////////////////////////////
  83. hstring& hstring::operator= (const hstring &str)
  84. {
  85. mHandle = str.mHandle;
  86. #ifdef _DEBUG
  87. mStr = str.mStr;
  88. #endif
  89. return *this;
  90. }
  91. ////////////////////////////////////////////////////////////////////////////////////////
  92. //
  93. ////////////////////////////////////////////////////////////////////////////////////////
  94. const char* hstring::c_str(void) const
  95. {
  96. if (!mHandle)
  97. {
  98. return("");
  99. }
  100. return ((const char*)Pool()[mHandle]);
  101. }
  102. ////////////////////////////////////////////////////////////////////////////////////////
  103. //
  104. ////////////////////////////////////////////////////////////////////////////////////////
  105. const char* hstring::operator *(void) const
  106. {
  107. return c_str();
  108. }
  109. ////////////////////////////////////////////////////////////////////////////////////
  110. //
  111. ////////////////////////////////////////////////////////////////////////////////////
  112. int hstring::length() const
  113. {
  114. return strlen(c_str());
  115. }
  116. ////////////////////////////////////////////////////////////////////////////////////
  117. //
  118. ////////////////////////////////////////////////////////////////////////////////////
  119. int hstring::handle() const
  120. {
  121. return mHandle;
  122. }
  123. ////////////////////////////////////////////////////////////////////////////////////
  124. //
  125. ////////////////////////////////////////////////////////////////////////////////////
  126. void hstring::init(const char *str)
  127. {
  128. if (!str)
  129. {
  130. mHandle = 0;
  131. }
  132. else
  133. {
  134. mHandle = Pool().get_handle(str, strlen(str)+1); // +1 for null character
  135. }
  136. #ifdef _DEBUG
  137. mStr = (char*)Pool()[mHandle];
  138. #endif
  139. }
  140. ////////////////////////////////////////////////////////////////////////////////////
  141. //
  142. ////////////////////////////////////////////////////////////////////////////////////
  143. #ifdef _DEBUG
  144. float hstring::ave_collisions() {return Pool().average_collisions();}
  145. int hstring::total_strings() {return Pool().total_allocs();}
  146. int hstring::total_bytes() {return Pool().size();}
  147. int hstring::total_finds() {return Pool().total_finds();}
  148. int hstring::total_collisions() {return Pool().total_collisions();}
  149. #endif
  150. #ifdef _XBOX
  151. } // dllNamespace
  152. #endif