|
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: Implementation of media base class |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <mpxlog.h> |
|
20 #include <mpxuser.h> |
|
21 #include "mpxmediabase.h" |
|
22 |
|
23 |
|
24 // ----------------------------------------------------------------------------- |
|
25 // C++ Constructor. |
|
26 // ----------------------------------------------------------------------------- |
|
27 // |
|
28 CMPXMediaBase::CMPXMediaBase() |
|
29 { |
|
30 // Open the global chunk and return the handle of the global chunk. |
|
31 // If called for the first time, creates the global chunk and heap manager. |
|
32 iClientHandle=MMPXData::ClientHandle(); |
|
33 MMPXData::AddClientRef(iClientHandle); // Ref for this thread |
|
34 } |
|
35 |
|
36 // ----------------------------------------------------------------------------- |
|
37 // Destructor |
|
38 // ----------------------------------------------------------------------------- |
|
39 // |
|
40 CMPXMediaBase::~CMPXMediaBase() |
|
41 { |
|
42 ResetLocal(); |
|
43 iValues.Close(); |
|
44 if (iData) |
|
45 { |
|
46 iData->Release(iClientHandle); |
|
47 MMPXData::ReleaseClient(iClientHandle); // This thread's reference count |
|
48 } |
|
49 } |
|
50 |
|
51 // ----------------------------------------------------------------------------- |
|
52 // CMPXMediaBase::ConstructL |
|
53 // ----------------------------------------------------------------------------- |
|
54 // |
|
55 void CMPXMediaBase::ConstructL() |
|
56 { |
|
57 MMPXData* d=MMPXData::NewL(iClientHandle); |
|
58 TInt dh=d->DataHandle(iClientHandle); |
|
59 ConstructL(dh,*d); |
|
60 } |
|
61 |
|
62 // ----------------------------------------------------------------------------- |
|
63 // CMPXMediaBase::ConstructL |
|
64 // ----------------------------------------------------------------------------- |
|
65 // |
|
66 void CMPXMediaBase::ConstructL(const CMPXMediaBase& aBase) |
|
67 { |
|
68 ConstructL(aBase.iDataHandle,*aBase.iData); |
|
69 } |
|
70 |
|
71 // ----------------------------------------------------------------------------- |
|
72 // CMPXMediaBase::CopyConstructL |
|
73 // ----------------------------------------------------------------------------- |
|
74 // |
|
75 void CMPXMediaBase::CopyConstructL(const CMPXMediaBase& aBase) |
|
76 { |
|
77 MMPXData* d=MMPXData::NewL(iClientHandle,*aBase.iData); |
|
78 TInt dh=d->DataHandle(iClientHandle); |
|
79 ConstructL(dh,*d); |
|
80 } |
|
81 |
|
82 // ----------------------------------------------------------------------------- |
|
83 // CMPXMediaBase::ConstructL |
|
84 // ----------------------------------------------------------------------------- |
|
85 // |
|
86 void CMPXMediaBase::ConstructL(TInt aDataHandle) |
|
87 { |
|
88 MMPXData* d=MMPXData::Data(iClientHandle,aDataHandle); |
|
89 ConstructL(aDataHandle,*d); |
|
90 } |
|
91 |
|
92 // ----------------------------------------------------------------------------- |
|
93 // CMPXMediaBase::ConstructL |
|
94 // ----------------------------------------------------------------------------- |
|
95 // |
|
96 void CMPXMediaBase::ConstructL(TInt aDataHandle,MMPXData& aData) |
|
97 { |
|
98 MPX_ASSERT(!iData&&!iDataHandle&&aDataHandle); |
|
99 if (aDataHandle==0) |
|
100 { |
|
101 User::Leave(KErrArgument); |
|
102 } |
|
103 iDataHandle=aDataHandle; |
|
104 iData=&aData; |
|
105 iData->AddRef(iClientHandle); |
|
106 iValues.ReserveL(Count()); |
|
107 } |
|
108 |
|
109 // ----------------------------------------------------------------------------- |
|
110 // CMPXMediaBase::Clear |
|
111 // ----------------------------------------------------------------------------- |
|
112 // |
|
113 void CMPXMediaBase::Clear() |
|
114 { |
|
115 if (iData) |
|
116 { |
|
117 ResetLocal(); // remove local data only, keep heap data for other media objects |
|
118 iData->Release(iClientHandle); // MMPXData's reference count |
|
119 iData=NULL; |
|
120 iDataHandle=0; |
|
121 } |
|
122 } |
|
123 |
|
124 // ----------------------------------------------------------------------------- |
|
125 // CMPXMediaBase::ResetLocal |
|
126 // ----------------------------------------------------------------------------- |
|
127 // |
|
128 void CMPXMediaBase::ResetLocal() |
|
129 { |
|
130 THashMapIter<TInt, TValue> iter(iValues); |
|
131 const TValue* v = NULL; |
|
132 while ((v = iter.NextValue())!=NULL) |
|
133 { |
|
134 DeletePtr(v->iValue, v->iType); |
|
135 iter.RemoveCurrent(); |
|
136 } |
|
137 iValues.Close(); |
|
138 } |
|
139 |
|
140 TInt CMPXMediaBase::SetLocal(const TValue& aValue) |
|
141 { |
|
142 DeleteLocalByUid(aValue.iUid); |
|
143 return iValues.Insert(aValue.iUid, aValue); |
|
144 } |
|
145 |
|
146 // ----------------------------------------------------------------------------- |
|
147 // CMPXMediaBase::LocalIndex |
|
148 // ----------------------------------------------------------------------------- |
|
149 // |
|
150 const CMPXMediaBase::TValue* CMPXMediaBase::LocalValue(TInt aUid) const |
|
151 { |
|
152 return iValues.Find(aUid); |
|
153 } |
|
154 |
|
155 // ----------------------------------------------------------------------------- |
|
156 // CMPXMediaBase::DeleteLocal |
|
157 // ----------------------------------------------------------------------------- |
|
158 // |
|
159 void CMPXMediaBase::DeleteLocalByUid(const TInt aUid) |
|
160 { |
|
161 TValue* val = iValues.Find(aUid); |
|
162 if (val) |
|
163 { |
|
164 DeletePtr(val->iValue,val->iType); |
|
165 iValues.Remove(aUid); |
|
166 } |
|
167 } |
|
168 |
|
169 // ----------------------------------------------------------------------------- |
|
170 // CMPXMediaBase::DeleteLocal |
|
171 // ----------------------------------------------------------------------------- |
|
172 // |
|
173 void CMPXMediaBase::DeleteLocal(TInt aIndex) |
|
174 { |
|
175 MPX_ASSERT(KErrNotFound != aIndex); |
|
176 TInt uid = iData->Uid(iClientHandle,aIndex); |
|
177 DeleteLocalByUid(uid); |
|
178 } |
|
179 |
|
180 // ----------------------------------------------------------------------------- |
|
181 // CMPXMediaBase::DeletePtr |
|
182 // ----------------------------------------------------------------------------- |
|
183 // |
|
184 void CMPXMediaBase::DeletePtr(TAny* aValue,TMPXAttributeType aType) |
|
185 { |
|
186 switch(aType) |
|
187 { |
|
188 case EMPXTypeCObject: |
|
189 delete static_cast<CBase*>(aValue); |
|
190 break; |
|
191 default: |
|
192 // Both T objects and text are held in TPtrC8 which are |
|
193 // allocated on the heap. |
|
194 // |
|
195 User::Free(aValue); |
|
196 break; |
|
197 } |
|
198 } |
|
199 |
|
200 // ----------------------------------------------------------------------------- |
|
201 // Externalize |
|
202 // ----------------------------------------------------------------------------- |
|
203 // |
|
204 void CMPXMediaBase::DoExternalizeL( |
|
205 RWriteStream& aStream, |
|
206 MMPXData::TMPXObjectType aType) const |
|
207 { |
|
208 // |
|
209 // NB: Client must persist the object until after it has been |
|
210 // internalized, i.e. there must always be at least one object |
|
211 // in existance for Internalize to work! |
|
212 // |
|
213 MPX_ASSERT(iDataHandle); |
|
214 // |
|
215 aStream.WriteInt32L(aType); |
|
216 aStream.WriteUint32L(iDataHandle); |
|
217 } |
|
218 |
|
219 // ----------------------------------------------------------------------------- |
|
220 // Internalize. ConstructL() will have been called prior to this so there |
|
221 // will definitely be a iClientHandle, iDataHandle and iData, though there may or |
|
222 // may not be any data added. Either way, the data should be reset. |
|
223 // ----------------------------------------------------------------------------- |
|
224 // |
|
225 void CMPXMediaBase::DoInternalizeL( |
|
226 RReadStream& aStream, |
|
227 MMPXData::TMPXObjectType /*aType*/) |
|
228 { |
|
229 MPX_ASSERT(iClientHandle); |
|
230 // |
|
231 Clear(); // Clear local data only from this object |
|
232 TInt type( aStream.ReadInt32L() ); // object type |
|
233 if ( type != MMPXData::EMedia && |
|
234 type != MMPXData::EMediaArray) |
|
235 { |
|
236 MPX_DEBUG1("CMPXMediaBase::DoInternalizeL(): Invalid Type"); |
|
237 User::Leave( KErrArgument ); |
|
238 } |
|
239 // |
|
240 iDataHandle=aStream.ReadUint32L(); |
|
241 iData=MMPXData::Data(iClientHandle,iDataHandle); |
|
242 iData->AddRef(iClientHandle); |
|
243 } |
|
244 |
|
245 // ---------------------------------------------------------------------------- |
|
246 // CMPXMediaBase::LockHeapLC |
|
247 // ---------------------------------------------------------------------------- |
|
248 // |
|
249 void CMPXMediaBase::LockHeapLC() const |
|
250 { |
|
251 MMPXData::LockHeap( iClientHandle ); |
|
252 CleanupStack::PushL(TCleanupItem(UnlockHeap, const_cast<CMPXMediaBase*>(this))); |
|
253 } |
|
254 |
|
255 // ---------------------------------------------------------------------------- |
|
256 // CMPXMediaBase::UnlockHeap |
|
257 // |
|
258 // Executed when the cleanup stack item is destroyed as a result of |
|
259 // CleanupStack::PopAndDestroy being called. This could either be called |
|
260 // manually or as a result of a leave being generated and trapped. |
|
261 // |
|
262 // This is a static method. |
|
263 // ---------------------------------------------------------------------------- |
|
264 // |
|
265 void CMPXMediaBase::UnlockHeap(TAny* aMediaBase) |
|
266 { |
|
267 CMPXMediaBase* base = static_cast<CMPXMediaBase*>(aMediaBase); |
|
268 MMPXData::UnlockHeap(base->iClientHandle); |
|
269 } |
|
270 |
|
271 // END OF FILE |