2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Symbian Foundation License v1.0" to Symbian Foundation members and "Symbian Foundation End User License Agreement v1.0" to non-members
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.symbianfoundation.org/legal/licencesv10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
27 #if (!defined METATYPE_H)
36 template<class TYPE> class TMeta;
37 typedef TMeta<TInt> TMetaNumber;
38 typedef TMeta<TTime> TMetaTime;
43 Abstract interface of a helper meta type. A meta type is responsible for handling
44 a particular type. A meta type knows how to copy an object of the type, store it
45 in a descriptor, load, etc, etc.
48 @released since v9.0 */
51 virtual TInt Load(TPtrC8& aBuffer) = 0;
52 virtual TInt Store(TDes8& aBuffer) const = 0;
53 virtual void Copy(const TAny* aData) = 0;
54 virtual TInt Length() const = 0;
58 class TMeta : public MMetaType
61 Implementation of MMetaType for simple types (e.g. symbian T-types).
64 @released since v9.0 */
67 inline static MMetaType* NewL(const TAny* aMem, const TAny* aData);
70 virtual TInt Load(TPtrC8& aBuffer);
71 virtual TInt Store(TDes8& aBuffer) const;
72 virtual void Copy(const TAny* aData);
73 virtual TInt Length() const;
76 TMeta(const TAny* aData);
83 class TMetaObject : public MMetaType
86 Implementation of MMetaType for meta objects
89 @released since v9.0 */
92 inline static MMetaType* NewL(const TAny* aMem, const TAny* aData);
95 virtual TInt Load(TPtrC8& aBuffer);
96 virtual TInt Store(TDes8& aBuffer) const;
97 virtual void Copy(const TAny* aData);
98 virtual TInt Length() const;
101 TMetaObject(const TAny* aData);
108 class TMetaObjectPtr : public MMetaType
111 Implementation of MMetaType for meta pointers to objects that are never created
114 @released since v9.0 */
117 inline static MMetaType* NewL(const TAny* aMem, const TAny* aData);
120 virtual TInt Load(TPtrC8& aBuffer);
121 virtual TInt Store(TDes8& aBuffer) const;
122 virtual void Copy(const TAny* aData);
123 virtual TInt Length() const;
126 TMetaObjectPtr(const TAny* aData);
133 class TMetaPtr : public MMetaType
136 Implementation of MMetaType for pointers to meta objects
139 @released since v9.0 */
142 inline static MMetaType* NewL(const TAny* aMem, const TAny* aData);
144 virtual TInt Load(TPtrC8& aBuffer);
145 virtual TInt Store(TDes8& aBuffer) const;
146 virtual void Copy(const TAny* aData);
147 virtual TInt Length() const;
150 TMetaPtr(const TAny* aData);
157 MMetaType* TMeta<TYPE>::NewL(const TAny* aMem, const TAny* aData)
159 * Instantiates a meta type of a particular type.
160 * Used for attribure registration (in the data v-table).
163 return ::new ((TUint8*)aMem) TMeta<TYPE>(aData);
167 inline TMeta<TYPE>::TMeta(const TAny* aData)
168 : iData((TYPE*)aData)
173 __ASSERT_DEBUG(iData!=NULL,User::Panic(_L("TMetaType"),KErrArgument));
177 TInt TMeta<TYPE>::Load(TPtrC8& aBuffer)
179 * Loads content of a meta object (in iData) from a descriptor
183 if (aBuffer.Length() < len)
187 Mem::Copy((TYPE*)iData,aBuffer.Ptr(),len);
188 aBuffer.Set(aBuffer.Ptr()+len, aBuffer.Length()-len); //update pointer
193 TInt TMeta<TYPE>::Store(TDes8& aBuffer) const
195 * Stores content of a meta object (in iData) to a descriptor
199 if (aBuffer.MaxLength() - aBuffer.Length() < len)
203 aBuffer.Append((TUint8*)iData, len);
208 void TMeta<TYPE>::Copy(const TAny* aData)
210 * Copies content of a meta object (in aData) into another meta object (in iData).
211 * This is a MMetaType implementation for simple (T) types so it just copies the memory.
214 Mem::Copy((TAny*)iData,aData,Length());
218 TInt TMeta<TYPE>::Length() const
220 * Returns the length of the handled meta object.
227 MMetaType* TMetaObject<TYPE>::NewL(const TAny* aMem, const TAny* aData)
229 * Instantiates a meta type of a particular type.
230 * Used for attribure registration (in the data v-table).
233 return ::new ((TUint8*)aMem) TMetaObject<TYPE>(aData);
237 inline TMetaObject<TYPE>::TMetaObject(const TAny* aData)
238 : iData((TYPE*)aData)
243 __ASSERT_DEBUG(iData!=NULL,User::Panic(_L("TMetaType"),KErrArgument));
247 TInt TMetaObject<TYPE>::Load(TPtrC8& aBuffer)
249 * Loads content of a meta object (in iData) from a descriptor
252 TInt err = iData->GetTypeId().Check( aBuffer );
253 return err == KErrNone ? iData->Load(aBuffer) : err;
257 TInt TMetaObject<TYPE>::Store(TDes8& aBuffer) const
259 * Stores content of a meta object (in iData) to a descriptor
262 return iData->Store(aBuffer);
266 void TMetaObject<TYPE>::Copy(const TAny* aData)
268 * Copies content of a meta object (in aData) into another meta object (in iData).
271 iData->Copy(*((TYPE*)aData));
275 TInt TMetaObject<TYPE>::Length() const
277 * Returns the length of the handled meta object.
280 return iData->Length();
284 MMetaType* TMetaObjectPtr<TYPE>::NewL(const TAny* aMem, const TAny* aData)
286 * Instantiates a meta type of a particular type.
287 * Used for attribure registration (in the data v-table).
290 return ::new ((TUint8*)aMem) TMetaObjectPtr<TYPE>(aData);
294 inline TMetaObjectPtr<TYPE>::TMetaObjectPtr(const TAny* aData)
295 : iData((TYPE**)aData)
300 __ASSERT_DEBUG(iData!=NULL,User::Panic(_L("TMetaType"),KErrArgument));
301 __ASSERT_DEBUG(*iData!=NULL,User::Panic(_L("TMetaType"),KErrArgument));
305 TInt TMetaObjectPtr<TYPE>::Load(TPtrC8& aBuffer)
307 * Loads content of a meta object (in iData) from a descriptor
310 TInt err = (*iData)->GetTypeId().Check( aBuffer );
311 return err == KErrNone ? (*iData)->Load(aBuffer) : err;
315 TInt TMetaObjectPtr<TYPE>::Store(TDes8& aBuffer) const
317 * Stores content of a meta object (in iData) to a descriptor
320 return (*iData)->Store(aBuffer);
324 void TMetaObjectPtr<TYPE>::Copy(const TAny* aData)
326 * Copies content of a meta object (in aData) into another meta object (in iData).
329 (*iData)->Copy(*((TYPE*)aData));
333 TInt TMetaObjectPtr<TYPE>::Length() const
335 * Returns the length of the handled meta object.
338 return (*iData)->Length();
342 inline TMetaPtr<TYPE>::TMetaPtr(const TAny* aData)
343 : iData((TYPE**)aData)
351 MMetaType* TMetaPtr<TYPE>::NewL(const TAny* aMem, const TAny* aData)
353 * Instantiates a meta type of a particular type.
354 * Used for attribure registration (in the data v-table).
357 return ::new ((TUint8*)aMem) TMetaPtr<TYPE>(aData);
361 TInt TMetaPtr<TYPE>::Load(TPtrC8& aBuffer)
363 * Loads content of a meta object (in iData) from a descriptor
366 if (aBuffer.Length() < (TInt) sizeof(TUint32))
371 // Check for a NULL pointer when stored (First four bytes == 0)
372 if (*((TUint32*)aBuffer.Ptr()) == 0)
374 // The pointer was NULL when it was stored
375 aBuffer.Set(aBuffer.Ptr() + sizeof(TUint32), aBuffer.Length() - sizeof(TUint32));
386 TRAPD(ret, *iData = reinterpret_cast<TYPE*>(TYPE::LoadL(aBuffer)));
391 return (*iData)->Load(aBuffer);
396 TInt TMetaPtr<TYPE>::Store(TDes8& aBuffer) const
398 * Stores content of a meta object (in iData) to a descriptor
403 const TUint32 KNullInt = 0;
404 aBuffer.Append((TUint8*)&KNullInt, sizeof(TUint32));
409 return (*iData)->Store(aBuffer);
414 void TMetaPtr<TYPE>::Copy(const TAny* aData)
416 * Copies content of a meta object (in aData) into another meta object (in iData).
419 (*iData)->Copy(*((TYPE*)aData));
423 TInt TMetaPtr<TYPE>::Length() const
425 * Returns the length of the handled meta object.
430 // Add length of null word (Used to represent a NULL pointer)
431 return sizeof(TUint32);
435 return (*iData)->Length();