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.

264 lines
5.8 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2000, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // iasobj.h
  8. //
  9. // SYNOPSIS
  10. //
  11. // Declares the classes ObjectPointer and ObjectVector for manipulating
  12. // reference counted objects.
  13. //
  14. // MODIFICATION HISTORY
  15. //
  16. // 02/08/2000 Original version.
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. #ifndef IASOBJ_H
  20. #define IASOBJ_H
  21. #if _MSC_VER >= 1000
  22. #pragma once
  23. #endif
  24. #include <iaswin32.h>
  25. ///////////////////////////////////////////////////////////////////////////////
  26. //
  27. // CLASS
  28. //
  29. // ObjectPointer<T>
  30. //
  31. // DESCRIPTION
  32. //
  33. // Smart pointer to a reference counted object. This is useful because it
  34. // doesn't require the object to derive from IUnknown like the ATL or VC
  35. // smart pointers.
  36. //
  37. ///////////////////////////////////////////////////////////////////////////////
  38. template <class T>
  39. class ObjectPointer
  40. {
  41. public:
  42. ObjectPointer() throw ()
  43. : p(NULL)
  44. { }
  45. ObjectPointer(T* lp, bool addRef = true) throw ()
  46. : p(lp)
  47. { if (addRef) _addref(); }
  48. ObjectPointer(const ObjectPointer& lp) throw ()
  49. : p(lp.p)
  50. { _addref(); }
  51. T* operator=(T* lp) throw ()
  52. {
  53. attach(lp);
  54. return p;
  55. }
  56. ObjectPointer& operator=(const ObjectPointer& lp) throw ()
  57. {
  58. if (this != &lp) attach(lp.p);
  59. return *this;
  60. }
  61. ~ObjectPointer() throw ()
  62. { _release(); }
  63. operator T*() const throw ()
  64. { return p; }
  65. T* operator->() const throw ()
  66. { return p; }
  67. T& operator*() const throw ()
  68. { return *p; }
  69. bool operator!() const throw ()
  70. { return p == NULL; }
  71. void attach(T* lp) throw ()
  72. {
  73. _release();
  74. p = lp;
  75. _addref();
  76. }
  77. void release() throw ()
  78. {
  79. _release();
  80. p = NULL;
  81. }
  82. private:
  83. // Safe versions of AddRef and Release.
  84. void _addref() throw ()
  85. { if (p) { p->AddRef(); } }
  86. void _release() throw ()
  87. { if (p) { p->Release(); } }
  88. T* p;
  89. };
  90. ///////////////////////////////////////////////////////////////////////////////
  91. //
  92. // CLASS
  93. //
  94. // ObjectVector<T>
  95. //
  96. // DESCRIPTION
  97. //
  98. // Maintains an array of reference counted objects.
  99. //
  100. ///////////////////////////////////////////////////////////////////////////////
  101. template <class T>
  102. class ObjectVector
  103. {
  104. public:
  105. typedef T* const* iterator;
  106. typedef int (__cdecl *SortFn)(
  107. const T* const* t1,
  108. const T* const* t2
  109. ) throw ();
  110. typedef int (__cdecl *SearchFn)(
  111. const void* key,
  112. const T* const* t
  113. ) throw ();
  114. ObjectVector() throw ()
  115. : first(NULL), last(NULL), cap(NULL)
  116. { }
  117. ObjectVector(const ObjectVector& v)
  118. : first(NULL), last(NULL), cap(NULL)
  119. { assign(v.begin(), v.end()); }
  120. ObjectVector(iterator first, iterator last)
  121. : first(NULL), last(NULL), cap(NULL)
  122. { assign(first, last); }
  123. ObjectVector(size_t nelem)
  124. : first(NULL), last(NULL), cap(NULL)
  125. { reserve(nelem); }
  126. ObjectVector& operator=(const ObjectVector& v)
  127. {
  128. if (this != &v) { assign(v.begin(), v.end()); }
  129. return *this;
  130. }
  131. ~ObjectVector() throw ()
  132. {
  133. clear();
  134. delete[] first;
  135. }
  136. void assign(iterator i1, iterator i2)
  137. {
  138. reserve(i2 - i1);
  139. clear();
  140. for (iterator i = i1; i != i2; ++i)
  141. { (*last++ = *i)->AddRef(); }
  142. }
  143. // Returns the capacity of the array.
  144. size_t capacity() const throw ()
  145. { return cap - first; }
  146. void clear() throw ()
  147. { while (last > first) (*--last)->Release(); }
  148. bool contains(T* elem) throw ()
  149. { return find(elem) != last; }
  150. bool empty() const throw ()
  151. { return last == first; }
  152. iterator erase(iterator i) throw ()
  153. {
  154. memmove((void*)i, (void*)(i + 1), (--last - i) * sizeof(T*));
  155. return i;
  156. }
  157. iterator find(T* elem) throw ()
  158. {
  159. for (iterator i = first; i != last && *i != elem; ++i) { }
  160. return i;
  161. }
  162. void push_back(T* elem) throw ()
  163. {
  164. if (last == cap) { reserve(empty() ? 1 : capacity() * 2); }
  165. (*last++ = elem)->AddRef();
  166. }
  167. bool remove(T* elem) throw ()
  168. {
  169. iterator i = find(elem);
  170. if (i == last) { return false; }
  171. (*i)->Release();
  172. return true;
  173. }
  174. void reserve(size_t nelem)
  175. {
  176. if (nelem > capacity())
  177. {
  178. T** t = new T*[nelem];
  179. memcpy(t, first, size() * sizeof(T*));
  180. last = t + size();
  181. cap = t + nelem;
  182. delete[] first;
  183. first = t;
  184. }
  185. }
  186. T* search(const void* key, SearchFn pfn) const throw ()
  187. {
  188. T** t = (T**)bsearch(key, begin(), size(), sizeof(T*), (CompFn)pfn);
  189. return t ? *t : NULL;
  190. }
  191. size_t size() const throw ()
  192. { return last - first; }
  193. void sort(SortFn pfn) throw ()
  194. { qsort((void*)begin(), size(), sizeof(T*), (CompFn)pfn); }
  195. void swap(ObjectVector& v) throw ()
  196. {
  197. T** firstTmp = first;
  198. T** lastTmp = last;
  199. T** capTmp = cap;
  200. first = v.first;
  201. last = v.last;
  202. cap = v.cap;
  203. v.first = firstTmp;
  204. v.last = lastTmp;
  205. v.cap = capTmp;
  206. }
  207. // Methods to iterate the array elements.
  208. iterator begin() const throw ()
  209. { return first; }
  210. iterator end() const throw ()
  211. { return last; }
  212. T* operator[](size_t index) const throw ()
  213. { return first[index]; }
  214. private:
  215. typedef int (__cdecl *CompFn)(const void*, const void*);
  216. T** first;
  217. T** last;
  218. T** cap;
  219. };
  220. #endif // IASOBJ_H