1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/epoc32/include/comms-infras/metatype.h Tue Mar 16 16:12:26 2010 +0000
1.3 @@ -0,0 +1,442 @@
1.4 +/**
1.5 +* Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* 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
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.symbianfoundation.org/legal/licencesv10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +
1.23 +
1.24 +
1.25 +/**
1.26 + @file MetaType.h
1.27 + @internalTechnology
1.28 +*/
1.29 +
1.30 +#if (!defined METATYPE_H)
1.31 +#define METATYPE_H
1.32 +
1.33 +#include <e32base.h>
1.34 +#include <e32std.h>
1.35 +
1.36 +namespace Meta
1.37 +{
1.38 +
1.39 +template<class TYPE> class TMeta;
1.40 +typedef TMeta<TInt> TMetaNumber;
1.41 +typedef TMeta<TTime> TMetaTime;
1.42 +
1.43 +class MMetaType
1.44 +/**
1.45 +
1.46 +Abstract interface of a helper meta type. A meta type is responsible for handling
1.47 +a particular type. A meta type knows how to copy an object of the type, store it
1.48 +in a descriptor, load, etc, etc.
1.49 +
1.50 +@internalComponent
1.51 +@released since v9.0 */
1.52 + {
1.53 +public:
1.54 + virtual TInt Load(TPtrC8& aBuffer) = 0;
1.55 + virtual TInt Store(TDes8& aBuffer) const = 0;
1.56 + virtual void Copy(const TAny* aData) = 0;
1.57 + virtual TInt Length() const = 0;
1.58 + };
1.59 +
1.60 +template<class TYPE>
1.61 +class TMeta : public MMetaType
1.62 +/**
1.63 +
1.64 +Implementation of MMetaType for simple types (e.g. symbian T-types).
1.65 +
1.66 +@internalComponent
1.67 +@released since v9.0 */
1.68 + {
1.69 +public:
1.70 + inline static MMetaType* NewL(const TAny* aMem, const TAny* aData);
1.71 +
1.72 +public:
1.73 + virtual TInt Load(TPtrC8& aBuffer);
1.74 + virtual TInt Store(TDes8& aBuffer) const;
1.75 + virtual void Copy(const TAny* aData);
1.76 + virtual TInt Length() const;
1.77 +
1.78 +protected:
1.79 + TMeta(const TAny* aData);
1.80 +
1.81 +private:
1.82 + const TYPE* iData;
1.83 + };
1.84 +
1.85 +template<class TYPE>
1.86 +class TMetaObject : public MMetaType
1.87 +/**
1.88 +
1.89 +Implementation of MMetaType for meta objects
1.90 +
1.91 +@internalComponent
1.92 +@released since v9.0 */
1.93 + {
1.94 +public:
1.95 + inline static MMetaType* NewL(const TAny* aMem, const TAny* aData);
1.96 +
1.97 +public:
1.98 + virtual TInt Load(TPtrC8& aBuffer);
1.99 + virtual TInt Store(TDes8& aBuffer) const;
1.100 + virtual void Copy(const TAny* aData);
1.101 + virtual TInt Length() const;
1.102 +
1.103 +protected:
1.104 + TMetaObject(const TAny* aData);
1.105 +
1.106 +private:
1.107 + TYPE* iData;
1.108 + };
1.109 +
1.110 +template<class TYPE>
1.111 +class TMetaObjectPtr : public MMetaType
1.112 +/**
1.113 +
1.114 +Implementation of MMetaType for meta pointers to objects that are never created
1.115 +
1.116 +@internalComponent
1.117 +@released since v9.0 */
1.118 + {
1.119 +public:
1.120 + inline static MMetaType* NewL(const TAny* aMem, const TAny* aData);
1.121 +
1.122 +public:
1.123 + virtual TInt Load(TPtrC8& aBuffer);
1.124 + virtual TInt Store(TDes8& aBuffer) const;
1.125 + virtual void Copy(const TAny* aData);
1.126 + virtual TInt Length() const;
1.127 +
1.128 +protected:
1.129 + TMetaObjectPtr(const TAny* aData);
1.130 +
1.131 +private:
1.132 + TYPE** iData;
1.133 + };
1.134 +
1.135 +template<class TYPE>
1.136 +class TMetaPtr : public MMetaType
1.137 +/**
1.138 +
1.139 +Implementation of MMetaType for pointers to meta objects
1.140 +
1.141 +@internalComponent
1.142 +@released since v9.0 */
1.143 + {
1.144 +public:
1.145 + inline static MMetaType* NewL(const TAny* aMem, const TAny* aData);
1.146 +
1.147 + virtual TInt Load(TPtrC8& aBuffer);
1.148 + virtual TInt Store(TDes8& aBuffer) const;
1.149 + virtual void Copy(const TAny* aData);
1.150 + virtual TInt Length() const;
1.151 +
1.152 +protected:
1.153 + TMetaPtr(const TAny* aData);
1.154 +
1.155 +private:
1.156 + TYPE** iData;
1.157 + };
1.158 +
1.159 +template<class TYPE>
1.160 +MMetaType* TMeta<TYPE>::NewL(const TAny* aMem, const TAny* aData)
1.161 +/**
1.162 + * Instantiates a meta type of a particular type.
1.163 + * Used for attribure registration (in the data v-table).
1.164 + */
1.165 + {
1.166 + return ::new ((TUint8*)aMem) TMeta<TYPE>(aData);
1.167 + }
1.168 +
1.169 +template<class TYPE>
1.170 +inline TMeta<TYPE>::TMeta(const TAny* aData)
1.171 +: iData((TYPE*)aData)
1.172 +/**
1.173 + * Constructor
1.174 + */
1.175 + {
1.176 + __ASSERT_DEBUG(iData!=NULL,User::Panic(_L("TMetaType"),KErrArgument));
1.177 + }
1.178 +
1.179 +template<class TYPE>
1.180 +TInt TMeta<TYPE>::Load(TPtrC8& aBuffer)
1.181 +/**
1.182 + * Loads content of a meta object (in iData) from a descriptor
1.183 + */
1.184 + {
1.185 + TInt len = Length();
1.186 + if (aBuffer.Length() < len)
1.187 + {
1.188 + return KErrArgument;
1.189 + }
1.190 + Mem::Copy((TYPE*)iData,aBuffer.Ptr(),len);
1.191 + aBuffer.Set(aBuffer.Ptr()+len, aBuffer.Length()-len); //update pointer
1.192 + return KErrNone;
1.193 + }
1.194 +
1.195 +template<class TYPE>
1.196 +TInt TMeta<TYPE>::Store(TDes8& aBuffer) const
1.197 +/**
1.198 + * Stores content of a meta object (in iData) to a descriptor
1.199 + */
1.200 + {
1.201 + TInt len = Length();
1.202 + if (aBuffer.MaxLength() - aBuffer.Length() < len)
1.203 + {
1.204 + return KErrOverflow;
1.205 + }
1.206 + aBuffer.Append((TUint8*)iData, len);
1.207 + return KErrNone;
1.208 + }
1.209 +
1.210 +template<class TYPE>
1.211 +void TMeta<TYPE>::Copy(const TAny* aData)
1.212 +/**
1.213 + * Copies content of a meta object (in aData) into another meta object (in iData).
1.214 + * This is a MMetaType implementation for simple (T) types so it just copies the memory.
1.215 + */
1.216 + {
1.217 + Mem::Copy((TAny*)iData,aData,Length());
1.218 + }
1.219 +
1.220 +template<class TYPE>
1.221 +TInt TMeta<TYPE>::Length() const
1.222 +/**
1.223 + * Returns the length of the handled meta object.
1.224 + */
1.225 + {
1.226 + return sizeof(TYPE);
1.227 + }
1.228 +
1.229 +template<class TYPE>
1.230 +MMetaType* TMetaObject<TYPE>::NewL(const TAny* aMem, const TAny* aData)
1.231 +/**
1.232 + * Instantiates a meta type of a particular type.
1.233 + * Used for attribure registration (in the data v-table).
1.234 + */
1.235 + {
1.236 + return ::new ((TUint8*)aMem) TMetaObject<TYPE>(aData);
1.237 + }
1.238 +
1.239 +template<class TYPE>
1.240 +inline TMetaObject<TYPE>::TMetaObject(const TAny* aData)
1.241 +: iData((TYPE*)aData)
1.242 +/**
1.243 + * Constructor
1.244 + */
1.245 + {
1.246 + __ASSERT_DEBUG(iData!=NULL,User::Panic(_L("TMetaType"),KErrArgument));
1.247 + }
1.248 +
1.249 +template<class TYPE>
1.250 +TInt TMetaObject<TYPE>::Load(TPtrC8& aBuffer)
1.251 +/**
1.252 + * Loads content of a meta object (in iData) from a descriptor
1.253 + */
1.254 + {
1.255 + TInt err = iData->GetTypeId().Check( aBuffer );
1.256 + return err == KErrNone ? iData->Load(aBuffer) : err;
1.257 + }
1.258 +
1.259 +template<class TYPE>
1.260 +TInt TMetaObject<TYPE>::Store(TDes8& aBuffer) const
1.261 +/**
1.262 + * Stores content of a meta object (in iData) to a descriptor
1.263 + */
1.264 + {
1.265 + return iData->Store(aBuffer);
1.266 + }
1.267 +
1.268 +template<class TYPE>
1.269 +void TMetaObject<TYPE>::Copy(const TAny* aData)
1.270 +/**
1.271 + * Copies content of a meta object (in aData) into another meta object (in iData).
1.272 + */
1.273 + {
1.274 + iData->Copy(*((TYPE*)aData));
1.275 + }
1.276 +
1.277 +template<class TYPE>
1.278 +TInt TMetaObject<TYPE>::Length() const
1.279 +/**
1.280 + * Returns the length of the handled meta object.
1.281 + */
1.282 + {
1.283 + return iData->Length();
1.284 + }
1.285 +
1.286 +template<class TYPE>
1.287 +MMetaType* TMetaObjectPtr<TYPE>::NewL(const TAny* aMem, const TAny* aData)
1.288 +/**
1.289 + * Instantiates a meta type of a particular type.
1.290 + * Used for attribure registration (in the data v-table).
1.291 + */
1.292 + {
1.293 + return ::new ((TUint8*)aMem) TMetaObjectPtr<TYPE>(aData);
1.294 + }
1.295 +
1.296 +template<class TYPE>
1.297 +inline TMetaObjectPtr<TYPE>::TMetaObjectPtr(const TAny* aData)
1.298 +: iData((TYPE**)aData)
1.299 +/**
1.300 + * Constructor
1.301 + */
1.302 + {
1.303 + __ASSERT_DEBUG(iData!=NULL,User::Panic(_L("TMetaType"),KErrArgument));
1.304 + __ASSERT_DEBUG(*iData!=NULL,User::Panic(_L("TMetaType"),KErrArgument));
1.305 + }
1.306 +
1.307 +template<class TYPE>
1.308 +TInt TMetaObjectPtr<TYPE>::Load(TPtrC8& aBuffer)
1.309 +/**
1.310 + * Loads content of a meta object (in iData) from a descriptor
1.311 + */
1.312 + {
1.313 + TInt err = (*iData)->GetTypeId().Check( aBuffer );
1.314 + return err == KErrNone ? (*iData)->Load(aBuffer) : err;
1.315 + }
1.316 +
1.317 +template<class TYPE>
1.318 +TInt TMetaObjectPtr<TYPE>::Store(TDes8& aBuffer) const
1.319 +/**
1.320 + * Stores content of a meta object (in iData) to a descriptor
1.321 + */
1.322 + {
1.323 + return (*iData)->Store(aBuffer);
1.324 + }
1.325 +
1.326 +template<class TYPE>
1.327 +void TMetaObjectPtr<TYPE>::Copy(const TAny* aData)
1.328 +/**
1.329 + * Copies content of a meta object (in aData) into another meta object (in iData).
1.330 + */
1.331 + {
1.332 + (*iData)->Copy(*((TYPE*)aData));
1.333 + }
1.334 +
1.335 +template<class TYPE>
1.336 +TInt TMetaObjectPtr<TYPE>::Length() const
1.337 +/**
1.338 + * Returns the length of the handled meta object.
1.339 + */
1.340 + {
1.341 + return (*iData)->Length();
1.342 + }
1.343 +
1.344 +template<class TYPE>
1.345 +inline TMetaPtr<TYPE>::TMetaPtr(const TAny* aData)
1.346 +: iData((TYPE**)aData)
1.347 +/**
1.348 + * Constructor
1.349 + */
1.350 + {
1.351 + }
1.352 +
1.353 +template<class TYPE>
1.354 +MMetaType* TMetaPtr<TYPE>::NewL(const TAny* aMem, const TAny* aData)
1.355 +/**
1.356 + * Instantiates a meta type of a particular type.
1.357 + * Used for attribure registration (in the data v-table).
1.358 + */
1.359 + {
1.360 + return ::new ((TUint8*)aMem) TMetaPtr<TYPE>(aData);
1.361 + }
1.362 +
1.363 +template<class TYPE>
1.364 +TInt TMetaPtr<TYPE>::Load(TPtrC8& aBuffer)
1.365 +/**
1.366 + * Loads content of a meta object (in iData) from a descriptor
1.367 + */
1.368 + {
1.369 + if (aBuffer.Length() < (TInt) sizeof(TUint32))
1.370 + {
1.371 + return KErrArgument;
1.372 + }
1.373 +
1.374 + // Check for a NULL pointer when stored (First four bytes == 0)
1.375 + if (*((TUint32*)aBuffer.Ptr()) == 0)
1.376 + {
1.377 + // The pointer was NULL when it was stored
1.378 + aBuffer.Set(aBuffer.Ptr() + sizeof(TUint32), aBuffer.Length() - sizeof(TUint32));
1.379 + if (*iData != NULL)
1.380 + {
1.381 + delete *iData;
1.382 + *iData = NULL;
1.383 + }
1.384 + return KErrNone;
1.385 + }
1.386 +
1.387 + if (*iData == NULL)
1.388 + {
1.389 + TRAPD(ret, *iData = reinterpret_cast<TYPE*>(TYPE::LoadL(aBuffer)));
1.390 + return ret;
1.391 + }
1.392 + else
1.393 + {
1.394 + return (*iData)->Load(aBuffer);
1.395 + }
1.396 + }
1.397 +
1.398 +template<class TYPE>
1.399 +TInt TMetaPtr<TYPE>::Store(TDes8& aBuffer) const
1.400 +/**
1.401 + * Stores content of a meta object (in iData) to a descriptor
1.402 + */
1.403 + {
1.404 + if (*iData == NULL)
1.405 + {
1.406 + const TUint32 KNullInt = 0;
1.407 + aBuffer.Append((TUint8*)&KNullInt, sizeof(TUint32));
1.408 + return KErrNone;
1.409 + }
1.410 + else
1.411 + {
1.412 + return (*iData)->Store(aBuffer);
1.413 + }
1.414 + }
1.415 +
1.416 +template<class TYPE>
1.417 +void TMetaPtr<TYPE>::Copy(const TAny* aData)
1.418 +/**
1.419 + * Copies content of a meta object (in aData) into another meta object (in iData).
1.420 + */
1.421 + {
1.422 + (*iData)->Copy(*((TYPE*)aData));
1.423 + }
1.424 +
1.425 +template<class TYPE>
1.426 +TInt TMetaPtr<TYPE>::Length() const
1.427 +/**
1.428 + * Returns the length of the handled meta object.
1.429 + */
1.430 + {
1.431 + if (*iData == NULL)
1.432 + {
1.433 + // Add length of null word (Used to represent a NULL pointer)
1.434 + return sizeof(TUint32);
1.435 + }
1.436 + else
1.437 + {
1.438 + return (*iData)->Length();
1.439 + }
1.440 + }
1.441 +
1.442 +} //namespace Meta
1.443 +
1.444 +
1.445 +#endif //METATYPE_H