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.

162 lines
3.7 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2000, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // objvec.h
  8. //
  9. // SYNOPSIS
  10. //
  11. // Declares the class ObjectVector.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 02/08/2000 Original version.
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #ifndef OBJVEC_H
  19. #define OBJVEC_H
  20. #if _MSC_VER >= 1000
  21. #pragma once
  22. #endif
  23. ///////////////////////////////////////////////////////////////////////////////
  24. //
  25. // CLASS
  26. //
  27. // ObjectVector<T>
  28. //
  29. // DESCRIPTION
  30. //
  31. // Maintains an array of reference counted objects.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////////
  34. template <class T>
  35. class ObjectVector
  36. {
  37. public:
  38. typedef int (__cdecl *SortFn)(
  39. const T* const* t1,
  40. const T* const* t2
  41. ) throw ();
  42. ObjectVector() throw ()
  43. : first(NULL), last(NULL), cap(NULL)
  44. { }
  45. ~ObjectVector() throw ()
  46. {
  47. clear();
  48. delete[] first;
  49. }
  50. // Removes and releases all the objects in the array, but doesn't release
  51. // the array itself, i.e., after this call size() will return zero but
  52. // capacity() is unchanged.
  53. void clear() throw ()
  54. {
  55. while (last > first) (*--last)->Release();
  56. }
  57. bool contains(T* elem) throw ()
  58. {
  59. for (T** i = first; i != last; ++i)
  60. {
  61. if (*i == elem) { return true; }
  62. }
  63. return false;
  64. }
  65. // Returns true if the array is empty.
  66. bool empty() const throw ()
  67. { return first == last; }
  68. bool erase(T* elem) throw ()
  69. {
  70. for (T** i = first; i != last; ++i)
  71. {
  72. if (*i == elem)
  73. {
  74. --last;
  75. memmove(i, i + 1, (last - i) * sizeof(T*));
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81. // Add 'elem' to the end of the array, resizing if necessary.
  82. void push_back(T* elem)
  83. {
  84. if (last == cap) { reserve(empty() ? 1 : capacity() * 2); }
  85. (*last++ = elem)->AddRef();
  86. }
  87. // Reserve space for at least 'nelem' items in the array. Useful to avoid
  88. // lots of resizing when you know in advance how many items you're planning
  89. // to store.
  90. void reserve(size_t nelem)
  91. {
  92. if (nelem > capacity())
  93. {
  94. T** t = new (AfxThrow) T*[nelem];
  95. memcpy(t, first, size() * sizeof(T*));
  96. last = t + size();
  97. cap = t + nelem;
  98. delete[] first;
  99. first = t;
  100. }
  101. }
  102. void sort(SortFn pfn) throw ()
  103. { qsort((void*)begin(), size(), sizeof(T*), (CompFn)pfn); }
  104. // Swap the contents of this with v.
  105. void swap(ObjectVector& v) throw ()
  106. {
  107. T** firstTmp = first;
  108. T** lastTmp = last;
  109. T** capTmp = cap;
  110. first = v.first;
  111. last = v.last;
  112. cap = v.cap;
  113. v.first = firstTmp;
  114. v.last = lastTmp;
  115. v.cap = capTmp;
  116. }
  117. // Returns the capacity of the array.
  118. size_t capacity() const throw ()
  119. { return cap - first; }
  120. // Returns the number of objects stored in the array.
  121. size_t size() const throw ()
  122. { return last - first; }
  123. typedef T* const* iterator;
  124. // Methods to iterate the array elements.
  125. iterator begin() const throw ()
  126. { return first; }
  127. iterator end() const throw ()
  128. { return last; }
  129. T* operator[](size_t index) const throw ()
  130. { return first[index]; }
  131. private:
  132. typedef int (__cdecl *CompFn)(const void*, const void*);
  133. T** first; // Begining of the array.
  134. T** last; // End of the elements.
  135. T** cap; // End of allocated storage.
  136. // Not implemented.
  137. ObjectVector(const ObjectVector&);
  138. ObjectVector& operator=(const ObjectVector&);
  139. };
  140. #endif // OBJVEC_H