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