1
1
/* 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
3
3
*
4
4
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
5
5
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
14
14
#ifndef BT_FACTORY_H
15
15
#define BT_FACTORY_H
16
16
17
+ #include < algorithm>
18
+ #include < cstring>
19
+ #include < filesystem>
17
20
#include < functional>
18
21
#include < memory>
19
22
#include < unordered_map>
20
23
#include < unordered_set>
21
- #include < cstring>
22
- #include < algorithm>
23
24
#include < set>
24
25
25
26
#include " behaviortree_cpp/contrib/magic_enum.hpp"
@@ -99,47 +100,19 @@ class Tree
99
100
};
100
101
101
102
std::vector<Subtree::Ptr> subtrees;
102
-
103
103
std::unordered_map<std::string, TreeNodeManifest> manifests;
104
104
105
- Tree ()
106
- {}
105
+ Tree ();
107
106
108
- // non-copyable. Only movable
109
107
Tree (const Tree&) = delete ;
110
108
Tree& operator =(const Tree&) = delete ;
111
109
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);
124
112
125
113
void initialize ();
126
114
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 ();
143
116
144
117
[[nodiscard]] TreeNode* rootNode () const ;
145
118
@@ -226,6 +199,13 @@ class BehaviorTreeFactory
226
199
{
227
200
public:
228
201
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 ;
229
209
230
210
// / Remove a registered ID.
231
211
bool unregisterBuilder (const std::string& ID);
@@ -302,14 +282,15 @@ class BehaviorTreeFactory
302
282
* where "tree_id" come from the XML attribute <BehaviorTree ID="tree_id">
303
283
*
304
284
*/
305
- void registerBehaviorTreeFromFile (const std::string & filename);
285
+ void registerBehaviorTreeFromFile (const std::filesystem::path & filename);
306
286
307
287
// / Same of registerBehaviorTreeFromFile, but passing the XML text,
308
288
// / instead of the filename.
309
289
void registerBehaviorTreeFromText (const std::string& xml_text);
310
290
311
291
// / Returns the ID of the trees registered either with
312
292
// / registerBehaviorTreeFromFile or registerBehaviorTreeFromText.
293
+ [[nodiscard]]
313
294
std::vector<std::string> registeredBehaviorTrees () const ;
314
295
315
296
/* *
@@ -325,6 +306,7 @@ class BehaviorTreeFactory
325
306
* @param config configuration that is passed to the constructor of the TreeNode.
326
307
* @return new node.
327
308
*/
309
+ [[nodiscard]]
328
310
std::unique_ptr<TreeNode> instantiateTreeNode (const std::string& name,
329
311
const std::string& ID,
330
312
const NodeConfig& config) const ;
@@ -392,20 +374,48 @@ class BehaviorTreeFactory
392
374
}
393
375
394
376
// / All the builders. Made available mostly for debug purposes.
377
+ [[nodiscard]]
395
378
const std::unordered_map<std::string, NodeBuilder>& builders () const ;
396
379
397
380
// / Manifests of all the registered TreeNodes.
381
+ [[nodiscard]]
398
382
const std::unordered_map<std::string, TreeNodeManifest>& manifests () const ;
399
383
400
384
// / List of builtin IDs.
385
+ [[nodiscard]]
401
386
const std::set<std::string>& builtinNodes () const ;
402
387
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]]
403
400
Tree createTreeFromText (const std::string& text,
404
401
Blackboard::Ptr blackboard = Blackboard::create());
405
402
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,
407
416
Blackboard::Ptr blackboard = Blackboard::create());
408
417
418
+ [[nodiscard]]
409
419
Tree createTree (const std::string& tree_name,
410
420
Blackboard::Ptr blackboard = Blackboard::create());
411
421
@@ -476,20 +486,14 @@ class BehaviorTreeFactory
476
486
/* *
477
487
* @brief substitutionRules return the current substitution rules.
478
488
*/
489
+ [[nodiscard]]
479
490
const std::unordered_map<std::string, SubstitutionRule>&
480
491
substitutionRules () const ;
481
492
482
493
private:
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_;
491
494
492
- std::unordered_map<std::string, SubstitutionRule> substitution_rules_;
495
+ struct PImpl ;
496
+ std::unique_ptr<PImpl> _p;
493
497
494
498
};
495
499
0 commit comments