tamlVisitor.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _TAML_VISITOR_H_
  23. #define _TAML_VISITOR_H_
  24. #include "core/stringTable.h"
  25. #include "core/strings/stringFunctions.h"
  26. //-----------------------------------------------------------------------------
  27. class TamlParser;
  28. //-----------------------------------------------------------------------------
  29. /// @ingroup tamlGroup
  30. /// @see tamlGroup
  31. class TamlVisitor
  32. {
  33. public:
  34. /// Visitor property state.
  35. struct PropertyState
  36. {
  37. private:
  38. StringTableEntry mObjectName;
  39. StringTableEntry mPropertyName;
  40. char mPropertyValue[4096];
  41. bool mPropertyValueDirty;
  42. bool mIsRootObject;
  43. public:
  44. PropertyState()
  45. {
  46. mObjectName = StringTable->EmptyString();
  47. mPropertyName = StringTable->EmptyString();
  48. *mPropertyValue = 0;
  49. mPropertyValueDirty = false;
  50. mIsRootObject = false;
  51. }
  52. inline void setObjectName( const char* pObjectName, const bool rootObject )
  53. {
  54. mObjectName = StringTable->insert( pObjectName );
  55. mIsRootObject = rootObject;
  56. }
  57. inline StringTableEntry getObjectName( void ) const { return mObjectName; }
  58. inline bool isRootObject( void ) const { return mIsRootObject; }
  59. inline void setProperty( const char* pPropertyName, const char* pPropertyValue )
  60. {
  61. // Set property name.
  62. mPropertyName = StringTable->insert( pPropertyName );
  63. // Format property value.
  64. dSprintf( mPropertyValue, sizeof(mPropertyValue), "%s", pPropertyValue );
  65. // Flag as not dirty.
  66. mPropertyValueDirty = false;
  67. }
  68. inline void updatePropertyValue( const char* pPropertyValue )
  69. {
  70. // Update property value.
  71. dSprintf( mPropertyValue, sizeof(mPropertyValue), "%s", pPropertyValue );
  72. // Flag as dirty.
  73. mPropertyValueDirty = true;
  74. }
  75. inline StringTableEntry getPropertyName( void ) const { return mPropertyName; }
  76. inline const char* getPropertyValue( void ) const { return mPropertyValue; }
  77. inline void resetPropertyValueDirty( void ) { mPropertyValueDirty = false; }
  78. inline bool getPropertyValueDirty( void ) const { return mPropertyValueDirty; }
  79. };
  80. public:
  81. TamlVisitor() {}
  82. virtual ~TamlVisitor() {}
  83. /// Whether the visitor wants to perform property changes.
  84. /// This allows a check against the parser which may not allow changes.
  85. virtual bool wantsPropertyChanges( void ) = 0;
  86. /// Whether the visitor wants to visit the root node only.
  87. virtual bool wantsRootOnly( void ) = 0;
  88. /// The state of the visited property.
  89. virtual bool visit( const TamlParser& parser, PropertyState& propertyState ) = 0;
  90. };
  91. #endif // _TAML_VISITOR_H_