Leaked source code of windows server 2003
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.

182 lines
4.1 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1997
  6. //
  7. // File: zstrt.h
  8. //
  9. //--------------------------------------------------------------------------
  10. //
  11. // ZSTRT.H: String table management
  12. //
  13. #ifndef _ZSTRT_H_
  14. #define _ZSTRT_H_
  15. #include <map>
  16. #include <set>
  17. #include <vector>
  18. #include "basics.h"
  19. #include "zstr.h"
  20. #include "refcnt.h"
  21. //using namespace std;
  22. // Token used in probability distribution
  23. class TKNPD;
  24. ////////////////////////////////////////////////////////////////////
  25. // class ZSTRT:
  26. // Class of reference-counted string maintained in string table
  27. ////////////////////////////////////////////////////////////////////
  28. class ZSTRT : public ZSTR, public REFCNT
  29. {
  30. friend class STZSTR;
  31. friend class ZSREF;
  32. friend class TKNPD;
  33. protected:
  34. ZSTRT( SZC szc = NULL )
  35. : ZSTR(szc)
  36. {}
  37. protected:
  38. void Dump () const;
  39. // Hide the assignment operator
  40. HIDE_AS(ZSTRT);
  41. };
  42. ////////////////////////////////////////////////////////////////////
  43. // class ZSREF:
  44. // Smart pointer acting as a reference to a string in a symbol
  45. // table (i.e., reference-counted).
  46. //
  47. // ZSREF contains appropriate operators for const strings.
  48. ////////////////////////////////////////////////////////////////////
  49. class ZSREF
  50. {
  51. friend class STZSTR;
  52. friend class TKNPD;
  53. protected:
  54. ZSREF( ZSTRT & zstrt )
  55. : _pzstrt(& zstrt)
  56. { IncRef(); }
  57. public:
  58. ZSREF ()
  59. : _pzstrt(& Zsempty)
  60. {}
  61. ~ZSREF()
  62. { IncRef(-1); }
  63. ZSREF( const ZSREF & zsr )
  64. : _pzstrt(zsr._pzstrt)
  65. { IncRef(); }
  66. ZSREF & operator = ( const ZSREF & zsr )
  67. {
  68. IncRef(-1);
  69. _pzstrt = zsr._pzstrt;
  70. IncRef(1);
  71. return *this;
  72. }
  73. const ZSTR & Zstr () const
  74. { return *Pzst(); }
  75. SZC Szc () const
  76. { return _pzstrt->c_str(); }
  77. operator SZC () const
  78. { return Szc() ; }
  79. bool operator == ( const ZSREF & zsr ) const
  80. {
  81. return _pzstrt == zsr._pzstrt
  82. || ((Pzst()->length() + zsr.Pzst()->length()) == 0) ;
  83. }
  84. bool operator < ( const ZSREF & zsr ) const
  85. { return *_pzstrt < *zsr._pzstrt; }
  86. bool operator == ( const ZSTR & zst ) const
  87. { return *_pzstrt == zst; }
  88. bool operator < ( const ZSTR & zst ) const
  89. { return *_pzstrt < zst; }
  90. const ZSTRT * operator -> () const
  91. { return Pzst(); }
  92. void Clear ()
  93. {
  94. IncRef(-1);
  95. _pzstrt = & Zsempty;
  96. }
  97. bool BEmpty () const
  98. { return _pzstrt == & Zsempty; }
  99. protected:
  100. ZSTRT * _pzstrt;
  101. void IncRef ( int i = 1 ) const
  102. { _pzstrt->IncRef(i); }
  103. const ZSTRT * Pzst () const
  104. { return _pzstrt; }
  105. static ZSTRT Zsempty;
  106. };
  107. // Define VZSREF
  108. DEFINEV(ZSREF);
  109. ////////////////////////////////////////////////////////////////////
  110. // class STZSTR_BASE and STZSTR.
  111. //
  112. // STZSTR_BASE is an ensemble of strings. STZSTR is a container
  113. // for a STZSTR_BASE. The STL does not adequately hide
  114. // implementations, so the string table had to be embedded into
  115. // a container to encapsulate it completely.
  116. ////////////////////////////////////////////////////////////////////
  117. class STZSTR_BASE : public set<ZSTRT, less<ZSTRT> > {};
  118. // Container for a string table. Returns only references to the string.
  119. class STZSTR
  120. {
  121. public:
  122. STZSTR() {}
  123. ~ STZSTR()
  124. {
  125. #if defined(DUMP)
  126. Dump();
  127. #endif
  128. }
  129. // The only public accessor: given a "const char *", return
  130. // a ZSREF, whether by creation of a new string in the table
  131. // or by returning a reference to an existing string.
  132. ZSREF Zsref (SZC szc)
  133. {
  134. ZSTRT zs(szc);
  135. STZSTR_BASE::_Pairib it = _stz.insert(zs);
  136. const ZSTRT & zst = *it.first;
  137. return ZSREF(const_cast<ZSTRT&>(zst));
  138. }
  139. void Clone ( const STZSTR & stzstr );
  140. protected:
  141. STZSTR_BASE _stz; // The contained string table
  142. protected:
  143. STZSTR_BASE & Stz ()
  144. { return _stz; }
  145. // Testing: iterator accessors for hidden string set
  146. STZSTR_BASE::const_iterator IterBegin () const
  147. { return _stz.begin(); }
  148. STZSTR_BASE::const_iterator IterEnd () const
  149. { return _stz.end(); }
  150. void Dump () const;
  151. HIDE_UNSAFE(STZSTR);
  152. };
  153. // End of ZSTRT.H
  154. #endif