Skip to content

Commit d28e5be

Browse files
committed
Fix #570: string_view set in blackboard
1 parent 2961ee8 commit d28e5be

File tree

4 files changed

+23
-13
lines changed

4 files changed

+23
-13
lines changed

include/behaviortree_cpp/blackboard.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,10 @@ class Blackboard
173173
auto it = storage_.find(key);
174174
if (it == storage_.end())
175175
{
176-
// Not defined before. Let's create an entry with a generic PortInfo
177-
if (std::is_constructible<StringView, T>::value)
178-
{
179-
PortInfo new_port(PortDirection::INOUT, typeid(std::string), {});
180-
storage_.emplace(key, std::make_unique<Entry>(Any(value), new_port));
181-
}
182-
else
183-
{
184-
PortInfo new_port(PortDirection::INOUT, typeid(T), {});
185-
storage_.emplace(key, std::make_unique<Entry>(Any(value), new_port));
186-
}
176+
// create a new entry
177+
Any new_value(value);
178+
PortInfo new_port(PortDirection::INOUT, new_value.type(), {});
179+
storage_.emplace(key, std::make_unique<Entry>(std::move(new_value), new_port));
187180
}
188181
else
189182
{
@@ -229,8 +222,7 @@ class Blackboard
229222
debugMessage();
230223

231224
throw LogicError("Blackboard::set() failed: once declared, the type of a port "
232-
"shall not change. "
233-
"Declared type [",
225+
"shall not change. Declared type [",
234226
BT::demangle(previous_type), "] != current type [",
235227
BT::demangle(typeid(T)), "]");
236228
}

include/behaviortree_cpp/utils/safe_any.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ class Any
9494
{
9595
}
9696

97+
explicit Any(const std::string_view& str) : _any(SafeAny::SimpleString(str)), _original_type( typeid(std::string) )
98+
{
99+
}
100+
97101
// all the other integrals are casted to int64_t
98102
template <typename T>
99103
explicit Any(const T& value, EnableIntegral<T> = 0) : _any(int64_t(value)), _original_type( typeid(T) )

include/behaviortree_cpp/utils/simple_string.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <stdexcept>
66
#include <limits>
77
#include <cstdint>
8+
#include <string_view>
89

910
namespace SafeAny{
1011

@@ -19,6 +20,9 @@ class SimpleString {
1920
SimpleString(const std::string &str): SimpleString(str.data(), str.size())
2021
{}
2122

23+
SimpleString(const std::string_view &str): SimpleString(str.data(), str.size())
24+
{}
25+
2226
SimpleString(const SimpleString& other): SimpleString(other.data(), other.size())
2327
{}
2428

tests/gtest_blackboard.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,13 @@ TEST(BlackboardTest, AnyPtrLocked)
350350
ASSERT_NE(cycles, value);
351351
}
352352
}
353+
354+
TEST(BlackboardTest, SetStringView)
355+
{
356+
auto bb = Blackboard::create();
357+
358+
constexpr BT::StringView string_view_const = "BehaviorTreeCpp";
359+
bb->set("string_view", string_view_const);
360+
361+
ASSERT_NO_THROW(bb->set("string_view", string_view_const));
362+
}

0 commit comments

Comments
 (0)