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 : #include "nsResourceSet.h"
40 :
41 0 : nsResourceSet::nsResourceSet(const nsResourceSet& aResourceSet)
42 : : mResources(nsnull),
43 : mCount(0),
44 0 : mCapacity(0)
45 : {
46 0 : ConstIterator last = aResourceSet.Last();
47 0 : for (ConstIterator resource = aResourceSet.First(); resource != last; ++resource)
48 0 : Add(*resource);
49 0 : }
50 :
51 :
52 : nsResourceSet&
53 0 : nsResourceSet::operator=(const nsResourceSet& aResourceSet)
54 : {
55 0 : Clear();
56 0 : ConstIterator last = aResourceSet.Last();
57 0 : for (ConstIterator resource = aResourceSet.First(); resource != last; ++resource)
58 0 : Add(*resource);
59 0 : return *this;
60 : }
61 :
62 0 : nsResourceSet::~nsResourceSet()
63 : {
64 0 : MOZ_COUNT_DTOR(nsResourceSet);
65 0 : Clear();
66 0 : delete[] mResources;
67 0 : }
68 :
69 : nsresult
70 0 : nsResourceSet::Clear()
71 : {
72 0 : while (--mCount >= 0) {
73 0 : NS_RELEASE(mResources[mCount]);
74 : }
75 0 : mCount = 0;
76 0 : return NS_OK;
77 : }
78 :
79 : nsresult
80 0 : nsResourceSet::Add(nsIRDFResource* aResource)
81 : {
82 0 : NS_PRECONDITION(aResource != nsnull, "null ptr");
83 0 : if (! aResource)
84 0 : return NS_ERROR_NULL_POINTER;
85 :
86 0 : if (Contains(aResource))
87 0 : return NS_OK;
88 :
89 0 : if (mCount >= mCapacity) {
90 0 : PRInt32 capacity = mCapacity + 4;
91 0 : nsIRDFResource** resources = new nsIRDFResource*[capacity];
92 0 : if (! resources)
93 0 : return NS_ERROR_OUT_OF_MEMORY;
94 :
95 0 : for (PRInt32 i = mCount - 1; i >= 0; --i)
96 0 : resources[i] = mResources[i];
97 :
98 0 : delete[] mResources;
99 :
100 0 : mResources = resources;
101 0 : mCapacity = capacity;
102 : }
103 :
104 0 : mResources[mCount++] = aResource;
105 0 : NS_ADDREF(aResource);
106 0 : return NS_OK;
107 : }
108 :
109 : void
110 0 : nsResourceSet::Remove(nsIRDFResource* aProperty)
111 : {
112 0 : bool found = false;
113 :
114 0 : nsIRDFResource** res = mResources;
115 0 : nsIRDFResource** limit = mResources + mCount;
116 0 : while (res < limit) {
117 0 : if (found) {
118 0 : *(res - 1) = *res;
119 : }
120 0 : else if (*res == aProperty) {
121 0 : NS_RELEASE(*res);
122 0 : found = true;
123 : }
124 0 : ++res;
125 : }
126 :
127 0 : if (found)
128 0 : --mCount;
129 0 : }
130 :
131 : bool
132 0 : nsResourceSet::Contains(nsIRDFResource* aResource) const
133 : {
134 0 : for (PRInt32 i = mCount - 1; i >= 0; --i) {
135 0 : if (mResources[i] == aResource)
136 0 : return true;
137 : }
138 :
139 0 : return false;
140 : }
141 :
|