11
11
*/
12
12
13
13
#include " behavior_tree_core/xml_parsing.h"
14
+ #include " tinyXML2/tinyxml2.h"
15
+
14
16
#include < functional>
15
17
16
18
namespace BT
17
19
{
18
- using namespace tinyxml2 ;
20
+
21
+ struct XMLParser ::Pimpl
22
+ {
23
+ TreeNode::Ptr treeParsing (const tinyxml2::XMLElement* root_element,
24
+ const NodeBuilder& node_builder,
25
+ std::vector<TreeNode::Ptr>& nodes,
26
+ const TreeNode::Ptr& root_parent);
27
+
28
+ tinyxml2::XMLDocument doc;
29
+
30
+ const BehaviorTreeFactory& factory;
31
+
32
+ Pimpl (const BehaviorTreeFactory &fact): factory(fact) {}
33
+ };
34
+
35
+
36
+ XMLParser::XMLParser (const BehaviorTreeFactory &factory) : _p( new Pimpl(factory) )
37
+ {
38
+ }
39
+
40
+ XMLParser::~XMLParser ()
41
+ {
42
+ delete _p;
43
+ }
19
44
20
45
void XMLParser::loadFromFile (const std::string& filename)
21
46
{
22
- XMLError err = doc_ .LoadFile (filename.c_str ());
47
+ tinyxml2:: XMLError err = _p-> doc .LoadFile (filename.c_str ());
23
48
24
49
if (err)
25
50
{
26
51
char buffer[200 ];
27
- sprintf (buffer, " Error parsing the XML: %s" , XMLDocument::ErrorIDToName (err));
52
+ sprintf (buffer, " Error parsing the XML: %s" , tinyxml2:: XMLDocument::ErrorIDToName (err));
28
53
throw std::runtime_error (buffer);
29
54
}
30
55
}
31
56
32
57
void XMLParser::loadFromText (const std::string& xml_text)
33
58
{
34
- XMLError err = doc_ .Parse (xml_text.c_str (), xml_text.size ());
59
+ tinyxml2:: XMLError err = _p-> doc .Parse (xml_text.c_str (), xml_text.size ());
35
60
36
61
if (err)
37
62
{
38
63
char buffer[200 ];
39
- sprintf (buffer, " Error parsing the XML: %s" , XMLDocument::ErrorIDToName (err));
64
+ sprintf (buffer, " Error parsing the XML: %s" , tinyxml2:: XMLDocument::ErrorIDToName (err));
40
65
throw std::runtime_error (buffer);
41
66
}
42
67
}
@@ -45,7 +70,7 @@ bool XMLParser::verifyXML(std::vector<std::string>& error_messages) const
45
70
{
46
71
error_messages.clear ();
47
72
48
- if (doc_ .Error ())
73
+ if (_p-> doc .Error ())
49
74
{
50
75
error_messages.emplace_back (" The XML was not correctly loaded" );
51
76
return false ;
@@ -64,7 +89,7 @@ bool XMLParser::verifyXML(std::vector<std::string>& error_messages) const
64
89
is_valid = false ;
65
90
};
66
91
67
- auto ChildrenCount = [](const XMLElement* parent_node) {
92
+ auto ChildrenCount = [](const tinyxml2:: XMLElement* parent_node) {
68
93
int count = 0 ;
69
94
for (auto node = parent_node->FirstChildElement (); node != nullptr ;
70
95
node = node->NextSiblingElement ())
@@ -76,7 +101,7 @@ bool XMLParser::verifyXML(std::vector<std::string>& error_messages) const
76
101
77
102
// -----------------------------
78
103
79
- const XMLElement* xml_root = doc_ .RootElement ();
104
+ const tinyxml2:: XMLElement* xml_root = _p-> doc .RootElement ();
80
105
81
106
if (!xml_root || !strEqual (xml_root->Name (), " root" ))
82
107
{
@@ -129,9 +154,9 @@ bool XMLParser::verifyXML(std::vector<std::string>& error_messages) const
129
154
// -------------------------------------------------
130
155
131
156
// function to be called recursively
132
- std::function<void (const XMLElement*)> recursiveStep;
157
+ std::function<void (const tinyxml2:: XMLElement*)> recursiveStep;
133
158
134
- recursiveStep = [&](const XMLElement* node) {
159
+ recursiveStep = [&](const tinyxml2:: XMLElement* node) {
135
160
const int children_count = ChildrenCount (node);
136
161
const char * name = node->Name ();
137
162
if (strEqual (name, " Decorator" ))
@@ -192,7 +217,7 @@ bool XMLParser::verifyXML(std::vector<std::string>& error_messages) const
192
217
{
193
218
// Last resort: MAYBE used ID as element name?
194
219
bool found = false ;
195
- for (const auto & model : factory_ .manifests ())
220
+ for (const auto & model : _p-> factory .manifests ())
196
221
{
197
222
if (model.registration_ID == name)
198
223
{
@@ -274,15 +299,15 @@ TreeNode::Ptr XMLParser::instantiateTree(std::vector<TreeNode::Ptr>& nodes)
274
299
}
275
300
276
301
// --------------------------------------
277
- XMLElement* xml_root = doc_ .RootElement ();
302
+ tinyxml2:: XMLElement* xml_root = _p-> doc .RootElement ();
278
303
279
304
std::string main_tree_ID;
280
305
if (xml_root->Attribute (" main_tree_to_execute" ))
281
306
{
282
307
main_tree_ID = xml_root->Attribute (" main_tree_to_execute" );
283
308
}
284
309
285
- std::map<std::string, XMLElement*> bt_roots;
310
+ std::map<std::string, tinyxml2:: XMLElement*> bt_roots;
286
311
287
312
int tree_count = 0 ;
288
313
@@ -309,7 +334,7 @@ TreeNode::Ptr XMLParser::instantiateTree(std::vector<TreeNode::Ptr>& nodes)
309
334
NodeBuilder node_builder = [&](const std::string& ID, const std::string& name,
310
335
const NodeParameters& params,
311
336
TreeNode::Ptr parent) -> TreeNode::Ptr {
312
- TreeNode::Ptr child_node = factory_ .instantiateTreeNode (ID, name, params);
337
+ TreeNode::Ptr child_node = _p-> factory .instantiateTreeNode (ID, name, params);
313
338
nodes.push_back (child_node);
314
339
if (parent)
315
340
{
@@ -329,20 +354,20 @@ TreeNode::Ptr XMLParser::instantiateTree(std::vector<TreeNode::Ptr>& nodes)
329
354
if (subtree_node)
330
355
{
331
356
auto subtree_elem = bt_roots[name]->FirstChildElement ();
332
- treeParsing (subtree_elem, node_builder, nodes, child_node);
357
+ _p-> treeParsing (subtree_elem, node_builder, nodes, child_node);
333
358
}
334
359
return child_node;
335
360
};
336
361
// --------------------------------------
337
362
338
363
auto root_element = bt_roots[main_tree_ID]->FirstChildElement ();
339
- return treeParsing (root_element, node_builder, nodes, TreeNode::Ptr ());
364
+ return _p-> treeParsing (root_element, node_builder, nodes, TreeNode::Ptr ());
340
365
}
341
366
342
- TreeNode::Ptr BT::XMLParser::treeParsing (const XMLElement* root_element,
343
- const NodeBuilder& node_builder,
344
- std::vector<TreeNode::Ptr>& nodes,
345
- const TreeNode::Ptr& root_parent)
367
+ TreeNode::Ptr BT::XMLParser::Pimpl:: treeParsing (const tinyxml2:: XMLElement* root_element,
368
+ const NodeBuilder& node_builder,
369
+ std::vector<TreeNode::Ptr>& nodes,
370
+ const TreeNode::Ptr& root_parent)
346
371
{
347
372
using namespace tinyxml2 ;
348
373
0 commit comments