/src/assimp/contrib/openddlparser/code/DDLNode.cpp
Line | Count | Source |
1 | | /*----------------------------------------------------------------------------------------------- |
2 | | The MIT License (MIT) |
3 | | |
4 | | Copyright (c) 2014-2020 Kim Kulling |
5 | | |
6 | | Permission is hereby granted, free of charge, to any person obtaining a copy of |
7 | | this software and associated documentation files (the "Software"), to deal in |
8 | | the Software without restriction, including without limitation the rights to |
9 | | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
10 | | the Software, and to permit persons to whom the Software is furnished to do so, |
11 | | subject to the following conditions: |
12 | | |
13 | | The above copyright notice and this permission notice shall be included in all |
14 | | copies or substantial portions of the Software. |
15 | | |
16 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
18 | | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
19 | | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
20 | | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
21 | | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
22 | | -----------------------------------------------------------------------------------------------*/ |
23 | | #include <openddlparser/DDLNode.h> |
24 | | #include <openddlparser/OpenDDLParser.h> |
25 | | #include <openddlparser/OpenDDLStream.h> |
26 | | |
27 | | #include <algorithm> |
28 | | |
29 | | BEGIN_ODDLPARSER_NS |
30 | | |
31 | | DDLNode::DllNodeList DDLNode::s_allocatedNodes; |
32 | | |
33 | | template <class T> |
34 | | inline static void releaseDataType(T *ptr) { |
35 | | if (nullptr == ptr) { |
36 | | return; |
37 | | } |
38 | | |
39 | | T *current(nullptr); |
40 | | while (ptr) { |
41 | | current = ptr; |
42 | | ptr = ptr->m_next; |
43 | | delete current; |
44 | | } |
45 | | } |
46 | | |
47 | 49 | static void releaseReferencedNames(Reference *ref) { |
48 | 49 | if (nullptr == ref) { |
49 | 49 | return; |
50 | 49 | } |
51 | | |
52 | 0 | delete ref; |
53 | 0 | } |
54 | | |
55 | | DDLNode::DDLNode(const std::string &type, const std::string &name, size_t idx, DDLNode *parent) : |
56 | 49 | m_type(type), |
57 | 49 | m_name(name), |
58 | 49 | m_parent(parent), |
59 | 49 | m_children(), |
60 | 49 | m_properties(nullptr), |
61 | 49 | m_value(nullptr), |
62 | 49 | m_dtArrayList(nullptr), |
63 | 49 | m_references(nullptr), |
64 | 49 | m_idx(idx) { |
65 | 49 | if (m_parent) { |
66 | 39 | m_parent->m_children.push_back(this); |
67 | 39 | } |
68 | 49 | } |
69 | | |
70 | 49 | DDLNode::~DDLNode() { |
71 | 49 | delete m_properties; |
72 | 49 | delete m_value; |
73 | 49 | releaseReferencedNames(m_references); |
74 | | |
75 | 49 | delete m_dtArrayList; |
76 | 49 | m_dtArrayList = nullptr; |
77 | 49 | if (s_allocatedNodes[m_idx] == this) { |
78 | 49 | s_allocatedNodes[m_idx] = nullptr; |
79 | 49 | } |
80 | 88 | for (size_t i = 0; i < m_children.size(); i++) { |
81 | 39 | delete m_children[i]; |
82 | 39 | } |
83 | 49 | } |
84 | | |
85 | 0 | void DDLNode::attachParent(DDLNode *parent) { |
86 | 0 | if (m_parent == parent) { |
87 | 0 | return; |
88 | 0 | } |
89 | | |
90 | 0 | m_parent = parent; |
91 | 0 | if (nullptr != m_parent) { |
92 | 0 | m_parent->m_children.push_back(this); |
93 | 0 | } |
94 | 0 | } |
95 | | |
96 | 0 | void DDLNode::detachParent() { |
97 | 0 | if (nullptr != m_parent) { |
98 | 0 | DDLNodeIt it = std::find(m_parent->m_children.begin(), m_parent->m_children.end(), this); |
99 | 0 | if (m_parent->m_children.end() != it) { |
100 | 0 | m_parent->m_children.erase(it); |
101 | 0 | } |
102 | 0 | m_parent = nullptr; |
103 | 0 | } |
104 | 0 | } |
105 | | |
106 | 0 | DDLNode *DDLNode::getParent() const { |
107 | 0 | return m_parent; |
108 | 0 | } |
109 | | |
110 | 0 | const DDLNode::DllNodeList &DDLNode::getChildNodeList() const { |
111 | 0 | return m_children; |
112 | 0 | } |
113 | | |
114 | 0 | void DDLNode::setType(const std::string &type) { |
115 | 0 | m_type = type; |
116 | 0 | } |
117 | | |
118 | 0 | const std::string &DDLNode::getType() const { |
119 | 0 | return m_type; |
120 | 0 | } |
121 | | |
122 | 0 | void DDLNode::setName(const std::string &name) { |
123 | 0 | m_name = name; |
124 | 0 | } |
125 | | |
126 | 0 | const std::string &DDLNode::getName() const { |
127 | 0 | return m_name; |
128 | 0 | } |
129 | | |
130 | 0 | void DDLNode::setProperties(Property *prop) { |
131 | 0 | if (m_properties != nullptr) |
132 | 0 | delete m_properties; |
133 | 0 | m_properties = prop; |
134 | 0 | } |
135 | | |
136 | 0 | Property *DDLNode::getProperties() const { |
137 | 0 | return m_properties; |
138 | 0 | } |
139 | | |
140 | 0 | bool DDLNode::hasProperty(const std::string &name) { |
141 | 0 | const Property *prop(findPropertyByName(name)); |
142 | 0 | return (nullptr != prop); |
143 | 0 | } |
144 | | |
145 | 0 | bool DDLNode::hasProperties() const { |
146 | 0 | return (nullptr != m_properties); |
147 | 0 | } |
148 | | |
149 | 0 | Property *DDLNode::findPropertyByName(const std::string &name) { |
150 | 0 | if (name.empty()) { |
151 | 0 | return nullptr; |
152 | 0 | } |
153 | | |
154 | 0 | if (nullptr == m_properties) { |
155 | 0 | return nullptr; |
156 | 0 | } |
157 | | |
158 | 0 | Property *current(m_properties); |
159 | 0 | while (nullptr != current) { |
160 | 0 | int res = strncmp(current->m_key->m_buffer, name.c_str(), name.size()); |
161 | 0 | if (0 == res) { |
162 | 0 | return current; |
163 | 0 | } |
164 | 0 | current = current->m_next; |
165 | 0 | } |
166 | | |
167 | 0 | return nullptr; |
168 | 0 | } |
169 | | |
170 | 9 | void DDLNode::setValue(Value *val) { |
171 | 9 | m_value = val; |
172 | 9 | } |
173 | | |
174 | 0 | Value *DDLNode::getValue() const { |
175 | 0 | return m_value; |
176 | 0 | } |
177 | | |
178 | 0 | void DDLNode::setDataArrayList(DataArrayList *dtArrayList) { |
179 | 0 | m_dtArrayList = dtArrayList; |
180 | 0 | } |
181 | | |
182 | 0 | DataArrayList *DDLNode::getDataArrayList() const { |
183 | 0 | return m_dtArrayList; |
184 | 0 | } |
185 | | |
186 | 0 | void DDLNode::setReferences(Reference *refs) { |
187 | 0 | m_references = refs; |
188 | 0 | } |
189 | | |
190 | 0 | Reference *DDLNode::getReferences() const { |
191 | 0 | return m_references; |
192 | 0 | } |
193 | | |
194 | 0 | void DDLNode::dump(IOStreamBase &stream) { |
195 | 0 | if (!stream.isOpen()) { |
196 | 0 | return; |
197 | 0 | } |
198 | | |
199 | 0 | const std::string &type = this->getType(); |
200 | 0 | stream.write("type = " + type); |
201 | 0 | Value::Iterator it(getValue()); |
202 | 0 | while (it.hasNext()) { |
203 | 0 | Value *v = it.getNext(); |
204 | 0 | v->dump(stream); |
205 | 0 | } |
206 | 0 | } |
207 | | |
208 | 49 | DDLNode *DDLNode::create(const std::string &type, const std::string &name, DDLNode *parent) { |
209 | 49 | const size_t idx(s_allocatedNodes.size()); |
210 | 49 | DDLNode *node = new DDLNode(type, name, idx, parent); |
211 | 49 | s_allocatedNodes.push_back(node); |
212 | | |
213 | 49 | return node; |
214 | 49 | } |
215 | | |
216 | 0 | void DDLNode::releaseNodes() { |
217 | 0 | if (s_allocatedNodes.size() > 0) { |
218 | 0 | for (DDLNodeIt it = s_allocatedNodes.begin(); it != s_allocatedNodes.end(); it++) { |
219 | 0 | if (*it) { |
220 | 0 | delete *it; |
221 | 0 | } |
222 | 0 | } |
223 | 0 | s_allocatedNodes.clear(); |
224 | 0 | } |
225 | 0 | } |
226 | | |
227 | | END_ODDLPARSER_NS |