Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / content / xul / templates / src / nsRDFBinding.h
blob2a8ab79973fc1279ff54e6d83462d3bc13acecbb
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
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/
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.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is Neil Deakin
18 * Portions created by the Initial Developer are Copyright (C) 2005
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either of the GNU General Public License Version 2 or later (the "GPL"),
25 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #ifndef nsRDFBinding_h__
38 #define nsRDFBinding_h__
40 #include "nsAutoPtr.h"
41 #include "nsIAtom.h"
42 #include "nsIRDFResource.h"
44 class nsXULTemplateResultRDF;
45 class nsBindingValues;
48 * Classes related to storing bindings for RDF handling.
52 * a <binding> descriptors
54 class RDFBinding {
56 public:
58 nsCOMPtr<nsIAtom> mSubjectVariable;
59 nsCOMPtr<nsIRDFResource> mPredicate;
60 nsCOMPtr<nsIAtom> mTargetVariable;
62 // indicates whether a binding is dependant on the result from a
63 // previous binding
64 PRBool mHasDependency;
66 RDFBinding* mNext;
68 private:
70 friend class RDFBindingSet;
72 RDFBinding(nsIAtom* aSubjectVariable,
73 nsIRDFResource* aPredicate,
74 nsIAtom* aTargetVariable)
75 : mSubjectVariable(aSubjectVariable),
76 mPredicate(aPredicate),
77 mTargetVariable(aTargetVariable),
78 mHasDependency(PR_FALSE),
79 mNext(nsnull)
81 MOZ_COUNT_CTOR(RDFBinding);
84 ~RDFBinding()
86 MOZ_COUNT_DTOR(RDFBinding);
91 * a collection of <binding> descriptors. This object is refcounted by
92 * nsBindingValues objects and the query processor.
94 class RDFBindingSet
96 protected:
98 // results hold a reference to a binding set in their nsBindingValues fields
99 PRInt32 mRefCnt;
101 // the number of bindings
102 PRInt32 mCount;
104 // pointer to the first binding in a linked list
105 RDFBinding* mFirst;
107 public:
109 RDFBindingSet()
110 : mRefCnt(0),
111 mCount(0),
112 mFirst(nsnull)
114 MOZ_COUNT_CTOR(RDFBindingSet);
117 ~RDFBindingSet();
119 PRInt32 AddRef() {
120 mRefCnt++;
121 NS_LOG_ADDREF(this, mRefCnt, "RDFBindingSet", sizeof(*this));
122 return mRefCnt;
125 PRInt32 Release()
127 PRInt32 refcnt = --mRefCnt;
128 NS_LOG_RELEASE(this, refcnt, "RDFBindingSet");
129 if (refcnt == 0) delete this;
130 return refcnt;
133 PRInt32 Count() const { return mCount; }
136 * Add a binding (aRef -> aPredicate -> aVar) to the set
138 nsresult
139 AddBinding(nsIAtom* aVar, nsIAtom* aRef, nsIRDFResource* aPredicate);
142 * Return true if the binding set contains a binding which would cause
143 * the result to need resynchronizing for an RDF triple. The member
144 * variable may be supplied as an optimization since bindings most
145 * commonly use the member variable as the subject. If aMemberVariable
146 * is set, aSubject must be the value of the member variable for the
147 * result. The supplied binding values aBindingValues must be values
148 * using this binding set (that is aBindingValues->GetBindingSet() == this)
150 * @param aSubject subject of the RDF triple
151 * @param aPredicate predicate of the RDF triple
152 * @param aTarget target of the RDF triple
153 * @param aMemberVariable member variable for the query for the binding
154 * @param aResult result to synchronize
155 * @param aBindingValues the values for the bindings for the result
157 PRBool
158 SyncAssignments(nsIRDFResource* aSubject,
159 nsIRDFResource* aPredicate,
160 nsIRDFNode* aTarget,
161 nsIAtom* aMemberVariable,
162 nsXULTemplateResultRDF* aResult,
163 nsBindingValues& aBindingValues);
166 * The query processor maintains a map of subjects to an array of results.
167 * This is used such that when a new assertion is added to the RDF graph,
168 * the results associated with the subject of that triple may be checked
169 * to see if their bindings have changed. The AddDependencies method adds
170 * these subject dependencies to the map.
172 void
173 AddDependencies(nsIRDFResource* aSubject,
174 nsXULTemplateResultRDF* aResult);
177 * Remove the results from the dependencies map when results are deleted.
179 void
180 RemoveDependencies(nsIRDFResource* aSubject,
181 nsXULTemplateResultRDF* aResult);
184 * The nsBindingValues classes stores an array of values, one for each
185 * target symbol that could be set by the bindings in the set.
186 * LookupTargetIndex determines the index into the array for a given
187 * target symbol.
189 PRInt32
190 LookupTargetIndex(nsIAtom* aTargetVariable, RDFBinding** aBinding);
194 * A set of values of bindings. This object is used once per result.
195 * This stores a reference to the binding set and an array of node values.
196 * Since the binding set is used once per query and the values are
197 * used once per result, we reduce size by only storing the value array's
198 * length in the binding set. This is possible since the array is always
199 * a fixed length for a particular binding set.
201 * XXX ndeakin We may want to revisit this later since it makes the code
202 * more complicated.
204 class nsBindingValues
206 protected:
208 // the binding set
209 nsRefPtr<RDFBindingSet> mBindings;
212 * A set of values for variable bindings. To look up a binding value,
213 * scan through the binding set in mBindings for the right target atom.
214 * Its index will correspond to the index in this array. The size of this
215 * array is determined by the RDFBindingSet's Count().
217 nsCOMPtr<nsIRDFNode>* mValues;
219 public:
221 nsBindingValues()
222 : mBindings(nsnull),
223 mValues(nsnull)
225 MOZ_COUNT_CTOR(nsBindingValues);
228 ~nsBindingValues();
232 * Clear the binding set, to be called when the nsBindingValues is deleted
233 * or a new binding set is being set.
235 void ClearBindingSet();
237 RDFBindingSet* GetBindingSet() { return mBindings; }
240 * Set the binding set to use. This needs to be called once a rule matches
241 * since it is then known which bindings will apply.
243 nsresult SetBindingSet(RDFBindingSet* aBindings);
245 nsCOMPtr<nsIRDFNode>* ValuesArray() { return mValues; }
248 * Retrieve the assignment for a particular variable
250 void
251 GetAssignmentFor(nsXULTemplateResultRDF* aResult,
252 nsIAtom* aVar,
253 nsIRDFNode** aValue);
256 * Remove depenedencies the bindings have on particular resources
258 void
259 RemoveDependencies(nsIRDFResource* aSubject,
260 nsXULTemplateResultRDF* aResult);
263 #endif // nsRDFBinding_h__