secureswitools/swisistools/source/sisxlibrary/container.h
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2007-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 "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * Note: This file may contain code to generate corrupt files for test purposes.
       
    16 * Such code is excluded from production builds by use of compiler defines;
       
    17 * it is recommended that such code should be removed if this code is ever published publicly.
       
    18 * common functionality for containers in SIS files
       
    19 *
       
    20 */
       
    21 
       
    22 
       
    23 /**
       
    24  @file 
       
    25  @internalComponent
       
    26  @released
       
    27 */
       
    28 
       
    29 #ifndef __CONTAINER_H__
       
    30 #define __CONTAINER_H__
       
    31 
       
    32 #include "field.h"
       
    33 #include <vector>
       
    34 
       
    35 template<class T, CSISFieldRoot::TFieldType FieldType> class CContainer : public CSISField <FieldType>
       
    36 	{
       
    37 public:
       
    38 	typedef std::vector <T*>		TMem;
       
    39 	typedef  typename TMem::iterator		TMemIter;
       
    40 	typedef  typename TMem::const_iterator	TMemConIter;
       
    41 	typedef  typename TMem::size_type		TMemSize;
       
    42 
       
    43 public:
       
    44 	/**
       
    45 	 * Default constructor
       
    46 	 */
       
    47 	CContainer ();
       
    48 	/**
       
    49 	 * Explicit parameterized constructor
       
    50 	 */
       
    51 	explicit CContainer (const bool aRequired);
       
    52 	/**
       
    53 	 * Copy Constructor
       
    54 	 */
       
    55 	CContainer (const CContainer <T, FieldType>& c);
       
    56 	/**
       
    57 	 * Constructor. It also reserves space.
       
    58 	 */
       
    59 	CContainer (const CContainer <T, FieldType>& c, const TMemSize aReserve);
       
    60 
       
    61 	/**
       
    62 	 * Retrieves the size of the structure.
       
    63 	 * @param aInsideArray - whether the structure is part of an array or not. 
       
    64 	 * @return byte count.
       
    65 	 */
       
    66 	virtual CSISFieldRoot::TFieldSize ByteCount (const bool aIsArray) const;
       
    67 	/**
       
    68 	 * Sets the size of the structure.
       
    69 	 * @param size - size of the structure. 
       
    70 	 */
       
    71 	virtual void SetByteCount (const CSISFieldRoot::TFieldSize size);
       
    72 	/**
       
    73 	 * This function verifies the structure
       
    74 	 * @param aLanguage - language
       
    75 	 */ 
       
    76 	virtual void Verify (const TUint32 aLanguages) const;
       
    77 	/**
       
    78 	 * If object not acceptable, modify it. Assumes, once modified, will not need modifying again
       
    79 	 */
       
    80 	virtual void MakeNeat ();
       
    81 	/**
       
    82 	 * Dump the entire content in hex format into the stream
       
    83 	 */
       
    84 	virtual void Dump (std::ostream& aStream, const int aLevel) const;
       
    85 	/**
       
    86 	 * Calculates CRC of the content
       
    87 	 * @param aCRC CRC value of the content
       
    88 	 * @param aIsArrayElement whether the structure is part of an array or not. 
       
    89 	 */ 
       
    90 	virtual void CalculateCrc (TCRC& aCRC, const bool aIsArrayElement) const;
       
    91 #ifdef GENERATE_ERRORS
       
    92 	virtual void CreateDefects ();
       
    93 #endif // GENERATE_ERRORS
       
    94 
       
    95 	/**
       
    96 	 * @return Item count
       
    97 	 */
       
    98 	TMemSize size () const
       
    99 		{
       
   100 		return iMem.size ();
       
   101 		}
       
   102 	/**
       
   103 	 * Assign new items to the container.
       
   104 	 * @param aBegin iterator start
       
   105 	 * @param aEnd iterator end
       
   106 	 */
       
   107 	void assign (TMemConIter aBegin, TMemConIter aEnd);
       
   108 	/**
       
   109 	 * Clear the items
       
   110 	 */
       
   111 	void clear ();
       
   112 
       
   113 	/**
       
   114 	 * Iterator functionality
       
   115 	 * @return iterator pointing to the begining of the item list
       
   116 	 */
       
   117 	TMemConIter begin () const
       
   118 		{
       
   119 		return iMem.begin ();
       
   120 		}
       
   121 	/**
       
   122 	 * Iterator functionality
       
   123 	 * @return iterator pointing to the end of the item list
       
   124 	 */
       
   125 	TMemConIter end () const
       
   126 		{
       
   127 		return iMem.end ();
       
   128 		}
       
   129 	/**
       
   130 	 * Iterator functionality
       
   131 	 * @return iterator pointing to the begining of the item list
       
   132 	 */
       
   133 	TMemIter begin ()
       
   134 		{
       
   135 		return iMem.begin ();
       
   136 		}
       
   137 	/**
       
   138 	 * Iterator functionality
       
   139 	 * @return iterator pointing to the end of the item list
       
   140 	 */
       
   141 	TMemIter end ()
       
   142 		{
       
   143 		return iMem.end ();
       
   144 		}
       
   145 
       
   146 	/**
       
   147 	 * Retrieve the most recent item from the container and delete it from 
       
   148 	 * the container list.
       
   149 	 */
       
   150 	void Pop ();
       
   151 
       
   152 protected:
       
   153 	const T& operator [] (const TMemSize& aIndex) const;
       
   154 	T& operator [] (const TMemSize& aIndex);
       
   155 	void Push (T* aData);
       
   156 	const T& Last () const;
       
   157 	T& Last ();
       
   158 
       
   159 protected:
       
   160 	TMem						iMem;
       
   161 	CSISFieldRoot::TFieldSize	iSize;
       
   162 	};
       
   163 
       
   164 // macros for accessing template member variable and type 
       
   165 #define ContainerMemSize(aType, aFieldType)  typename CContainer<aType, aFieldType>::TMemSize
       
   166 #define ContainerMem(aType,aFieldType) CContainer<aType, aFieldType>::iMem
       
   167 #define ContainerIter(aType) typename CContainer<aType, FieldType>::TMemConIter
       
   168 
       
   169 
       
   170 template <class T, CSISFieldRoot::TFieldType FieldType> inline
       
   171 	CContainer <T, FieldType>::CContainer () :
       
   172 		iSize (0)
       
   173 	{
       
   174 	}
       
   175 
       
   176 template <class T, CSISFieldRoot::TFieldType FieldType> inline
       
   177 	CContainer <T, FieldType>::CContainer (const bool aRequired) :
       
   178 		iSize (0), 
       
   179 		CSISField <FieldType> (aRequired)
       
   180 	{
       
   181 	}
       
   182 
       
   183 template <class T, CSISFieldRoot::TFieldType FieldType> inline
       
   184 	CContainer <T, FieldType>::CContainer (const CContainer <T, FieldType>& c) :
       
   185 		CSISField <FieldType> (c),
       
   186 		iMem (c.iMem),
       
   187 		iSize (c.iSize)
       
   188 	{
       
   189 	}
       
   190 
       
   191 template <class T, CSISFieldRoot::TFieldType FieldType> inline
       
   192 	CContainer <T, FieldType>::CContainer (const CContainer <T, FieldType>& c, const TMemSize aReserve) :
       
   193 		CSISField <FieldType> (c),
       
   194 		iMem (aReserve),
       
   195 		iSize (0)
       
   196 	{
       
   197 	}
       
   198 
       
   199 
       
   200 template <class T, CSISFieldRoot::TFieldType FieldType> 
       
   201 		void CContainer <T, FieldType>::clear ()
       
   202 	{
       
   203 	iMem.clear ();
       
   204 	}
       
   205 
       
   206 template <class T, CSISFieldRoot::TFieldType FieldType> inline
       
   207 		void CContainer <T, FieldType>::assign (TMemConIter aBegin, TMemConIter aEnd)
       
   208 	{
       
   209 	iMem.assign (aBegin, aEnd);
       
   210 	}
       
   211 
       
   212 
       
   213 template <class T, CSISFieldRoot::TFieldType FieldType> 
       
   214 		void CContainer <T, FieldType>::Verify (const TUint32 aLanguages) const
       
   215 	{
       
   216 	for (TMemConIter iterMemb = iMem.begin (); iterMemb != iMem.end (); iterMemb++)
       
   217 		{
       
   218 		assert (*iterMemb != NULL);
       
   219 		(*iterMemb) -> Verify (aLanguages);
       
   220 		}
       
   221 	}
       
   222 
       
   223 template <class T, CSISFieldRoot::TFieldType FieldType>
       
   224 		void CContainer <T, FieldType>::MakeNeat ()
       
   225 	{
       
   226 	for (TMemIter iterMemb = iMem.begin (); iterMemb != iMem.end (); iterMemb++)
       
   227 		{
       
   228 		assert (*iterMemb != NULL);
       
   229 		(*iterMemb) -> MakeNeat ();
       
   230 		}
       
   231 	}
       
   232 
       
   233 template <class T, CSISFieldRoot::TFieldType FieldType>
       
   234 		void CContainer <T, FieldType>::Dump (std::ostream& aStream, const int aLevel) const
       
   235 	{
       
   236 	for (TMemSize index = 0; index < size (); index++)
       
   237 		{
       
   238 		if (index > 0) 
       
   239 			{
       
   240 			aStream << std::string (aLevel, ' ');
       
   241 			}
       
   242 		if (iMem [index] -> Name ().empty ()) 
       
   243 			{
       
   244 			aStream << "  ";
       
   245 			}
       
   246 		else
       
   247 			{
       
   248 			aStream << iMem [index] -> Name ();
       
   249 			if (FieldType == CSISFieldRoot::ESISArray) 
       
   250 				{
       
   251 				aStream << " [" << index << "]";
       
   252 				}
       
   253 			aStream << iMem [index] -> NoteIfOptional ();
       
   254 			aStream << std::endl << std::string (aLevel + 2, ' ');
       
   255 			}
       
   256 		iMem [index] -> Dump (aStream, aLevel + 2);
       
   257 		if (index + 1 < size ()) 
       
   258 			{
       
   259 			aStream << std::endl;
       
   260 			}
       
   261 		}
       
   262 	}
       
   263 
       
   264 
       
   265 template <class T, CSISFieldRoot::TFieldType FieldType>
       
   266 		CSISFieldRoot::TFieldSize CContainer <T, FieldType>::ByteCount (const bool aIsArray) const
       
   267 	{
       
   268 	if (WasteOfSpace ()) 
       
   269 		{
       
   270 		return 0;
       
   271 		}
       
   272 	CSISFieldRoot::TFieldSize size = 0;
       
   273 	for (TMemConIter iterMemb = iMem.begin (); iterMemb != iMem.end (); iterMemb++)
       
   274 		{
       
   275 		assert (*iterMemb != NULL);
       
   276 		size += (*iterMemb) -> ByteCountWithHeader (aIsArray);
       
   277 		}
       
   278 	return size; 
       
   279 	}
       
   280 
       
   281 template <class T, CSISFieldRoot::TFieldType FieldType> inline
       
   282 		void CContainer <T, FieldType>::SetByteCount (const CSISFieldRoot::TFieldSize aSize)
       
   283 	{
       
   284 	iSize = aSize;
       
   285 	}
       
   286 
       
   287 
       
   288 template <class T, CSISFieldRoot::TFieldType FieldType>
       
   289 		void CContainer <T, FieldType>::CalculateCrc (TCRC& aCRC, const bool aIsArrayElement) const
       
   290 	{ 
       
   291 	if(!WasteOfSpace())
       
   292 		{
       
   293 		for (TMemConIter iterMemb = iMem.begin (); iterMemb != iMem.end (); iterMemb++)
       
   294 			{
       
   295 			assert (*iterMemb != NULL);
       
   296 			(*iterMemb) -> CalculateCrc (aCRC, aIsArrayElement);
       
   297 			}
       
   298 		CSISFieldRoot::PaddingCrc(aCRC, ByteCount(false));
       
   299 		}
       
   300 	}
       
   301 
       
   302 #ifdef GENERATE_ERRORS
       
   303 template <class T, CSISFieldRoot::TFieldType FieldType>
       
   304 		void CContainer <T, FieldType>::CreateDefects ()
       
   305 	{ 
       
   306 	for (TMemConIter iterMemb = iMem.begin (); iterMemb != iMem.end (); iterMemb++)
       
   307 		{
       
   308 		assert (*iterMemb != NULL);
       
   309 		(*iterMemb) -> CreateDefects ();
       
   310 		}
       
   311 	}
       
   312 #endif // GENERATE_ERRORS
       
   313 
       
   314 
       
   315 template <class T, CSISFieldRoot::TFieldType FieldType> 
       
   316 		const T& CContainer <T, FieldType>::operator [] (const TMemSize& aIndex) const
       
   317 	{
       
   318 	assert (aIndex < size ());
       
   319 	assert (iMem [aIndex] != NULL);
       
   320 	return (*iMem [aIndex]);
       
   321 	}
       
   322 
       
   323 template <class T, CSISFieldRoot::TFieldType FieldType> 
       
   324 		T& CContainer <T, FieldType>::operator [] (const TMemSize& aIndex)
       
   325 	{
       
   326 	assert (aIndex < size ());
       
   327 	assert (iMem [aIndex] != NULL);
       
   328 	return (*iMem [aIndex]);
       
   329 	}
       
   330 
       
   331 template <class T, CSISFieldRoot::TFieldType FieldType> inline
       
   332 		void CContainer <T, FieldType>::Push (T* aData)
       
   333 	{
       
   334 	iMem.push_back (aData);
       
   335 	}
       
   336 
       
   337 template <class T, CSISFieldRoot::TFieldType FieldType> inline
       
   338 		const T& CContainer <T, FieldType>::Last () const
       
   339 	{
       
   340 	assert (size () > 0);
       
   341 	assert (iMem [iMem.size () - 1] != NULL);
       
   342 	return * (iMem [iMem.size () - 1]);
       
   343 	}
       
   344 
       
   345 template <class T, CSISFieldRoot::TFieldType FieldType> inline
       
   346 		T& CContainer <T, FieldType>::Last ()
       
   347 	{
       
   348 	assert (size () > 0);
       
   349 	assert (iMem [iMem.size () - 1] != NULL);
       
   350 	return * (iMem [iMem.size () - 1]);
       
   351 	}
       
   352 
       
   353 template <class T, CSISFieldRoot::TFieldType FieldType> inline
       
   354 		void CContainer <T, FieldType>::Pop ()
       
   355 	{
       
   356 	assert (size () > 0);
       
   357 	assert (iMem [iMem.size () - 1] != NULL);
       
   358 	T* ptr = iMem [iMem.size () - 1];
       
   359 	iMem.pop_back ();
       
   360 	delete ptr;
       
   361 	}
       
   362 
       
   363 #endif // __SISXTYPE_H__
       
   364