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.

184 lines
5.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef DMELEMENTDICTIONARY_H
  7. #define DMELEMENTDICTIONARY_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "tier1/utlvector.h"
  12. #include "datamodel/idatamodel.h"
  13. #include "datamodel/dmattribute.h"
  14. #include "tier1/utlrbtree.h"
  15. #include "tier1/utlhash.h"
  16. //-----------------------------------------------------------------------------
  17. // Forward declarations
  18. //-----------------------------------------------------------------------------
  19. class CDmElement;
  20. class CDmAttribute;
  21. //-----------------------------------------------------------------------------
  22. // Element dictionary used in unserialization
  23. //-----------------------------------------------------------------------------
  24. typedef int DmElementDictHandle_t;
  25. enum
  26. {
  27. ELEMENT_DICT_HANDLE_INVALID = (DmElementDictHandle_t)~0
  28. };
  29. class CDmElementDictionary
  30. {
  31. public:
  32. CDmElementDictionary();
  33. DmElementDictHandle_t InsertElement( CDmElement *pElement );
  34. CDmElement *GetElement( DmElementDictHandle_t handle );
  35. void AddAttribute( CDmAttribute *pAttribute, const DmObjectId_t &pElementId );
  36. void AddArrayAttribute( CDmAttribute *pAttribute, DmElementDictHandle_t hChild );
  37. void AddArrayAttribute( CDmAttribute *pAttribute, const DmObjectId_t &pElementId );
  38. DmElementHandle_t SetElementId( DmElementDictHandle_t hDictHandle,
  39. const DmObjectId_t &newId,
  40. DmConflictResolution_t idConflictResolution );
  41. // Finds an element into the table
  42. DmElementDictHandle_t FindElement( CDmElement *pElement );
  43. // Hook up all element references (which were unserialized as object ids)
  44. void HookUpElementReferences();
  45. // Clears the dictionary
  46. void Clear();
  47. // iteration through elements
  48. DmElementDictHandle_t FirstElement() { return 0; }
  49. DmElementDictHandle_t NextElement( DmElementDictHandle_t h )
  50. {
  51. return m_Dict.IsValidIndex( h+1 ) ? h+1 : ELEMENT_DICT_HANDLE_INVALID;
  52. }
  53. private:
  54. struct AttributeInfo_t
  55. {
  56. CDmAttribute *m_pAttribute;
  57. int m_nType; // AT_ELEMENT or AT_OBJECTID
  58. union
  59. {
  60. DmElementDictHandle_t m_hElement;
  61. DmObjectId_t m_ObjectId;
  62. };
  63. };
  64. typedef CUtlVector<AttributeInfo_t> AttributeList_t;
  65. struct DmIdPair_t
  66. {
  67. DmObjectId_t m_oldId;
  68. DmObjectId_t m_newId;
  69. DmIdPair_t() {}
  70. DmIdPair_t( const DmObjectId_t &id )
  71. {
  72. CopyUniqueId( id, &m_oldId );
  73. }
  74. DmIdPair_t( const DmObjectId_t &oldId, const DmObjectId_t &newId )
  75. {
  76. CopyUniqueId( oldId, &m_oldId );
  77. CopyUniqueId( newId, &m_newId );
  78. }
  79. DmIdPair_t &operator=( const DmIdPair_t &that )
  80. {
  81. CopyUniqueId( that.m_oldId, &m_oldId );
  82. CopyUniqueId( that.m_newId, &m_newId );
  83. return *this;
  84. }
  85. static unsigned int HashKey( const DmIdPair_t& that )
  86. {
  87. return *( unsigned int* )&that.m_oldId.m_Value;
  88. }
  89. static bool Compare( const DmIdPair_t& a, const DmIdPair_t& b )
  90. {
  91. return IsUniqueIdEqual( a.m_oldId, b.m_oldId );
  92. }
  93. };
  94. struct DeletionInfo_t
  95. {
  96. DeletionInfo_t() {}
  97. DeletionInfo_t( DmElementHandle_t hElement ) : m_hElementToDelete( hElement ) {}
  98. bool operator==( const DeletionInfo_t& src ) const { return m_hElementToDelete == src.m_hElementToDelete; }
  99. DmElementDictHandle_t m_hDictHandle;
  100. DmElementHandle_t m_hElementToDelete;
  101. DmElementHandle_t m_hReplacementElement;
  102. };
  103. // Hook up all element references (which were unserialized as object ids)
  104. void HookUpElementAttributes();
  105. void HookUpElementArrayAttributes();
  106. void RemoveAttributeInfosOfElement( AttributeList_t &attributes, DmElementHandle_t hElement );
  107. CUtlVector< DmElementHandle_t > m_Dict;
  108. AttributeList_t m_Attributes;
  109. AttributeList_t m_ArrayAttributes;
  110. CUtlVector< DeletionInfo_t > m_elementsToDelete;
  111. CUtlHash< DmIdPair_t > m_idmap;
  112. };
  113. //-----------------------------------------------------------------------------
  114. // Element dictionary used in serialization
  115. //-----------------------------------------------------------------------------
  116. class CDmElementSerializationDictionary
  117. {
  118. public:
  119. CDmElementSerializationDictionary();
  120. // Creates the list of all things to serialize
  121. void BuildElementList( CDmElement *pRoot, bool bFlatMode );
  122. // Should I inline the serialization of this element?
  123. bool ShouldInlineElement( CDmElement *pElement );
  124. // Clears the dictionary
  125. void Clear();
  126. // Iterates over all root elements to serialize
  127. DmElementDictHandle_t FirstRootElement() const;
  128. DmElementDictHandle_t NextRootElement( DmElementDictHandle_t h ) const;
  129. CDmElement* GetRootElement( DmElementDictHandle_t h );
  130. // Finds the handle of the element
  131. DmElementDictHandle_t Find( CDmElement *pElement );
  132. // How many root elements do we have?
  133. int RootElementCount() const;
  134. private:
  135. struct ElementInfo_t
  136. {
  137. bool m_bRoot;
  138. CDmElement* m_pElement;
  139. };
  140. // Creates the list of all things to serialize
  141. void BuildElementList_R( CDmElement *pRoot, bool bFlatMode, bool bIsRoot );
  142. static bool LessFunc( const ElementInfo_t &lhs, const ElementInfo_t &rhs );
  143. CUtlBlockRBTree< ElementInfo_t, DmElementDictHandle_t > m_Dict;
  144. };
  145. #endif // DMELEMENTDICTIONARY_H