11/* Copyright (C) 2018 Michele Colledanchise - All Rights Reserved
2- * Copyright (C) 2018-2020 Davide Faconti, Eurecat - All Rights Reserved
2+ * Copyright (C) 2018-2023 Davide Faconti - All Rights Reserved
33*
44* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
55* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
1414#ifndef BT_FACTORY_H
1515#define BT_FACTORY_H
1616
17+ #include < algorithm>
18+ #include < cstring>
19+ #include < filesystem>
1720#include < functional>
1821#include < memory>
1922#include < unordered_map>
2023#include < unordered_set>
21- #include < cstring>
22- #include < algorithm>
2324#include < set>
2425
2526#include " behaviortree_cpp/contrib/magic_enum.hpp"
@@ -99,47 +100,19 @@ class Tree
99100 };
100101
101102 std::vector<Subtree::Ptr> subtrees;
102-
103103 std::unordered_map<std::string, TreeNodeManifest> manifests;
104104
105- Tree ()
106- {}
105+ Tree ();
107106
108- // non-copyable. Only movable
109107 Tree (const Tree&) = delete ;
110108 Tree& operator =(const Tree&) = delete ;
111109
112- Tree (Tree&& other)
113- {
114- (*this ) = std::move (other);
115- }
116-
117- Tree& operator =(Tree&& other)
118- {
119- subtrees = std::move (other.subtrees );
120- manifests = std::move (other.manifests );
121- wake_up_ = other.wake_up_ ;
122- return *this ;
123- }
110+ Tree (Tree&& other);
111+ Tree& operator =(Tree&& other);
124112
125113 void initialize ();
126114
127- void haltTree ()
128- {
129- if (!rootNode ())
130- {
131- return ;
132- }
133- // the halt should propagate to all the node if the nodes
134- // have been implemented correctly
135- rootNode ()->haltNode ();
136-
137- // but, just in case.... this should be no-op
138- auto visitor = [](BT::TreeNode* node) { node->haltNode (); };
139- BT::applyRecursiveVisitor (rootNode (), visitor);
140-
141- rootNode ()->resetStatus ();
142- }
115+ void haltTree ();
143116
144117 [[nodiscard]] TreeNode* rootNode () const ;
145118
@@ -226,6 +199,13 @@ class BehaviorTreeFactory
226199{
227200public:
228201 BehaviorTreeFactory ();
202+ ~BehaviorTreeFactory ();
203+
204+ BehaviorTreeFactory (const BehaviorTreeFactory& other) = delete ;
205+ BehaviorTreeFactory& operator =(const BehaviorTreeFactory& other) = delete ;
206+
207+ BehaviorTreeFactory (BehaviorTreeFactory&& other) = default ;
208+ BehaviorTreeFactory& operator =(BehaviorTreeFactory&& other) = default ;
229209
230210 // / Remove a registered ID.
231211 bool unregisterBuilder (const std::string& ID);
@@ -302,14 +282,15 @@ class BehaviorTreeFactory
302282 * where "tree_id" come from the XML attribute <BehaviorTree ID="tree_id">
303283 *
304284 */
305- void registerBehaviorTreeFromFile (const std::string & filename);
285+ void registerBehaviorTreeFromFile (const std::filesystem::path & filename);
306286
307287 // / Same of registerBehaviorTreeFromFile, but passing the XML text,
308288 // / instead of the filename.
309289 void registerBehaviorTreeFromText (const std::string& xml_text);
310290
311291 // / Returns the ID of the trees registered either with
312292 // / registerBehaviorTreeFromFile or registerBehaviorTreeFromText.
293+ [[nodiscard]]
313294 std::vector<std::string> registeredBehaviorTrees () const ;
314295
315296 /* *
@@ -325,6 +306,7 @@ class BehaviorTreeFactory
325306 * @param config configuration that is passed to the constructor of the TreeNode.
326307 * @return new node.
327308 */
309+ [[nodiscard]]
328310 std::unique_ptr<TreeNode> instantiateTreeNode (const std::string& name,
329311 const std::string& ID,
330312 const NodeConfig& config) const ;
@@ -392,20 +374,48 @@ class BehaviorTreeFactory
392374 }
393375
394376 // / All the builders. Made available mostly for debug purposes.
377+ [[nodiscard]]
395378 const std::unordered_map<std::string, NodeBuilder>& builders () const ;
396379
397380 // / Manifests of all the registered TreeNodes.
381+ [[nodiscard]]
398382 const std::unordered_map<std::string, TreeNodeManifest>& manifests () const ;
399383
400384 // / List of builtin IDs.
385+ [[nodiscard]]
401386 const std::set<std::string>& builtinNodes () const ;
402387
388+ /* *
389+ * @brief createTreeFromText will parse the XML directly from string.
390+ * The XML needs to contain either a single <BehaviorTree> or specify
391+ * the attribute [main_tree_to_execute].
392+ *
393+ * Consider using instead registerBehaviorTreeFromText() and createTree().
394+ *
395+ * @param text string containing the XML
396+ * @param blackboard blackboard of the root tree
397+ * @return the newly created tree
398+ */
399+ [[nodiscard]]
403400 Tree createTreeFromText (const std::string& text,
404401 Blackboard::Ptr blackboard = Blackboard::create());
405402
406- Tree createTreeFromFile (const std::string& file_path,
403+ /* *
404+ * @brief createTreeFromFile will parse the XML from a given file.
405+ * The XML needs to contain either a single <BehaviorTree> or specify
406+ * the attribute [main_tree_to_execute].
407+ *
408+ * Consider using instead registerBehaviorTreeFromFile() and createTree().
409+ *
410+ * @param file_path location of the file to load
411+ * @param blackboard blackboard of the root tree
412+ * @return the newly created tree
413+ */
414+ [[nodiscard]]
415+ Tree createTreeFromFile (const std::filesystem::path& file_path,
407416 Blackboard::Ptr blackboard = Blackboard::create());
408417
418+ [[nodiscard]]
409419 Tree createTree (const std::string& tree_name,
410420 Blackboard::Ptr blackboard = Blackboard::create());
411421
@@ -476,20 +486,14 @@ class BehaviorTreeFactory
476486 /* *
477487 * @brief substitutionRules return the current substitution rules.
478488 */
489+ [[nodiscard]]
479490 const std::unordered_map<std::string, SubstitutionRule>&
480491 substitutionRules () const ;
481492
482493private:
483- std::unordered_map<std::string, NodeBuilder> builders_;
484- std::unordered_map<std::string, TreeNodeManifest> manifests_;
485- std::set<std::string> builtin_IDs_;
486- std::unordered_map<std::string, Any> behavior_tree_definitions_;
487-
488- std::shared_ptr<std::unordered_map<std::string, int >> scripting_enums_;
489-
490- std::shared_ptr<BT::Parser> parser_;
491494
492- std::unordered_map<std::string, SubstitutionRule> substitution_rules_;
495+ struct PImpl ;
496+ std::unique_ptr<PImpl> _p;
493497
494498};
495499
0 commit comments