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.

128 lines
3.0 KiB

  1. #include <windows.h>
  2. #include <wbemidl.h>
  3. #include <wchar.h>
  4. #include "Containers.h"
  5. HRESULT LikeTemplateList::Add(IWbemClassObject* pObj)
  6. {
  7. HRESULT hr = WBEM_S_NO_ERROR;
  8. if (pObj)
  9. {
  10. pObj->AddRef();
  11. m_objects.Add(pObj);
  12. }
  13. else
  14. hr = WBEM_E_INVALID_PARAMETER;
  15. return hr;
  16. }
  17. LikeTemplateList::~LikeTemplateList()
  18. {
  19. for (int i = 0; i < m_objects.Size(); i++)
  20. if (m_objects[i])
  21. ((IUnknown*)m_objects[i])->Release();
  22. }
  23. // returns safearray of objects
  24. // callers responsibility to destroy
  25. SAFEARRAY* LikeTemplateList::GetArray()
  26. {
  27. SAFEARRAYBOUND bounds = {m_objects.Size(), 0};
  28. SAFEARRAY* pArray = SafeArrayCreate(VT_UNKNOWN, 1, &bounds);
  29. if (pArray)
  30. {
  31. IUnknown** pObj;
  32. if (FAILED(SafeArrayAccessData(pArray, (void**) &pObj)))
  33. {
  34. SafeArrayDestroy(pArray);
  35. pArray = NULL;
  36. }
  37. else
  38. {
  39. for (int i = 0; i < m_objects.Size(); i++)
  40. {
  41. pObj[i] = (IUnknown*)m_objects[i];
  42. ((IUnknown*)pObj[i])->AddRef();
  43. }
  44. SafeArrayUnaccessData(pArray);
  45. }
  46. }
  47. return pArray;
  48. }
  49. TemplateMap::~TemplateMap()
  50. {
  51. for (int i = 0; i < m_lists.Size(); i++)
  52. if (m_lists[i])
  53. delete m_lists[i];
  54. }
  55. // inserts object into proper list
  56. // creates new list entry if needed
  57. HRESULT TemplateMap::Add(WCHAR* pPath, IWbemClassObject* pObj)
  58. {
  59. HRESULT hr = WBEM_S_NO_ERROR;
  60. bool bItPlaced = false;
  61. for (int i = 0; (i < m_lists.Size()) && !bItPlaced; i++)
  62. {
  63. int nCompare = ((LikeTemplateList*)m_lists[i])->Compare(pPath);
  64. // if is less than current position
  65. // insert a new one at current position
  66. if (nCompare < 0)
  67. {
  68. bItPlaced = true;
  69. LikeTemplateList* pList = new LikeTemplateList(pPath);
  70. if (!pList)
  71. hr = WBEM_E_OUT_OF_MEMORY;
  72. else
  73. {
  74. if (CFlexArray::no_error == m_lists.InsertAt(i, pList))
  75. ((LikeTemplateList*)m_lists[i])->Add(pObj);
  76. else
  77. hr = WBEM_E_FAILED;
  78. }
  79. }
  80. // equal - add it to existing list
  81. else if (nCompare == 0)
  82. {
  83. bItPlaced = true;
  84. hr = ((LikeTemplateList*)m_lists[i])->Add(pObj);
  85. }
  86. }
  87. // fell off end of list w/o placing it - add to end
  88. if (!bItPlaced && (SUCCEEDED(hr)))
  89. {
  90. LikeTemplateList* pList = new LikeTemplateList(pPath);
  91. if (pList)
  92. {
  93. pList->Add(pObj);
  94. m_lists.Add(pList);
  95. }
  96. else
  97. hr = WBEM_E_OUT_OF_MEMORY;
  98. }
  99. return hr;
  100. }
  101. // cookie is index into array
  102. // meant to be used as a "get next"
  103. // will increment cookie before returning;
  104. // set to zero to get first one
  105. SAFEARRAY* TemplateMap::GetTemplateList(int& cookie)
  106. {
  107. if (cookie < m_lists.Size())
  108. return ((LikeTemplateList*)m_lists[cookie++])->GetArray();
  109. else
  110. return NULL;
  111. }