os/ossrv/lowlevellibsandfws/apputils/src/StringPoolImplementation.h
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/lowlevellibsandfws/apputils/src/StringPoolImplementation.h	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,332 @@
     1.4 +// Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#ifndef __STRINGPOOLIMPLEMENTATION_H__
    1.20 +#define __STRINGPOOLIMPLEMENTATION_H__
    1.21 +
    1.22 +#include <e32base.h>
    1.23 +#include <stringpool.h>
    1.24 +#include <stringtablesupport.h>
    1.25 +
    1.26 +/**
    1.27 +This is an 8 bit value with every bit set except bit 5 (value
    1.28 +32). The ASCII codes of upper and lower case characters differ by
    1.29 +32.
    1.30 +@internalComponent 
    1.31 +*/
    1.32 +const TUint KCaseInsensitive = 223;
    1.33 +
    1.34 +/**
    1.35 +@internalComponent 
    1.36 +*/
    1.37 +const TInt KHashModulo = 32;
    1.38 +
    1.39 +/**
    1.40 +Bit 0 represents if it is a table (set) or not (unset).
    1.41 +Bit 1 represents case sensitivity (set) or not (unset)
    1.42 +
    1.43 +For non-static table values (dynamic strings), the remaining bits 
    1.44 +are interpreted as a pointer to a CStringPoolNode (which is assumed 
    1.45 +to be word aligned). 
    1.46 +
    1.47 +Bits 2-20 represent the table index.
    1.48 +Bits 21-31 represent the table uid (that is the address of the table).
    1.49 +*/
    1.50 +
    1.51 +/**
    1.52 +Bit 1 is used in tokens to indicate case sensitivity. So mask it
    1.53 +out to get the node.
    1.54 +@internalComponent 
    1.55 +*/
    1.56 +const TUint KTokenToNode = 0xfffffffd;
    1.57 +
    1.58 +/**
    1.59 +Determine if this id corresponds to a table.
    1.60 +@internalComponent
    1.61 +*/
    1.62 +#define IS_TABLE_ENTRY(tableId)(tableId & 0x01)
    1.63 +
    1.64 +/**
    1.65 +Obtain the Table Index.
    1.66 +@internalComponent
    1.67 +*/
    1.68 +#define TABLE_INDEX(val)((val & 0xffffc)>>2)
    1.69 +
    1.70 +/**
    1.71 +Obtain the Table UID.
    1.72 +@internalComponent
    1.73 +*/
    1.74 +#define TABLE_UID(val)(val>>20)
    1.75 +
    1.76 +/**
    1.77 +This is used to mark the reference count of a node so that is will not be deleted when closed
    1.78 +@internalComponent
    1.79 +*/
    1.80 +const TUint KMarkedForNoDeleted = 0xffff;
    1.81 +
    1.82 +
    1.83 +
    1.84 +class RStringTokenEither;
    1.85 +
    1.86 +/**
    1.87 +@internalComponent
    1.88 +*/
    1.89 +struct TStringIdMap
    1.90 +	{
    1.91 +	TInt32 iSourceTableVal;
    1.92 +	TInt32 iTargetTableVal;
    1.93 +	};
    1.94 +
    1.95 +
    1.96 +/**
    1.97 +Internal node class
    1.98 +@internalComponent
    1.99 +*/
   1.100 +NONSHARABLE_CLASS(CStringPoolNode) : public CBase
   1.101 +	{
   1.102 +public:
   1.103 +	~CStringPoolNode();
   1.104 +
   1.105 + public:
   1.106 +	HBufC8* iDes;
   1.107 +	TUint16 iRefcount;
   1.108 +	TUint8  iHash;
   1.109 +	};
   1.110 +
   1.111 +/**
   1.112 +@internalComponent
   1.113 +*/
   1.114 +NONSHARABLE_CLASS(CStringPoolImplementation) : public CBase
   1.115 +	{
   1.116 +public:
   1.117 +	CStringPoolImplementation();
   1.118 +	// Destructor
   1.119 +	~CStringPoolImplementation();
   1.120 +
   1.121 +	// Constructs a string pool
   1.122 +	static CStringPoolImplementation* NewL();
   1.123 +
   1.124 +	// USed for cleaning up when a OpenTableL leaves
   1.125 +	static void CleanupHashCS(TAny* aImplementation);
   1.126 +	static void CleanupHashCI(TAny* aImplementation);
   1.127 +	static void CleanupIdMap(TAny* aImplementation);
   1.128 +	static void CleanUpHash(RPointerArray <RStringTokenEither>* aHashCleanup, CArrayFixSeg<RStringTokenEither>* hash[KHashModulo]);
   1.129 +
   1.130 +	// Adds a table to the pool. (Note this currently only works once)
   1.131 +	void AddTableL(const TStringTable& aTable);
   1.132 +	void AddCallBackL( MStringPoolCloseCallBack& aCallBack);
   1.133 +
   1.134 +
   1.135 +	// Find FirstVal given duplicate val
   1.136 +	TInt32 FindFirstValFromDuplicate(TInt32 aDuplicateVal) const;
   1.137 +
   1.138 +	// Find table index Val given first val & table UID
   1.139 +	TInt FindTableIndexFromFirstVal(TInt32 aDuplicateVal, TInt aTableUid) const;
   1.140 +
   1.141 +
   1.142 +	TInt16 TableUid(const TStringTable& aTable) const;
   1.143 +
   1.144 +	const TStringTable& TableRef(TInt32 aVal) const;
   1.145 +
   1.146 +	// Looks up a particular index in the pre-loaded tables
   1.147 +	const TDesC8& TableLookup(TInt aIndex, TInt aTableUid) const;
   1.148 +
   1.149 +	// Finds or creates a string. Increments the reference count if needed.
   1.150 +	RStringTokenEither OpenL( const TDesC8& aString, TBool aCaseInsensitive); 
   1.151 +
   1.152 +	// Closes a string (decrements the reference count and deletes if 0
   1.153 +	void Close(RStringTokenEither aString);
   1.154 +
   1.155 +	// Increments the reference count on a string
   1.156 +	void IncrementCount(RStringTokenEither aString);
   1.157 +
   1.158 +	inline RStringPool Handle();
   1.159 +
   1.160 +private:	
   1.161 +
   1.162 +	// Check for any undeletable string and delete them now
   1.163 +	void DeleteUndeletableStrings(CArrayFixSeg<RStringTokenEither>* aArray[KHashModulo], TInt i);
   1.164 +
   1.165 +private:
   1.166 +
   1.167 +	// Finds a string in the pool.
   1.168 +	RStringTokenEither FindDes( const TDesC8& aString, TBool aCaseInsensitive);
   1.169 +
   1.170 +	// Calculates a hash for a descriptor
   1.171 +	TUint Hash( const TDesC8& ) const;
   1.172 +
   1.173 +	static TBool CompareCS(const TDesC8& s1, const TDesC8& s2);
   1.174 +	static TBool CompareCI(const TDesC8& s1, const TDesC8& s2);
   1.175 +
   1.176 + private:
   1.177 +	// The table.
   1.178 +	CArrayFixSeg<RStringTokenEither>* iCSHashTable[KHashModulo];
   1.179 +	CArrayFixSeg<RStringTokenEither>* iCIHashTable[KHashModulo];	
   1.180 +	RPointerArray<TStringTable> iTablePtrs;	// Stores array of tables, where the index is the table UID
   1.181 +	RArray <TStringIdMap> iStringMapList;
   1.182 +	RArray <TStringIdMap> iStringMapListReverse;
   1.183 +
   1.184 +	// For rolling back when a leave occurs during CreateTableL
   1.185 +	RPointerArray <TStringIdMap> iRollbackMapList;	
   1.186 +	RPointerArray <RStringTokenEither> iRollbackHashListCS;
   1.187 +	RPointerArray <RStringTokenEither> iRollbackHashListCI;
   1.188 +	RPointerArray<MStringPoolCloseCallBack> iCallBacks;
   1.189 +	};
   1.190 +
   1.191 +/** 
   1.192 +An internal version of the string token class. This class can hold
   1.193 +either folding or non-folding versions, and it is up to the user
   1.194 +to get it right.
   1.195 +@internalComponent
   1.196 +*/
   1.197 +class RStringTokenEither : public RStringTokenBase
   1.198 +	{
   1.199 + public:
   1.200 +	inline RStringTokenEither();
   1.201 +
   1.202 +	inline RStringTokenEither(TUint32 aVal);
   1.203 +
   1.204 +	/** Comparison operator
   1.205 +		@param aVal The string to compare. */
   1.206 +	inline TBool operator==(RStringTokenEither aVal) const;
   1.207 +
   1.208 +	/** Comparison operator
   1.209 +		@param aVal The string to compare. */
   1.210 +	inline TBool operator!=(RStringTokenEither aVal) const;
   1.211 +
   1.212 +	/** Assignment operator; makes a string token from a string.
   1.213 +		@param aVal The string to copy */
   1.214 +	inline RStringTokenEither operator=(RStringBase aVal);
   1.215 +
   1.216 +	friend class RStringPool;
   1.217 +	friend class RStringEither;
   1.218 +	friend class CStringPoolImplementation;
   1.219 +	};
   1.220 +
   1.221 +class RStringEither : public RStringBase
   1.222 +/**
   1.223 +@internalComponent
   1.224 +*/
   1.225 +	{
   1.226 +	public:
   1.227 +	RStringEither(CStringPoolImplementation* aPool, RStringTokenEither aVal);
   1.228 +	};
   1.229 +
   1.230 +class StringUtils
   1.231 +/**
   1.232 +@internalComponent
   1.233 +*/
   1.234 +	{
   1.235 + public:
   1.236 +	static inline TBool IsTableEntry(TInt aVal);
   1.237 +	static inline TInt TableIndex(TInt aVal);
   1.238 +	static inline TInt16 TableUid(TInt aVal);
   1.239 +	static inline CStringPoolNode* NodePtr(TInt aVal);
   1.240 +	static TInt ValFromIndex(TInt aIndex, TUint16 aTableId);
   1.241 +	static TInt ValFromIndexF(TInt aIndex, TUint16 aTableId);
   1.242 +	static TInt ValFromIndex(TInt aIndex, TUint16 aTableId, TBool aCaseSensitive);
   1.243 +	
   1.244 +	static void LogIt(TRefByValue<const TDesC8> aFmt, ...);
   1.245 +	static void LogIt1(TRefByValue<const TDesC8> aFmt);
   1.246 +	};
   1.247 +
   1.248 +#define _LOGGING
   1.249 +
   1.250 +#if defined (_DEBUG) && defined (_LOGGING)
   1.251 +
   1.252 +/**
   1.253 +HTTP Logging macros
   1.254 +@internalComponent
   1.255 +*/
   1.256 +#define __LOG(C)			StringUtils::LogIt1(C);
   1.257 +#define __LOG1(C, X)		StringUtils::LogIt(C, X);
   1.258 +#define __LOG2(C, X, Y)		StringUtils::LogIt(C, X, Y);
   1.259 +#define __LOG3(C, X, Y, Z)	StringUtils::LogIt(C, X, Y, Z);
   1.260 +
   1.261 +#else
   1.262 +
   1.263 +/**
   1.264 +NULL macros
   1.265 +@internalComponent
   1.266 +*/
   1.267 +#define __LOG(C)			
   1.268 +#define __LOG1(C, X)		
   1.269 +#define __LOG2(C, X, Y)		
   1.270 +#define __LOG3(C, X, Y, Z)	
   1.271 +#endif // !_DEBUG
   1.272 +
   1.273 +
   1.274 +inline TBool StringUtils::IsTableEntry(TInt aVal)
   1.275 +	{
   1.276 +	return IS_TABLE_ENTRY(aVal);
   1.277 +	}
   1.278 +
   1.279 +inline TInt StringUtils::TableIndex(TInt aVal)
   1.280 +	{
   1.281 +	return TABLE_INDEX(aVal);
   1.282 +	}
   1.283 +
   1.284 +inline TInt16 StringUtils::TableUid(TInt aVal)
   1.285 +	{
   1.286 +	return (TInt16) TABLE_UID(aVal);
   1.287 +	}
   1.288 +
   1.289 +inline CStringPoolNode* StringUtils::NodePtr(TInt aVal)
   1.290 +	{
   1.291 +	return reinterpret_cast<CStringPoolNode*>(aVal & KTokenToNode);
   1.292 +	}
   1.293 +
   1.294 +inline RStringEither::RStringEither(CStringPoolImplementation* aPool, 
   1.295 +							 RStringTokenEither aVal)
   1.296 +	{
   1.297 +	iPool = aPool->Handle();
   1.298 +	iVal = aVal.iVal;
   1.299 +	}
   1.300 +
   1.301 +inline RStringPool CStringPoolImplementation::Handle()
   1.302 +	{
   1.303 +	RStringPool p;
   1.304 +	p.iImplementation = this;
   1.305 +	return p;
   1.306 +	}
   1.307 +
   1.308 +inline RStringTokenEither::RStringTokenEither()
   1.309 +	{
   1.310 +	}
   1.311 +
   1.312 +inline RStringTokenEither::RStringTokenEither(TUint32 aVal)
   1.313 +	{
   1.314 +	iVal = aVal;
   1.315 +	}
   1.316 +
   1.317 +inline TBool RStringTokenEither::operator==(RStringTokenEither aVal) const
   1.318 +	{
   1.319 +	return iVal == aVal.iVal;
   1.320 +	}
   1.321 +
   1.322 +inline TBool RStringTokenEither::operator!=(RStringTokenEither aVal) const
   1.323 +	{
   1.324 +	return iVal != aVal.iVal;
   1.325 +	}
   1.326 +
   1.327 +inline RStringTokenEither RStringTokenEither::operator=(RStringBase aVal)
   1.328 +	{
   1.329 +	RStringTokenBase b = aVal;
   1.330 +	iVal = b.iVal;
   1.331 +	return *this;
   1.332 +	}
   1.333 +
   1.334 +
   1.335 +#endif // __STRINGPOOLIMPLEMENTATION_H__