Back to index

lightning-sunbird  0.9+nobinonly
nsResourceSet.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Chris Waterson <waterson@netscape.com>
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 "nsResourceSet.h"
00040 
00041 nsResourceSet::nsResourceSet(const nsResourceSet& aResourceSet)
00042     : mResources(nsnull),
00043       mCount(0),
00044       mCapacity(0)
00045 {
00046     ConstIterator last = aResourceSet.Last();
00047     for (ConstIterator resource = aResourceSet.First(); resource != last; ++resource)
00048         Add(*resource);
00049 }
00050 
00051 
00052 nsResourceSet&
00053 nsResourceSet::operator=(const nsResourceSet& aResourceSet)
00054 {
00055     Clear();
00056     ConstIterator last = aResourceSet.Last();
00057     for (ConstIterator resource = aResourceSet.First(); resource != last; ++resource)
00058         Add(*resource);
00059     return *this;
00060 }
00061 
00062 nsResourceSet::~nsResourceSet()
00063 {
00064     MOZ_COUNT_DTOR(nsResourceSet);
00065     Clear();
00066     delete[] mResources;
00067 }
00068 
00069 nsresult
00070 nsResourceSet::Clear()
00071 {
00072     while (--mCount >= 0) {
00073         NS_RELEASE(mResources[mCount]);
00074     }
00075     mCount = 0;
00076     return NS_OK;
00077 }
00078 
00079 nsresult
00080 nsResourceSet::Add(nsIRDFResource* aResource)
00081 {
00082     NS_PRECONDITION(aResource != nsnull, "null ptr");
00083     if (! aResource)
00084         return NS_ERROR_NULL_POINTER;
00085 
00086     if (Contains(aResource))
00087         return NS_OK;
00088 
00089     if (mCount >= mCapacity) {
00090         PRInt32 capacity = mCapacity + 4;
00091         nsIRDFResource** resources = new nsIRDFResource*[capacity];
00092         if (! resources)
00093             return NS_ERROR_OUT_OF_MEMORY;
00094 
00095         for (PRInt32 i = mCount - 1; i >= 0; --i)
00096             resources[i] = mResources[i];
00097 
00098         delete[] mResources;
00099 
00100         mResources = resources;
00101         mCapacity = capacity;
00102     }
00103 
00104     mResources[mCount++] = aResource;
00105     NS_ADDREF(aResource);
00106     return NS_OK;
00107 }
00108 
00109 void
00110 nsResourceSet::Remove(nsIRDFResource* aProperty)
00111 {
00112     PRBool found = PR_FALSE;
00113 
00114     nsIRDFResource** res = mResources;
00115     nsIRDFResource** limit = mResources + mCount;
00116     while (res < limit) {
00117         if (found) {
00118             *(res - 1) = *res;
00119         }
00120         else if (*res == aProperty) {
00121             NS_RELEASE(*res);
00122             found = PR_TRUE;
00123         }
00124         ++res;
00125     }
00126 
00127     if (found)
00128         --mCount;
00129 }
00130 
00131 PRBool
00132 nsResourceSet::Contains(nsIRDFResource* aResource) const
00133 {
00134     for (PRInt32 i = mCount - 1; i >= 0; --i) {
00135         if (mResources[i] == aResource)
00136             return PR_TRUE;
00137     }
00138 
00139     return PR_FALSE;
00140 }
00141