epoc32/include/comms-infras/metatypevariablelen.h
author William Roberts <williamr@symbian.org>
Tue, 16 Mar 2010 16:12:26 +0000
branchSymbian2
changeset 2 2fe1408b6811
permissions -rw-r--r--
Final list of Symbian^2 public API header files
     1 // Copyright (c) 2004-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 the License "Symbian Foundation License v1.0" to Symbian Foundation members and "Symbian Foundation End User License Agreement v1.0" to non-members
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.symbianfoundation.org/legal/licencesv10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // MetaTypeVariableLen.h
    15 // 
    16 //
    17 
    18 
    19 
    20 /**
    21  @file MetaTypeVariableLen.h
    22  @internalTechnology
    23 */
    24 
    25 #if (!defined METATYPEVARIABLELEN_H)
    26 #define METATYPEVARIABLELEN_H
    27 
    28 #include <comms-infras/metatype.h>
    29 
    30 namespace Meta
    31 {
    32 
    33 template<class TYPE> class TMetaVarLen8;
    34 template<class TYPE> class TMetaVarLen16;
    35 typedef TMetaVarLen8<TDes8> TMetaDes8;
    36 typedef TMetaVarLen16<TDes16> TMetaDes16;
    37 
    38 #if defined(_UNICODE)
    39 typedef TMetaDes16 TMetaDes;
    40 #else
    41 typedef TMetaDes8 TMetaDes;
    42 #endif
    43 
    44 class TMetaVarLenBase : public MMetaType
    45 	{
    46 	public:
    47 		IMPORT_C virtual TInt Load(TPtrC8& aBuffer);
    48 		IMPORT_C virtual TInt Store(TDes8& aBuffer) const;
    49 		
    50 	protected:
    51 		TInt CheckBuf( TPtrC8& aBuffer );
    52 		virtual void CopyBuf(TPtrC8& aBuffer,TInt aLen) = 0;
    53 		virtual void AppendToBuf( TDes8& aBuffer ) const = 0;
    54 	};
    55 	
    56 template<class TYPE>
    57 class TMetaVarLen : public TMetaVarLenBase
    58 /**
    59 
    60 Implementation of MMetaType for anything that provides Length(), SetLength(), MaxLength(),
    61 Copy() and Left() functions with arbitrarry element size
    62 
    63 @internalComponent
    64 @released since v9.0 */
    65 	{
    66 public:
    67 	virtual void Copy(const TAny* aData);
    68 
    69 protected:
    70 	TMetaVarLen(const TAny* aData);
    71 
    72 protected:
    73 	TYPE* iData;
    74 	};
    75 
    76 template<class TYPE>
    77 inline TMetaVarLen<TYPE>::TMetaVarLen(const TAny* aData)
    78 :    iData((TYPE*)aData)
    79 /**
    80  * Constructor
    81  */
    82     {
    83     __ASSERT_DEBUG(iData!=NULL,User::Panic(_L("TMetaVarLen"),KErrArgument));
    84     }
    85 
    86 template<class TYPE>
    87 void TMetaVarLen<TYPE>::Copy(const TAny* aData)
    88 /**
    89  * Copies content of the member
    90  */
    91 	{
    92 	const TYPE& var = *((TYPE*)aData);
    93 	iData->Copy(var.Left( iData->MaxLength() ));
    94 	}
    95 
    96 template<class TYPE>
    97 class TMetaVarLen8 : public TMetaVarLen<TYPE>
    98 /**
    99 
   100 Implementation of MMetaType for anything that provides Length(), SetLength(), MaxLength(),
   101 Copy() and Left() functions with element size 1 byte
   102 Like for example TDes8, TBuf8, TPtr8.
   103 
   104 @internalComponent
   105 @released since v9.0 */
   106 	{
   107 public:
   108 	inline static MMetaType* NewL(const TAny* aMem, const TAny* aData);
   109 
   110 	virtual TInt Length() const;
   111 
   112 protected:
   113 	TMetaVarLen8(const TAny* aData);
   114 	virtual void AppendToBuf( TDes8& aBuffer ) const;
   115 	virtual void CopyBuf(TPtrC8& aBuffer,TInt aLen);
   116 
   117 	};
   118 
   119 template<class TYPE>
   120 class TMetaVarLen16 : public TMetaVarLen<TYPE>
   121 /**
   122 
   123 Implementation of MMetaType for anything that provides Length(), SetLength(), MaxLength(),
   124 Copy() and Left() functions with element size 2 bytes
   125 Like for example TDes16, TBuf16, TPtr16 (ELEMENT is short).
   126 
   127 @internalComponent
   128 @released since v9.0 */
   129 	{
   130 public:
   131 	inline static MMetaType* NewL(const TAny* aMem, const TAny* aData);
   132 
   133 	virtual TInt Length() const;
   134 
   135 protected:
   136 	TMetaVarLen16(const TAny* aData);
   137 	virtual void AppendToBuf( TDes8& aBuffer ) const;
   138 	virtual void CopyBuf(TPtrC8& aBuffer,TInt aLen);
   139 
   140 	};
   141 
   142 
   143 template<class TYPE>
   144 inline TMetaVarLen16<TYPE>::TMetaVarLen16(const TAny* aData)
   145 :    TMetaVarLen<TYPE>(aData)
   146 /**
   147  * Constructor
   148  */
   149     {
   150     }
   151 
   152 template<class TYPE>
   153 MMetaType* TMetaVarLen16<TYPE>::NewL(const TAny* aMem, const TAny* aData)
   154 /**
   155  * Instantiates a meta type of a particular type.
   156  * Used for attribure registration (in the data v-table).
   157  */
   158     {
   159     return ::new ((TUint8*)aMem) TMetaVarLen16<TYPE>(aData);
   160     }
   161 
   162 template<class TYPE>
   163 void TMetaVarLen16<TYPE>::AppendToBuf( TDes8& aBuffer ) const
   164 	{
   165 	//copy whatever fits in (no loosing high byte as the ::Copy normally would)
   166 	//a litle bit dirty solution assuming a flat memory model and byte addressing mode
   167 	aBuffer.Append(reinterpret_cast<const TUint8*>(this->iData->Ptr()), this->iData->Length() * 2);
   168 	}
   169 
   170 template<class TYPE>
   171 void TMetaVarLen16<TYPE>::CopyBuf(TPtrC8& aBuffer,TInt aLen)
   172 	{
   173 	//copy whatever fits in
   174 	//a litle bit dirty solution assuming a flat memory model and byte addressing mode
   175 	this->iData->Copy( reinterpret_cast<const TUint16*>(aBuffer.Ptr()), Min( this->iData->MaxLength(), aLen / 2 ));
   176 	}
   177 
   178 template<class TYPE>
   179 TInt TMetaVarLen16<TYPE>::Length() const
   180 /**
   181  * Returns the length of the data member
   182  */
   183 	{
   184 	return this->iData->Length() * 2 + sizeof(TUint32);
   185 	}
   186 
   187 template<class TYPE>
   188 inline TMetaVarLen8<TYPE>::TMetaVarLen8(const TAny* aData)
   189 :    TMetaVarLen<TYPE>(aData)
   190 /**
   191  * Constructor
   192  */
   193     {
   194     }
   195 
   196 template<class TYPE>
   197 MMetaType* TMetaVarLen8<TYPE>::NewL(const TAny* aMem, const TAny* aData)
   198 /**
   199  * Instantiates a meta type of a particular type.
   200  * Used for attribure registration (in the data v-table).
   201  */
   202     {
   203     return ::new ((TUint8*)aMem) TMetaVarLen8<TYPE>(aData);
   204     }
   205 
   206 template<class TYPE>
   207 #ifdef SYMBIAN_NETWORKING_CFTRANSPORT
   208 void TMetaVarLen8<TYPE>::CopyBuf(TPtrC8& aBuffer,TInt aLen)
   209 	{
   210 	//copy whatever fits in
   211 	this->iData->Copy( aBuffer.Left( Min(this->iData->MaxLength(), aLen) ) );
   212 	}
   213 #else
   214 void TMetaVarLen8<TYPE>::CopyBuf(TPtrC8& aBuffer,TInt /*aLen*/)
   215 	{
   216 	this->iData->Copy( aBuffer.Left( this->iData->MaxLength() ) );
   217 	}
   218 #endif
   219 
   220 template<class TYPE>
   221 void TMetaVarLen8<TYPE>::AppendToBuf( TDes8& aBuffer ) const
   222 	{
   223 	aBuffer.Append(*this->iData);
   224 	}
   225 
   226 template<class TYPE>
   227 TInt TMetaVarLen8<TYPE>::Length() const
   228 /**
   229  * Returns the length of the data member
   230  */
   231 	{
   232 	return this->iData->Length() + sizeof(TUint32);
   233 	}
   234 
   235 } //namespace Meta
   236 
   237 
   238 #endif //METATYPE_H