/* * This is a part of the BugTrap package. * Copyright (c) 2005-2009 IntelleSoft. * All rights reserved. * * Description: Prototypes of collection helpers. * Author: Maksim Pyatkovskiy. * * This source code is only intended as a supplement to the * BugTrap package reference and related electronic documentation * provided with the product. See these sources for detailed * information regarding the BugTrap package. */ #pragma once #include "StrHolder.h" #include "StrStream.h" /// Object instantiation traits. template class CObjectTraits { public: /// Calls the constructor of @a DATA_TYPE class by default. static void Constructor(DATA_TYPE& rData); /// Calls the destructor of @a DATA_TYPE class by default. static void Destructor(DATA_TYPE& rData); /// Calls the assignment operator of @a DATA_TYPE class by default. static void Assignment(DATA_TYPE& rDstData, const DATA_TYPE& rSrcData); }; /** * @param rData - reference to the memory placement of new object. */ template inline void CObjectTraits::Constructor(DATA_TYPE& rData) { ::new((void*)&rData) DATA_TYPE; } /** * @param rData - reference to the memory placement of deleted object. */ template inline void CObjectTraits::Destructor(DATA_TYPE& rData) { rData; rData.~DATA_TYPE(); } /** * @param rDstData - reference to the memory placement of destination object. * @param rSrcData - reference to the memory placement of source object. */ template inline void CObjectTraits::Assignment(DATA_TYPE& rDstData, const DATA_TYPE& rSrcData) { rDstData = rSrcData; } /// Value initialization traits. template class CValueTraits { public: /// Calls the constructor of @a DATA_TYPE by default. static void Constructor(DATA_TYPE& rData); /// Calls the destructor of @a DATA_TYPE by default static void Destructor(DATA_TYPE& rData); /// Calls the assignment operator of @a DATA_TYPE by default. static void Assignment(DATA_TYPE& rDstData, const DATA_TYPE& rSrcData); }; /** * @param rData - reference to the memory placement of new object. */ template inline void CValueTraits::Constructor(DATA_TYPE& rData) { rData = value; } /** * @param rData - reference to the memory placement of deleted object. */ template inline void CValueTraits::Destructor(DATA_TYPE& /*rData*/) { } /** * @param rDstData - reference to the memory placement of destination object. * @param rSrcData - reference to the memory placement of source object. */ template inline void CValueTraits::Assignment(DATA_TYPE& rDstData, const DATA_TYPE& rSrcData) { rDstData = rSrcData; } /// Dynamic instantiation traits. template class CDynamicTraits { public: /// Calls the constructor of @a DATA_TYPE class by default. static void Constructor(DATA_TYPE& rData); /// Calls the destructor of @a DATA_TYPE class by default. static void Destructor(DATA_TYPE& rData); /// Calls the assignment operator of @a DATA_TYPE class by default. static void Assignment(DATA_TYPE& rDstData, const DATA_TYPE& rSrcData); }; /** * @param rData - reference to the memory placement of new object. */ template inline void CDynamicTraits::Constructor(DATA_TYPE& rData) { rData = NULL; } /** * @param rData - reference to the memory placement of deleted object. */ template inline void CDynamicTraits::Destructor(DATA_TYPE& rData) { delete rData; } /** * @param rDstData - reference to the memory placement of destination object. * @param rSrcData - reference to the memory placement of source object. */ template inline void CDynamicTraits::Assignment(DATA_TYPE& rDstData, const DATA_TYPE& rSrcData) { delete rDstData; rDstData = rSrcData; } /// Data comparator traits. template class CCompareTraits { public: /// Determines whatever two elements are equal to each other. static bool Compare(const DATA_TYPE& rData1, const DATA_TYPE& rData2); /// Compares two elements according to their order. static int OrderedCompare(const DATA_TYPE& rData1, const DATA_TYPE& rData2); }; /** * @param rData1 - reference to the first element. * @param rData2 - reference to the second element. * @return true if elements are equal. */ template inline bool CCompareTraits::Compare(const DATA_TYPE& rData1, const DATA_TYPE& rData2) { return (rData1 == rData2); } /** * @param rData1 - reference to the first element. * @param rData2 - reference to the second element. * @return comparison result: * - < 0 if first element is less than second element; * - = 0 if first element is equal to the second element; * - > 0 if first element is greater than second element. */ template int CCompareTraits::OrderedCompare(const DATA_TYPE& rData1, const DATA_TYPE& rData2) { if (rData1 < rData2) return -1; else if (rData2 < rData1) return +1; else return 0; } /// Explicit template specialization for strings. template <> class CCompareTraits { public: /// Determines whatever two elements are equal to each other. static bool Compare(TCHAR * const & rData1, TCHAR * const & rData2); /// Compares two elements according to their order. static int OrderedCompare(TCHAR * const & rData1, TCHAR * const & rData2); }; /** * @param rData1 - reference to the first element. * @param rData2 - reference to the second element. * @return true if elements are equal. */ inline bool CCompareTraits::Compare(TCHAR * const & rData1, TCHAR * const & rData2) { return (_tcscmp(rData1, rData2) == 0); } /** * @param rData1 - reference to the first element. * @param rData2 - reference to the second element. * @return comparison result: * - < 0 if first element is less than second element; * - = 0 if first element is equal to the second element; * - > 0 if first element is greater than second element. */ inline int CCompareTraits::OrderedCompare(TCHAR * const & rData1, TCHAR * const & rData2) { return _tcscmp(rData1, rData2); } /// Explicit template specialization for strings. template <> class CCompareTraits { public: /// Determines whatever two elements are equal to each other. static bool Compare(TCHAR const * const & rData1, TCHAR const * const & rData2); /// Compares two elements according to their order. static int OrderedCompare(TCHAR const * const & rData1, TCHAR const * const & rData2); }; /** * @param rData1 - reference to the first element. * @param rData2 - reference to the second element. * @return true if elements are equal. */ inline bool CCompareTraits::Compare(TCHAR const * const & rData1, TCHAR const * const & rData2) { return (_tcscmp(rData1, rData2) == 0); } /** * @param rData1 - reference to the first element. * @param rData2 - reference to the second element. * @return comparison result: * - < 0 if first element is less than second element; * - = 0 if first element is equal to the second element; * - > 0 if first element is greater than second element. */ inline int CCompareTraits::OrderedCompare(TCHAR const * const & rData1, TCHAR const * const & rData2) { return _tcscmp(rData1, rData2); } /// Comparator traits used for incomparable objects. template class CIncomparableTraits { public: /// Determines whatever two elements are equal to each other. static bool Compare(const DATA_TYPE& rData1, const DATA_TYPE& rData2); /// Compares two elements according to their order. static int OrderedCompare(const DATA_TYPE& rData1, const DATA_TYPE& rData2); }; /** * @param rData1 - reference to the first element. * @param rData2 - reference to the second element. * @return true if elements are equal. */ template inline bool CIncomparableTraits::Compare(const DATA_TYPE& /*rData1*/, const DATA_TYPE& /*rData2*/) { _ASSERT(FALSE); return false; } /** * @param rData1 - reference to the first element. * @param rData2 - reference to the second element. * @return comparison result: * - < 0 if first element is less than second element; * - = 0 if first element is equal to the second element; * - > 0 if first element is greater than second element. */ template int CIncomparableTraits::OrderedCompare(const DATA_TYPE& /*rData1*/, const DATA_TYPE& /*rData2*/) { _ASSERT(FALSE); return 0; } /// Hash key traits. template class CHashTraits { public: /// Computes the hash value for the given object instance. static unsigned HashKey(const KEY_TYPE& key); }; /** * @param key - hash value must be generated for this object. * @return hash value of the given object. */ template inline unsigned CHashTraits::HashKey(const KEY_TYPE& key) { return ((unsigned)key >> 4); } /// Explicit template specialization for string hash tables. template <> class CHashTraits { public: /// Computes the hash value for the given object instance. static unsigned HashKey(const CStrHolder& strKey); }; /** * @param strKey - hash value must be generated for this string. * @return hash value of the given object. */ inline unsigned CHashTraits::HashKey(const CStrHolder& strKey) { extern unsigned GetStringHashValue(const TCHAR* pszKey); return GetStringHashValue((PCTSTR)strKey); } /// Explicit template specialization for string hash tables. template <> class CHashTraits { public: /// Computes the hash value for the given object instance. static unsigned HashKey(const CStrStream& strKey); }; /** * @param strKey - hash value must be generated for this string. * @return hash value of the given object. */ inline unsigned CHashTraits::HashKey(const CStrStream& strKey) { extern unsigned GetStringHashValue(const TCHAR* pszKey); return GetStringHashValue((PCTSTR)strKey); } /// Explicit template specialization for string hash tables. template <> class CHashTraits { public: /// Computes the hash value for the given object instance. static unsigned HashKey(TCHAR * const & pszKey); }; /** * @param pszKey - hash value must be generated for this string. * @return hash value of the given object. */ inline unsigned CHashTraits::HashKey(TCHAR * const & pszKey) { extern unsigned GetStringHashValue(const TCHAR* pszKey); return GetStringHashValue(pszKey); } /// Explicit template specialization for string hash tables. template <> class CHashTraits { public: /// Computes the hash value for the given object instance. static unsigned HashKey(TCHAR const * const & pszKey); }; /** * @param pszKey - hash value must be generated for this string. * @return hash value of the given object. */ inline unsigned CHashTraits::HashKey(TCHAR const * const & pszKey) { extern unsigned GetStringHashValue(const TCHAR* pszKey); return GetStringHashValue(pszKey); }