Skip to content

Commit

Permalink
Fix issue #767 and #768
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide committed Feb 13, 2024
1 parent 3326553 commit 83fce13
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
58 changes: 39 additions & 19 deletions include/behaviortree_cpp/basic_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,39 @@ inline std::pair<std::string, PortInfo> BidirectionalPort(StringView name,
return CreatePort<T>(PortDirection::INOUT, name, description);
}
//----------

namespace details {

template <typename T = AnyTypeAllowed, typename DefaultT = T> [[nodiscard]]
inline std::pair<std::string, PortInfo> PortWithDefault(
PortDirection direction,
StringView name,
const DefaultT& default_value,
StringView description)
{
static_assert(IsConvertibleToString<DefaultT>() ||
std::is_convertible_v<T, DefaultT> ||
std::is_constructible_v<T, DefaultT>,
"The default value must be either the same of the port or string");

auto out = CreatePort<T>(direction, name, description);

if constexpr(std::is_constructible_v<T, DefaultT>)
{
out.second.setDefaultValue(T(default_value));
}
else if constexpr(IsConvertibleToString<DefaultT>())
{
out.second.setDefaultValue(std::string(default_value));
}
else {
out.second.setDefaultValue(default_value);
}
return out;
}

} // end namespace details

/** Syntactic sugar to invoke CreatePort<T>(PortDirection::INPUT,...)
* It also sets the PortInfo::defaultValue()
*
Expand All @@ -437,14 +470,7 @@ inline std::pair<std::string, PortInfo> InputPort(StringView name,
const DefaultT& default_value,
StringView description)
{
static_assert(std::is_same_v<T, DefaultT> ||
IsConvertibleToString<DefaultT>() ||
std::is_convertible_v<DefaultT, T>,
"The default value must be either the same of the port or a string");

auto out = CreatePort<T>(PortDirection::INPUT, name, description);
out.second.setDefaultValue(default_value);
return out;
return details::PortWithDefault<T, DefaultT>(PortDirection::INPUT, name, default_value, description);
}

/** Syntactic sugar to invoke CreatePort<T>(PortDirection::INOUT,...)
Expand All @@ -459,14 +485,7 @@ inline std::pair<std::string, PortInfo> BidirectionalPort(StringView name,
const DefaultT& default_value,
StringView description)
{
static_assert(std::is_same_v<T, DefaultT> ||
IsConvertibleToString<DefaultT>() ||
std::is_convertible_v<DefaultT, T>,
"The default value must be either the same of the port or a string");

auto out = CreatePort<T>(PortDirection::INOUT, name, description);
out.second.setDefaultValue(default_value);
return out;
return details::PortWithDefault<T, DefaultT>(PortDirection::INOUT, name, default_value, description);
}

/** Syntactic sugar to invoke CreatePort<T>(PortDirection::OUTPUT,...)
Expand All @@ -477,9 +496,10 @@ inline std::pair<std::string, PortInfo> BidirectionalPort(StringView name,
* @param description optional human-readable description
*/
template <typename T = AnyTypeAllowed> [[nodiscard]]
inline std::pair<std::string, PortInfo> OutputPort(StringView name,
StringView default_value,
StringView description)
inline std::pair<std::string, PortInfo> OutputPort(
StringView name,
StringView default_value,
StringView description)
{
if(default_value.empty() || default_value.front() != '{' || default_value.back() != '}')
{
Expand Down
13 changes: 13 additions & 0 deletions tests/gtest_ports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,16 @@ TEST(PortTest, DefaultInputStrings)
ASSERT_EQ(status, NodeStatus::SUCCESS);
}


TEST(PortTest, Default_Issues_767_768)
{
using namespace BT;

ASSERT_NO_THROW(auto p = InputPort<std::optional<Point2D>>("opt_A", std::nullopt, "default nullopt"));
ASSERT_NO_THROW(auto p = InputPort<std::optional<std::string>>("opt_B", std::nullopt, "default nullopt"));

ASSERT_NO_THROW(auto p = InputPort<std::shared_ptr<Point2D>>("ptr_A", nullptr, "default nullptr"));
ASSERT_NO_THROW(auto p = InputPort<std::shared_ptr<std::string>>("ptr_B", nullptr, "default nullptr"));
}


0 comments on commit 83fce13

Please sign in to comment.