Extract and reuse nodes creation code

This commit is contained in:
Dmitry-Me 2017-02-27 15:53:40 +03:00
parent 962732fd3e
commit 2aebfb7123
2 changed files with 24 additions and 33 deletions

View File

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

View File

@ -1819,8 +1819,21 @@ private:
static const char* _errorNames[XML_ERROR_COUNT];
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