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