Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support conversion between vector<Any> and vector<typename T::value_type> #873

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion include/behaviortree_cpp/blackboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <memory>
#include <unordered_map>
#include <mutex>
#include <regex>

#include "behaviortree_cpp/basic_types.h"
#include "behaviortree_cpp/contrib/json.hpp"
Expand All @@ -25,6 +26,19 @@ struct StampedValue
Timestamp stamp;
};

// Helper trait to check if templated type is a std::vector
template <typename T>
struct is_vector : std::false_type {};

template <typename T, typename A>
struct is_vector<std::vector<T, A>> : std::true_type {};

// Helper function to check if a demangled type string is a std::vector<..>
inline bool isVector(const std::string& type_name)
{
return std::regex_match(type_name, std::regex(R"(^std::vector<.*>$)"));
}

/**
* @brief The Blackboard is the mechanism used by BehaviorTrees to exchange
* typed data.
Expand Down Expand Up @@ -257,8 +271,14 @@ inline void Blackboard::set(const std::string& key, const T& value)

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

// Allow mismatch if going from vector -> vector<Any>.
auto prev_type_demangled = BT::demangle(entry.value.type());
bool previous_is_vector = BT::isVector(prev_type_demangled);
bool new_is_vector_any = new_value.type() == typeid(std::vector<Any>);

// check type mismatch
if(previous_type != std::type_index(typeid(T)) && previous_type != new_value.type())
if(previous_type != std::type_index(typeid(T)) && previous_type != new_value.type() &&
!(previous_is_vector && new_is_vector_any))
{
bool mismatching = true;
if(std::is_constructible<StringView, T>::value)
Expand Down
35 changes: 33 additions & 2 deletions include/behaviortree_cpp/tree_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

namespace BT
{

/// This information is used mostly by the XMLParser.
struct TreeNodeManifest
{
Expand Down Expand Up @@ -521,6 +520,26 @@ inline Expected<Timestamp> TreeNode::getInputStamped(const std::string& key,

if(!entry->value.empty())
{
// Support vector<Any> -> vector<typename T::value_type> conversion.
// Only want to compile this path when T is a vector type.
if constexpr (is_vector<T>::value)
{
if (!std::is_same_v<T, std::vector<Any>> && any_value.type() == typeid(std::vector<Any>))
{
// If the object was originally placed on the blackboard as a vector<Any>, attempt to unwrap the vector
// elements according to the templated type.
auto any_vec = any_value.cast<std::vector<Any>>();
if (!any_vec.empty() && any_vec.front().type() != typeid(typename T::value_type))
{
return nonstd::make_unexpected("Invalid cast requested from vector<Any> to vector<typename T::value_type>."
" Element type does not align.");
}
destination = T();
std::transform(any_vec.begin(), any_vec.end(), std::back_inserter(destination),
[](Any &element) { return element.cast<typename T::value_type>(); });
return Timestamp{ entry->sequence_id, entry->stamp };
}
}
if(!std::is_same_v<T, std::string> && any_value.isString())
{
destination = parseString<T>(any_value.cast<std::string>());
Expand Down Expand Up @@ -593,7 +612,19 @@ inline Result TreeNode::setOutput(const std::string& key, const T& value)
}

remapped_key = stripBlackboardPointer(remapped_key);
config().blackboard->set(static_cast<std::string>(remapped_key), value);

if constexpr(is_vector<T>::value && !std::is_same_v<T, std::vector<Any>>)
{
// If the object is a vector but not a vector<Any>, convert it to vector<Any> before placing it on the blackboard.
auto any_vec = std::vector<Any>();
std::transform(value.begin(), value.end(), std::back_inserter(any_vec),
[](const auto &element) { return BT::Any(element); });
config().blackboard->set(static_cast<std::string>(remapped_key), any_vec);
}
else
{
config().blackboard->set(static_cast<std::string>(remapped_key), value);
}

return {};
}
Expand Down
9 changes: 7 additions & 2 deletions src/blackboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,18 @@ std::shared_ptr<Blackboard::Entry> Blackboard::createEntryImpl(const std::string
if(storage_it != storage_.end())
{
const auto& prev_info = storage_it->second->info;
auto prev_type_demangled = BT::demangle(prev_info.type());
// Allow mismatch if going from vector -> vector<Any>.
bool previous_is_vector = BT::isVector(prev_type_demangled);
bool new_is_vector_any = info.type() == typeid(std::vector<Any>);

if(prev_info.type() != info.type() && prev_info.isStronglyTyped() &&
info.isStronglyTyped())
info.isStronglyTyped() && !(previous_is_vector && new_is_vector_any))
{
auto msg = StrCat("Blackboard entry [", key,
"]: once declared, the type of a port"
" shall not change. Previously declared type [",
BT::demangle(prev_info.type()), "], current type [",
prev_type_demangled, "], current type [",
BT::demangle(info.type()), "]");

throw LogicError(msg);
Expand Down
11 changes: 9 additions & 2 deletions src/xml_parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -786,8 +786,15 @@ TreeNode::Ptr XMLParser::PImpl::createNodeFromXML(const XMLElement* element,

// special case related to convertFromString
bool const string_input = (prev_info->type() == typeid(std::string));

if(port_type_mismatch && !string_input)
// special case related to unwrapping vector<Any> -> vector<T> objects.
bool const vec_any_input = (prev_info->type() == typeid(std::vector<Any>));
// special case related to wrapping vector<T> -> vector<Any> objects.
auto prev_type_demangled = demangle(prev_info->type());
bool previous_is_vector = BT::isVector(prev_type_demangled);
bool new_is_vector_any = port_info.type() == typeid(std::vector<Any>);

if(port_type_mismatch && !string_input &&
!vec_any_input & !(previous_is_vector && new_is_vector_any))
{
blackboard->debugMessage();

Expand Down
Loading