Counter Strike : Global Offensive Source Code
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.

252 lines
9.1 KiB

  1. //====== Copyright � 1996-2004, 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. #define DECLARE_DMX_ATTRIBUTE_TYPE_INTERNAL( _className, _storageType, _attributeType, _attributeName, _defaultSetStatement ) \
  22. template< > class CDmAttributeInfo< _className > \
  23. { \
  24. private: \
  25. enum { ATTRIBUTE_TYPE = _attributeType }; \
  26. typedef _storageType StorageType_t; \
  27. static DmAttributeType_t AttributeType() { return _attributeType; } \
  28. static const char *AttributeTypeName() { return _attributeName; } \
  29. static void SetDefaultValue( _className& value ) { _defaultSetStatement } \
  30. friend class CDmxAttribute; \
  31. friend class CDmxElement; \
  32. }; \
  33. #define DECLARE_DMX_ATTRIBUTE_ARRAY_TYPE_INTERNAL( _className, _storageType, _attributeType, _attributeName ) \
  34. template< > class CDmAttributeInfo< CUtlVector<_className> > \
  35. { \
  36. private: \
  37. enum { ATTRIBUTE_TYPE = _attributeType }; \
  38. typedef _storageType StorageType_t; \
  39. static DmAttributeType_t AttributeType() { return _attributeType; } \
  40. static const char *AttributeTypeName() { return _attributeName; } \
  41. static void SetDefaultValue( CUtlVector< _className >& value ) { value.RemoveAll(); } \
  42. friend class CDmxAttribute; \
  43. friend class CDmxElement; \
  44. }; \
  45. #define DECLARE_DMX_ATTRIBUTE_TYPE( _className, _attributeType, _attributeName, _defaultSetStatement ) \
  46. DECLARE_DMX_ATTRIBUTE_TYPE_INTERNAL( _className, _className, _attributeType, _attributeName, _defaultSetStatement )
  47. #define DECLARE_DMX_ATTRIBUTE_ARRAY_TYPE( _className, _attributeType, _attributeName )\
  48. DECLARE_DMX_ATTRIBUTE_ARRAY_TYPE_INTERNAL( _className, CUtlVector< _className >, _attributeType, _attributeName )
  49. //-----------------------------------------------------------------------------
  50. // Attribute info, modified for use in mod code
  51. //-----------------------------------------------------------------------------
  52. DECLARE_DMX_ATTRIBUTE_TYPE( CDmxElement*, AT_ELEMENT, "element", value = 0; )
  53. DECLARE_DMX_ATTRIBUTE_ARRAY_TYPE( CDmxElement*, AT_ELEMENT_ARRAY, "element_array" )
  54. DECLARE_DMX_ATTRIBUTE_TYPE( CUtlString, AT_STRING, "string", value.Set( NULL ); )
  55. DECLARE_DMX_ATTRIBUTE_ARRAY_TYPE( CUtlString, AT_STRING_ARRAY, "string_array" )
  56. //-----------------------------------------------------------------------------
  57. // Purpose:
  58. //-----------------------------------------------------------------------------
  59. class CDmxAttribute
  60. {
  61. DECLARE_DMX_ALLOCATOR( );
  62. public:
  63. // Returns attribute name and type
  64. DmAttributeType_t GetType() const;
  65. const char *GetTypeString() const;
  66. template< class T > bool IsA() const;
  67. // Returns the name. NOTE: The utlsymbol
  68. // can be turned into a string by using g_pDataModel->String();
  69. const char *GetName() const;
  70. CUtlSymbolLarge GetNameSymbol() const;
  71. void SetName( const char *pName );
  72. // Gets values
  73. template< class T > const T& GetValue( ) const;
  74. template< class T > const CUtlVector< T >& GetArray( ) const;
  75. const char *GetValueString() const;
  76. // Sets values (+ type)
  77. template< class T > void SetValue( const T& value );
  78. void SetValue( const char *pString );
  79. void SetValue( char *pString );
  80. void SetValue( const void *pBuffer, size_t nLen );
  81. void SetValue( const CDmxAttribute *pAttribute );
  82. // Method to set values in an array (just directly operate on the array)
  83. // NOTE: This will create a new array of the appropriate type if
  84. // the type doesn't match the current type
  85. template< class T > CUtlVector< T >& GetArrayForEdit();
  86. // Sets the attribute to its default value based on its type
  87. void SetToDefaultValue();
  88. // Convert to and from string
  89. void SetValueFromString( const char *pValue );
  90. const char *GetValueAsString( char *pBuffer, size_t nBufLen ) const;
  91. // Gets the size of an array, returns 0 if it's not an array type
  92. int GetArrayCount() const;
  93. // Read from file
  94. bool Unserialize( DmAttributeType_t type, CUtlBuffer &buf );
  95. bool UnserializeElement( DmAttributeType_t type, CUtlBuffer &buf );
  96. bool Serialize( CUtlBuffer &buf ) const;
  97. bool SerializeElement( int nIndex, CUtlBuffer &buf ) const;
  98. bool SerializesOnMultipleLines() const;
  99. // Returns the size of the variables storing the various attribute types
  100. static int AttributeDataSize( DmAttributeType_t type );
  101. // Gets the basic type for a given array attribute type (e.g. AT_INT_ARRAY -> AT_INT)
  102. static DmAttributeType_t ArrayAttributeBasicType( DmAttributeType_t type );
  103. private:
  104. CDmxAttribute( const char *pAttributeName );
  105. CDmxAttribute( CUtlSymbolLarge attributeName );
  106. ~CDmxAttribute();
  107. // Allocate, free memory for data
  108. void AllocateDataMemory( DmAttributeType_t type );
  109. void AllocateDataMemory_AndConstruct( DmAttributeType_t type );
  110. void FreeDataMemory( );
  111. // Untyped methods for getting/setting used by unpack
  112. void SetValue( DmAttributeType_t type, const void *pSrc, int nLen );
  113. // NOTE: [Get|Set]ArrayValue don't currently support AT_STRING_ARRAY, AT_STRING_VOID or AT_ELEMENT_ARRAY
  114. void SetArrayValue( DmAttributeType_t type, const void *pSrc, int nDataTypeSize, int nArrayLength, int nSrcStride );
  115. void GetArrayValue( DmAttributeType_t type, void *pDest, int nDataTypeSize, int nArrayLength, const char *pDefaultString = NULL ) const;
  116. void SetArrayCount( int nArrayCount );
  117. const void *GetArrayBase( void ) const;
  118. // Helper templated methods called from untyped methods (VT is vector datatype, T is basic datatype, VT will be the same as T if the attribute is non-array)
  119. template < class VT, class T > void ConstructDataMemory( void );
  120. template < class VT, class T > void DestructDataMemory( void );
  121. template < class VT, class T > void SetArrayCount( int nArrayCount );
  122. template < class VT, class T > void GetArrayCount( int &nArrayCount ) const;
  123. template < class VT, class T > void GetArrayBase( const void * &pBasePtr ) const;
  124. template < class VT, class T > void SerializesOnMultipleLines( bool &bResult ) const;
  125. template < class VT, class T > void SerializeType( bool &bSuccess, CUtlBuffer &buf ) const;
  126. template < class VT, class T > void SerializeTypedElement( bool &bSuccess, int nIndex, CUtlBuffer &buf ) const;
  127. template < class VT, class T > void UnserializeType( bool &bSuccess, CUtlBuffer &buf );
  128. template < class VT, class T > void UnserializeTypedElement( bool &bSuccess, CUtlBuffer &buf );
  129. template < class VT, class T > void SetDefaultValue( void );
  130. DmAttributeType_t m_Type;
  131. CUtlSymbolLarge m_Name;
  132. void *m_pData;
  133. static CUtlSymbolTableLargeMT s_AttributeNameSymbols;
  134. friend class CDmxElement;
  135. public:
  136. static const char *s_pAttributeTypeName[AT_TYPE_COUNT];
  137. };
  138. //-----------------------------------------------------------------------------
  139. // Inline methods
  140. //-----------------------------------------------------------------------------
  141. inline DmAttributeType_t CDmxAttribute::GetType() const
  142. {
  143. return m_Type;
  144. }
  145. template< class T > inline bool CDmxAttribute::IsA() const
  146. {
  147. return GetType() == CDmAttributeInfo< T >::ATTRIBUTE_TYPE;
  148. }
  149. inline CUtlSymbolLarge CDmxAttribute::GetNameSymbol() const
  150. {
  151. return m_Name;
  152. }
  153. //-----------------------------------------------------------------------------
  154. // Sets a value in the attribute
  155. //-----------------------------------------------------------------------------
  156. template< class T > void CDmxAttribute::SetValue( const T& value )
  157. {
  158. AllocateDataMemory( CDmAttributeInfo<T>::AttributeType() );
  159. CopyConstruct( (T*)m_pData, value );
  160. }
  161. //-----------------------------------------------------------------------------
  162. // Returns data in the attribute
  163. //-----------------------------------------------------------------------------
  164. inline const char *CDmxAttribute::GetValueString() const
  165. {
  166. if ( m_Type == AT_STRING )
  167. return *(CUtlString*)m_pData;
  168. return "";
  169. }
  170. template< class T >
  171. inline const T& CDmxAttribute::GetValue( ) const
  172. {
  173. if ( CDmAttributeInfo<T>::AttributeType() == m_Type )
  174. return *(T*)m_pData;
  175. static T defaultValue;
  176. CDmAttributeInfo<T>::SetDefaultValue( defaultValue );
  177. return defaultValue;
  178. }
  179. template< class T >
  180. inline const CUtlVector< T >& CDmxAttribute::GetArray( ) const
  181. {
  182. if ( CDmAttributeInfo< CUtlVector< T > >::AttributeType() == m_Type )
  183. return *( CUtlVector< T > *)m_pData;
  184. static CUtlVector<T> defaultArray;
  185. return defaultArray;
  186. }
  187. template< class T >
  188. inline CUtlVector< T >& CDmxAttribute::GetArrayForEdit( )
  189. {
  190. if ( CDmAttributeInfo< CUtlVector< T > >::AttributeType() == m_Type )
  191. return *( CUtlVector< T > *)m_pData;
  192. AllocateDataMemory( CDmAttributeInfo< CUtlVector< T > >::AttributeType() );
  193. Construct( (CUtlVector<T>*)m_pData );
  194. return *(CUtlVector< T > *)m_pData;
  195. }
  196. #endif // DMXATTRIBUTE_H