[hxcommon] CR CHXHeader size reduction

[hxcommon] CR CHXHeader size reduction

Aaron Colwell acolwell at real.com
Tue Jul 8 18:27:41 PDT 2003


These changes reduce the code size of the CHXHeader implementation by
eliminating duplicate code. These changes shrink the code size for Symbian
by 24% (or about 540 bytes). Every little bit counts.

Aaron

Index: old_hxpckts.cpp
===================================================================
RCS file: /cvs/common/container/old_hxpckts.cpp,v
retrieving revision 1.4
diff -u -r1.4 old_hxpckts.cpp
--- old_hxpckts.cpp	17 Jun 2003 17:08:16 -0000	1.4
+++ old_hxpckts.cpp	9 Jul 2003 00:11:22 -0000
@@ -642,31 +642,9 @@

 CHXHeader::~CHXHeader()
 {
-    CHXMapStringToOb::Iterator i;
-
-    // clean up UINT32 properties
-    for (i = m_ULONG32Map.Begin(); i != m_ULONG32Map.End(); ++i)
-    {
-	_CStoreNameUINT32Pair* pnupExpired = (_CStoreNameUINT32Pair*)*i;
-	delete pnupExpired;
-    }
-    m_ULONG32Map.RemoveAll();
-
-    // clean up buffer properties
-    for (i = m_BufferMap.Begin(); i != m_BufferMap.End(); ++i)
-    {
-	_CStoreNameBufferPair* pnbpExpired = (_CStoreNameBufferPair*)*i;
-	delete pnbpExpired;
-    }
-    m_BufferMap.RemoveAll();
-
-    // clean up string properties
-    for (i = m_CStringMap.Begin(); i != m_CStringMap.End(); ++i)
-    {
-	_CStoreNameBufferPair* pnbpExpired = (_CStoreNameBufferPair*)*i;
-	delete pnbpExpired;
-    }
-    m_CStringMap.RemoveAll();
+    ClearMap(m_ULONG32Map);
+    ClearMap(m_BufferMap);
+    ClearMap(m_CStringMap);
 }

 STDMETHODIMP
@@ -674,38 +652,23 @@
 		const char*          pPropertyName,
 		ULONG32              uPropertyValue)
 {
-    _CStoreNameUINT32Pair* pnupNew = new _CStoreNameUINT32Pair;
-    if(!pnupNew)
-    {
-        return HXR_OUTOFMEMORY;
-    }
+    HX_RESULT rc = HXR_OK;

-    pnupNew->SetName(pPropertyName);
-    pnupNew->SetValue(uPropertyValue);
+    _CStoreNameUINT32Pair* pnupNew = new _CStoreNameUINT32Pair;

-    char* pMapString = new_string(pPropertyName);
-    if(!pMapString)
+    if(!pnupNew)
     {
-        HX_DELETE(pnupNew);
-        return HXR_OUTOFMEMORY;
+        rc = HXR_OUTOFMEMORY;
     }
-
-    if(!m_bPreserveCase)
+    else
     {
-	strlwr(pMapString);
-    }
+	pnupNew->SetName(pPropertyName);
+	pnupNew->SetValue(uPropertyValue);

-    // First make sure there isn't already a property by that name set!
-    _CStoreNameUINT32Pair* pnupExpired = NULL;
-    if (m_ULONG32Map.Lookup(pMapString, (void*&)pnupExpired))
-    {
-	/* Delete the previously added property */
-	delete pnupExpired;
+	rc = SetValue(m_ULONG32Map, pPropertyName, pnupNew);
     }

-    m_ULONG32Map[pMapString] = (void*)pnupNew;
-    delete [] pMapString;
-    return HXR_OK;
+    return rc;
 }

 STDMETHODIMP
@@ -713,32 +676,15 @@
 		const char*          pPropertyName,
 		REF(ULONG32)         uPropertyValue)
 {
-    HX_RESULT rc = HXR_OK;
-
     _CStoreNameUINT32Pair* pnupData = NULL;

-    char* pMapString = new_string(pPropertyName);
-    if( !pMapString )
-    {
-        return HXR_OUTOFMEMORY;
-    }
-
-    if(!m_bPreserveCase)
-    {
-	strlwr(pMapString);
-    }
-
-    if (!m_ULONG32Map.Lookup(pMapString, (void*&)pnupData))
-    {
-	rc = HXR_FAILED;
-    }
-    else
+    HX_RESULT rc = GetValue(m_ULONG32Map, pPropertyName,
(void*&)pnupData);
+
+    if (HXR_OK == rc)
     {
 	uPropertyValue = pnupData->GetValue();
     }

-    delete[] pMapString;
-
     return rc;
 }

