|
1 /* |
|
2 * Copyright (c) 2005-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: |
|
15 * @file |
|
16 * This contains CDataDictionary |
|
17 * |
|
18 */ |
|
19 |
|
20 |
|
21 |
|
22 /** |
|
23 @prototype |
|
24 @test |
|
25 */ |
|
26 |
|
27 #include <e32std.h> |
|
28 |
|
29 #include "datadictionary.h" |
|
30 #include "datawrapper.h" |
|
31 |
|
32 TUint32 CDataDictionary::Hash(const TDataDictionaryName& aName) |
|
33 { |
|
34 return DefaultHash::Des16(aName); |
|
35 } |
|
36 |
|
37 TBool CDataDictionary::Identity(const TDataDictionaryName& aName1, const TDataDictionaryName& aName2) |
|
38 { |
|
39 return aName1.Compare(aName2)==0; |
|
40 } |
|
41 |
|
42 CDataDictionary::CDataDictionary() |
|
43 /** |
|
44 * Constructor |
|
45 */ |
|
46 : iStore(Hash, Identity) |
|
47 , iCurrentObject(NULL) |
|
48 { |
|
49 } |
|
50 |
|
51 |
|
52 CDataDictionary::~CDataDictionary() |
|
53 /** |
|
54 * Destructor |
|
55 */ |
|
56 { |
|
57 Empty(); |
|
58 } |
|
59 |
|
60 void CDataDictionary::AddDataL(const TDataDictionaryName& aName, CDataWrapper* aData) |
|
61 /** |
|
62 * Add a data entry to the dictionary |
|
63 * |
|
64 * @param aName - name of the dictionary entry |
|
65 * @param aData - the data to add. The dictionary becomes owner of the object |
|
66 * and is responsble for it's deletion |
|
67 * |
|
68 * @see TDataDictionaryName |
|
69 * @see CDataWrapper |
|
70 * |
|
71 * @leave KErrAlreadyExists if the dictionary entry aName, or aData already exists. |
|
72 * @leave KErrNoMemory if memory could not be allocated to store the copies of aName and aData. |
|
73 * @leave system wide errors |
|
74 */ |
|
75 { |
|
76 // Ensure name does not already exist |
|
77 if ( iStore.Find(aName) != NULL ) |
|
78 { |
|
79 User::Leave(KErrAlreadyExists); |
|
80 } |
|
81 |
|
82 // Ensure data does not already exist |
|
83 TDataIter iter(iStore); |
|
84 iter.Reset(); |
|
85 for ( CDataWrapper*const * data=iter.NextValue(); data!=NULL; data=iter.NextValue() ) |
|
86 { |
|
87 if ( aData == *data ) |
|
88 { |
|
89 User::Leave(KErrAlreadyExists); |
|
90 } |
|
91 } |
|
92 |
|
93 iStore.InsertL(aName, aData); |
|
94 if ( iCurrentObject == NULL ) |
|
95 { |
|
96 iCurrentObject=aData; |
|
97 } |
|
98 } |
|
99 |
|
100 void CDataDictionary::DeleteDataL(const TDataDictionaryName& aName) |
|
101 /** |
|
102 * Delete a data entry from the dictionary |
|
103 * |
|
104 * @param aName - name of the dictionary entry |
|
105 * |
|
106 * @see TDataDictionaryName |
|
107 * |
|
108 * @leave KErrNotFound if the dictionary entry aName is not found |
|
109 */ |
|
110 { |
|
111 // Ensure name already exist |
|
112 CDataWrapper** wrapper=iStore.Find(aName); |
|
113 |
|
114 if ( wrapper != NULL ) |
|
115 { |
|
116 delete* wrapper; |
|
117 iStore.Remove(aName); |
|
118 } |
|
119 else |
|
120 { |
|
121 User::Leave(KErrNotFound); |
|
122 } |
|
123 } |
|
124 |
|
125 void CDataDictionary::SetCurrentDataL(const TDataDictionaryName& aName) |
|
126 /** |
|
127 * Set the current active dictionary entry |
|
128 * |
|
129 * @param aName - name of the dictionary entry |
|
130 * |
|
131 * @see TDataDictionaryName |
|
132 * |
|
133 * @leave KErrNotFound if the dictionary entry aName is not found |
|
134 */ |
|
135 { |
|
136 // Ensure name already exist |
|
137 CDataWrapper** wrapper=iStore.Find(aName); |
|
138 |
|
139 if ( wrapper != NULL ) |
|
140 { |
|
141 iCurrentObject=*wrapper; |
|
142 } |
|
143 else |
|
144 { |
|
145 User::Leave(KErrNotFound); |
|
146 } |
|
147 } |
|
148 |
|
149 CDataWrapper* CDataDictionary::GetDataL(const TDataDictionaryName& aName) |
|
150 /** |
|
151 * Get an object in the dictionary |
|
152 * |
|
153 * @param aName - name of the dictionary entry |
|
154 * |
|
155 * @see TDataDictionaryName |
|
156 * |
|
157 * @leave KErrNotFound if the dictionary entry aName is not found |
|
158 * |
|
159 * @return The data object found |
|
160 */ |
|
161 { |
|
162 // Ensure name already exist |
|
163 CDataWrapper** ret=iStore.Find(aName); |
|
164 |
|
165 if ( ret == NULL ) |
|
166 { |
|
167 User::Leave(KErrNotFound); |
|
168 } |
|
169 |
|
170 return *ret; |
|
171 } |
|
172 |
|
173 TAny* CDataDictionary::GetObjectL(const TDesC& aName) |
|
174 /** |
|
175 * Get an object in the dictionary |
|
176 * |
|
177 * @param aName - name of the dictionary entry |
|
178 * |
|
179 * @return The object found |
|
180 * |
|
181 * @leave KErrNotFound if the dictionary entry aName is not found |
|
182 */ |
|
183 { |
|
184 // Ensure name already exist |
|
185 CDataWrapper** wrapper=iStore.Find(aName); |
|
186 TAny* ret=NULL; |
|
187 |
|
188 if ( wrapper != NULL ) |
|
189 { |
|
190 ret=(*wrapper)->GetObject(); |
|
191 } |
|
192 else |
|
193 { |
|
194 User::Leave(KErrNotFound); |
|
195 } |
|
196 |
|
197 return ret; |
|
198 } |
|
199 |
|
200 void CDataDictionary::Empty() |
|
201 /** |
|
202 * Empty the object dictionary |
|
203 */ |
|
204 { |
|
205 TDataIter iter(iStore); |
|
206 iter.Reset(); |
|
207 for ( CDataWrapper*const * data=iter.NextValue(); data!=NULL; data=iter.NextValue() ) |
|
208 { |
|
209 delete *data; |
|
210 } |
|
211 iStore.Close(); |
|
212 |
|
213 iCurrentObject=NULL; |
|
214 } |
|
215 |
|
216 TInt CDataDictionary::Outstanding(const TDesC& aName, TBool& aMoreToDo) |
|
217 /** |
|
218 * Delegates to a data object to see if |
|
219 * it has any outstanding requests |
|
220 * |
|
221 * @param aName - Name of the dictionary entry |
|
222 * |
|
223 * @leave KErrNotFound if the dictionary entry aName is not found |
|
224 * |
|
225 * @return ETrue if there are any outstanding requests |
|
226 */ |
|
227 { |
|
228 TInt retCode=KErrNone; |
|
229 |
|
230 aMoreToDo=EFalse; |
|
231 if ( aName.Compare(KTEFNull)==0 ) |
|
232 { |
|
233 TDataIter iter(iStore); |
|
234 iter.Reset(); |
|
235 for ( CDataWrapper*const * data=iter.NextValue(); (data!=NULL) && (!aMoreToDo); data=iter.NextValue() ) |
|
236 { |
|
237 aMoreToDo = (*data)->Outstanding(); |
|
238 } |
|
239 } |
|
240 else |
|
241 { |
|
242 // Ensure name already exist |
|
243 CDataWrapper** wrapper=iStore.Find(aName); |
|
244 |
|
245 if ( wrapper != NULL ) |
|
246 { |
|
247 aMoreToDo = (*wrapper)->Outstanding(); |
|
248 } |
|
249 else |
|
250 { |
|
251 retCode=KErrNotFound; |
|
252 } |
|
253 } |
|
254 |
|
255 return retCode; |
|
256 } |
|
257 |
|
258 TAny* CDataDictionary::CurrentObject() |
|
259 /** |
|
260 * Get the object of the current active dictionary entry |
|
261 * |
|
262 * @return The object that the current data wraps |
|
263 */ |
|
264 { |
|
265 return iCurrentObject ? iCurrentObject->GetObject() : NULL; |
|
266 } |
|
267 |
|
268 /** |
|
269 * Set an object in the dictionary |
|
270 * |
|
271 * @param aName - name of the dictionary entry |
|
272 * @param aObject - object that the wrapper is testing |
|
273 * |
|
274 * @see TDataDictionaryName |
|
275 * |
|
276 * @leave KErrNotFound if the dictionary entry aName is not found |
|
277 * @leave KErrNotSupported if the the function is not supported |
|
278 */ |
|
279 void CDataDictionary::SetObjectL(const TDataDictionaryName& aName, TAny* aObject) |
|
280 { |
|
281 GetDataL(aName)->SetObjectL(aObject); |
|
282 } |