Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
5.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef DMXATTRIBUTE_H
  7. #define DMXATTRIBUTE_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "datamodel/dmattributetypes.h"
  12. #include "tier1/utlvector.h"
  13. #include "tier1/utlrbtree.h"
  14. #include "tier1/utlsymbol.h"
  15. #include "tier1/mempool.h"
  16. #include "dmxloader/dmxloader.h"
  17. //-----------------------------------------------------------------------------
  18. // Forward declarations:
  19. //-----------------------------------------------------------------------------
  20. class CDmxElement;
  21. //-----------------------------------------------------------------------------
  22. // Attribute info, modified for use in mod code
  23. //-----------------------------------------------------------------------------
  24. DECLARE_ATTRIBUTE_TYPE( CDmxElement*, AT_ELEMENT, "element", value = 0; )
  25. DECLARE_ATTRIBUTE_ARRAY_TYPE( CDmxElement*, AT_ELEMENT_ARRAY, "element_array" )
  26. //-----------------------------------------------------------------------------
  27. // Purpose:
  28. //-----------------------------------------------------------------------------
  29. class CDmxAttribute
  30. {
  31. DECLARE_DMX_ALLOCATOR( );
  32. public:
  33. // Returns attribute name and type
  34. DmAttributeType_t GetType() const;
  35. const char *GetTypeString() const;
  36. template< class T > bool IsA() const;
  37. // Returns the name. NOTE: The utlsymbol
  38. // can be turned into a string by using g_pDataModel->String();
  39. const char *GetName() const;
  40. CUtlSymbol GetNameSymbol() const;
  41. void SetName( const char *pName );
  42. // Gets values
  43. template< class T > const T& GetValue( ) const;
  44. template< class T > const CUtlVector< T >& GetArray( ) const;
  45. const char *GetValueString() const;
  46. // Sets values (+ type)
  47. template< class T > void SetValue( const T& value );
  48. void SetValue( const char *pString );
  49. void SetValue( const void *pBuffer, size_t nLen );
  50. void SetValue( const CDmxAttribute *pAttribute );
  51. // Method to set values in an array (just directly operate on the array)
  52. // NOTE: This will create a new array of the appropriate type if
  53. // the type doesn't match the current type
  54. template< class T > CUtlVector< T >& GetArrayForEdit();
  55. // Sets the attribute to its default value based on its type
  56. void SetToDefaultValue();
  57. // Convert to and from string
  58. void SetValueFromString( const char *pValue );
  59. const char *GetValueAsString( char *pBuffer, size_t nBufLen ) const;
  60. // Gets the size of an array, returns 0 if it's not an array type
  61. int GetArrayCount() const;
  62. // Read from file
  63. bool Unserialize( DmAttributeType_t type, CUtlBuffer &buf );
  64. bool UnserializeElement( DmAttributeType_t type, CUtlBuffer &buf );
  65. bool Serialize( CUtlBuffer &buf ) const;
  66. bool SerializeElement( int nIndex, CUtlBuffer &buf ) const;
  67. bool SerializesOnMultipleLines() const;
  68. // Returns the size of the variables storing the various attribute types
  69. static int AttributeDataSize( DmAttributeType_t type );
  70. private:
  71. CDmxAttribute( const char *pAttributeName );
  72. CDmxAttribute( CUtlSymbol attributeName );
  73. ~CDmxAttribute();
  74. // Allocate, free memory for data
  75. void AllocateDataMemory( DmAttributeType_t type );
  76. void FreeDataMemory( );
  77. // Untyped method for setting used by unpack
  78. void SetValue( DmAttributeType_t type, const void *pSrc, int nLen );
  79. DmAttributeType_t m_Type;
  80. CUtlSymbol m_Name;
  81. void *m_pData;
  82. static CUtlSymbolTableMT s_AttributeNameSymbols;
  83. friend class CDmxElement;
  84. };
  85. //-----------------------------------------------------------------------------
  86. // Inline methods
  87. //-----------------------------------------------------------------------------
  88. inline DmAttributeType_t CDmxAttribute::GetType() const
  89. {
  90. return m_Type;
  91. }
  92. template< class T > inline bool CDmxAttribute::IsA() const
  93. {
  94. return GetType() == CDmAttributeInfo< T >::ATTRIBUTE_TYPE;
  95. }
  96. inline CUtlSymbol CDmxAttribute::GetNameSymbol() const
  97. {
  98. return m_Name;
  99. }
  100. //-----------------------------------------------------------------------------
  101. // Sets a value in the attribute
  102. //-----------------------------------------------------------------------------
  103. template< class T > void CDmxAttribute::SetValue( const T& value )
  104. {
  105. AllocateDataMemory( CDmAttributeInfo<T>::AttributeType() );
  106. CopyConstruct( (T*)m_pData, value );
  107. }
  108. //-----------------------------------------------------------------------------
  109. // Returns data in the attribute
  110. //-----------------------------------------------------------------------------
  111. inline const char *CDmxAttribute::GetValueString() const
  112. {
  113. if ( m_Type == AT_STRING )
  114. return *(CUtlString*)m_pData;
  115. return "";
  116. }
  117. template< class T >
  118. inline const T& CDmxAttribute::GetValue( ) const
  119. {
  120. if ( CDmAttributeInfo<T>::AttributeType() == m_Type )
  121. return *(T*)m_pData;
  122. static T defaultValue;
  123. CDmAttributeInfo<T>::SetDefaultValue( defaultValue );
  124. return defaultValue;
  125. }
  126. template< class T >
  127. inline const CUtlVector< T >& CDmxAttribute::GetArray( ) const
  128. {
  129. if ( CDmAttributeInfo< CUtlVector< T > >::AttributeType() == m_Type )
  130. return *( CUtlVector< T > *)m_pData;
  131. static CUtlVector<T> defaultArray;
  132. return defaultArray;
  133. }
  134. template< class T >
  135. inline CUtlVector< T >& CDmxAttribute::GetArrayForEdit( )
  136. {
  137. if ( CDmAttributeInfo< CUtlVector< T > >::AttributeType() == m_Type )
  138. return *( CUtlVector< T > *)m_pData;
  139. AllocateDataMemory( CDmAttributeInfo< CUtlVector< T > >::AttributeType() );
  140. Construct( (CUtlVector<T>*)m_pData );
  141. return *(CUtlVector< T > *)m_pData;
  142. }
  143. #endif // DMXATTRIBUTE_H