|
1 /* |
|
2 * Copyright (c) 2006 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: Contains CNcdDbMaxSizeMap class |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "ncddbmaxsizemap.h" |
|
20 #include "catalogsdebug.h" |
|
21 |
|
22 |
|
23 |
|
24 CNcdDbMaxSizeMap* CNcdDbMaxSizeMap::NewL( const TInt aDefaultMaxSize ) |
|
25 { |
|
26 CNcdDbMaxSizeMap* self = |
|
27 CNcdDbMaxSizeMap::NewLC( aDefaultMaxSize ); |
|
28 CleanupStack::Pop( self ); |
|
29 return self; |
|
30 } |
|
31 |
|
32 CNcdDbMaxSizeMap* CNcdDbMaxSizeMap::NewLC( const TInt aDefaultMaxSize ) |
|
33 { |
|
34 CNcdDbMaxSizeMap* self = |
|
35 new(ELeave) CNcdDbMaxSizeMap( aDefaultMaxSize); |
|
36 CleanupStack::PushL( self ); |
|
37 self->ConstructL(); |
|
38 return self; |
|
39 } |
|
40 |
|
41 |
|
42 CNcdDbMaxSizeMap::CNcdDbMaxSizeMap( const TInt aDefaultMaxSize ) |
|
43 : KElementWasNotFound( -1 ), |
|
44 iDefaultMaxSize( aDefaultMaxSize ) |
|
45 { |
|
46 // Nothing to do here |
|
47 } |
|
48 |
|
49 void CNcdDbMaxSizeMap::ConstructL() |
|
50 { |
|
51 // Nothing to do here |
|
52 } |
|
53 |
|
54 CNcdDbMaxSizeMap::~CNcdDbMaxSizeMap() |
|
55 { |
|
56 // Release the arrays |
|
57 iKeyArray.Reset(); |
|
58 iValueArray.Reset(); |
|
59 } |
|
60 |
|
61 |
|
62 TInt CNcdDbMaxSizeMap::SetValue( const TUid& aKey, |
|
63 TInt aValue ) |
|
64 { |
|
65 DLTRACEIN(("")); |
|
66 |
|
67 TInt error( KErrNone ); |
|
68 TInt keyIndex( KeyIndex( aKey ) ); |
|
69 |
|
70 if ( keyIndex != KElementWasNotFound ) |
|
71 { |
|
72 DLINFO(("Key element already exists.")) |
|
73 // Because the key was already in the array |
|
74 // we also got the value index. So, just replace |
|
75 // the old value by the new one. |
|
76 iValueArray[ keyIndex ] = aValue; |
|
77 } |
|
78 else |
|
79 { |
|
80 DLINFO(("Key element was not found. Create new pair.")); |
|
81 // The key was not found. So new key and corresponding |
|
82 // value has to be set. |
|
83 error = iKeyArray.Append( aKey ); |
|
84 if ( error != KErrNone ) |
|
85 { |
|
86 // Do not do anything else because appending |
|
87 // could not be done. |
|
88 DLERROR(("Key array append error: %d", error)); |
|
89 DLTRACEOUT(("")); |
|
90 return error; |
|
91 } |
|
92 |
|
93 // Key was appended succesfully |
|
94 error = iValueArray.Append( aValue ); |
|
95 if ( error != KErrNone ) |
|
96 { |
|
97 DLERROR(("value array appenderror: %d", error)); |
|
98 |
|
99 // Value could not be appended |
|
100 // Remove the key that was appended above |
|
101 // from the end of thekey array |
|
102 // because we could not append the corresponding |
|
103 // value. This way the key and value pairs will |
|
104 // be in sync. |
|
105 iKeyArray.Remove( iKeyArray.Count() - 1 ); |
|
106 |
|
107 // Leave with the given value. |
|
108 DLTRACEOUT(("")); |
|
109 return error; |
|
110 } |
|
111 } |
|
112 |
|
113 DLTRACEOUT(("")); |
|
114 |
|
115 return error; |
|
116 } |
|
117 |
|
118 |
|
119 TInt CNcdDbMaxSizeMap::Value( const TUid& aKey ) const |
|
120 { |
|
121 DLTRACEIN(("")); |
|
122 |
|
123 TInt index( KeyIndex( aKey ) ); |
|
124 |
|
125 if ( index != KElementWasNotFound ) |
|
126 { |
|
127 // Index was found. So, return the value. |
|
128 // Notice that key value has always the corresponding |
|
129 // value in the value array. The indexes are same for |
|
130 // the key and the value. |
|
131 DLTRACEOUT(("Value index found: %d", index)); |
|
132 return iValueArray[ index ]; |
|
133 } |
|
134 else |
|
135 { |
|
136 // The key was not found. So, return the default. |
|
137 DLTRACEOUT(("Value index not found. Use default: %d", iDefaultMaxSize)); |
|
138 return iDefaultMaxSize; |
|
139 } |
|
140 } |
|
141 |
|
142 |
|
143 TInt CNcdDbMaxSizeMap::KeyIndex( const TUid& aKey ) const |
|
144 { |
|
145 for ( TInt i = 0; i < iKeyArray.Count(); ++i ) |
|
146 { |
|
147 if ( iKeyArray[ i ] == aKey ) |
|
148 { |
|
149 // Correct key was found. |
|
150 // So, return the index. |
|
151 return i; |
|
152 } |
|
153 } |
|
154 |
|
155 // Key was not found. |
|
156 return KElementWasNotFound; |
|
157 } |