MCPcopy Create free account
hub / github.com/BehaviorTree/BehaviorTree.CPP / loadXmlAndResolveTreeId

Function loadXmlAndResolveTreeId

src/bt_factory.cpp:58–93  ·  view source on GitHub ↗

Load XML into parser and resolve which tree to instantiate. Returns the resolved tree ID (may be empty if parser should use default).

Source from the content-addressed store, hash-verified

56// Load XML into parser and resolve which tree to instantiate.
57// Returns the resolved tree ID (may be empty if parser should use default).
58std::string loadXmlAndResolveTreeId(Parser* parser, const std::string& main_tree_ID,
59 const std::function<void()>& load_func)
60{
61 // When the main tree couldn't be determined from the raw XML
62 // (e.g. <BehaviorTree> without an ID), snapshot registered trees
63 // before loading so we can diff afterwards.
64 std::set<std::string> before_set;
65 if(main_tree_ID.empty())
66 {
67 const auto before = parser->registeredBehaviorTrees();
68 before_set.insert(before.begin(), before.end());
69 }
70
71 load_func();
72
73 // Try to identify the newly added tree by diffing.
74 if(main_tree_ID.empty())
75 {
76 const auto after = parser->registeredBehaviorTrees();
77 std::string single_new_tree;
78 int new_count = 0;
79 for(const auto& name : after)
80 {
81 if(before_set.count(name) == 0)
82 {
83 single_new_tree = name;
84 new_count++;
85 }
86 }
87 if(new_count == 1)
88 {
89 return single_new_tree;
90 }
91 }
92 return main_tree_ID;
93}
94
95} // namespace
96

Callers 2

createTreeFromTextMethod · 0.85
createTreeFromFileMethod · 0.85

Calls 4

emptyMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected