Source code of Windows XP (NT5)
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.

193 lines
3.6 KiB

  1. //=======================================================================
  2. //
  3. // Copyright (c) 1998-1999 Microsoft Corporation. All Rights Reserved.
  4. //
  5. // File: wustl.h
  6. //
  7. // Owner:
  8. //
  9. // Description:
  10. //
  11. // Common definition and templates useful acros projects
  12. //
  13. //
  14. //=======================================================================
  15. #ifndef _WUSTL_H
  16. #pragma warning(disable:4530) // C++ exception handler used, but unwind semantics are not enabled. Specify -GX
  17. #include <vector>
  18. #include <map>
  19. #include <string>
  20. using std::string;
  21. using std::wstring;
  22. using std::pair;
  23. using std::vector;
  24. using std::map;
  25. #include <ar.h>
  26. //----------------------------------------------------------------------
  27. //
  28. // Common definitions...
  29. //
  30. //----------------------------------------------------------------------
  31. #define sizeOfArray(a) (sizeof(a) / sizeof(a[0]))
  32. #pragma intrinsic(memcmp)
  33. #pragma intrinsic(memcpy)
  34. #pragma intrinsic(memset)
  35. #pragma intrinsic(strcat)
  36. #pragma intrinsic(strcmp)
  37. #pragma intrinsic(strcpy)
  38. #pragma intrinsic(strlen)
  39. //----------------------------------------------------------------------
  40. //
  41. // class frozen_array
  42. // Resizable buffer
  43. //
  44. //----------------------------------------------------------------------
  45. template< class _Ty >
  46. class frozen_array
  47. {
  48. public:
  49. typedef pair<_Ty, bool> _My_pair;
  50. typedef vector< _My_pair > _My_vector;
  51. frozen_array() : _cnFree(0)
  52. {
  53. }
  54. int add(const _Ty& x)
  55. {
  56. if (_cnFree > 0)
  57. {
  58. //Find spare handle or add one
  59. int index = 0;
  60. while(_v[index].second)
  61. {
  62. // ASSERT(index < g_handleArray.size());
  63. index ++;
  64. }
  65. _v[index].first = x;
  66. _v[index].second = true;
  67. _cnFree --;
  68. return index;
  69. }
  70. else
  71. {
  72. _v.push_back(_My_pair(x, true));
  73. return _v.size() - 1;
  74. }
  75. }
  76. void remove(int index)
  77. {
  78. _v[index].second = false;
  79. _cnFree ++;
  80. }
  81. _Ty& operator[](int index)
  82. {
  83. return _v[index].first;
  84. }
  85. bool valid_index(int index)
  86. {
  87. return 0 <= index && index < _v.size() && _v[index].second;
  88. };
  89. protected:
  90. _My_vector _v;
  91. int _cnFree;
  92. };
  93. //----------------------------------------------------------------------
  94. //
  95. // class buffer
  96. // Resizable buffer
  97. //
  98. //----------------------------------------------------------------------
  99. template< class _Ty >
  100. class safe_buffer {
  101. public:
  102. safe_buffer(int cb = 0)
  103. : _cb(0), _pb(0)
  104. {
  105. if (cb)
  106. resize(cb);
  107. }
  108. safe_buffer(const safe_buffer& b)
  109. : _cb(0), _pb(0)
  110. {
  111. if (b._cb)
  112. resize(b._cb);
  113. // do copy if resize ok
  114. if (_cb)
  115. memcpy(_pb, b._pb, _cb * sizeof(_Ty));
  116. }
  117. int size() const
  118. {
  119. return _cb;
  120. }
  121. void resize(int cb)
  122. {
  123. _pb = (_Ty*)realloc(_pb, cb * sizeof(_Ty));
  124. _cb = (0 != _pb) ? cb : 0;
  125. }
  126. bool valid() const
  127. {
  128. return _pb != 0;
  129. }
  130. operator _Ty*() const
  131. {
  132. return _pb;
  133. }
  134. operator const _Ty*() const
  135. {
  136. return _pb;
  137. }
  138. void operator <<(safe_buffer& b)
  139. {
  140. free(_pb);
  141. _cb = b._cb;
  142. _pb = b._pb;
  143. b._cb = 0;
  144. b._pb = 0;
  145. }
  146. void zero_buffer()
  147. {
  148. memset(_pb, '\0', _cb);
  149. }
  150. _Ty* detach()
  151. {
  152. _Ty* pb = _pb;
  153. _pb = 0;
  154. _cb = 0;
  155. return pb;
  156. }
  157. ~safe_buffer()
  158. {
  159. free(_pb);
  160. }
  161. protected:
  162. _Ty* _pb;
  163. int _cb;
  164. private:
  165. // forbid
  166. operator bool() const {}
  167. bool operator !() const {}
  168. };
  169. typedef safe_buffer< BYTE > byte_buffer;
  170. typedef safe_buffer< char > char_buffer;
  171. typedef safe_buffer< WCHAR > wchar_buffer;
  172. #ifdef _UNICODE
  173. #define tchar_buffer wchar_buffer
  174. #else
  175. #define tchar_buffer char_buffer
  176. #endif
  177. #define _WUSTL_H
  178. #endif