From a60caa28ccb20569034a9ee23460ecc8943636d5 Mon Sep 17 00:00:00 2001 From: Dmitry-Me Date: Tue, 22 Nov 2016 18:28:08 +0300 Subject: [PATCH] Reuse attribute creation code --- tinyxml2.cpp | 21 +++++++++++++-------- tinyxml2.h | 1 + 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/tinyxml2.cpp b/tinyxml2.cpp index 826eb9b..941bd37 100755 --- a/tinyxml2.cpp +++ b/tinyxml2.cpp @@ -1714,9 +1714,8 @@ XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name ) } } if ( !attrib ) { - TIXMLASSERT( sizeof( XMLAttribute ) == _document->_attributePool.ItemSize() ); - attrib = new (_document->_attributePool.Alloc() ) XMLAttribute(); - attrib->_memPool = &_document->_attributePool; + attrib = CreateAttribute(); + TIXMLASSERT( attrib ); if ( last ) { last->_next = attrib; } @@ -1724,7 +1723,6 @@ XMLAttribute* XMLElement::FindOrCreateAttribute( const char* name ) _rootAttribute = attrib; } attrib->SetName( name ); - attrib->_memPool->SetTracked(); // always created and linked. } return attrib; } @@ -1764,10 +1762,8 @@ char* XMLElement::ParseAttributes( char* p ) // attribute. if (XMLUtil::IsNameStartChar( *p ) ) { - TIXMLASSERT( sizeof( XMLAttribute ) == _document->_attributePool.ItemSize() ); - XMLAttribute* attrib = new (_document->_attributePool.Alloc() ) XMLAttribute(); - attrib->_memPool = &_document->_attributePool; - attrib->_memPool->SetTracked(); + XMLAttribute* attrib = CreateAttribute(); + TIXMLASSERT( attrib ); p = attrib->ParseDeep( p, _document->ProcessEntities() ); if ( !p || Attribute( attrib->Name() ) ) { @@ -1816,6 +1812,15 @@ void XMLElement::DeleteAttribute( XMLAttribute* attribute ) pool->Free( attribute ); } +XMLAttribute* XMLElement::CreateAttribute() +{ + TIXMLASSERT( sizeof( XMLAttribute ) == _document->_attributePool.ItemSize() ); + XMLAttribute* attrib = new (_document->_attributePool.Alloc() ) XMLAttribute(); + attrib->_memPool = &_document->_attributePool; + attrib->_memPool->SetTracked(); + return attrib; +} + // // // foobar diff --git a/tinyxml2.h b/tinyxml2.h index 12b204f..bee739e 100755 --- a/tinyxml2.h +++ b/tinyxml2.h @@ -1563,6 +1563,7 @@ private: //void LinkAttribute( XMLAttribute* attrib ); char* ParseAttributes( char* p ); static void DeleteAttribute( XMLAttribute* attribute ); + XMLAttribute* CreateAttribute(); enum { BUF_SIZE = 200 }; int _closingType;