|
1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include "VersitAdditionalStorage.h" |
|
17 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS |
|
18 #include "versit_internal.h" |
|
19 #endif |
|
20 |
|
21 // User includes |
|
22 #include <versit.h> |
|
23 |
|
24 // Constants |
|
25 const TInt KVersitAdditionalStorageArrayGranularity = 2; |
|
26 |
|
27 class TVersitAdditionalStorageItem |
|
28 /** |
|
29 @internalComponent |
|
30 @released |
|
31 */ |
|
32 { |
|
33 public: |
|
34 inline TVersitAdditionalStorageItem(const TAny* aKey, CBase* aValue) |
|
35 : iKey(aKey), iValue(aValue) |
|
36 { |
|
37 } |
|
38 |
|
39 public: |
|
40 const TAny* iKey; |
|
41 CBase* iValue; |
|
42 }; |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 CVersitAdditionalStorage::CVersitAdditionalStorage() |
|
48 : iStorage(KVersitAdditionalStorageArrayGranularity) |
|
49 { |
|
50 } |
|
51 |
|
52 |
|
53 CVersitAdditionalStorage::~CVersitAdditionalStorage() |
|
54 { |
|
55 const TInt count = iStorage.Count(); |
|
56 for(TInt i=0; i<count; i++) |
|
57 { |
|
58 TVersitAdditionalStorageItem& item = iStorage[i]; |
|
59 delete item.iValue; |
|
60 } |
|
61 iStorage.Close(); |
|
62 } |
|
63 |
|
64 |
|
65 CVersitAdditionalStorage* CVersitAdditionalStorage::NewL() |
|
66 { |
|
67 return new(ELeave) CVersitAdditionalStorage(); |
|
68 } |
|
69 |
|
70 |
|
71 void CVersitAdditionalStorage::AddToStorageL(const TAny* aKey, CBase* aValue) |
|
72 { |
|
73 __ASSERT_ALWAYS(aValue != NULL, Panic(EVersitPanicNullValueAssignedToAdditionalStorageSlot)); |
|
74 |
|
75 // Check if an additional value has already been stored for this item |
|
76 CBase* value = FromStorage(aKey); |
|
77 __ASSERT_ALWAYS(value == NULL, Panic(EVersitPanicAdditionalStorageSlotAlreadyInUse)); |
|
78 |
|
79 // Create new item |
|
80 TVersitAdditionalStorageItem newItem(aKey, aValue); |
|
81 User::LeaveIfError(iStorage.Append(newItem)); |
|
82 } |
|
83 |
|
84 |
|
85 CBase* CVersitAdditionalStorage::FromStorage(const TAny* aKey) |
|
86 { |
|
87 CBase* value = NULL; |
|
88 TInt index = KErrNotFound; |
|
89 TVersitAdditionalStorageItem* item = ItemByKey(aKey, index); |
|
90 if (item) |
|
91 { |
|
92 value = item->iValue; |
|
93 } |
|
94 return value; |
|
95 } |
|
96 |
|
97 void CVersitAdditionalStorage::FreeStorage(const TAny* aKey) |
|
98 { |
|
99 TInt index = KErrNotFound; |
|
100 TVersitAdditionalStorageItem* item = ItemByKey(aKey, index); |
|
101 if (item) |
|
102 { |
|
103 delete item->iValue; |
|
104 iStorage.Remove(index); |
|
105 } |
|
106 } |
|
107 |
|
108 TVersitAdditionalStorageItem* CVersitAdditionalStorage::ItemByKey(const TAny* aKey, TInt& aIndex) |
|
109 { |
|
110 TVersitAdditionalStorageItem* foundItem = NULL; |
|
111 TVersitAdditionalStorageItem searchFor(aKey, NULL); |
|
112 TIdentityRelation<TVersitAdditionalStorageItem> relation(ItemsMatch); |
|
113 // |
|
114 aIndex = iStorage.Find(searchFor, relation); |
|
115 if (aIndex >= 0) |
|
116 { |
|
117 foundItem = &iStorage[aIndex]; |
|
118 } |
|
119 // |
|
120 return foundItem; |
|
121 } |
|
122 |
|
123 TBool CVersitAdditionalStorage::ItemsMatch(const TVersitAdditionalStorageItem& aLeft, const TVersitAdditionalStorageItem& aRight) |
|
124 { |
|
125 return aLeft.iKey == aRight.iKey; |
|
126 } |