Skip to content

Commit fbb6583

Browse files
committed
Convert vector to vector<Any> before placing on the blackboard
Also update checks to allow mismatch when a port was declared as a vector<T> and we have an input port that takes it in as a vector<Any>
1 parent e3b0cd7 commit fbb6583

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

include/behaviortree_cpp/blackboard.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,13 @@ inline void Blackboard::set(const std::string& key, const T& value)
257257

258258
std::type_index previous_type = entry.info.type();
259259

260+
// Allow mismatch if going from vector -> vector<Any>.
261+
bool previous_is_vector = std::string(previous_type.name()).find("vector") != std::string::npos;
262+
bool new_is_vector_any = new_value.type() == typeid(std::vector<Any>);
263+
260264
// check type mismatch
261-
if(previous_type != std::type_index(typeid(T)) && previous_type != new_value.type())
265+
if(previous_type != std::type_index(typeid(T)) && previous_type != new_value.type() &&
266+
!(previous_is_vector && new_is_vector_any))
262267
{
263268
bool mismatching = true;
264269
if(std::is_constructible<StringView, T>::value)

include/behaviortree_cpp/tree_node.h

+13-1
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,19 @@ inline Result TreeNode::setOutput(const std::string& key, const T& value)
619619
}
620620

621621
remapped_key = stripBlackboardPointer(remapped_key);
622-
config().blackboard->set(static_cast<std::string>(remapped_key), value);
622+
623+
if constexpr(is_vector<T>::value && !std::is_same_v<T, std::vector<Any>>)
624+
{
625+
// If the object is a vector but not a vector<Any>, convert it to vector<Any> before placing it on the blackboard.
626+
auto any_vec = std::vector<Any>();
627+
std::transform(value.begin(), value.end(), std::back_inserter(any_vec),
628+
[](const auto &element) { return BT::Any(element); });
629+
config().blackboard->set(static_cast<std::string>(remapped_key), any_vec);
630+
}
631+
else
632+
{
633+
config().blackboard->set(static_cast<std::string>(remapped_key), value);
634+
}
623635

624636
return {};
625637
}

src/xml_parsing.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -786,10 +786,14 @@ TreeNode::Ptr XMLParser::PImpl::createNodeFromXML(const XMLElement* element,
786786

787787
// special case related to convertFromString
788788
bool const string_input = (prev_info->type() == typeid(std::string));
789-
// special case related to unwrapping vector<Any> objects.
789+
// special case related to unwrapping vector<Any> -> vector<T> objects.
790790
bool const vec_any_input = (prev_info->type() == typeid(std::vector<Any>));
791+
// special case related to wrapping vector<T> -> vector<Any> objects.
792+
bool previous_is_vector = std::string(prev_info->type().name()).find("vector") != std::string::npos;
793+
bool new_is_vector_any = port_info.type() == typeid(std::vector<Any>);
791794

792-
if(port_type_mismatch && !string_input && !vec_any_input)
795+
if(port_type_mismatch && !string_input &&
796+
!vec_any_input & !(previous_is_vector && new_is_vector_any))
793797
{
794798
blackboard->debugMessage();
795799

0 commit comments

Comments
 (0)