@@ -787,40 +733,30 @@
 		const char*         pPropertyName,
 		IHXBuffer*         pPropertyValue)
 {
-    if (!pPropertyValue) return HXR_UNEXPECTED;
+    HX_RESULT rc = HXR_OK;

-    _CStoreNameBufferPair* pnbpNew = new _CStoreNameBufferPair;
-    if(!pnbpNew)
+    if (!pPropertyValue)
     {
-        return HXR_OUTOFMEMORY;
+	rc = HXR_UNEXPECTED;
     }
-
-    pnbpNew->SetName(pPropertyName);
-    pnbpNew->SetValue(pPropertyValue);
-
-    char* pMapString = new_string(pPropertyName);
-    if(!pMapString)
+    else
     {
-        HX_DELETE(pnbpNew);
-        return HXR_OUTOFMEMORY;
-    }
+	_CStoreNameBufferPair* pnbpNew = new _CStoreNameBufferPair;

-    if(!m_bPreserveCase)
-    {
-	strlwr(pMapString);
-    }
+	if(!pnbpNew)
+	{
+	    rc = HXR_OUTOFMEMORY;
+	}
+	else
+	{
+	    pnbpNew->SetName(pPropertyName);
+	    pnbpNew->SetValue(pPropertyValue);

-    // First make sure there isn't already a property by that name set!
-    _CStoreNameBufferPair* pnbpExpired = NULL;
-    if (m_BufferMap.Lookup(pMapString, (void*&)pnbpExpired))
-    {
-	/* Delete the previously added property */
-	delete pnbpExpired;
+	    rc = SetValue(m_BufferMap, pPropertyName, pnbpNew);
+	}
     }

-    m_BufferMap[pMapString] = (void*)pnbpNew;
-    delete [] pMapString;
-    return HXR_OK;
+    return rc;
 }

 STDMETHODIMP
@@ -828,31 +764,15 @@
 		const char*         pPropertyName,
 		REF(IHXBuffer*)    pPropertyValue)
 {
-    HX_RESULT rc = HXR_OK;
-
     _CStoreNameBufferPair* pnbpData = NULL;

-    char* pMapString = new_string(pPropertyName);
-    if(!pMapString)
-    {
-        return HXR_OUTOFMEMORY;
-    }
+    HX_RESULT rc = GetValue(m_BufferMap, pPropertyName,
(void*&)pnbpData);

-    if(!m_bPreserveCase)
-    {
-	strlwr(pMapString);
-    }
-
-    if (!m_BufferMap.Lookup(pMapString, (void*&)pnbpData))
-    {
-	rc = HXR_FAILED;
-    }
-    else
+    if (HXR_OK == rc)
     {
 	pPropertyValue = pnbpData->GetValue();
     }

-    delete [] pMapString;
     return rc;
 }

@@ -925,40 +845,30 @@
 		const char*         pPropertyName,
 		IHXBuffer*         pPropertyValue)
 {
-    if (!pPropertyValue) return HXR_UNEXPECTED;
-
-    _CStoreNameBufferPair* pnbpNew = new _CStoreNameBufferPair;
-    if(!pnbpNew)
-    {
-        return HXR_OUTOFMEMORY;
-    }
-
-    pnbpNew->SetName(pPropertyName);
-    pnbpNew->SetValue(pPropertyValue);
-
-    char* pMapString = new_string(pPropertyName);
-    if(!pMapString)
-    {
-        HX_DELETE(pnbpNew);
-        return HXR_OUTOFMEMORY;
-    }
+    HX_RESULT rc = HXR_OK;

-    if(!m_bPreserveCase)
+    if (!pPropertyValue)
     {
-	strlwr(pMapString);
+	rc = HXR_UNEXPECTED;
     }
-
-    // First make sure there isn't already a property by that name set!
-    _CStoreNameBufferPair* pnbpExpired = NULL;
-    if (m_CStringMap.Lookup(pMapString, (void*&)pnbpExpired))
+    else
     {
-	/* Delete the previously added property */
-	delete pnbpExpired;
+	_CStoreNameBufferPair* pnbpNew = new _CStoreNameBufferPair;
+
+	if (!pnbpNew)
+	{
+	    rc = HXR_OUTOFMEMORY;
+	}
+	else
+	{
+	    pnbpNew->SetName(pPropertyName);
+	    pnbpNew->SetValue(pPropertyValue);
+
+	    rc = SetValue(m_CStringMap, pPropertyName, pnbpNew);
+	}
     }

-    m_CStringMap[pMapString] = (void*)pnbpNew;
-    delete[] pMapString;
-    return HXR_OK;
+    return rc;
 }

 STDMETHODIMP
