Tranfered Issue: Proper read only access for nodes of const getter methods
The behavior tree is managed by shared pointers, which is fine. But in some cases (e.g. composite node) access to nodes are provided via const methods to const shared pointers, which does still provide write access to the returned node (the internal pointer is not const in this case). Providing shared pointers access also could lead to the reuse of the same node instance in multiple decision trees, which would invalidate them because of the internal dependencies of the tree like the blackboard. To solve this problem, there are several options that we should discuss:
- We could stick with shared pointers and address the issues by
- Introduce an additional shared pointer holding a const pointer to the node instance and use it to return correct const nodes
- Access to nodes should always be read only, so passing e.g. the full child list by passing a const vector with non read only shared pointers should be prevented. In this case, a visitor could be provided, which allows more control over access without creating extra copies of shared pointer with the correct access rights.
- Adding child nodes (composite, decorator, etc.) are always exclusive, so the interfaces should use unique pointer instead shared pointers to enforce it.
- We could use unique pointers for the tree and provide access to nodes via const references only.
While using unique pointers could slightly improve performance, it is actually not the appropriate way to represent a tree, since cycles occur (parent contains childs, child refers to parent), which should ideally be represented by shared and weak pointers. With shared pointers, access to nodes can also be realized exclusively via references. However, it should be noted that the casting of a reference in case of an error (exception) behaves differently than with a pointer (nullptr).
Maintainer
FYI @rparisha2 : We created this issues here to discuss the improvements regarding the excessive use of shared_ptr. I will invite you both to an meeting soon to discuss it in person