1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: osn vector pointer implementation. |
|
15 * |
|
16 */ |
|
17 |
|
18 #if defined(__GNUC__) |
|
19 #include <stdlib.h> |
|
20 #include <string.h> |
|
21 #else |
|
22 #include <libc/string.h> |
|
23 #endif |
|
24 |
|
25 #include <osn/alfptrvectorimpl.h> |
|
26 #include <stdexcept> |
|
27 |
|
28 namespace osncore |
|
29 { |
|
30 |
|
31 // ------------------------------------------------------------------------- |
|
32 // ------------------------------------------------------------------------- |
|
33 // |
|
34 OSN_EXPORT AlfPtrVectorImpl::AlfPtrVectorImpl(IDeleter& aDeleter) |
|
35 : mdata(0), msize(0), mcount(0), mDeleter(aDeleter) |
|
36 { |
|
37 } |
|
38 |
|
39 // ------------------------------------------------------------------------- |
|
40 // ------------------------------------------------------------------------- |
|
41 // |
|
42 OSN_EXPORT AlfPtrVectorImpl::AlfPtrVectorImpl(uint aSize, IDeleter& aDeleter): |
|
43 mdata(0), |
|
44 msize(aSize), |
|
45 mcount(0), mDeleter(aDeleter) |
|
46 { |
|
47 mdata = (void **)malloc(aSize * sizeof(void *)); |
|
48 if(!mdata) |
|
49 { |
|
50 throw std::bad_alloc(); |
|
51 } |
|
52 memset(mdata, 0, aSize * sizeof(void *)); |
|
53 } |
|
54 |
|
55 |
|
56 // ------------------------------------------------------------------------- |
|
57 // ------------------------------------------------------------------------- |
|
58 // |
|
59 OSN_EXPORT AlfPtrVectorImpl::~AlfPtrVectorImpl() |
|
60 { |
|
61 free(mdata); |
|
62 } |
|
63 |
|
64 // ------------------------------------------------------------------------- |
|
65 // ------------------------------------------------------------------------- |
|
66 // |
|
67 OSN_EXPORT void AlfPtrVectorImpl::clear(bool aDelItems) |
|
68 { |
|
69 mcount = 0; |
|
70 |
|
71 if (aDelItems) |
|
72 { |
|
73 for (uint i = 0; i < msize; ++i) |
|
74 { |
|
75 void *item = mdata[i]; |
|
76 if (item) |
|
77 { |
|
78 mDeleter.deleteItem(item); |
|
79 } |
|
80 } |
|
81 } |
|
82 free(mdata); |
|
83 mdata = 0; |
|
84 msize = 0; |
|
85 } |
|
86 |
|
87 // ------------------------------------------------------------------------- |
|
88 // ------------------------------------------------------------------------- |
|
89 // |
|
90 OSN_EXPORT bool AlfPtrVectorImpl::remove(uint aCount, bool aDelItems) |
|
91 { |
|
92 if (aCount >= msize) { |
|
93 return false; |
|
94 } |
|
95 |
|
96 void *item = mdata[aCount]; |
|
97 |
|
98 --mcount; |
|
99 |
|
100 //Move all the items below the deleted items up by 1 index |
|
101 for(uint i =aCount; i< msize && msize > aCount;++i) |
|
102 { |
|
103 mdata[i]= mdata[i+1]; |
|
104 } |
|
105 |
|
106 //Reset the items to null - from index = count to the size after the shuffling. |
|
107 for(uint i=mcount;i<msize;++i) |
|
108 { |
|
109 mdata[i] = 0; |
|
110 } |
|
111 |
|
112 // Array is now in good shape. Can call out of the class via destructors |
|
113 if (item) { |
|
114 if (aDelItems) { |
|
115 mDeleter.deleteItem(item); |
|
116 } |
|
117 } |
|
118 return true; |
|
119 } |
|
120 |
|
121 // ------------------------------------------------------------------------- |
|
122 // ------------------------------------------------------------------------- |
|
123 // |
|
124 OSN_EXPORT bool AlfPtrVectorImpl::resize(uint aSize, bool aDelItems) |
|
125 { |
|
126 uint oldSize = msize; |
|
127 |
|
128 for (uint i = aSize; i < oldSize; ++i) { |
|
129 void *item = mdata[i]; |
|
130 if (item) { |
|
131 --mcount; |
|
132 } |
|
133 } |
|
134 |
|
135 for (uint i = aSize; i < oldSize; ++i) { |
|
136 void *item = mdata[i]; |
|
137 if (item) { |
|
138 if (aDelItems) { |
|
139 mDeleter.deleteItem(item); |
|
140 } |
|
141 } |
|
142 } |
|
143 |
|
144 void** data = (void **)realloc(mdata, aSize * sizeof(void *)); |
|
145 if(!data) |
|
146 { |
|
147 throw std::bad_alloc(); |
|
148 } |
|
149 mdata = data; |
|
150 msize = aSize; |
|
151 |
|
152 if (aSize > oldSize) |
|
153 { |
|
154 memset(&mdata[oldSize], 0, (aSize - oldSize) * sizeof(void *)); |
|
155 } |
|
156 return true; |
|
157 } |
|
158 |
|
159 // ------------------------------------------------------------------------- |
|
160 // ------------------------------------------------------------------------- |
|
161 // |
|
162 OSN_EXPORT bool AlfPtrVectorImpl::insert(uint aCount, void *aItem, bool /*aDelItems*/) |
|
163 { |
|
164 uint iNum=0; |
|
165 //If there are no items and the position to add > 0 or <0 then return false. Do not do anything |
|
166 //if the Index is greater than the current count then Do not do anything |
|
167 //if aCount== count There will be scope to add at the most one item |
|
168 if((aCount > mcount) || (mcount==0 && aCount >iNum)||(mcount==0 && aCount < iNum)) |
|
169 { |
|
170 return false; |
|
171 } |
|
172 |
|
173 |
|
174 if(mcount>=msize) |
|
175 { |
|
176 // 1. Let's increase the count |
|
177 int oldSize = msize; |
|
178 msize++; |
|
179 |
|
180 // 2. Re-allocate memeory area |
|
181 void** tmp = (void **)realloc(mdata, msize * sizeof(void *)); |
|
182 if(!tmp) |
|
183 { |
|
184 msize--; |
|
185 throw std::bad_alloc(); |
|
186 } |
|
187 mdata = tmp; |
|
188 memset(&mdata[oldSize], 0, (msize- oldSize) * sizeof(void *)); |
|
189 } |
|
190 |
|
191 //Now move elements from nth element to the last element by 1 position |
|
192 //Move all |
|
193 if(aCount < mcount) |
|
194 { |
|
195 for(int i =mcount;i > aCount; --i) |
|
196 { |
|
197 mdata[i]= mdata[i-1]; |
|
198 } |
|
199 } |
|
200 //Now assign the new element at this position and increment the count |
|
201 |
|
202 mdata[aCount] = aItem; |
|
203 if (aItem) |
|
204 { |
|
205 ++mcount; |
|
206 } |
|
207 |
|
208 return true; |
|
209 } |
|
210 |
|
211 // ------------------------------------------------------------------------- |
|
212 // ------------------------------------------------------------------------- |
|
213 // |
|
214 OSN_EXPORT int AlfPtrVectorImpl::findRef(void *aItem) |
|
215 { |
|
216 for (unsigned i = 0; i < mcount; i++) { |
|
217 if (mdata[i] == aItem) { |
|
218 return i; |
|
219 } |
|
220 } |
|
221 |
|
222 return -1; |
|
223 } |
|
224 |
|
225 |
|
226 |
|
227 } //osncore |
|