From 6808723b85b4305584451049a66ecb0b8c6cda23 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Fri, 6 Oct 2006 06:26:53 +0000 Subject: [PATCH] XNodeValue base class, Copy() --- stepmania/src/ActorUtil.cpp | 2 +- stepmania/src/ThemeManager.cpp | 2 +- stepmania/src/XmlFile.cpp | 22 +++++++++++----------- stepmania/src/XmlFile.h | 25 ++++++++++++++++++++++++- stepmania/src/XmlFileUtil.cpp | 6 +++--- 5 files changed, 40 insertions(+), 17 deletions(-) diff --git a/stepmania/src/ActorUtil.cpp b/stepmania/src/ActorUtil.cpp index 82c846febe..eaca28ec82 100644 --- a/stepmania/src/ActorUtil.cpp +++ b/stepmania/src/ActorUtil.cpp @@ -321,7 +321,7 @@ static void MergeActorXML( XNode *pChild, const XNode *pParent ) pParent->GetName().c_str() ); Dialog::OK( sWarning, "XML_ATTRIB_OVERRIDE" ); } - pChild->AppendAttr( p->first, new XNodeValue(*p->second) ); + pChild->AppendAttr( p->first, p->second->Copy() ); } } diff --git a/stepmania/src/ThemeManager.cpp b/stepmania/src/ThemeManager.cpp index aea660cbd3..e369167740 100644 --- a/stepmania/src/ThemeManager.cpp +++ b/stepmania/src/ThemeManager.cpp @@ -284,7 +284,7 @@ static void MergeIniUnder( XNode *pFrom, XNode *pTo ) FOREACHM( RString, XNodeValue *, pSectionNode->m_attrs, it2 ) { /* Don't overwrite existing nodes. */ - pChildNode->AppendAttr( it2->first, new XNodeValue(*it2->second), false ); + pChildNode->AppendAttr( it2->first, it2->second->Copy(), false ); } } diff --git a/stepmania/src/XmlFile.cpp b/stepmania/src/XmlFile.cpp index 96ef0605eb..04ad738f7d 100644 --- a/stepmania/src/XmlFile.cpp +++ b/stepmania/src/XmlFile.cpp @@ -18,7 +18,7 @@ XNode::XNode( const XNode &cpy ): m_Value( cpy.m_Value ) { FOREACH_CONST_Attr( &cpy, pAttr ) - this->AppendAttr( pAttr->first, new XNodeValue(*pAttr->second) ); + this->AppendAttr( pAttr->first, pAttr->second->Copy() ); FOREACH_CONST_Child( &cpy, c ) this->AppendChild( new XNode(*c) ); } @@ -38,16 +38,16 @@ void XNode::Clear() m_attrs.clear(); } -void XNodeValue::GetValue( RString &out ) const { out = m_sValue; } -void XNodeValue::GetValue( int &out ) const { out = atoi(m_sValue); } -void XNodeValue::GetValue( float &out ) const { out = StringToFloat(m_sValue); } -void XNodeValue::GetValue( bool &out ) const { out = atoi(m_sValue) != 0; } -void XNodeValue::GetValue( unsigned &out ) const { out = strtoul(m_sValue,NULL,0); } +void XNodeStringValue::GetValue( RString &out ) const { out = m_sValue; } +void XNodeStringValue::GetValue( int &out ) const { out = atoi(m_sValue); } +void XNodeStringValue::GetValue( float &out ) const { out = StringToFloat(m_sValue); } +void XNodeStringValue::GetValue( bool &out ) const { out = atoi(m_sValue) != 0; } +void XNodeStringValue::GetValue( unsigned &out ) const { out = strtoul(m_sValue,NULL,0); } -void XNodeValue::SetValue( const RString &v ) { m_sValue = v; } -void XNodeValue::SetValue( int v ) { m_sValue = ssprintf("%d",v); } -void XNodeValue::SetValue( float v ) { m_sValue = ssprintf("%f",v); } -void XNodeValue::SetValue( unsigned v ) { m_sValue = ssprintf("%u",v); } +void XNodeStringValue::SetValue( const RString &v ) { m_sValue = v; } +void XNodeStringValue::SetValue( int v ) { m_sValue = ssprintf("%d",v); } +void XNodeStringValue::SetValue( float v ) { m_sValue = ssprintf("%f",v); } +void XNodeStringValue::SetValue( unsigned v ) { m_sValue = ssprintf("%u",v); } const XNodeValue *XNode::GetAttr( const RString &attrname ) const { @@ -153,7 +153,7 @@ XNodeValue *XNode::AppendAttr( const RString &sName ) DEBUG_ASSERT( sName.size() ); pair ret = m_attrs.insert( make_pair(sName, (XNodeValue *) NULL) ); if( ret.second ) - ret.first->second = new XNodeValue(); + ret.first->second = new XNodeStringValue; return ret.first->second; // already existed } diff --git a/stepmania/src/XmlFile.h b/stepmania/src/XmlFile.h index 77ec3ebeba..936f9d9f83 100644 --- a/stepmania/src/XmlFile.h +++ b/stepmania/src/XmlFile.h @@ -9,9 +9,32 @@ class RageFileBasic; class XNodeValue { +public: + virtual ~XNodeValue() { } + virtual XNodeValue *Copy() const = 0; + + virtual void GetValue( RString &out ) const = 0; + virtual void GetValue( int &out ) const = 0; + virtual void GetValue( float &out ) const = 0; + virtual void GetValue( bool &out ) const = 0; + virtual void GetValue( unsigned &out ) const = 0; + + template + T GetValue() const { T val; GetValue(val); return val; } + + virtual void SetValue( const RString &v ) = 0; + virtual void SetValue( int v ) = 0; + virtual void SetValue( float v ) = 0; + virtual void SetValue( unsigned v ) = 0; +}; + +class XNodeStringValue: public XNodeValue +{ public: RString m_sValue; + XNodeValue *Copy() const { return new XNodeStringValue( *this ); } + void GetValue( RString &out ) const; void GetValue( int &out ) const; void GetValue( float &out ) const; @@ -60,7 +83,7 @@ class XNode { public: RString m_sName; // a duplicate of the m_sName in the parent's map - XNodeValue m_Value; + XNodeStringValue m_Value; XNodes m_childs; // child node XAttrs m_attrs; // attributes diff --git a/stepmania/src/XmlFileUtil.cpp b/stepmania/src/XmlFileUtil.cpp index d0d96a2769..dfa57b40ac 100644 --- a/stepmania/src/XmlFileUtil.cpp +++ b/stepmania/src/XmlFileUtil.cpp @@ -150,8 +150,7 @@ unsigned LoadAttributes( XNode *pNode, const RString &xml, RString &sErrorOut, u // add new attribute DEBUG_ASSERT( sName.size() ); - pair it = pNode->m_attrs.insert( make_pair(sName, new XNodeValue()) ); - RString &sValue = it.first->second->m_sValue; + XNodeValue *pAttr = pNode->AppendAttr( sName ); iOffset = iEnd; // XML Attr Value @@ -188,9 +187,10 @@ unsigned LoadAttributes( XNode *pNode, const RString &xml, RString &sErrorOut, u return string::npos; } + RString sValue; SetString( xml, iOffset, iEnd, &sValue, true ); + pAttr->SetValue( sValue ); iOffset = iEnd; - // ATTRVALUE ReplaceEntityText( sValue, g_mapEntitiesToChars ); if( quote == '"' || quote == '\'' )