diff -r 666f914201fb -r 2fe1408b6811 epoc32/include/comms-infras/metatype.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/epoc32/include/comms-infras/metatype.h Tue Mar 16 16:12:26 2010 +0000 @@ -0,0 +1,442 @@ +/** +* Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). +* All rights reserved. +* This component and the accompanying materials are made available +* 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 +* which accompanies this distribution, and is available +* at the URL "http://www.symbianfoundation.org/legal/licencesv10.html". +* +* Initial Contributors: +* Nokia Corporation - initial contribution. +* +* Contributors: +* +* Description: +* +*/ + + + + + +/** + @file MetaType.h + @internalTechnology +*/ + +#if (!defined METATYPE_H) +#define METATYPE_H + +#include +#include + +namespace Meta +{ + +template class TMeta; +typedef TMeta TMetaNumber; +typedef TMeta TMetaTime; + +class MMetaType +/** + +Abstract interface of a helper meta type. A meta type is responsible for handling +a particular type. A meta type knows how to copy an object of the type, store it +in a descriptor, load, etc, etc. + +@internalComponent +@released since v9.0 */ + { +public: + virtual TInt Load(TPtrC8& aBuffer) = 0; + virtual TInt Store(TDes8& aBuffer) const = 0; + virtual void Copy(const TAny* aData) = 0; + virtual TInt Length() const = 0; + }; + +template +class TMeta : public MMetaType +/** + +Implementation of MMetaType for simple types (e.g. symbian T-types). + +@internalComponent +@released since v9.0 */ + { +public: + inline static MMetaType* NewL(const TAny* aMem, const TAny* aData); + +public: + virtual TInt Load(TPtrC8& aBuffer); + virtual TInt Store(TDes8& aBuffer) const; + virtual void Copy(const TAny* aData); + virtual TInt Length() const; + +protected: + TMeta(const TAny* aData); + +private: + const TYPE* iData; + }; + +template +class TMetaObject : public MMetaType +/** + +Implementation of MMetaType for meta objects + +@internalComponent +@released since v9.0 */ + { +public: + inline static MMetaType* NewL(const TAny* aMem, const TAny* aData); + +public: + virtual TInt Load(TPtrC8& aBuffer); + virtual TInt Store(TDes8& aBuffer) const; + virtual void Copy(const TAny* aData); + virtual TInt Length() const; + +protected: + TMetaObject(const TAny* aData); + +private: + TYPE* iData; + }; + +template +class TMetaObjectPtr : public MMetaType +/** + +Implementation of MMetaType for meta pointers to objects that are never created + +@internalComponent +@released since v9.0 */ + { +public: + inline static MMetaType* NewL(const TAny* aMem, const TAny* aData); + +public: + virtual TInt Load(TPtrC8& aBuffer); + virtual TInt Store(TDes8& aBuffer) const; + virtual void Copy(const TAny* aData); + virtual TInt Length() const; + +protected: + TMetaObjectPtr(const TAny* aData); + +private: + TYPE** iData; + }; + +template +class TMetaPtr : public MMetaType +/** + +Implementation of MMetaType for pointers to meta objects + +@internalComponent +@released since v9.0 */ + { +public: + inline static MMetaType* NewL(const TAny* aMem, const TAny* aData); + + virtual TInt Load(TPtrC8& aBuffer); + virtual TInt Store(TDes8& aBuffer) const; + virtual void Copy(const TAny* aData); + virtual TInt Length() const; + +protected: + TMetaPtr(const TAny* aData); + +private: + TYPE** iData; + }; + +template +MMetaType* TMeta::NewL(const TAny* aMem, const TAny* aData) +/** + * Instantiates a meta type of a particular type. + * Used for attribure registration (in the data v-table). + */ + { + return ::new ((TUint8*)aMem) TMeta(aData); + } + +template +inline TMeta::TMeta(const TAny* aData) +: iData((TYPE*)aData) +/** + * Constructor + */ + { + __ASSERT_DEBUG(iData!=NULL,User::Panic(_L("TMetaType"),KErrArgument)); + } + +template +TInt TMeta::Load(TPtrC8& aBuffer) +/** + * Loads content of a meta object (in iData) from a descriptor + */ + { + TInt len = Length(); + if (aBuffer.Length() < len) + { + return KErrArgument; + } + Mem::Copy((TYPE*)iData,aBuffer.Ptr(),len); + aBuffer.Set(aBuffer.Ptr()+len, aBuffer.Length()-len); //update pointer + return KErrNone; + } + +template +TInt TMeta::Store(TDes8& aBuffer) const +/** + * Stores content of a meta object (in iData) to a descriptor + */ + { + TInt len = Length(); + if (aBuffer.MaxLength() - aBuffer.Length() < len) + { + return KErrOverflow; + } + aBuffer.Append((TUint8*)iData, len); + return KErrNone; + } + +template +void TMeta::Copy(const TAny* aData) +/** + * Copies content of a meta object (in aData) into another meta object (in iData). + * This is a MMetaType implementation for simple (T) types so it just copies the memory. + */ + { + Mem::Copy((TAny*)iData,aData,Length()); + } + +template +TInt TMeta::Length() const +/** + * Returns the length of the handled meta object. + */ + { + return sizeof(TYPE); + } + +template +MMetaType* TMetaObject::NewL(const TAny* aMem, const TAny* aData) +/** + * Instantiates a meta type of a particular type. + * Used for attribure registration (in the data v-table). + */ + { + return ::new ((TUint8*)aMem) TMetaObject(aData); + } + +template +inline TMetaObject::TMetaObject(const TAny* aData) +: iData((TYPE*)aData) +/** + * Constructor + */ + { + __ASSERT_DEBUG(iData!=NULL,User::Panic(_L("TMetaType"),KErrArgument)); + } + +template +TInt TMetaObject::Load(TPtrC8& aBuffer) +/** + * Loads content of a meta object (in iData) from a descriptor + */ + { + TInt err = iData->GetTypeId().Check( aBuffer ); + return err == KErrNone ? iData->Load(aBuffer) : err; + } + +template +TInt TMetaObject::Store(TDes8& aBuffer) const +/** + * Stores content of a meta object (in iData) to a descriptor + */ + { + return iData->Store(aBuffer); + } + +template +void TMetaObject::Copy(const TAny* aData) +/** + * Copies content of a meta object (in aData) into another meta object (in iData). + */ + { + iData->Copy(*((TYPE*)aData)); + } + +template +TInt TMetaObject::Length() const +/** + * Returns the length of the handled meta object. + */ + { + return iData->Length(); + } + +template +MMetaType* TMetaObjectPtr::NewL(const TAny* aMem, const TAny* aData) +/** + * Instantiates a meta type of a particular type. + * Used for attribure registration (in the data v-table). + */ + { + return ::new ((TUint8*)aMem) TMetaObjectPtr(aData); + } + +template +inline TMetaObjectPtr::TMetaObjectPtr(const TAny* aData) +: iData((TYPE**)aData) +/** + * Constructor + */ + { + __ASSERT_DEBUG(iData!=NULL,User::Panic(_L("TMetaType"),KErrArgument)); + __ASSERT_DEBUG(*iData!=NULL,User::Panic(_L("TMetaType"),KErrArgument)); + } + +template +TInt TMetaObjectPtr::Load(TPtrC8& aBuffer) +/** + * Loads content of a meta object (in iData) from a descriptor + */ + { + TInt err = (*iData)->GetTypeId().Check( aBuffer ); + return err == KErrNone ? (*iData)->Load(aBuffer) : err; + } + +template +TInt TMetaObjectPtr::Store(TDes8& aBuffer) const +/** + * Stores content of a meta object (in iData) to a descriptor + */ + { + return (*iData)->Store(aBuffer); + } + +template +void TMetaObjectPtr::Copy(const TAny* aData) +/** + * Copies content of a meta object (in aData) into another meta object (in iData). + */ + { + (*iData)->Copy(*((TYPE*)aData)); + } + +template +TInt TMetaObjectPtr::Length() const +/** + * Returns the length of the handled meta object. + */ + { + return (*iData)->Length(); + } + +template +inline TMetaPtr::TMetaPtr(const TAny* aData) +: iData((TYPE**)aData) +/** + * Constructor + */ + { + } + +template +MMetaType* TMetaPtr::NewL(const TAny* aMem, const TAny* aData) +/** + * Instantiates a meta type of a particular type. + * Used for attribure registration (in the data v-table). + */ + { + return ::new ((TUint8*)aMem) TMetaPtr(aData); + } + +template +TInt TMetaPtr::Load(TPtrC8& aBuffer) +/** + * Loads content of a meta object (in iData) from a descriptor + */ + { + if (aBuffer.Length() < (TInt) sizeof(TUint32)) + { + return KErrArgument; + } + + // Check for a NULL pointer when stored (First four bytes == 0) + if (*((TUint32*)aBuffer.Ptr()) == 0) + { + // The pointer was NULL when it was stored + aBuffer.Set(aBuffer.Ptr() + sizeof(TUint32), aBuffer.Length() - sizeof(TUint32)); + if (*iData != NULL) + { + delete *iData; + *iData = NULL; + } + return KErrNone; + } + + if (*iData == NULL) + { + TRAPD(ret, *iData = reinterpret_cast(TYPE::LoadL(aBuffer))); + return ret; + } + else + { + return (*iData)->Load(aBuffer); + } + } + +template +TInt TMetaPtr::Store(TDes8& aBuffer) const +/** + * Stores content of a meta object (in iData) to a descriptor + */ + { + if (*iData == NULL) + { + const TUint32 KNullInt = 0; + aBuffer.Append((TUint8*)&KNullInt, sizeof(TUint32)); + return KErrNone; + } + else + { + return (*iData)->Store(aBuffer); + } + } + +template +void TMetaPtr::Copy(const TAny* aData) +/** + * Copies content of a meta object (in aData) into another meta object (in iData). + */ + { + (*iData)->Copy(*((TYPE*)aData)); + } + +template +TInt TMetaPtr::Length() const +/** + * Returns the length of the handled meta object. + */ + { + if (*iData == NULL) + { + // Add length of null word (Used to represent a NULL pointer) + return sizeof(TUint32); + } + else + { + return (*iData)->Length(); + } + } + +} //namespace Meta + + +#endif //METATYPE_H