123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2013 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #ifndef _TAML_CUSTOM_H_
- #define _TAML_CUSTOM_H_
- #ifndef _FACTORY_CACHE_H_
- #include "core/factoryCache.h"
- #endif
- #ifndef _STRINGTABLE_H_
- #include "core/stringTable.h"
- #endif
- #ifndef _CONSOLE_H_
- #include "console/console.h"
- #endif
- #ifndef _CONSOLETYPES_H_
- #include "console/consoleTypes.h"
- #endif
- #ifndef B2_MATH_H
- //TODO: Look at this
- //#include "box2d/Common/b2Math.h"
- #endif
- #ifndef _COLOR_H_
- #include "core/color.h"
- #endif
- #ifndef _SIMBASE_H_
- #include "console/simBase.h"
- #endif
- #include "core/util/safeDelete.h"
- #include "math/mMath.h"
- //-----------------------------------------------------------------------------
- #define MAX_TAML_NODE_FIELDVALUE_LENGTH 2048
- //-----------------------------------------------------------------------------
- class TamlWriteNode;
- class TamlCustomNode;
- class TamlCustomField;
- extern FactoryCache<TamlCustomNode> TamlCustomNodeFactory;
- extern FactoryCache<TamlCustomField> TamlCustomFieldFactory;
- typedef Vector<TamlCustomNode*> TamlCustomNodeVector;
- typedef Vector<TamlCustomField*> TamlCustomFieldVector;
- //-----------------------------------------------------------------------------
- class TamlCustomField : public IFactoryObjectReset
- {
- public:
- TamlCustomField()
- {
- resetState();
- }
- virtual ~TamlCustomField()
- {
- // Everything should already be cleared in a state reset.
- // Touching any memory here is dangerous as this type is typically
- // held in a static factory cache until shutdown at which point
- // pretty much anything or everything could be invalid!
- }
- virtual void resetState( void )
- {
- mFieldName = StringTable->EmptyString();
- *mFieldValue = 0;
- }
- void set( const char* pFieldName, const char* pFieldValue );
- inline void setFieldValue( const char* pFieldName, const ColorI& fieldValue )
- {
- // Fetch the field value.
- const char* pFieldValue = Con::getData( TypeColorI, &const_cast<ColorI&>(fieldValue), 0 );
- // Did we get a field value?
- if ( pFieldValue == NULL )
- {
- // No, so warn.
- Con::warnf( "Taml: Failed to add node field name '%s' with ColorI value.", pFieldName );
- pFieldValue = StringTable->EmptyString();
- }
- set( pFieldName, pFieldValue );
- }
- inline void setFieldValue( const char* pFieldName, const LinearColorF& fieldValue )
- {
- // Fetch the field value.
- const char* pFieldValue = Con::getData( TypeColorF, &const_cast<LinearColorF&>(fieldValue), 0 );
- // Did we get a field value?
- if ( pFieldValue == NULL )
- {
- // No, so warn.
- Con::warnf( "Taml: Failed to add node field name '%s' with LinearColorF value.", pFieldName );
- pFieldValue = StringTable->EmptyString();
- }
- set( pFieldName, pFieldValue );
- }
- inline void setFieldValue( const char* pFieldName, const Point2I& fieldValue )
- {
- char fieldValueBuffer[32];
- dSprintf( fieldValueBuffer, sizeof(fieldValueBuffer), "%d %d", fieldValue.x, fieldValue.y );
- set( pFieldName, fieldValueBuffer );
- }
- inline void setFieldValue( const char* pFieldName, const Point2F& fieldValue )
- {
- char fieldValueBuffer[32];
- dSprintf( fieldValueBuffer, sizeof(fieldValueBuffer), "%.5g %0.5g", fieldValue.x, fieldValue.y );
- set( pFieldName, fieldValueBuffer );
- }
- inline void setFieldValue( const char* pFieldName, const Point3I& fieldValue )
- {
- char fieldValueBuffer[32];
- dSprintf( fieldValueBuffer, sizeof(fieldValueBuffer), "%d %d %d", fieldValue.x, fieldValue.y, fieldValue.z );
- set( pFieldName, fieldValueBuffer );
- }
-
- inline void setFieldValue( const char* pFieldName, const Point3F& fieldValue )
- {
- char fieldValueBuffer[32];
- dSprintf( fieldValueBuffer, sizeof(fieldValueBuffer), "%.5g %0.5g %.5g", fieldValue.x, fieldValue.y, fieldValue.z );
- set( pFieldName, fieldValueBuffer );
- }
-
- inline void setFieldValue( const char* pFieldName, const RectF& fieldValue )
- {
- char fieldValueBuffer[32];
- dSprintf( fieldValueBuffer, sizeof(fieldValueBuffer), "%.5g %0.5g %.5g %.5g",
- fieldValue.point.x, fieldValue.point.y, fieldValue.extent.x, fieldValue.extent.y);
- set( pFieldName, fieldValueBuffer );
- }
-
- inline void setFieldValue( const char* pFieldName, const QuatF& fieldValue )
- {
- char fieldValueBuffer[32];
- dSprintf( fieldValueBuffer, sizeof(fieldValueBuffer), "%.5g %0.5g %.5g %.5g", fieldValue.x, fieldValue.y, fieldValue.z, fieldValue.w );
- set( pFieldName, fieldValueBuffer );
- }
-
- inline void setFieldValue( const char* pFieldName, const AngAxisF& fieldValue )
- {
- char fieldValueBuffer[32];
- dSprintf( fieldValueBuffer, sizeof(fieldValueBuffer), "%.5g %0.5g %.5g %.5g", fieldValue.axis.x, fieldValue.axis.y, fieldValue.axis.z, fieldValue.angle );
- set( pFieldName, fieldValueBuffer );
- }
- inline void setFieldValue( const char* pFieldName, const U32 fieldValue )
- {
- char fieldValueBuffer[16];
- dSprintf( fieldValueBuffer, sizeof(fieldValueBuffer), "%d", fieldValue );
- set( pFieldName, fieldValueBuffer );
- }
- inline void setFieldValue( const char* pFieldName, const bool fieldValue )
- {
- char fieldValueBuffer[16];
- dSprintf( fieldValueBuffer, sizeof(fieldValueBuffer), "%d", fieldValue );
- set( pFieldName, fieldValueBuffer );
- }
- inline void setFieldValue( const char* pFieldName, const S32 fieldValue )
- {
- char fieldValueBuffer[16];
- dSprintf( fieldValueBuffer, sizeof(fieldValueBuffer), "%d", fieldValue );
- set( pFieldName, fieldValueBuffer );
- }
- inline void setFieldValue( const char* pFieldName, const float fieldValue )
- {
- char fieldValueBuffer[16];
- dSprintf( fieldValueBuffer, sizeof(fieldValueBuffer), "%.5g", fieldValue );
- set( pFieldName, fieldValueBuffer );
- }
- inline void setFieldValue( const char* pFieldName, const char* fieldValue )
- {
- set( pFieldName, fieldValue );
- }
- inline void getFieldValue( LinearColorF& fieldValue ) const
- {
- fieldValue.set( 1.0f, 1.0f, 1.0f, 1.0f );
- // Set color.
- const char* argv = (char*)mFieldValue;
- Con::setData( TypeColorF, &fieldValue, 0, 1, &argv );
- }
- inline void getFieldValue( ColorI& fieldValue ) const
- {
- fieldValue.set( 255, 255, 255, 255 );
- // Set color.
- const char* argv = (char*)mFieldValue;
- Con::setData( TypeColorI, &fieldValue, 0, 1, &argv );
- }
- inline void getFieldValue( Point2I& fieldValue ) const
- {
- if ( dSscanf( mFieldValue, "%d %d", &fieldValue.x, &fieldValue.y ) != 2 )
- {
- // Warn.
- Con::warnf( "TamlCustomField - Reading point2I but it has an incorrect format: '%s'.", mFieldValue );
- }
- }
- inline void getFieldValue( Point2F& fieldValue ) const
- {
- if ( dSscanf( mFieldValue, "%g %g", &fieldValue.x, &fieldValue.y ) != 2 )
- {
- // Warn.
- Con::warnf( "TamlCustomField - Reading point2F but it has an incorrect format: '%s'.", mFieldValue );
- }
- }
- inline void getFieldValue( Point3I& fieldValue ) const
- {
- if ( dSscanf( mFieldValue, "%d %d %d", &fieldValue.x, &fieldValue.y, &fieldValue.z ) != 3 )
- {
- // Warn.
- Con::warnf( "TamlCustomField - Reading point3I but it has an incorrect format: '%s'.", mFieldValue );
- }
- }
- inline void getFieldValue( Point3F& fieldValue ) const
- {
- if ( dSscanf( mFieldValue, "%g %g %g", &fieldValue.x, &fieldValue.y, &fieldValue.z ) != 3 )
- {
- // Warn.
- Con::warnf( "TamlCustomField - Reading point3F but it has an incorrect format: '%s'.", mFieldValue );
- }
- }
- inline void getFieldValue( RectF& fieldValue ) const
- {
- if ( dSscanf( mFieldValue, "%g %g %g %g", &fieldValue.point.x, &fieldValue.point.y, &fieldValue.extent.x, &fieldValue.extent.y ) != 3 )
- {
- // Warn.
- Con::warnf( "TamlCustomField - Reading RectF but it has an incorrect format: '%s'.", mFieldValue );
- }
- }
-
- inline void getFieldValue( QuatF& fieldValue ) const
- {
- if ( dSscanf( mFieldValue, "%g %g %g %g", &fieldValue.x, &fieldValue.y, &fieldValue.z, &fieldValue.w ) != 4 )
- {
- // Warn.
- Con::warnf( "TamlCustomField - Reading QuatF but it has an incorrect format: '%s'.", mFieldValue );
- }
- }
-
- inline void getFieldValue( AngAxisF& fieldValue ) const
- {
- if ( dSscanf( mFieldValue, "%g %g %g %g", &fieldValue.axis.x, &fieldValue.axis.y, &fieldValue.axis.z, &fieldValue.angle ) != 4 )
- {
- // Warn.
- Con::warnf( "TamlCustomField - Reading AngAxisF but it has an incorrect format: '%s'.", mFieldValue );
- }
- }
- inline void getFieldValue( bool& fieldValue ) const
- {
- fieldValue = dAtob( mFieldValue );
- }
- inline void getFieldValue( S32& fieldValue ) const
- {
- fieldValue = dAtoi( mFieldValue );
- }
- inline void getFieldValue( U32& fieldValue ) const
- {
- fieldValue = (U32)dAtoi( mFieldValue );
- }
- inline void getFieldValue( F32& fieldValue ) const
- {
- fieldValue = dAtof( mFieldValue );
- }
- inline const char* getFieldValue( void ) const
- {
- return mFieldValue;
- }
- inline StringTableEntry getFieldName( void ) const { return mFieldName; }
- bool fieldNameBeginsWith( const char* pComparison ) const
- {
- const U32 comparisonLength = dStrlen( pComparison );
- const U32 fieldNameLength = dStrlen( mFieldName );
- if ( comparisonLength == 0 || fieldNameLength == 0 || comparisonLength > fieldNameLength )
- return false;
- StringTableEntry comparison = StringTable->insert( pComparison );
- char fieldNameBuffer[1024];
- // Sanity!
- AssertFatal( fieldNameLength < sizeof(fieldNameBuffer), "TamlCustomField: Field name is too long." );
- dStrcpy( fieldNameBuffer, mFieldName, 1024 );
- fieldNameBuffer[fieldNameLength-1] = 0;
- StringTableEntry fieldName = StringTable->insert( fieldNameBuffer );
- return ( fieldName == comparison );
- }
- inline bool isValueEmpty( void ) const { return *mFieldValue == 0; }
- private:
- StringTableEntry mFieldName;
- char mFieldValue[MAX_TAML_NODE_FIELDVALUE_LENGTH];
- };
- //-----------------------------------------------------------------------------
- class TamlCustomNode : public IFactoryObjectReset
- {
- public:
- TamlCustomNode()
- {
- // Reset proxy object.
- // NOTE: This MUST be done before the state is reset otherwise we'll be touching uninitialized stuff.
- mpProxyWriteNode = NULL;
- mpProxyObject = NULL;
- resetState();
- }
- virtual ~TamlCustomNode()
- {
- // Everything should already be cleared in a state reset.
- // Touching any memory here is dangerous as this type is typically
- // held in a static factory cache until shutdown at which point
- // pretty much anything or everything could be invalid!
- }
- virtual void resetState( void )
- {
- // We don't need to delete the write node as it'll get destroyed when the compilation is reset!
- mpProxyWriteNode = NULL;
- mpProxyObject = NULL;
- // Cache the children.
- while ( mChildren.size() > 0 )
- {
- TamlCustomNodeFactory.cacheObject( mChildren.back() );
- mChildren.pop_back();
- }
- // Cache the fields.
- while( mFields.size() > 0 )
- {
- TamlCustomFieldFactory.cacheObject( mFields.back() );
- mFields.pop_back();
- }
- // Reset the node name.
- mNodeName = StringTable->EmptyString();
- // Reset node text.
- mNodeText.resetState();
- // Reset the ignore empty flag.
- mIgnoreEmpty = false;
- }
- inline TamlCustomNode* addNode( SimObject* pProxyObject )
- {
- // Sanity!
- AssertFatal( pProxyObject != NULL, "Field object cannot be NULL." );
- AssertFatal( mpProxyWriteNode == NULL, "Field write node must be NULL." );
- // Create a custom node.
- TamlCustomNode* pCustomNode = TamlCustomNodeFactory.createObject();
- // Set node name.
- pCustomNode->setNodeName( pProxyObject->getClassName() );
- // Set proxy object.
- pCustomNode->mpProxyObject = pProxyObject;
- // Store node.
- mChildren.push_back( pCustomNode );
- return pCustomNode;
- }
- inline TamlCustomNode* addNode( const char* pNodeName, const bool ignoreEmpty = true )
- {
- // Create a custom node.
- TamlCustomNode* pCustomNode = TamlCustomNodeFactory.createObject();
- // Fetch node name.
- pCustomNode->setNodeName( pNodeName );
- // Set ignore-empty flag.
- pCustomNode->setIgnoreEmpty( ignoreEmpty );
- // Store node.
- mChildren.push_back( pCustomNode );
- return pCustomNode;
- }
- inline void removeNode( const U32 index )
- {
- // Sanity!
- AssertFatal( index < (U32)mChildren.size(), "tamlCustomNode::removeNode() - Index is out of bounds." );
- // Cache the custom node.
- TamlCustomNodeFactory.cacheObject( mChildren[index] );
- // Remove it.
- mChildren.erase( index );
- }
- inline const TamlCustomNode* findNode( const char* pNodeName ) const
- {
- // Sanity!
- AssertFatal( pNodeName != NULL, "Cannot find Taml node name that is NULL." );
- // Fetch node name.
- StringTableEntry nodeName = StringTable->insert( pNodeName );
- // Find node.
- for( Vector<TamlCustomNode*>::const_iterator nodeItr = mChildren.begin(); nodeItr != mChildren.end(); ++nodeItr )
- {
- if ( (*nodeItr)->getNodeName() == nodeName )
- return (*nodeItr);
- }
- return NULL;
- }
- inline TamlCustomField* addField( const char* pFieldName, const ColorI& fieldValue )
- {
- TamlCustomField* pNodeField = TamlCustomFieldFactory.createObject();
- pNodeField->setFieldValue( pFieldName, fieldValue );
- return registerField( pNodeField );
- }
- inline TamlCustomField* addField( const char* pFieldName, const LinearColorF& fieldValue )
- {
- TamlCustomField* pNodeField = TamlCustomFieldFactory.createObject();
- pNodeField->setFieldValue( pFieldName, fieldValue );
- return registerField( pNodeField );
- }
- inline TamlCustomField* addField( const char* pFieldName, const Point2I& fieldValue )
- {
- TamlCustomField* pNodeField = TamlCustomFieldFactory.createObject();
- pNodeField->setFieldValue( pFieldName, fieldValue );
- return registerField( pNodeField );
- }
- inline TamlCustomField* addField( const char* pFieldName, const Point2F& fieldValue )
- {
- TamlCustomField* pNodeField = TamlCustomFieldFactory.createObject();
- pNodeField->setFieldValue( pFieldName, fieldValue );
- return registerField( pNodeField );
- }
- inline TamlCustomField* addField( const char* pFieldName, const Point3I& fieldValue )
- {
- TamlCustomField* pNodeField = TamlCustomFieldFactory.createObject();
- pNodeField->setFieldValue( pFieldName, fieldValue );
- return registerField( pNodeField );
- }
- inline TamlCustomField* addField( const char* pFieldName, const Point3F& fieldValue )
- {
- TamlCustomField* pNodeField = TamlCustomFieldFactory.createObject();
- pNodeField->setFieldValue( pFieldName, fieldValue );
- return registerField( pNodeField );
- }
- inline TamlCustomField* addField( const char* pFieldName, const RectF& fieldValue )
- {
- TamlCustomField* pNodeField = TamlCustomFieldFactory.createObject();
- pNodeField->setFieldValue( pFieldName, fieldValue );
- return registerField( pNodeField );
- }
-
- inline TamlCustomField* addField( const char* pFieldName, const QuatF& fieldValue )
- {
- TamlCustomField* pNodeField = TamlCustomFieldFactory.createObject();
- pNodeField->setFieldValue( pFieldName, fieldValue );
- return registerField( pNodeField );
- }
-
- inline TamlCustomField* addField( const char* pFieldName, const AngAxisF& fieldValue )
- {
- TamlCustomField* pNodeField = TamlCustomFieldFactory.createObject();
- pNodeField->setFieldValue( pFieldName, fieldValue );
- return registerField( pNodeField );
- }
- inline TamlCustomField* addField( const char* pFieldName, const U32 fieldValue )
- {
- TamlCustomField* pNodeField = TamlCustomFieldFactory.createObject();
- pNodeField->setFieldValue( pFieldName, fieldValue );
- return registerField( pNodeField );
- }
- inline TamlCustomField* addField( const char* pFieldName, const bool fieldValue )
- {
- TamlCustomField* pNodeField = TamlCustomFieldFactory.createObject();
- pNodeField->setFieldValue( pFieldName, fieldValue );
- return registerField( pNodeField );
- }
- inline TamlCustomField* addField( const char* pFieldName, const S32 fieldValue )
- {
- TamlCustomField* pNodeField = TamlCustomFieldFactory.createObject();
- pNodeField->setFieldValue( pFieldName, fieldValue );
- return registerField( pNodeField );
- }
- inline TamlCustomField* addField( const char* pFieldName, const float fieldValue )
- {
- TamlCustomField* pNodeField = TamlCustomFieldFactory.createObject();
- pNodeField->setFieldValue( pFieldName, fieldValue );
- return registerField( pNodeField );
- }
- inline TamlCustomField* addField( const char* pFieldName, const char* fieldValue )
- {
- TamlCustomField* pNodeField = TamlCustomFieldFactory.createObject();
- pNodeField->setFieldValue( pFieldName, fieldValue );
- return registerField( pNodeField );
- }
- inline const TamlCustomField* findField( const char* pFieldName ) const
- {
- // Sanity!
- AssertFatal( pFieldName != NULL, "Cannot find Taml field name that is NULL." );
- // Fetch field name.
- StringTableEntry fieldName = StringTable->insert( pFieldName );
- // Find node field.
- for( TamlCustomFieldVector::const_iterator fieldItr = mFields.begin(); fieldItr != mFields.end(); ++fieldItr )
- {
- if ( (*fieldItr)->getFieldName() == fieldName )
- return (*fieldItr);
- }
- return NULL;
- }
- inline void setNodeName( const char* pNodeName )
- {
- // Sanity!
- AssertFatal( pNodeName != NULL, "Cannot add a NULL node name." );
- mNodeName = StringTable->insert( pNodeName );
- }
- inline StringTableEntry getNodeName( void ) const { return mNodeName; }
- void setWriteNode( TamlWriteNode* pWriteNode );
- inline void setNodeText( const char* pNodeText )
- {
- AssertFatal( dStrlen( pNodeText ) < MAX_TAML_NODE_FIELDVALUE_LENGTH, "Custom node text is too long." );
- mNodeText.set( StringTable->EmptyString(), pNodeText );
- }
- inline const TamlCustomField& getNodeTextField( void ) const { return mNodeText; }
- inline TamlCustomField& getNodeTextField( void ) { return mNodeText; }
- inline const Vector<TamlCustomNode*>& getChildren( void ) const { return mChildren; }
- inline const TamlCustomFieldVector& getFields( void ) const { return mFields; }
- inline bool isProxyObject( void ) const { return mpProxyObject != NULL; }
- template<typename T> T* getProxyObject( const bool deleteIfNotType ) const
- {
- // Return nothing if no proxy object.
- if ( mpProxyObject == NULL )
- return NULL;
- // Cast object to specified type.
- T* pTypeCast = dynamic_cast<T*>( mpProxyObject );
- // Destroy the object if not the specified type and requested to do so.
- if ( deleteIfNotType && pTypeCast == NULL )
- {
- mpProxyObject->deleteObject();
- return NULL;
- }
- return pTypeCast;
- }
- inline const TamlWriteNode* getProxyWriteNode( void ) const { return mpProxyWriteNode; }
- inline bool isEmpty( void ) const { return mNodeText.isValueEmpty() && mFields.size() == 0 && mChildren.size() == 0; }
- inline void setIgnoreEmpty( const bool ignoreEmpty ) { mIgnoreEmpty = ignoreEmpty; }
- inline bool getIgnoreEmpty( void ) const { return mIgnoreEmpty; }
- private:
- inline TamlCustomField* registerField( TamlCustomField* pCustomField )
- {
- #ifdef TORQUE_DEBUG
- // Ensure a field name conflict does not exist.
- for( Vector<TamlCustomField*>::iterator nodeFieldItr = mFields.begin(); nodeFieldItr != mFields.end(); ++nodeFieldItr )
- {
- // Skip if field name is not the same.
- if ( pCustomField->getFieldName() != (*nodeFieldItr)->getFieldName() )
- continue;
- // Warn!
- Con::warnf("Conflicting Taml node field name of '%s' in node '%s'.", pCustomField->getFieldName(), mNodeName );
- // Cache node field.
- TamlCustomFieldFactory.cacheObject( pCustomField );
- return NULL;
- }
- // Ensure the field value is not too long.
- if ( dStrlen( pCustomField->getFieldValue() ) >= MAX_TAML_NODE_FIELDVALUE_LENGTH )
- {
- // Warn.
- Con::warnf("Taml field name '%s' has a field value that is too long (Max:%d): '%s'.",
- pCustomField->getFieldName(),
- MAX_TAML_NODE_FIELDVALUE_LENGTH,
- pCustomField->getFieldValue() );
- // Cache node field.
- TamlCustomFieldFactory.cacheObject( pCustomField );
- return NULL;
- }
- #endif
- // Store node field.
- mFields.push_back( pCustomField );
- return pCustomField;
- }
- inline TamlCustomField* createField( void ) const { return TamlCustomFieldFactory.createObject(); }
- private:
- StringTableEntry mNodeName;
- TamlCustomField mNodeText;
- Vector<TamlCustomNode*> mChildren;
- TamlCustomFieldVector mFields;
- bool mIgnoreEmpty;
- SimObject* mpProxyObject;
- TamlWriteNode* mpProxyWriteNode;
- };
- //-----------------------------------------------------------------------------
- class TamlCustomNodes : public IFactoryObjectReset
- {
- public:
- TamlCustomNodes()
- {
- }
- virtual ~TamlCustomNodes()
- {
- resetState();
- }
- virtual void resetState( void )
- {
- // Cache the nodes.
- while ( mNodes.size() > 0 )
- {
- TamlCustomNodeFactory.cacheObject( mNodes.back() );
- mNodes.pop_back();
- }
- }
- inline TamlCustomNode* addNode( const char* pNodeName, const bool ignoreEmpty = true )
- {
- // Create a custom node.
- TamlCustomNode* pCustomNode = TamlCustomNodeFactory.createObject();
- // Set node name.
- pCustomNode->setNodeName( pNodeName );
- // Set ignore-empty flag.
- pCustomNode->setIgnoreEmpty( ignoreEmpty );
- #ifdef TORQUE_DEBUG
- // Ensure a node name conflict does not exist.
- for( TamlCustomNodeVector::iterator nodeItr = mNodes.begin(); nodeItr != mNodes.end(); ++nodeItr )
- {
- // Skip if node name is not the same.
- if ( pCustomNode->getNodeName() != (*nodeItr)->getNodeName() )
- continue;
- // Warn!
- Con::warnf("Conflicting Taml custom node name of '%s'.", pNodeName );
- // Cache node.
- TamlCustomNodeFactory.cacheObject( pCustomNode );
- return NULL;
- }
- #endif
- // Store node.
- mNodes.push_back( pCustomNode );
- return pCustomNode;
- }
- inline void removeNode( const U32 index )
- {
- // Sanity!
- AssertFatal( index < (U32)mNodes.size(), "tamlCustomNode::removeNode() - Index is out of bounds." );
- // Cache the custom node.
- TamlCustomNodeFactory.cacheObject( mNodes[index] );
- // Remove it.
- mNodes.erase( index );
- }
- inline const TamlCustomNode* findNode( const char* pNodeName ) const
- {
- // Sanity!
- AssertFatal( pNodeName != NULL, "Cannot find Taml node name that is NULL." );
- // Fetch node name.
- StringTableEntry nodeName = StringTable->insert( pNodeName );
- // Find node.
- for( Vector<TamlCustomNode*>::const_iterator nodeItr = mNodes.begin(); nodeItr != mNodes.end(); ++nodeItr )
- {
- if ( (*nodeItr)->getNodeName() == nodeName )
- return (*nodeItr);
- }
- return NULL;
- }
- inline const TamlCustomNodeVector& getNodes( void ) const { return mNodes; }
- private:
- TamlCustomNodeVector mNodes;
- };
- #endif // _TAML_CUSTOM_H_
|