@@ -966,31 +876,15 @@
 		const char*         pPropertyName,
 		REF(IHXBuffer*)    pPropertyValue)
 {
-    HX_RESULT rc = HXR_OK;
-
     _CStoreNameBufferPair* pnbpData = NULL;

-    char* pMapString = new_string(pPropertyName);
-    if(!pMapString)
-    {
-        return HXR_OUTOFMEMORY;
-    }
-
-    if(!m_bPreserveCase)
-    {
-	strlwr(pMapString);
-    }
-
-    if (!m_CStringMap.Lookup(pMapString, (void*&)pnbpData))
-    {
-	rc = HXR_FAILED;
-    }
-    else
+    HX_RESULT rc = GetValue(m_CStringMap, pPropertyName,
(void*&)pnbpData);
+
+    if (HXR_OK == rc)
     {
 	pPropertyValue = pnbpData->GetValue();
     }

-    delete[] pMapString;
     return rc;
 }

@@ -1255,3 +1149,81 @@
     return pEncodedBuffer;
 }
 #endif
+
+char* CHXHeader::CreateLookupKey(const char* pPropName) const
+{
+    char* pMapString = new_string(pPropName);
+
+    if(pMapString && !m_bPreserveCase)
+    {
+	strlwr(pMapString);
+    }
+
+    return pMapString;
+}
+
+HX_RESULT CHXHeader::GetValue(CHXMapStringToOb& map,
+			      const char* pPropName, void*& pValue) const
+{
+    HX_RESULT rc = HXR_FAILED;
+
+    char* pMapString = CreateLookupKey(pPropName);
+
+    if( !pMapString )
+    {
+        rc = HXR_OUTOFMEMORY;
+    }
+    else
+    {
+	if (map.Lookup(pMapString, pValue))
+	{
+	    rc = HXR_OK;
+	}
+
+	delete [] pMapString;
+    }
+
+    return rc;
+}
+
+HX_RESULT CHXHeader::SetValue(CHXMapStringToOb& map,
+			      const char* pPropName, _CStoreName* pNew)
const
+{
+    HX_RESULT rc = HXR_OK;
+
+    char* pMapString = CreateLookupKey(pPropName);
+
+    if (!pMapString)
+    {
+	delete pNew;
+
+	rc = HXR_OUTOFMEMORY;
+    }
+    else
+    {
+	_CStoreName* pExpired = 0;
+	if (map.Lookup(pMapString, (void*&)pExpired))
+	{
+	    delete pExpired;
+	}
+
+	map[pMapString] = pNew;
+
+	delete [] pMapString;
+    }
+
+    return rc;
+}
+
+void CHXHeader::ClearMap(CHXMapStringToOb& map) const
+{
+    CHXMapStringToOb::Iterator i;
+
+    // clean up UINT32 properties
+    for (i = map.Begin(); i != map.End(); ++i)
+    {
+	_CStoreName* pnupExpired = (_CStoreName*)*i;
+	delete pnupExpired;
+    }
+    map.RemoveAll();
+}
Index: pub/old_hxpckts.h
===================================================================
RCS file: /cvs/common/container/pub/old_hxpckts.h,v
retrieving revision 1.1.1.1
diff -u -r1.1.1.1 old_hxpckts.h
--- pub/old_hxpckts.h	18 Oct 2002 01:40:48 -0000	1.1.1.1
+++ pub/old_hxpckts.h	9 Jul 2003 00:11:22 -0000
@@ -263,6 +263,7 @@

     UINT32 GetValue();
     void SetValue(UINT32 ulValue);
+
 private:
     UINT32 m_ulValue;
 };
@@ -275,6 +276,7 @@

     IHXBuffer* GetValue();
     void SetValue(IHXBuffer* pbufValue);
+
 private:
     IHXBuffer* m_pbufValue;
 };
@@ -322,6 +324,16 @@
 	IHXBuffer*		encodeBuffer(IHXBuffer* pBuffer);
 #endif

+    char* CreateLookupKey(const char* pPropName) const;
+
+    HX_RESULT GetValue(CHXMapStringToOb& map,
+		       const char* pPropName, void*& pValue) const;
+
+    HX_RESULT SetValue(CHXMapStringToOb& map,
+		       const char* pPropName,
+		       _CStoreName* pNew) const;
+
+    void ClearMap(CHXMapStringToOb& map) const;
 public:
 	CHXHeader()
 		: m_lRefCount(0)


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe at common.helixcommunity.org
For additional commands, e-mail: dev-help at common.helixcommunity.org




More information about the Common-dev mailing list
 

Site Map   |   Terms of Use   |   Privacy Policy   |   Contact Us

Copyright © 1995-2007 RealNetworks, Inc. All rights reserved. RealNetworks and Helix are trademarks of RealNetworks.
All other trademarks or registered trademarks are the property of their respective holders.