secureswitools/swisistools/source/sisxlibrary/element.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 * smart pointers; used as elements in SIS arrays and elsewhere
       
    19 *
       
    20 */
       
    21 
       
    22 
       
    23 /**
       
    24  @file 
       
    25  @internalComponent
       
    26  @released
       
    27 */
       
    28 
       
    29 #ifndef __ELEMENT_H__
       
    30 #define __ELEMENT_H__
       
    31 
       
    32 #include <string>
       
    33 #include <iostream>
       
    34 
       
    35 #include "sharedcount.h"
       
    36 
       
    37 
       
    38 
       
    39 
       
    40 
       
    41 template<class T> struct shared_ptr_traits
       
    42 	{
       
    43 	typedef T& reference;
       
    44 	};
       
    45 
       
    46 
       
    47 template<> struct shared_ptr_traits<void>
       
    48 	{
       
    49 	typedef void reference;
       
    50 	};
       
    51 
       
    52 
       
    53 
       
    54 
       
    55 template <class T> class CElement
       
    56 
       
    57 	{
       
    58 public:
       
    59 	typedef T*											TPtr;
       
    60 	typedef typename shared_ptr_traits<T>::reference	TRef;
       
    61 
       
    62 	CElement ();
       
    63 
       
    64 	template <class Y> explicit CElement (Y* ay) :
       
    65 			iPtr (ay), 
       
    66 			iCount (ay)									{ } 
       
    67 
       
    68 	template <class Y>
       
    69 	CElement& operator = (CElement<Y> const& r)
       
    70 		{
       
    71 		iPtr = r.iPtr;
       
    72 		iCount = r.iCount;
       
    73 		return *this;
       
    74 		}
       
    75 
       
    76 	template <class Y> void reset (Y* ay)
       
    77 		{
       
    78 		assert (ay == 0 || ay != iPtr);	// can't self reset
       
    79 		this_type (ay).swap (*this);
       
    80 		}
       
    81 
       
    82 	template <class Y>
       
    83 	bool _internal_less (CElement<Y> const& rhs) const
       
    84 		{
       
    85 		return iCount < rhs.iCount;
       
    86 		}
       
    87 	void reset ();
       
    88 	TRef operator* () const
       
    89 		{
       
    90 		assert (iPtr != NULL);
       
    91 		return *iPtr;
       
    92 		}
       
    93 	T* operator -> () const;
       
    94 	T* get () const;
       
    95 	bool operator! () const;
       
    96 	long UseCount () const;
       
    97 	void swap (CElement<T>& aOther);
       
    98 
       
    99 	void Verify (const TUint32 aLanguages) const;
       
   100 	TUint32 ByteCount (const bool aInArray) const;
       
   101 	bool WasteOfSpace () const;
       
   102 	void MakeNeat ();
       
   103 	std::string Name () const;
       
   104 	void Dump (std::ostream& aStream, const int aLevel) const;
       
   105 	TUint32 ByteCountWithHeader (const bool aInArray) const;
       
   106 	void CalculateCrc (TCRC& aCRC, const bool aIsArrayElement) const;
       
   107 	std::string NoteIfOptional () const;
       
   108 #ifdef GENERATE_ERRORS
       
   109 	virtual void CreateDefects ();
       
   110 #endif // GENERATE_ERRORS
       
   111 	void SkipOldWriteNew (TSISStream& aFile) const;
       
   112 
       
   113 private:
       
   114 	T*				iPtr;
       
   115 	CSharedCount	iCount;
       
   116 	};
       
   117 
       
   118 
       
   119 
       
   120 template <class T> inline CElement <T>::CElement () :
       
   121 		iPtr (NULL) 
       
   122 	{ 
       
   123 	}
       
   124 
       
   125 
       
   126 template<class T> inline void CElement <T>::reset ()
       
   127 	{ 
       
   128 	this_type ().swap (*this);
       
   129 	}
       
   130 
       
   131 
       
   132 template<class T> T* CElement <T>::operator -> () const
       
   133 	{
       
   134 	assert (iPtr != NULL);
       
   135 	return iPtr;
       
   136 	}
       
   137 			
       
   138 template<class T> inline T* CElement <T>::get () const
       
   139 	{ 
       
   140 	return iPtr; 
       
   141 	}
       
   142 
       
   143 template<class T> inline bool CElement <T>::operator! () const
       
   144 	{
       
   145 	return iPtr == 0;
       
   146 	}
       
   147 
       
   148 template<class T> inline long CElement <T>::UseCount () const
       
   149 	{ 
       
   150 	return iCount.UseCount (); 
       
   151 	}
       
   152 
       
   153 template<class T> inline void CElement <T>::swap (CElement<T>& aOther)
       
   154 	{
       
   155 	std::swap (iPtr, aOther.iPtr);
       
   156 	iCount.swap (aOther.iCount);
       
   157 	}	
       
   158 
       
   159 template<class T> inline
       
   160 		void CElement <T>::Verify (const TUint32 aLanguages) const
       
   161 	{ 
       
   162 	iPtr -> Verify (aLanguages); 
       
   163 	}
       
   164 
       
   165 template<class T> inline TUint32 CElement <T>::ByteCount (const bool aInArray) const
       
   166 	{ 
       
   167 	return iPtr -> ByteCount (aInArray);
       
   168 	}
       
   169 
       
   170 template<class T> inline bool CElement <T>::WasteOfSpace () const
       
   171 	{
       
   172 	return iPtr -> WasteOfSpace ();
       
   173 	}
       
   174 
       
   175 template<class T> inline void CElement <T>::MakeNeat ()
       
   176 	{
       
   177 	iPtr -> MakeNeat (); 
       
   178 	}
       
   179 
       
   180 template<class T> inline std::string CElement <T>::Name () const
       
   181 	{
       
   182 	return iPtr -> Name (); 
       
   183 	}
       
   184 
       
   185 template<class T> inline
       
   186 		void CElement <T>::Dump (std::ostream& aStream, const int aLevel) const
       
   187 	{
       
   188 	iPtr -> Dump (aStream, aLevel);
       
   189 	}
       
   190 
       
   191 template<class T> inline
       
   192 		TUint32 CElement <T>::ByteCountWithHeader (const bool aInArray) const
       
   193 	{
       
   194 	return iPtr -> ByteCountWithHeader (aInArray);
       
   195 	}
       
   196 
       
   197 template <class T> inline
       
   198 		void CElement <T>::CalculateCrc (TCRC& aCRC, const bool aIsArrayElement) const
       
   199 	{
       
   200 	iPtr -> CalculateCrc (aCRC, aIsArrayElement);
       
   201 	}
       
   202 
       
   203 template <class T> inline std::string CElement <T>::NoteIfOptional () const
       
   204 	{
       
   205 	return iPtr -> NoteIfOptional ();
       
   206 	}
       
   207 
       
   208 #ifdef GENERATE_ERRORS
       
   209 template <class T> inline void CElement <T>::CreateDefects ()
       
   210 	{
       
   211 	iPtr -> CreateDefects ();
       
   212 	}
       
   213 #endif // GENERATE_ERRORS
       
   214 
       
   215 template <class T> inline void CElement <T>::SkipOldWriteNew (TSISStream& aFile) const
       
   216 	{
       
   217 	iPtr -> SkipOldWriteNew (aFile);
       
   218 	}
       
   219 
       
   220 template <class T, class U> inline bool operator == (CElement<T> const& a, CElement<U> const& b)
       
   221 	{
       
   222 	return a.get () == b.get ();
       
   223 	}
       
   224 
       
   225 template<class T, class U> inline bool operator != (CElement<T> const& a, CElement<U> const& b)
       
   226 	{
       
   227 	return a.get () != b.get ();
       
   228 	}
       
   229 
       
   230 template<class T, class U> inline bool operator < (CElement<T> const& a, CElement<U> const& b)
       
   231 	{
       
   232 	return a._internal_less (b);
       
   233 	}
       
   234 
       
   235 template<class T> inline void swap (CElement<T>& a, CElement<T>& b)
       
   236 	{
       
   237 	a.swap (b);
       
   238 	}
       
   239 
       
   240 template<class T> inline T* get_pointer (CElement<T> const& p)
       
   241 	{
       
   242 	return p.get ();
       
   243 	}
       
   244 
       
   245 
       
   246 
       
   247 #endif // __ELEMENT_H__