|
1 /* |
|
2 * Copyright (c) 2002-2004 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: Associative container. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 #include "xnmap.h" |
|
22 #include "xncomparator.h" |
|
23 #include "xnpanic.h" |
|
24 |
|
25 // EXTERNAL DATA STRUCTURES |
|
26 //extern ?external_data; |
|
27 |
|
28 // EXTERNAL FUNCTION PROTOTYPES |
|
29 //extern ?external_function( ?arg_type,?arg_type ); |
|
30 |
|
31 // CONSTANTS |
|
32 //const ?type ?constant_var = ?constant; |
|
33 |
|
34 // MACROS |
|
35 //#define ?macro ?macro_def |
|
36 |
|
37 // LOCAL CONSTANTS AND MACROS |
|
38 //const ?type ?constant_var = ?constant; |
|
39 //#define ?macro_name ?macro_def |
|
40 |
|
41 // MODULE DATA STRUCTURES |
|
42 //enum ?declaration |
|
43 //typedef ?declaration |
|
44 |
|
45 // LOCAL FUNCTION PROTOTYPES |
|
46 //?type ?function_name( ?arg_type, ?arg_type ); |
|
47 |
|
48 // FORWARD DECLARATIONS |
|
49 //class ?FORWARD_CLASSNAME; |
|
50 |
|
51 // ============================= LOCAL FUNCTIONS =============================== |
|
52 |
|
53 // ----------------------------------------------------------------------------- |
|
54 // ?function_name ?description. |
|
55 // ?description |
|
56 // Returns: ?value_1: ?description |
|
57 // ?value_n: ?description_line1 |
|
58 // ?description_line2 |
|
59 // ----------------------------------------------------------------------------- |
|
60 // |
|
61 //?type ?function_name( |
|
62 // ?arg_type arg, // ?description |
|
63 // ?arg_type arg) // ?description |
|
64 // { |
|
65 |
|
66 // ?code // ?comment |
|
67 |
|
68 // ?comment |
|
69 // ?code |
|
70 // } |
|
71 |
|
72 |
|
73 // ============================ MEMBER FUNCTIONS =============================== |
|
74 |
|
75 // ----------------------------------------------------------------------------- |
|
76 // CXnMap::CXnMap |
|
77 // C++ default constructor can NOT contain any code, that |
|
78 // might leave. |
|
79 // ----------------------------------------------------------------------------- |
|
80 // |
|
81 CXnMap::CXnMap() : iContainer(4) |
|
82 { |
|
83 } |
|
84 |
|
85 // ----------------------------------------------------------------------------- |
|
86 // CXnMap::ConstructL |
|
87 // Symbian 2nd phase constructor can leave. |
|
88 // ----------------------------------------------------------------------------- |
|
89 // |
|
90 void CXnMap::ConstructL(MXnComparator* aComparatorFunctor) |
|
91 { |
|
92 iComparator = aComparatorFunctor; |
|
93 } |
|
94 |
|
95 // ----------------------------------------------------------------------------- |
|
96 // CXnMap::NewL |
|
97 // Two-phased constructor. |
|
98 // ----------------------------------------------------------------------------- |
|
99 // |
|
100 EXPORT_C CXnMap* CXnMap::NewL(MXnComparator* aComparatorFunctor) |
|
101 { |
|
102 CXnMap* self = new( ELeave ) CXnMap; |
|
103 |
|
104 CleanupStack::PushL( self ); |
|
105 self->ConstructL(aComparatorFunctor); |
|
106 CleanupStack::Pop(); |
|
107 |
|
108 return self; |
|
109 } |
|
110 |
|
111 |
|
112 // Destructor |
|
113 CXnMap::~CXnMap() |
|
114 { |
|
115 delete iComparator; |
|
116 iContainer.ResetAndDestroy(); |
|
117 } |
|
118 // ----------------------------------------------------------------------------- |
|
119 // CXnMap::PutL |
|
120 // Finds an equal object in the map. If found, destroys it and |
|
121 // replaces it |
|
122 // with the object given as a parameter. If not found, inserts |
|
123 // the parameter object to the map. |
|
124 // ----------------------------------------------------------------------------- |
|
125 // |
|
126 EXPORT_C void CXnMap::PutL(CBase* aObject) |
|
127 { |
|
128 CBase* tmp = Get(*aObject); |
|
129 if (tmp == aObject) |
|
130 { |
|
131 // object already in the map, do nothing |
|
132 return; |
|
133 } |
|
134 if (tmp != NULL) |
|
135 { |
|
136 // if an equal object is already in the map, replace it |
|
137 TInt index = iContainer.Find(tmp); |
|
138 // add new object |
|
139 TInt error = iContainer.Append(aObject); |
|
140 if (error != KErrNone) |
|
141 { |
|
142 User::Leave(KXnErrAppendingMapItemFailed_1); |
|
143 } |
|
144 // remove old object |
|
145 delete tmp; |
|
146 iContainer.Remove(index); |
|
147 return; |
|
148 } |
|
149 // object is not in the map, insert it |
|
150 TInt err = iContainer.Append(aObject); |
|
151 if (err != KErrNone) |
|
152 { |
|
153 User::Leave(KXnErrAppendingMapItemFailed_2); |
|
154 } |
|
155 } |
|
156 // ----------------------------------------------------------------------------- |
|
157 // CXnMap::Get |
|
158 // Finds an object that is equal to the parameter. If found, |
|
159 // returns the object. If not found, returns NULL. |
|
160 // (other items were commented in a header). |
|
161 // ----------------------------------------------------------------------------- |
|
162 // |
|
163 EXPORT_C CBase* CXnMap::Get(CBase& aObject) const |
|
164 { |
|
165 TUint containerCount = iContainer.Count(); |
|
166 CBase* returnValue = NULL; |
|
167 for (TUint i = 0; i < containerCount; ++i) |
|
168 { |
|
169 CBase* tmp = iContainer[i]; |
|
170 if (iComparator->Equals(aObject, *tmp)) |
|
171 { |
|
172 returnValue = tmp; |
|
173 break; |
|
174 } |
|
175 } |
|
176 return returnValue; |
|
177 } |
|
178 |
|
179 // ----------------------------------------------------------------------------- |
|
180 // CXnMap::Find |
|
181 // Finds an object that is equal to the parameter. If found, |
|
182 // returns the object. If not found, returns NULL. |
|
183 // (other items were commented in a header). |
|
184 // ----------------------------------------------------------------------------- |
|
185 // |
|
186 EXPORT_C CBase* CXnMap::Find(CBase& aObject, MXnComparator& aComparator) const |
|
187 { |
|
188 TUint containerCount = iContainer.Count(); |
|
189 CBase* returnValue = NULL; |
|
190 for (TUint i = 0; i < containerCount; ++i) |
|
191 { |
|
192 CBase* tmp = iContainer[i]; |
|
193 if (aComparator.Equals(aObject, *tmp)) |
|
194 { |
|
195 returnValue = tmp; |
|
196 break; |
|
197 } |
|
198 } |
|
199 return returnValue; |
|
200 } |
|
201 |
|
202 |
|
203 // ----------------------------------------------------------------------------- |
|
204 // CXnMap::Container |
|
205 // Gets access to the underlying container |
|
206 // ----------------------------------------------------------------------------- |
|
207 EXPORT_C RPointerArray<CBase>& CXnMap::Container() |
|
208 { |
|
209 return iContainer; |
|
210 } |
|
211 |
|
212 // ========================== OTHER EXPORTED FUNCTIONS ========================= |
|
213 |
|
214 // ----------------------------------------------------------------------------- |
|
215 // ?function_name implements... |
|
216 // ?implementation_description. |
|
217 // Returns: ?value_1: ?description |
|
218 // ?value_n: ?description |
|
219 // ?description |
|
220 // ----------------------------------------------------------------------------- |
|
221 // |
|
222 //?type ?function_name( |
|
223 // ?arg_type arg, // ?description |
|
224 // ?arg_type arg ) // ?description |
|
225 // { |
|
226 |
|
227 // ?code |
|
228 |
|
229 // } |
|
230 |
|
231 |
|
232 // End of File |