nsTemplateMatch.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #ifndef nsTemplateMatch_h__
  6. #define nsTemplateMatch_h__
  7. #include "mozilla/Attributes.h"
  8. #include "nsIContent.h"
  9. #include "nsIXULTemplateQueryProcessor.h"
  10. #include "nsIXULTemplateResult.h"
  11. #include "nsRuleNetwork.h"
  12. /**
  13. * A match object, where each match object is associated with one result.
  14. * There will be one match list for each unique id generated. However, since
  15. * there are multiple querysets and each may generate results with the same
  16. * id, they are all chained together in a linked list, ordered in the same
  17. * order as the respective <queryset> elements they were generated from.
  18. * A match can be identified by the container and id. The id is retrievable
  19. * from the result.
  20. *
  21. * Only one match per container and id pair is active at a time, but which
  22. * match is active may change as new results are added or removed. When a
  23. * match is active, content is generated for that match.
  24. *
  25. * Matches are stored and owned by the mMatchToMap hash in the template
  26. * builder.
  27. */
  28. class nsTemplateRule;
  29. class nsTemplateQuerySet;
  30. class nsTemplateMatch {
  31. private:
  32. // Hide so that only Create() and Destroy() can be used to
  33. // allocate and deallocate from the heap
  34. void* operator new(size_t) CPP_THROW_NEW { MOZ_ASSERT(0); return nullptr; }
  35. void operator delete(void*, size_t) { MOZ_ASSERT(0); }
  36. public:
  37. nsTemplateMatch(uint16_t aQuerySetPriority,
  38. nsIXULTemplateResult* aResult,
  39. nsIContent* aContainer)
  40. : mRuleIndex(-1),
  41. mQuerySetPriority(aQuerySetPriority),
  42. mContainer(aContainer),
  43. mResult(aResult),
  44. mNext(nullptr)
  45. {
  46. MOZ_COUNT_CTOR(nsTemplateMatch);
  47. }
  48. ~nsTemplateMatch()
  49. {
  50. MOZ_COUNT_DTOR(nsTemplateMatch);
  51. }
  52. static nsTemplateMatch*
  53. Create(uint16_t aQuerySetPriority,
  54. nsIXULTemplateResult* aResult,
  55. nsIContent* aContainer) {
  56. return ::new nsTemplateMatch(aQuerySetPriority, aResult, aContainer);
  57. }
  58. static void Destroy(nsTemplateMatch*& aMatch, bool aRemoveResult);
  59. // return true if the the match is active, and has generated output
  60. bool IsActive() {
  61. return mRuleIndex >= 0;
  62. }
  63. // indicate that a rule is no longer active, used when a query with a
  64. // lower priority has overriden the match
  65. void SetInactive() {
  66. mRuleIndex = -1;
  67. }
  68. // return matching rule index
  69. int16_t RuleIndex() {
  70. return mRuleIndex;
  71. }
  72. // return priority of query set
  73. uint16_t QuerySetPriority() {
  74. return mQuerySetPriority;
  75. }
  76. // return container, not addrefed. May be null.
  77. nsIContent* GetContainer() {
  78. return mContainer;
  79. }
  80. nsresult RuleMatched(nsTemplateQuerySet* aQuerySet,
  81. nsTemplateRule* aRule,
  82. int16_t aRuleIndex,
  83. nsIXULTemplateResult* aResult);
  84. private:
  85. /**
  86. * The index of the rule that matched, or -1 if the match is not active.
  87. */
  88. int16_t mRuleIndex;
  89. /**
  90. * The priority of the queryset for this rule
  91. */
  92. uint16_t mQuerySetPriority;
  93. /**
  94. * The container the content generated for the match is inside.
  95. */
  96. nsCOMPtr<nsIContent> mContainer;
  97. public:
  98. /**
  99. * The result associated with this match
  100. */
  101. nsCOMPtr<nsIXULTemplateResult> mResult;
  102. /**
  103. * Matches are stored in a linked list, in priority order. This first
  104. * match that has a rule set (mRule) is the active match and generates
  105. * content. The next match is owned by the builder, which will delete
  106. * template matches when needed.
  107. */
  108. nsTemplateMatch *mNext;
  109. private:
  110. nsTemplateMatch(const nsTemplateMatch& aMatch) = delete;
  111. void operator=(const nsTemplateMatch& aMatch) = delete;
  112. };
  113. #endif // nsTemplateMatch_h__