Back to index

lightning-sunbird  0.9+nobinonly
nsChildIterator.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 // vim:cindent:ts=2:et:sw=2:
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsCOMPtr.h"
00040 #include "nsIContent.h"
00041 #include "nsIDOMNodeList.h"
00042 #include "nsIDOMNode.h"
00043 
00050 class ChildIterator
00051 {
00052 protected:
00053   nsCOMPtr<nsIContent> mContent;
00054   PRUint32 mIndex;
00055   nsCOMPtr<nsIDOMNodeList> mNodes;
00056 
00057 public:
00058   ChildIterator()
00059     : mIndex(0) {}
00060 
00061   ChildIterator(const ChildIterator& aOther)
00062     : mContent(aOther.mContent),
00063       mIndex(aOther.mIndex),
00064       mNodes(aOther.mNodes) {}
00065 
00066   ChildIterator& operator=(const ChildIterator& aOther) {
00067     mContent = aOther.mContent;
00068     mIndex = aOther.mIndex;
00069     mNodes = aOther.mNodes;
00070     return *this;
00071   }
00072 
00073   ChildIterator& operator++() {
00074     ++mIndex;
00075     return *this;
00076   }
00077 
00078   ChildIterator operator++(int) {
00079     ChildIterator result(*this);
00080     ++mIndex;
00081     return result;
00082   }
00083 
00084   ChildIterator& operator--() {
00085     --mIndex;
00086     return *this;
00087   }
00088 
00089   ChildIterator operator--(int) {
00090     ChildIterator result(*this);
00091     --mIndex;
00092     return result;
00093   }
00094 
00095   already_AddRefed<nsIContent> get() const {
00096     nsIContent* result = nsnull;
00097     if (mNodes) {
00098       nsCOMPtr<nsIDOMNode> node;
00099       mNodes->Item(mIndex, getter_AddRefs(node));
00100       CallQueryInterface(node, &result);
00101     } else {
00102       result = mContent->GetChildAt(PRInt32(mIndex));
00103       NS_IF_ADDREF(result);
00104     }
00105 
00106     return result;
00107   }
00108 
00109   already_AddRefed<nsIContent> operator*() const { return get(); }
00110 
00111   PRBool operator==(const ChildIterator& aOther) const {
00112     return mContent == aOther.mContent && mIndex == aOther.mIndex;
00113   }
00114 
00115   PRBool operator!=(const ChildIterator& aOther) const {
00116     return !aOther.operator==(*this);
00117   }
00118 
00119   PRUint32 index() {
00120     return mIndex;
00121   }
00122 
00123   void seek(PRUint32 aIndex) {
00124     // Make sure that aIndex is reasonable.  This should be |#ifdef
00125     // DEBUG|, but we need these numbers for the temporary workaround
00126     // for bug 133219.
00127     PRUint32 length;
00128     if (mNodes)
00129       mNodes->GetLength(&length);
00130     else
00131       length = mContent->GetChildCount();
00132 
00133     NS_ASSERTION(PRInt32(aIndex) >= 0 && aIndex <= length, "out of bounds");
00134 
00135     // Temporary workaround for bug 133219.
00136     if (aIndex > length)
00137       aIndex = length;
00138 
00139     mIndex = aIndex;
00140   }
00141 
00147   static nsresult Init(nsIContent*    aContent,
00148                        ChildIterator* aFirst,
00149                        ChildIterator* aLast);
00150 };