Skip to content

Commit 5e16d72

Browse files
committed
fix issue 489
1 parent e147009 commit 5e16d72

File tree

3 files changed

+65
-14
lines changed

3 files changed

+65
-14
lines changed

include/behaviortree_cpp/basic_types.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,7 @@ using StringView = std::string_view;
7272
template <typename T>
7373
inline T convertFromString(StringView /*str*/)
7474
{
75-
auto type_name = BT::demangle(typeid(T));
76-
77-
std::cerr << "You (maybe indirectly) called BT::convertFromString() for type ["
78-
<< type_name << "], but I can't find the template specialization.\n"
79-
<< std::endl;
80-
81-
throw LogicError(std::string("You didn't implement the template specialization of "
82-
"convertFromString for this type: ") +
83-
type_name);
75+
static_assert(true, "This template specialization of convertFromString doesn't exist");
8476
}
8577

8678
template <>
@@ -269,7 +261,7 @@ class PortInfo
269261

270262
bool isStronglyTyped() const
271263
{
272-
return _type_info == typeid(AnyTypeAllowed);
264+
return _type_info != typeid(AnyTypeAllowed);
273265
}
274266

275267
const StringConverter& converter() const

src/xml_parsing.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -578,10 +578,15 @@ TreeNode::Ptr XMLParser::Pimpl::createNodeFromXML(const XMLElement* element,
578578
// if the entry already exists, check that the type is the same
579579
if (auto prev_info = blackboard->portInfo(port_key))
580580
{
581-
// found. check consistency
582-
// null type means that everything is valid
583-
if (!prev_info->isStronglyTyped() && !port_info.isStronglyTyped() &&
584-
prev_info->type() != port_info.type())
581+
// Check consistency of types.
582+
bool const port_type_mismatch = (prev_info->isStronglyTyped() &&
583+
port_info.isStronglyTyped() &&
584+
prev_info->type() != port_info.type());
585+
586+
// special case related to convertFromString
587+
bool const string_input = (prev_info->type() == typeid(std::string));
588+
589+
if(port_type_mismatch && !string_input)
585590
{
586591
blackboard->debugMessage();
587592

tests/gtest_ports.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,57 @@ TEST(PortTest, IllegalPorts)
176176
BehaviorTreeFactory factory;
177177
ASSERT_ANY_THROW(factory.registerNodeType<IllegalPorts>("nope"));
178178
}
179+
180+
181+
class ActionVectorIn : public SyncActionNode
182+
{
183+
public:
184+
ActionVectorIn(const std::string& name, const NodeConfig& config,
185+
std::vector<double>* states) :
186+
SyncActionNode(name, config),
187+
states_(states)
188+
{}
189+
190+
NodeStatus tick() override
191+
{
192+
getInput("states", *states_);
193+
return NodeStatus::SUCCESS;
194+
}
195+
196+
static PortsList providedPorts()
197+
{
198+
return {BT::InputPort<std::vector<double>>("states")};
199+
}
200+
std::vector<double>* states_;
201+
};
202+
203+
204+
TEST(PortTest, SubtreeStringInput_Issue489)
205+
{
206+
std::string xml_txt = R"(
207+
<root BTCPP_format="4" >
208+
<BehaviorTree ID="Main">
209+
<SubTree ID="Subtree_A" states="3;7"/>
210+
</BehaviorTree>
211+
212+
<BehaviorTree ID="Subtree_A">
213+
<ActionVectorIn states="{states}"/>
214+
</BehaviorTree>
215+
</root>)";
216+
217+
std::vector<double> states;
218+
219+
BehaviorTreeFactory factory;
220+
factory.registerNodeType<ActionVectorIn>("ActionVectorIn", &states);
221+
222+
factory.registerBehaviorTreeFromText(xml_txt);
223+
auto tree = factory.createTree("Main");
224+
225+
NodeStatus status = tree.tickWhileRunning();
226+
227+
ASSERT_EQ(status, NodeStatus::SUCCESS);
228+
ASSERT_EQ(2, states.size());
229+
ASSERT_EQ(3, states[0]);
230+
ASSERT_EQ(7, states[1]);
231+
}
232+

0 commit comments

Comments
 (0)