1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Communicator client code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Chris Waterson <waterson@netscape.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either of the GNU General Public License Version 2 or later (the "GPL"),
27 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef nsTemplateMatch_h__
40 : #define nsTemplateMatch_h__
41 :
42 : #include "nsFixedSizeAllocator.h"
43 : #include "nsIContent.h"
44 : #include "nsIXULTemplateQueryProcessor.h"
45 : #include "nsIXULTemplateResult.h"
46 : #include "nsRuleNetwork.h"
47 : #include NEW_H
48 :
49 : /**
50 : * A match object, where each match object is associated with one result.
51 : * There will be one match list for each unique id generated. However, since
52 : * there are multiple querysets and each may generate results with the same
53 : * id, they are all chained together in a linked list, ordered in the same
54 : * order as the respective <queryset> elements they were generated from.
55 : * A match can be identified by the container and id. The id is retrievable
56 : * from the result.
57 : *
58 : * Only one match per container and id pair is active at a time, but which
59 : * match is active may change as new results are added or removed. When a
60 : * match is active, content is generated for that match.
61 : *
62 : * Matches are stored and owned by the mMatchToMap hash in the template
63 : * builder.
64 : */
65 :
66 : class nsTemplateRule;
67 : class nsTemplateQuerySet;
68 :
69 : class nsTemplateMatch {
70 : private:
71 : // Hide so that only Create() and Destroy() can be used to
72 : // allocate and deallocate from the heap
73 : void* operator new(size_t) CPP_THROW_NEW { return 0; }
74 : void operator delete(void*, size_t) {}
75 :
76 : public:
77 0 : nsTemplateMatch(PRUint16 aQuerySetPriority,
78 : nsIXULTemplateResult* aResult,
79 : nsIContent* aContainer)
80 : : mRuleIndex(-1),
81 : mQuerySetPriority(aQuerySetPriority),
82 : mContainer(aContainer),
83 : mResult(aResult),
84 0 : mNext(nsnull)
85 : {
86 0 : MOZ_COUNT_CTOR(nsTemplateMatch);
87 0 : }
88 :
89 0 : ~nsTemplateMatch()
90 0 : {
91 0 : MOZ_COUNT_DTOR(nsTemplateMatch);
92 0 : }
93 :
94 : static nsTemplateMatch*
95 0 : Create(nsFixedSizeAllocator& aPool,
96 : PRUint16 aQuerySetPriority,
97 : nsIXULTemplateResult* aResult,
98 : nsIContent* aContainer) {
99 0 : void* place = aPool.Alloc(sizeof(nsTemplateMatch));
100 : return place ? ::new (place) nsTemplateMatch(aQuerySetPriority,
101 0 : aResult, aContainer)
102 0 : : nsnull; }
103 :
104 : static void Destroy(nsFixedSizeAllocator& aPool,
105 : nsTemplateMatch*& aMatch,
106 : bool aRemoveResult);
107 :
108 : // return true if the the match is active, and has generated output
109 0 : bool IsActive() {
110 0 : return mRuleIndex >= 0;
111 : }
112 :
113 : // indicate that a rule is no longer active, used when a query with a
114 : // lower priority has overriden the match
115 0 : void SetInactive() {
116 0 : mRuleIndex = -1;
117 0 : }
118 :
119 : // return matching rule index
120 0 : PRInt16 RuleIndex() {
121 0 : return mRuleIndex;
122 : }
123 :
124 : // return priority of query set
125 0 : PRUint16 QuerySetPriority() {
126 0 : return mQuerySetPriority;
127 : }
128 :
129 : // return container, not addrefed. May be null.
130 0 : nsIContent* GetContainer() {
131 0 : return mContainer;
132 : }
133 :
134 : nsresult RuleMatched(nsTemplateQuerySet* aQuerySet,
135 : nsTemplateRule* aRule,
136 : PRInt16 aRuleIndex,
137 : nsIXULTemplateResult* aResult);
138 :
139 : private:
140 :
141 : /**
142 : * The index of the rule that matched, or -1 if the match is not active.
143 : */
144 : PRInt16 mRuleIndex;
145 :
146 : /**
147 : * The priority of the queryset for this rule
148 : */
149 : PRUint16 mQuerySetPriority;
150 :
151 : /**
152 : * The container the content generated for the match is inside.
153 : */
154 : nsCOMPtr<nsIContent> mContainer;
155 :
156 : public:
157 :
158 : /**
159 : * The result associated with this match
160 : */
161 : nsCOMPtr<nsIXULTemplateResult> mResult;
162 :
163 : /**
164 : * Matches are stored in a linked list, in priority order. This first
165 : * match that has a rule set (mRule) is the active match and generates
166 : * content. The next match is owned by the builder, which will delete
167 : * template matches when needed.
168 : */
169 : nsTemplateMatch *mNext;
170 :
171 : private:
172 :
173 : nsTemplateMatch(const nsTemplateMatch& aMatch); // not to be implemented
174 : void operator=(const nsTemplateMatch& aMatch); // not to be implemented
175 : };
176 :
177 : #endif // nsTemplateMatch_h__
178 :
|