epoc32/include/comms-infras/metatype.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 /**
     2 * Copyright (c) 2004-2009 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 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".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description:
    15 *
    16 */
    17 
    18 
    19 
    20 
    21 
    22 /**
    23  @file MetaType.h
    24  @internalTechnology
    25 */
    26 
    27 #if (!defined METATYPE_H)
    28 #define METATYPE_H
    29 
    30 #include <e32base.h>
    31 #include <e32std.h>
    32 
    33 namespace Meta
    34 {
    35 
    36 template<class TYPE> class TMeta;
    37 typedef TMeta<TInt> TMetaNumber;
    38 typedef TMeta<TTime> TMetaTime;
    39 
    40 class MMetaType
    41 /**
    42 
    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.
    46 
    47 @internalComponent
    48 @released since v9.0 */
    49     {
    50 public:
    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;
    55     };
    56 
    57 template<class TYPE>
    58 class TMeta : public MMetaType
    59 /**
    60 
    61 Implementation of MMetaType for simple types (e.g. symbian T-types).
    62 
    63 @internalComponent
    64 @released since v9.0 */
    65     {
    66 public:
    67     inline static MMetaType* NewL(const TAny* aMem, const TAny* aData);
    68 
    69 public:
    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;
    74 
    75 protected:
    76     TMeta(const TAny* aData);
    77 
    78 private:
    79     const TYPE* iData;
    80     };
    81 
    82 template<class TYPE>
    83 class TMetaObject : public MMetaType
    84 /**
    85 
    86 Implementation of MMetaType for meta objects
    87 
    88 @internalComponent
    89 @released since v9.0 */
    90 	{
    91 public:
    92     inline static MMetaType* NewL(const TAny* aMem, const TAny* aData);
    93 
    94 public:
    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;
    99 
   100 protected:
   101     TMetaObject(const TAny* aData);
   102 
   103 private:
   104     TYPE* iData;
   105 	};
   106 
   107 template<class TYPE>
   108 class TMetaObjectPtr : public MMetaType
   109 /**
   110 
   111 Implementation of MMetaType for meta pointers to objects that are never created
   112 
   113 @internalComponent
   114 @released since v9.0 */
   115 	{
   116 public:
   117     inline static MMetaType* NewL(const TAny* aMem, const TAny* aData);
   118 
   119 public:
   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;
   124 
   125 protected:
   126     TMetaObjectPtr(const TAny* aData);
   127 
   128 private:
   129     TYPE** iData;
   130 	};
   131 
   132 template<class TYPE>
   133 class TMetaPtr : public MMetaType
   134 /**
   135 
   136 Implementation of MMetaType for pointers to meta objects
   137 
   138 @internalComponent
   139 @released since v9.0 */
   140 	{
   141 public:
   142     inline static MMetaType* NewL(const TAny* aMem, const TAny* aData);
   143 
   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;
   148 
   149 protected:
   150     TMetaPtr(const TAny* aData);
   151 
   152 private:
   153     TYPE** iData;
   154 	};
   155 
   156 template<class TYPE>
   157 MMetaType* TMeta<TYPE>::NewL(const TAny* aMem, const TAny* aData)
   158 /**
   159  * Instantiates a meta type of a particular type.
   160  * Used for attribure registration (in the data v-table).
   161  */
   162     {
   163     return ::new ((TUint8*)aMem) TMeta<TYPE>(aData);
   164     }
   165 
   166 template<class TYPE>
   167 inline TMeta<TYPE>::TMeta(const TAny* aData)
   168 :    iData((TYPE*)aData)
   169 /**
   170  * Constructor
   171  */
   172     {
   173     __ASSERT_DEBUG(iData!=NULL,User::Panic(_L("TMetaType"),KErrArgument));
   174     }
   175 
   176 template<class TYPE>
   177 TInt TMeta<TYPE>::Load(TPtrC8& aBuffer)
   178 /**
   179  * Loads content of a meta object (in iData) from a descriptor
   180  */
   181     {
   182 	TInt len = Length();
   183 	if (aBuffer.Length() < len)
   184 		{
   185 		return KErrArgument;
   186 		}
   187     Mem::Copy((TYPE*)iData,aBuffer.Ptr(),len);
   188 	aBuffer.Set(aBuffer.Ptr()+len, aBuffer.Length()-len); //update pointer
   189 	return KErrNone;
   190     }
   191 
   192 template<class TYPE>
   193 TInt TMeta<TYPE>::Store(TDes8& aBuffer) const
   194 /**
   195  * Stores content of a meta object (in iData) to a descriptor
   196  */
   197     {
   198 	TInt len = Length();
   199 	if (aBuffer.MaxLength() - aBuffer.Length() < len)
   200 		{
   201 		return KErrOverflow;
   202 		}
   203 	aBuffer.Append((TUint8*)iData, len);
   204 	return KErrNone;
   205     }
   206 
   207 template<class TYPE>
   208 void TMeta<TYPE>::Copy(const TAny* aData)
   209 /**
   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.
   212  */
   213     {
   214     Mem::Copy((TAny*)iData,aData,Length());
   215     }
   216 
   217 template<class TYPE>
   218 TInt TMeta<TYPE>::Length() const
   219 /**
   220  * Returns the length of the handled meta object.
   221  */
   222     {
   223     return sizeof(TYPE);
   224     }
   225 
   226 template<class TYPE>
   227 MMetaType* TMetaObject<TYPE>::NewL(const TAny* aMem, const TAny* aData)
   228 /**
   229  * Instantiates a meta type of a particular type.
   230  * Used for attribure registration (in the data v-table).
   231  */
   232     {
   233     return ::new ((TUint8*)aMem) TMetaObject<TYPE>(aData);
   234     }
   235 
   236 template<class TYPE>
   237 inline TMetaObject<TYPE>::TMetaObject(const TAny* aData)
   238 :    iData((TYPE*)aData)
   239 /**
   240  * Constructor
   241  */
   242     {
   243     __ASSERT_DEBUG(iData!=NULL,User::Panic(_L("TMetaType"),KErrArgument));
   244     }
   245 
   246 template<class TYPE>
   247 TInt TMetaObject<TYPE>::Load(TPtrC8& aBuffer)
   248 /**
   249  * Loads content of a meta object (in iData) from a descriptor
   250  */
   251    {
   252 	TInt err = iData->GetTypeId().Check( aBuffer );
   253 	return err == KErrNone ? iData->Load(aBuffer) : err;
   254    }
   255 
   256 template<class TYPE>
   257 TInt TMetaObject<TYPE>::Store(TDes8& aBuffer) const
   258 /**
   259  * Stores content of a meta object (in iData) to a descriptor
   260  */
   261     {
   262 	return iData->Store(aBuffer);
   263     }
   264 
   265 template<class TYPE>
   266 void TMetaObject<TYPE>::Copy(const TAny* aData)
   267 /**
   268  * Copies content of a meta object (in aData) into another meta object (in iData).
   269  */
   270     {
   271 	iData->Copy(*((TYPE*)aData));
   272     }
   273 
   274 template<class TYPE>
   275 TInt TMetaObject<TYPE>::Length() const
   276 /**
   277  * Returns the length of the handled meta object.
   278  */
   279     {
   280 	return iData->Length();
   281     }
   282 
   283 template<class TYPE>
   284 MMetaType* TMetaObjectPtr<TYPE>::NewL(const TAny* aMem, const TAny* aData)
   285 /**
   286  * Instantiates a meta type of a particular type.
   287  * Used for attribure registration (in the data v-table).
   288  */
   289     {
   290     return ::new ((TUint8*)aMem) TMetaObjectPtr<TYPE>(aData);
   291     }
   292 
   293 template<class TYPE>
   294 inline TMetaObjectPtr<TYPE>::TMetaObjectPtr(const TAny* aData)
   295 :    iData((TYPE**)aData)
   296 /**
   297  * Constructor
   298  */
   299     {
   300     __ASSERT_DEBUG(iData!=NULL,User::Panic(_L("TMetaType"),KErrArgument));
   301     __ASSERT_DEBUG(*iData!=NULL,User::Panic(_L("TMetaType"),KErrArgument));
   302     }
   303 
   304 template<class TYPE>
   305 TInt TMetaObjectPtr<TYPE>::Load(TPtrC8& aBuffer)
   306 /**
   307  * Loads content of a meta object (in iData) from a descriptor
   308  */
   309    {
   310 	TInt err = (*iData)->GetTypeId().Check( aBuffer );
   311 	return err == KErrNone ? (*iData)->Load(aBuffer) : err;
   312    }
   313 
   314 template<class TYPE>
   315 TInt TMetaObjectPtr<TYPE>::Store(TDes8& aBuffer) const
   316 /**
   317  * Stores content of a meta object (in iData) to a descriptor
   318  */
   319     {
   320 	return (*iData)->Store(aBuffer);
   321     }
   322 
   323 template<class TYPE>
   324 void TMetaObjectPtr<TYPE>::Copy(const TAny* aData)
   325 /**
   326  * Copies content of a meta object (in aData) into another meta object (in iData).
   327  */
   328     {
   329 	(*iData)->Copy(*((TYPE*)aData));
   330     }
   331 
   332 template<class TYPE>
   333 TInt TMetaObjectPtr<TYPE>::Length() const
   334 /**
   335  * Returns the length of the handled meta object.
   336  */
   337     {
   338 	return (*iData)->Length();
   339     }
   340 
   341 template<class TYPE>
   342 inline TMetaPtr<TYPE>::TMetaPtr(const TAny* aData)
   343 :    iData((TYPE**)aData)
   344 /**
   345  * Constructor
   346  */
   347     {
   348     }
   349 
   350 template<class TYPE>
   351 MMetaType* TMetaPtr<TYPE>::NewL(const TAny* aMem, const TAny* aData)
   352 /**
   353  * Instantiates a meta type of a particular type.
   354  * Used for attribure registration (in the data v-table).
   355  */
   356     {
   357     return ::new ((TUint8*)aMem) TMetaPtr<TYPE>(aData);
   358     }
   359 
   360 template<class TYPE>
   361 TInt TMetaPtr<TYPE>::Load(TPtrC8& aBuffer)
   362 /**
   363  * Loads content of a meta object (in iData) from a descriptor
   364  */
   365     {
   366 	if (aBuffer.Length() < (TInt) sizeof(TUint32))
   367 		{
   368 		return KErrArgument;
   369 		}
   370 
   371 	// Check for a NULL pointer when stored (First four bytes == 0)
   372 	if (*((TUint32*)aBuffer.Ptr()) == 0)
   373 		{
   374 		// The pointer was NULL when it was stored
   375 		aBuffer.Set(aBuffer.Ptr() + sizeof(TUint32), aBuffer.Length() - sizeof(TUint32));
   376 		if (*iData != NULL)
   377 			{
   378 			delete *iData;
   379 			*iData = NULL;
   380 			}
   381 		return KErrNone;
   382 		}
   383 
   384 	if (*iData == NULL)
   385 		{
   386 		TRAPD(ret, *iData = reinterpret_cast<TYPE*>(TYPE::LoadL(aBuffer)));
   387 		return ret;
   388 		}
   389 	else
   390 		{
   391 		return (*iData)->Load(aBuffer);
   392 		}
   393     }
   394 
   395 template<class TYPE>
   396 TInt TMetaPtr<TYPE>::Store(TDes8& aBuffer) const
   397 /**
   398  * Stores content of a meta object (in iData) to a descriptor
   399  */
   400     {
   401 	if (*iData == NULL)
   402 		{
   403 		const TUint32 KNullInt = 0;
   404 		aBuffer.Append((TUint8*)&KNullInt, sizeof(TUint32));
   405 		return KErrNone;
   406 		}
   407 	else
   408 		{
   409 		return (*iData)->Store(aBuffer);
   410 		}
   411     }
   412 
   413 template<class TYPE>
   414 void TMetaPtr<TYPE>::Copy(const TAny* aData)
   415 /**
   416  * Copies content of a meta object (in aData) into another meta object (in iData).
   417  */
   418     {
   419 	(*iData)->Copy(*((TYPE*)aData));
   420     }
   421 
   422 template<class TYPE>
   423 TInt TMetaPtr<TYPE>::Length() const
   424 /**
   425  * Returns the length of the handled meta object.
   426  */
   427     {
   428 	if (*iData == NULL)
   429 		{
   430 		// Add length of null word (Used to represent a NULL pointer)
   431 		return sizeof(TUint32);
   432 		}
   433 	else
   434 		{
   435 		return (*iData)->Length();
   436 		}
   437     }
   438 
   439 } //namespace Meta
   440 
   441 
   442 #endif //METATYPE_H