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.

138 lines
2.9 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 2000
  6. //
  7. // File: cdpa.h
  8. //
  9. //--------------------------------------------------------------------------
  10. #ifndef __TASKUI_CDPA_H
  11. #define __TASKUI_CDPA_H
  12. //
  13. // Each one of these "CpaDestroyer_XXXX" classes implements a single
  14. // "Destroy" function to free one item held in a DPA. Currently there
  15. // are only two flavors, one that calls "delete" and one that calls
  16. // "LocalFree". By default the CDpa class uses the CDpaDestoyer_Delete
  17. // class as that is the most commont form of freeing required. To use
  18. // another type, just specify another similar class as the 'D' template
  19. // argument to CDpa.
  20. //
  21. template <typename T>
  22. class CDpaDestroyer_Delete
  23. {
  24. public:
  25. static void Destroy(T* p)
  26. { delete p; }
  27. };
  28. template <typename T>
  29. class CDpaDestroyer_Free
  30. {
  31. public:
  32. static void Destroy(T* p)
  33. { if (p) LocalFree(p); }
  34. };
  35. class CDpaDestroyer_None
  36. {
  37. public:
  38. static void Destroy(void*)
  39. { }
  40. };
  41. //-----------------------------------------------------------------------------
  42. // CDpa - Template class.
  43. //
  44. // Simplifies working with a DPA.
  45. //-----------------------------------------------------------------------------
  46. template <typename T, typename D = CDpaDestroyer_Delete<T> >
  47. class CDpa
  48. {
  49. public:
  50. explicit CDpa(int cGrow = 4)
  51. : m_hdpa(DPA_Create(cGrow)) { }
  52. ~CDpa(void) { Destroy(); }
  53. bool IsValid(void) const { return NULL != m_hdpa; }
  54. int Count(void) const
  55. {
  56. return IsValid() ? DPA_GetPtrCount(m_hdpa) : 0;
  57. }
  58. const T* Get(int i) const
  59. {
  60. ASSERT(IsValid());
  61. ASSERT(i >= 0 && i < Count());
  62. return (const T*)DPA_GetPtr(m_hdpa, i);
  63. }
  64. T* Get(int i)
  65. {
  66. ASSERT(IsValid());
  67. ASSERT(i >= 0 && i < Count());
  68. return (T*)DPA_GetPtr(m_hdpa, i);
  69. }
  70. const T* operator [](int i) const
  71. {
  72. return Get(i);
  73. }
  74. T* operator [](int i)
  75. {
  76. return Get(i);
  77. }
  78. void Set(int i, T* p)
  79. {
  80. ASSERT(IsValid());
  81. ASSERT(i < Count());
  82. DPA_SetPtr(m_hdpa, i, p);
  83. }
  84. int Append(T* p)
  85. {
  86. ASSERT(IsValid());
  87. return DPA_AppendPtr(m_hdpa, p);
  88. }
  89. T* Remove(int i)
  90. {
  91. ASSERT(IsValid());
  92. ASSERT(i >= 0 && i < Count());
  93. return (T*)DPA_DeletePtr(m_hdpa, i);
  94. }
  95. private:
  96. HDPA m_hdpa;
  97. void Destroy(void)
  98. {
  99. if (NULL != m_hdpa)
  100. {
  101. const int c = Count();
  102. for (int i = 0; i < c; i++)
  103. {
  104. D::Destroy(Get(i));
  105. }
  106. DPA_Destroy(m_hdpa);
  107. m_hdpa = NULL;
  108. }
  109. }
  110. //
  111. // Prevent copy.
  112. //
  113. CDpa(const CDpa& rhs);
  114. CDpa& operator = (const CDpa& rhs);
  115. };
  116. #endif // __TASKUI_CDPA_H