Skip to content

Fix Blackboard::set() for StringView #570

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 4 additions & 3 deletions include/behaviortree_cpp/blackboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ class Blackboard
Any new_value(value);

// special case: entry exists but it is not strongly typed... yet
// std::string_view entries are the only entries that are strongly-typed from the beginning.
if (!port_info.isStronglyTyped())
{
// Use the new type to create a new entry that is strongly typed.
Expand All @@ -214,10 +215,10 @@ class Blackboard
previous_type != new_value.type())
{
bool mismatching = true;
if (std::is_constructible<StringView, T>::value)
if constexpr (std::is_constructible<StringView, T>::value)
{
Any any_from_string = port_info.parseString(value);
if (any_from_string.empty() == false)
Any any_from_string = Any(std::string(value));
if (!any_from_string.empty())
{
mismatching = false;
new_value = std::move(any_from_string);
Expand Down
10 changes: 10 additions & 0 deletions tests/gtest_blackboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,13 @@ TEST(BlackboardTest, AnyPtrLocked)
ASSERT_NE(cycles, value);
}
}

TEST(BlackboardTest, SetStringView)
{
auto bb = Blackboard::create();

constexpr BT::StringView string_view_const = "BehaviorTreeCpp";
bb->set("string_view", string_view_const);

ASSERT_NO_THROW(bb->set("string_view", string_view_const));
}