nsResourceSet.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsResourceSet.h"
  6. nsResourceSet::nsResourceSet(const nsResourceSet& aResourceSet)
  7. : mResources(nullptr),
  8. mCount(0),
  9. mCapacity(0)
  10. {
  11. ConstIterator last = aResourceSet.Last();
  12. for (ConstIterator resource = aResourceSet.First(); resource != last; ++resource)
  13. Add(*resource);
  14. }
  15. nsResourceSet&
  16. nsResourceSet::operator=(const nsResourceSet& aResourceSet)
  17. {
  18. Clear();
  19. ConstIterator last = aResourceSet.Last();
  20. for (ConstIterator resource = aResourceSet.First(); resource != last; ++resource)
  21. Add(*resource);
  22. return *this;
  23. }
  24. nsResourceSet::~nsResourceSet()
  25. {
  26. MOZ_COUNT_DTOR(nsResourceSet);
  27. Clear();
  28. delete[] mResources;
  29. }
  30. nsresult
  31. nsResourceSet::Clear()
  32. {
  33. while (--mCount >= 0) {
  34. NS_RELEASE(mResources[mCount]);
  35. }
  36. mCount = 0;
  37. return NS_OK;
  38. }
  39. nsresult
  40. nsResourceSet::Add(nsIRDFResource* aResource)
  41. {
  42. NS_PRECONDITION(aResource != nullptr, "null ptr");
  43. if (! aResource)
  44. return NS_ERROR_NULL_POINTER;
  45. if (Contains(aResource))
  46. return NS_OK;
  47. if (mCount >= mCapacity) {
  48. int32_t capacity = mCapacity + 4;
  49. nsIRDFResource** resources = new nsIRDFResource*[capacity];
  50. for (int32_t i = mCount - 1; i >= 0; --i)
  51. resources[i] = mResources[i];
  52. delete[] mResources;
  53. mResources = resources;
  54. mCapacity = capacity;
  55. }
  56. mResources[mCount++] = aResource;
  57. NS_ADDREF(aResource);
  58. return NS_OK;
  59. }
  60. void
  61. nsResourceSet::Remove(nsIRDFResource* aProperty)
  62. {
  63. bool found = false;
  64. nsIRDFResource** res = mResources;
  65. nsIRDFResource** limit = mResources + mCount;
  66. while (res < limit) {
  67. if (found) {
  68. *(res - 1) = *res;
  69. }
  70. else if (*res == aProperty) {
  71. NS_RELEASE(*res);
  72. found = true;
  73. }
  74. ++res;
  75. }
  76. if (found)
  77. --mCount;
  78. }
  79. bool
  80. nsResourceSet::Contains(nsIRDFResource* aResource) const
  81. {
  82. for (int32_t i = mCount - 1; i >= 0; --i) {
  83. if (mResources[i] == aResource)
  84. return true;
  85. }
  86. return false;
  87. }