nsSVGAttrTearoffTable.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  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. #ifndef NS_SVGATTRTEAROFFTABLE_H_
  6. #define NS_SVGATTRTEAROFFTABLE_H_
  7. #include "nsDataHashtable.h"
  8. #include "nsDebug.h"
  9. #include "nsHashKeys.h"
  10. /**
  11. * Global hashmap to associate internal SVG data types (e.g. nsSVGLength2) with
  12. * DOM tear-off objects (e.g. nsIDOMSVGLength). This allows us to always return
  13. * the same object for subsequent requests for DOM objects.
  14. *
  15. * We don't keep an owning reference to the tear-off objects so they are
  16. * responsible for removing themselves from this table when they die.
  17. */
  18. template<class SimpleType, class TearoffType>
  19. class nsSVGAttrTearoffTable
  20. {
  21. public:
  22. #ifdef DEBUG
  23. ~nsSVGAttrTearoffTable()
  24. {
  25. MOZ_ASSERT(!mTable, "Tear-off objects remain in hashtable at shutdown.");
  26. }
  27. #endif
  28. TearoffType* GetTearoff(SimpleType* aSimple);
  29. void AddTearoff(SimpleType* aSimple, TearoffType* aTearoff);
  30. void RemoveTearoff(SimpleType* aSimple);
  31. private:
  32. typedef nsPtrHashKey<SimpleType> SimpleTypePtrKey;
  33. typedef nsDataHashtable<SimpleTypePtrKey, TearoffType* > TearoffTable;
  34. TearoffTable* mTable;
  35. };
  36. template<class SimpleType, class TearoffType>
  37. TearoffType*
  38. nsSVGAttrTearoffTable<SimpleType, TearoffType>::GetTearoff(SimpleType* aSimple)
  39. {
  40. if (!mTable)
  41. return nullptr;
  42. TearoffType *tearoff = nullptr;
  43. #ifdef DEBUG
  44. bool found =
  45. #endif
  46. mTable->Get(aSimple, &tearoff);
  47. MOZ_ASSERT(!found || tearoff,
  48. "null pointer stored in attribute tear-off map");
  49. return tearoff;
  50. }
  51. template<class SimpleType, class TearoffType>
  52. void
  53. nsSVGAttrTearoffTable<SimpleType, TearoffType>::AddTearoff(SimpleType* aSimple,
  54. TearoffType* aTearoff)
  55. {
  56. if (!mTable) {
  57. mTable = new TearoffTable;
  58. }
  59. // We shouldn't be adding a tear-off if there already is one. If that happens,
  60. // something is wrong.
  61. if (mTable->Get(aSimple, nullptr)) {
  62. MOZ_ASSERT(false, "There is already a tear-off for this object.");
  63. return;
  64. }
  65. mTable->Put(aSimple, aTearoff);
  66. }
  67. template<class SimpleType, class TearoffType>
  68. void
  69. nsSVGAttrTearoffTable<SimpleType, TearoffType>::RemoveTearoff(
  70. SimpleType* aSimple)
  71. {
  72. if (!mTable) {
  73. // Perhaps something happened in between creating the SimpleType object and
  74. // registering it
  75. return;
  76. }
  77. mTable->Remove(aSimple);
  78. if (mTable->Count() == 0) {
  79. delete mTable;
  80. mTable = nullptr;
  81. }
  82. }
  83. #endif // NS_SVGATTRTEAROFFTABLE_H_