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.

137 lines
4.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "toolutils/attributeelementchoicelist.h"
  9. #include "datamodel/dmelement.h"
  10. typedef CUtlRBTree< CDmElement *, int > ElementDict_t;
  11. //-----------------------------------------------------------------------------
  12. // returns the choice string that AddElementsRecursively would have returned
  13. //-----------------------------------------------------------------------------
  14. const char *GetChoiceString( CDmElement *pElement )
  15. {
  16. return pElement->GetName();
  17. }
  18. //-----------------------------------------------------------------------------
  19. // Recursively adds all elements referred to this element into the list of elements
  20. //-----------------------------------------------------------------------------
  21. void AddElementsRecursively_R( CDmElement *pElement, ElementChoiceList_t &list, ElementDict_t &dict, const char *pElementType )
  22. {
  23. if ( !pElement )
  24. return;
  25. if ( dict.Find( pElement ) != dict.InvalidIndex() )
  26. return;
  27. dict.Insert( pElement );
  28. if ( pElement->IsA( pElementType ) )
  29. {
  30. int nIndex = list.AddToTail( );
  31. ElementChoice_t &entry = list[nIndex];
  32. entry.m_pValue = pElement;
  33. entry.m_pChoiceString = GetChoiceString( pElement );
  34. }
  35. for ( CDmAttribute *pAttribute = pElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->NextAttribute() )
  36. {
  37. char const *attributeName = pAttribute->GetName( );
  38. DmAttributeType_t attrType = pAttribute->GetType( );
  39. if ( attrType == AT_ELEMENT )
  40. {
  41. CDmElement *pChild = pElement->GetValueElement< CDmElement >( attributeName );
  42. AddElementsRecursively_R( pChild, list, dict, pElementType );
  43. }
  44. else if ( attrType == AT_ELEMENT_ARRAY )
  45. {
  46. const CDmrElementArray<CDmElement> children( pElement, attributeName );
  47. uint n = children.Count();
  48. for ( uint i = 0; i < n; ++i )
  49. {
  50. CDmElement *pChild = children[ i ];
  51. AddElementsRecursively_R( pChild, list, dict, pElementType );
  52. }
  53. }
  54. }
  55. }
  56. //-----------------------------------------------------------------------------
  57. // Recursively adds all elements referred to this element into the list of elements
  58. //-----------------------------------------------------------------------------
  59. void AddElementsRecursively_R( CDmElement *pElement, DmeHandleVec_t &list, ElementDict_t &dict, const char *pElementType )
  60. {
  61. if ( !pElement )
  62. return;
  63. if ( dict.Find( pElement ) != dict.InvalidIndex() )
  64. return;
  65. dict.Insert( pElement );
  66. if ( pElement->IsA( pElementType ) )
  67. {
  68. int nIndex = list.AddToTail( );
  69. list[nIndex] = pElement;
  70. }
  71. for ( CDmAttribute *pAttribute = pElement->FirstAttribute(); pAttribute; pAttribute = pAttribute->NextAttribute() )
  72. {
  73. char const *attributeName = pAttribute->GetName( );
  74. DmAttributeType_t attrType = pAttribute->GetType( );
  75. if ( attrType == AT_ELEMENT )
  76. {
  77. CDmElement *pChild = pElement->GetValueElement< CDmElement >( attributeName );
  78. AddElementsRecursively_R( pChild, list, dict, pElementType );
  79. }
  80. else if ( attrType == AT_ELEMENT_ARRAY )
  81. {
  82. const CDmrElementArray<CDmElement> children( pElement, attributeName );
  83. uint n = children.Count();
  84. for ( uint i = 0; i < n; ++i )
  85. {
  86. CDmElement *pChild = children[ i ];
  87. AddElementsRecursively_R( pChild, list, dict, pElementType );
  88. }
  89. }
  90. }
  91. }
  92. //-----------------------------------------------------------------------------
  93. // Recursively adds all elements referred to this element into the list of elements
  94. //-----------------------------------------------------------------------------
  95. void AddElementsRecursively( CDmElement *obj, ElementChoiceList_t &list, const char *pElementType )
  96. {
  97. if ( !pElementType )
  98. {
  99. pElementType = g_pDataModel->GetString( CDmElement::GetStaticTypeSymbol() );
  100. }
  101. ElementDict_t dict( 0, 0, DefLessFunc( CDmElement * ) );
  102. AddElementsRecursively_R( obj, list, dict, pElementType );
  103. }
  104. //-----------------------------------------------------------------------------
  105. // Recursively adds all elements of the specified type under pElement into the vector
  106. //-----------------------------------------------------------------------------
  107. void AddElementsRecursively( CDmElement *pElement, DmeHandleVec_t &list, const char *pElementType )
  108. {
  109. if ( !pElementType )
  110. {
  111. pElementType = g_pDataModel->GetString( CDmElement::GetStaticTypeSymbol() );
  112. }
  113. ElementDict_t dict( 0, 0, DefLessFunc( CDmElement * ) );
  114. AddElementsRecursively_R( pElement, list, dict, pElementType );
  115. }