Merge pull request #527 from Dmitry-Me/reuseNodesCreationCode

Extract and reuse nodes creation code
This commit is contained in:
Lee Thomason 2017-03-01 13:15:52 -08:00 committed by GitHub
commit 92c0ef327f
2 changed files with 24 additions and 33 deletions

View File

@ -678,46 +678,34 @@ char* XMLDocument::Identify( char* p, XMLNode** node )
TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool TIXMLASSERT( sizeof( XMLComment ) == sizeof( XMLDeclaration ) ); // use same memory pool
XMLNode* returnNode = 0; XMLNode* returnNode = 0;
if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) { if ( XMLUtil::StringEqual( p, xmlHeader, xmlHeaderLen ) ) {
TIXMLASSERT( sizeof( XMLDeclaration ) == _commentPool.ItemSize() ); returnNode = CreateUnlinkedNode<XMLDeclaration>( _commentPool );
returnNode = new (_commentPool.Alloc()) XMLDeclaration( this );
returnNode->_parseLineNum = _parseCurLineNum; returnNode->_parseLineNum = _parseCurLineNum;
returnNode->_memPool = &_commentPool;
p += xmlHeaderLen; p += xmlHeaderLen;
} }
else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) { else if ( XMLUtil::StringEqual( p, commentHeader, commentHeaderLen ) ) {
TIXMLASSERT( sizeof( XMLComment ) == _commentPool.ItemSize() ); returnNode = CreateUnlinkedNode<XMLComment>( _commentPool );
returnNode = new (_commentPool.Alloc()) XMLComment( this );
returnNode->_parseLineNum = _parseCurLineNum; returnNode->_parseLineNum = _parseCurLineNum;
returnNode->_memPool = &_commentPool;
p += commentHeaderLen; p += commentHeaderLen;
} }
else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) { else if ( XMLUtil::StringEqual( p, cdataHeader, cdataHeaderLen ) ) {
TIXMLASSERT( sizeof( XMLText ) == _textPool.ItemSize() ); XMLText* text = CreateUnlinkedNode<XMLText>( _textPool );
XMLText* text = new (_textPool.Alloc()) XMLText( this );
returnNode = text; returnNode = text;
returnNode->_parseLineNum = _parseCurLineNum; returnNode->_parseLineNum = _parseCurLineNum;
returnNode->_memPool = &_textPool;
p += cdataHeaderLen; p += cdataHeaderLen;
text->SetCData( true ); text->SetCData( true );
} }
else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) { else if ( XMLUtil::StringEqual( p, dtdHeader, dtdHeaderLen ) ) {
TIXMLASSERT( sizeof( XMLUnknown ) == _commentPool.ItemSize() ); returnNode = CreateUnlinkedNode<XMLUnknown>( _commentPool );
returnNode = new (_commentPool.Alloc()) XMLUnknown( this );
returnNode->_parseLineNum = _parseCurLineNum; returnNode->_parseLineNum = _parseCurLineNum;
returnNode->_memPool = &_commentPool;
p += dtdHeaderLen; p += dtdHeaderLen;
} }
else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) { else if ( XMLUtil::StringEqual( p, elementHeader, elementHeaderLen ) ) {
TIXMLASSERT( sizeof( XMLElement ) == _elementPool.ItemSize() ); returnNode = CreateUnlinkedNode<XMLElement>( _elementPool );
returnNode = new (_elementPool.Alloc()) XMLElement( this );
returnNode->_parseLineNum = _parseCurLineNum; returnNode->_parseLineNum = _parseCurLineNum;
returnNode->_memPool = &_elementPool;
p += elementHeaderLen; p += elementHeaderLen;
} }
else { else {
TIXMLASSERT( sizeof( XMLText ) == _textPool.ItemSize() ); returnNode = CreateUnlinkedNode<XMLText>( _textPool );
returnNode = new (_textPool.Alloc()) XMLText( this );
returnNode->_memPool = &_textPool;
returnNode->_parseLineNum = _parseCurLineNum; // Report line of first non-whitespace character returnNode->_parseLineNum = _parseCurLineNum; // Report line of first non-whitespace character
p = start; // Back it up, all the text counts. p = start; // Back it up, all the text counts.
_parseCurLineNum = startLine; _parseCurLineNum = startLine;
@ -2021,9 +2009,7 @@ void XMLDocument::Clear()
XMLElement* XMLDocument::NewElement( const char* name ) XMLElement* XMLDocument::NewElement( const char* name )
{ {
TIXMLASSERT( sizeof( XMLElement ) == _elementPool.ItemSize() ); XMLElement* ele = CreateUnlinkedNode<XMLElement>( _elementPool );
XMLElement* ele = new (_elementPool.Alloc()) XMLElement( this );
ele->_memPool = &_elementPool;
ele->SetName( name ); ele->SetName( name );
return ele; return ele;
} }
@ -2031,9 +2017,7 @@ XMLElement* XMLDocument::NewElement( const char* name )
XMLComment* XMLDocument::NewComment( const char* str ) XMLComment* XMLDocument::NewComment( const char* str )
{ {
TIXMLASSERT( sizeof( XMLComment ) == _commentPool.ItemSize() ); XMLComment* comment = CreateUnlinkedNode<XMLComment>( _commentPool );
XMLComment* comment = new (_commentPool.Alloc()) XMLComment( this );
comment->_memPool = &_commentPool;
comment->SetValue( str ); comment->SetValue( str );
return comment; return comment;
} }
@ -2041,9 +2025,7 @@ XMLComment* XMLDocument::NewComment( const char* str )
XMLText* XMLDocument::NewText( const char* str ) XMLText* XMLDocument::NewText( const char* str )
{ {
TIXMLASSERT( sizeof( XMLText ) == _textPool.ItemSize() ); XMLText* text = CreateUnlinkedNode<XMLText>( _textPool );
XMLText* text = new (_textPool.Alloc()) XMLText( this );
text->_memPool = &_textPool;
text->SetValue( str ); text->SetValue( str );
return text; return text;
} }
@ -2051,9 +2033,7 @@ XMLText* XMLDocument::NewText( const char* str )
XMLDeclaration* XMLDocument::NewDeclaration( const char* str ) XMLDeclaration* XMLDocument::NewDeclaration( const char* str )
{ {
TIXMLASSERT( sizeof( XMLDeclaration ) == _commentPool.ItemSize() ); XMLDeclaration* dec = CreateUnlinkedNode<XMLDeclaration>( _commentPool );
XMLDeclaration* dec = new (_commentPool.Alloc()) XMLDeclaration( this );
dec->_memPool = &_commentPool;
dec->SetValue( str ? str : "xml version=\"1.0\" encoding=\"UTF-8\"" ); dec->SetValue( str ? str : "xml version=\"1.0\" encoding=\"UTF-8\"" );
return dec; return dec;
} }
@ -2061,9 +2041,7 @@ XMLDeclaration* XMLDocument::NewDeclaration( const char* str )
XMLUnknown* XMLDocument::NewUnknown( const char* str ) XMLUnknown* XMLDocument::NewUnknown( const char* str )
{ {
TIXMLASSERT( sizeof( XMLUnknown ) == _commentPool.ItemSize() ); XMLUnknown* unk = CreateUnlinkedNode<XMLUnknown>( _commentPool );
XMLUnknown* unk = new (_commentPool.Alloc()) XMLUnknown( this );
unk->_memPool = &_commentPool;
unk->SetValue( str ); unk->SetValue( str );
return unk; return unk;
} }

View File

@ -1830,8 +1830,21 @@ private:
static const char* _errorNames[XML_ERROR_COUNT]; static const char* _errorNames[XML_ERROR_COUNT];
void Parse(); void Parse();
template<class NodeType, int PoolElementSize>
NodeType* CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool );
}; };
template<class NodeType, int PoolElementSize>
inline NodeType* XMLDocument::CreateUnlinkedNode( MemPoolT<PoolElementSize>& pool )
{
TIXMLASSERT( sizeof( NodeType ) == PoolElementSize );
TIXMLASSERT( sizeof( NodeType ) == pool.ItemSize() );
NodeType* returnNode = new (pool.Alloc()) NodeType( this );
TIXMLASSERT( returnNode );
returnNode->_memPool = &pool;
return returnNode;
}
/** /**
A XMLHandle is a class that wraps a node pointer with null checks; this is A XMLHandle is a class that wraps a node pointer with null checks; this is