Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / content / xul / templates / src / nsTreeRows.h
blobbf1beec28121ab4be32f131de966cc63b6d55109
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 Communicator client code.
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.
22 * Contributor(s):
23 * Chris Waterson <waterson@netscape.com>
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.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef nsTreeRows_h__
40 #define nsTreeRows_h__
42 #include "nsCOMPtr.h"
43 #include "nsTArray.h"
44 #include "pldhash.h"
45 #include "nsIXULTemplateResult.h"
46 #include "nsTemplateMatch.h"
47 #include "nsIRDFResource.h"
50 /**
51 * This class maintains the state of the XUL tree builder's
52 * rows. It maps a row number to the nsTemplateMatch object that
53 * populates the row.
55 class nsTreeRows
57 public:
58 class iterator;
59 friend class iterator;
61 enum Direction { eDirection_Forwards = +1, eDirection_Backwards = -1 };
63 enum ContainerType {
64 eContainerType_Unknown = 0,
65 eContainerType_Noncontainer = 1,
66 eContainerType_Container = 2
69 enum ContainerState {
70 eContainerState_Unknown = 0,
71 eContainerState_Open = 1,
72 eContainerState_Closed = 2
75 enum ContainerFill {
76 eContainerFill_Unknown = 0,
77 eContainerFill_Empty = 1,
78 eContainerFill_Nonempty = 2
81 class Subtree;
83 /**
84 * A row in the tree. Contains the match that the row
85 * corresponds to, and a pointer to the row's subtree, if there
86 * are any.
88 struct Row {
89 nsTemplateMatch* mMatch;
90 ContainerType mContainerType : 4;
91 ContainerState mContainerState : 4;
92 ContainerFill mContainerFill : 4;
94 Subtree* mSubtree; // XXX eventually move to hashtable
97 /**
98 * A subtree in the tree. A subtree contains rows, which may
99 * contain other subtrees.
101 class Subtree {
102 protected:
103 friend class nsTreeRows; // so that it can access members, for now
106 * The parent subtree; null if we're the root
108 Subtree* mParent;
111 * The number of immediate children in this subtree
113 PRInt32 mCount;
116 * The capacity of the subtree
118 PRInt32 mCapacity;
121 * The total number of rows in this subtree, recursively
122 * including child subtrees.
124 PRInt32 mSubtreeSize;
127 * The array of rows in the subtree
129 Row* mRows;
131 public:
133 * Creates a subtree with the specified parent.
135 Subtree(Subtree* aParent)
136 : mParent(aParent),
137 mCount(0),
138 mCapacity(0),
139 mSubtreeSize(0),
140 mRows(nsnull) {}
142 ~Subtree();
145 * Return the number of immediate child rows in the subtree
147 PRInt32 Count() const { return mCount; }
150 * Return the number of rows in this subtree, as well as all
151 * the subtrees it contains.
153 PRInt32 GetSubtreeSize() const { return mSubtreeSize; }
156 * Retrieve the immediate child row at the specified index.
158 const Row& operator[](PRInt32 aIndex) const {
159 NS_PRECONDITION(aIndex >= 0 && aIndex < mCount, "bad index");
160 return mRows[aIndex]; }
163 * Retrieve the immediate row at the specified index.
165 Row& operator[](PRInt32 aIndex) {
166 NS_PRECONDITION(aIndex >= 0 && aIndex < mCount, "bad index");
167 return mRows[aIndex]; }
170 * Remove all rows from the subtree.
172 void Clear();
174 protected:
176 * Insert an immediate child row at the specified index.
178 iterator InsertRowAt(nsTemplateMatch* aMatch, PRInt32 aIndex);
181 * Remove an immediate child row from the specified index.
183 void RemoveRowAt(PRInt32 aChildIndex);
186 friend class Subtree;
188 protected:
190 * A link in the path through the view's tree.
192 struct Link {
193 Subtree* mParent;
194 PRInt32 mChildIndex;
196 Link&
197 operator=(const Link& aLink) {
198 mParent = aLink.mParent;
199 mChildIndex = aLink.mChildIndex;
200 return *this; }
202 PRBool
203 operator==(const Link& aLink) const {
204 return (mParent == aLink.mParent)
205 && (mChildIndex == aLink.mChildIndex); }
207 Subtree* GetParent() { return mParent; }
208 const Subtree* GetParent() const { return mParent; }
210 PRInt32 GetChildIndex() const { return mChildIndex; }
212 Row& GetRow() { return (*mParent)[mChildIndex]; }
213 const Row& GetRow() const { return (*mParent)[mChildIndex]; }
216 public:
218 * An iterator that can be used to traverse the tree view.
220 class iterator {
221 protected:
222 PRInt32 mRowIndex;
223 nsAutoTArray<Link, 8> mLink;
225 void Next();
226 void Prev();
228 friend class Subtree; // so InsertRowAt can initialize us
229 friend class nsTreeRows; // so nsTreeRows can initialize us
232 * Used by operator[]() to initialize an iterator.
234 void Append(Subtree* aParent, PRInt32 aChildIndex);
237 * Used by InsertRowAt() to initialize an iterator.
239 void Push(Subtree *aParent, PRInt32 aChildIndex);
242 * Used by operator[]() and InsertRowAt() to initialize an iterator.
244 void SetRowIndex(PRInt32 aRowIndex) { mRowIndex = aRowIndex; }
247 * Handy accessors to the top element.
249 Link& GetTop() { return mLink[mLink.Length() - 1]; }
250 const Link& GetTop() const { return mLink[mLink.Length() - 1]; }
252 public:
253 iterator() : mRowIndex(-1) {}
255 iterator(const iterator& aIterator);
256 iterator& operator=(const iterator& aIterator);
258 PRBool operator==(const iterator& aIterator) const;
260 PRBool operator!=(const iterator& aIterator) const {
261 return !aIterator.operator==(*this); }
263 const Row& operator*() const { return GetTop().GetRow(); }
264 Row& operator*() { return GetTop().GetRow(); }
266 const Row* operator->() const { return &(GetTop().GetRow()); }
267 Row* operator->() { return &(GetTop().GetRow()); }
269 iterator& operator++() { Next(); return *this; }
270 iterator operator++(int) { iterator temp(*this); Next(); return temp; }
271 iterator& operator--() { Prev(); return *this; }
272 iterator operator--(int) { iterator temp(*this); Prev(); return temp; }
275 * Return the current parent link
277 Subtree* GetParent() { return GetTop().GetParent(); }
279 const Subtree* GetParent() const { return GetTop().GetParent(); }
282 * Return the current child index
284 PRInt32 GetChildIndex() const { return GetTop().GetChildIndex(); }
287 * Return the depth of the path the iterator is maintaining
288 * into the tree.
290 PRInt32 GetDepth() const { return mLink.Length(); }
293 * Return the current row index of the iterator
295 PRInt32 GetRowIndex() const { return mRowIndex; }
298 * Pop the iterator up a level.
300 iterator& Pop() { mLink.SetLength(GetDepth() - 1); return *this; }
304 * Retrieve the first element in the view
306 iterator First();
309 * Retrieve (one past) the last element in the view
311 iterator Last();
314 * Find the row that contains the given resource
316 iterator FindByResource(nsIRDFResource* aResource);
319 * Find the row that contains the result
321 iterator Find(nsIXULTemplateResult* aResult);
324 * Retrieve the ith element in the view
326 iterator operator[](PRInt32 aIndex);
328 nsTreeRows() : mRoot(nsnull) {}
329 ~nsTreeRows() {}
332 * Ensure that a child subtree exists within the specified parent
333 * at the specified child index within the parent. (In other
334 * words, create a subtree if one doesn't already exist.)
336 Subtree*
337 EnsureSubtreeFor(Subtree* aParent, PRInt32 aChildIndex);
340 * Ensure that a child subtree exists at the iterator's position.
342 Subtree*
343 EnsureSubtreeFor(iterator& aIterator) {
344 return EnsureSubtreeFor(aIterator.GetParent(),
345 aIterator.GetChildIndex()); }
348 * Get the child subtree for the specified parent at the specified
349 * child index. Optionally return the child subtree's size. Will
350 * return `null' if no subtree exists.
352 Subtree*
353 GetSubtreeFor(const Subtree* aParent,
354 PRInt32 aChildIndex,
355 PRInt32* aSubtreeSize = nsnull);
358 * Retrieve the size of the subtree within the specified parent.
360 PRInt32
361 GetSubtreeSizeFor(const Subtree* aParent,
362 PRInt32 aChildIndex) {
363 PRInt32 size;
364 GetSubtreeFor(aParent, aChildIndex, &size);
365 return size; }
368 * Retrieve the size of the subtree within the specified parent.
370 PRInt32
371 GetSubtreeSizeFor(const iterator& aIterator) {
372 PRInt32 size;
373 GetSubtreeFor(aIterator.GetParent(), aIterator.GetChildIndex(), &size);
374 return size; }
377 * Remove the specified subtree for a row, leaving the row itself
378 * intact.
380 void
381 RemoveSubtreeFor(Subtree* aParent, PRInt32 aChildIndex);
384 * Remove the specified subtree for a row, leaving the row itself
385 * intact.
387 void
388 RemoveSubtreeFor(iterator& aIterator) {
389 RemoveSubtreeFor(aIterator.GetParent(), aIterator.GetChildIndex()); }
392 * Remove the specified row from the view
394 PRInt32
395 RemoveRowAt(iterator& aIterator) {
396 iterator temp = aIterator--;
397 Subtree* parent = temp.GetParent();
398 parent->RemoveRowAt(temp.GetChildIndex());
399 InvalidateCachedRow();
400 return parent->Count(); }
403 * Insert a new match into the view
405 iterator
406 InsertRowAt(nsTemplateMatch* aMatch, Subtree* aSubtree, PRInt32 aChildIndex) {
407 InvalidateCachedRow();
408 return aSubtree->InsertRowAt(aMatch, aChildIndex); }
411 * Raw access to the rows; e.g., for sorting.
413 Row*
414 GetRowsFor(Subtree* aSubtree) { return aSubtree->mRows; }
417 * Remove all of the rows
419 void Clear();
422 * Return the total number of rows in the tree view.
424 PRInt32 Count() const { return mRoot.GetSubtreeSize(); }
427 * Retrieve the root subtree
429 Subtree* GetRoot() { return &mRoot; }
432 * Set the root resource for the view
434 void SetRootResource(nsIRDFResource* aResource) {
435 mRootResource = aResource; }
438 * Retrieve the root resource for the view
440 nsIRDFResource* GetRootResource() {
441 return mRootResource.get(); }
444 * Invalidate the cached row; e.g., because the view has changed
445 * in a way that would corrupt the iterator.
447 void
448 InvalidateCachedRow() { mLastRow = iterator(); }
450 protected:
452 * The root subtree.
454 Subtree mRoot;
457 * The root resource for the view
459 nsCOMPtr<nsIRDFResource> mRootResource;
462 * The last row that was asked for by operator[]. By remembering
463 * this, we can usually avoid the O(n) search through the row
464 * array to find the row at the specified index.
466 iterator mLastRow;
470 #endif // nsTreeRows_h__