Skip to content

Commit 77c0571

Browse files
committed
Fix #580 : more informative error when not specializing BT::toStr
1 parent dae4cac commit 77c0571

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

include/behaviortree_cpp/basic_types.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,30 @@ inline StringConverter GetAnyFromStringFunctor<void>()
144144

145145
//------------------------------------------------------------------
146146

147-
template <typename T> [[nodiscard]]
148-
std::string toStr(T value)
147+
template<typename T> [[nodiscard]]
148+
std::string toStr(const T& value)
149149
{
150+
static_assert(std::is_arithmetic_v<T>,
151+
"You need a template specialization of BT::toStr() and "
152+
"it must be consistent with the implementation of BT::convertFromString");
150153
return std::to_string(value);
151154
}
152155

153-
std::string toStr(const std::string& value);
156+
template <> [[nodiscard]] inline
157+
std::string toStr<bool>(const bool& value) {
158+
return value ? "true" : "false";
159+
}
154160

155-
template <>
156-
std::string toStr<BT::NodeStatus>(BT::NodeStatus status);
161+
template <> [[nodiscard]]
162+
std::string toStr<std::string>(const std::string& value);
163+
164+
template <> [[nodiscard]]
165+
std::string toStr<BT::NodeStatus>(const BT::NodeStatus& status);
157166

158167
/**
159168
* @brief toStr converts NodeStatus to string. Optionally colored.
160169
*/
170+
[[nodiscard]]
161171
std::string toStr(BT::NodeStatus status, bool colored);
162172

163173
std::ostream& operator<<(std::ostream& os, const BT::NodeStatus& status);
@@ -166,12 +176,12 @@ std::ostream& operator<<(std::ostream& os, const BT::NodeStatus& status);
166176
* @brief toStr converts NodeType to string.
167177
*/
168178
template <> [[nodiscard]]
169-
std::string toStr<BT::NodeType>(BT::NodeType type);
179+
std::string toStr<BT::NodeType>(const BT::NodeType& type);
170180

171181
std::ostream& operator<<(std::ostream& os, const BT::NodeType& type);
172182

173183
template <> [[nodiscard]]
174-
std::string toStr<BT::PortDirection>(BT::PortDirection direction);
184+
std::string toStr<BT::PortDirection>(const BT::PortDirection& direction);
175185

176186
std::ostream& operator<<(std::ostream& os, const BT::PortDirection& type);
177187

@@ -203,6 +213,7 @@ using enable_if_not = typename std::enable_if<!Predicate::value>::type*;
203213
* */
204214
template <typename T>
205215
using Expected = nonstd::expected<T, std::string>;
216+
206217
#ifdef USE_BTCPP3_OLD_NAMES
207218
// note: we also use the name Optional instead of expected because it is more intuitive
208219
// for users that are not up to date with "modern" C++

include/behaviortree_cpp/tree_node.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ enum class PostCond
6464
};
6565

6666
template <>
67-
std::string toStr<BT::PostCond>(BT::PostCond status);
67+
std::string toStr<BT::PostCond>(const BT::PostCond& status);
6868

6969
template <>
70-
std::string toStr<BT::PreCond>(BT::PreCond status);
70+
std::string toStr<BT::PreCond>(const BT::PreCond& status);
7171

7272
using ScriptingEnumsRegistry = std::unordered_map<std::string, int>;
7373

src/basic_types.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace BT
88
{
99
template <>
10-
std::string toStr<NodeStatus>(NodeStatus status)
10+
std::string toStr<NodeStatus>(const NodeStatus& status)
1111
{
1212
switch (status)
1313
{
@@ -63,7 +63,7 @@ std::string toStr(NodeStatus status, bool colored)
6363
}
6464

6565
template <>
66-
std::string toStr<PortDirection>(PortDirection direction)
66+
std::string toStr<PortDirection>(const PortDirection& direction)
6767
{
6868
switch (direction)
6969
{
@@ -78,7 +78,7 @@ std::string toStr<PortDirection>(PortDirection direction)
7878
}
7979

8080
template <>
81-
std::string toStr<NodeType>(NodeType type)
81+
std::string toStr<NodeType>(const NodeType& type)
8282
{
8383
switch (type)
8484
{

src/tree_node.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ void TreeNode::modifyPortsRemapping(const PortsRemapping& new_remapping)
376376
}
377377

378378
template <>
379-
std::string toStr<PreCond>(PreCond pre)
379+
std::string toStr<PreCond>(const PreCond& pre)
380380
{
381381
switch (pre)
382382
{
@@ -394,7 +394,7 @@ std::string toStr<PreCond>(PreCond pre)
394394
}
395395

396396
template <>
397-
std::string toStr<PostCond>(PostCond pre)
397+
std::string toStr<PostCond>(const PostCond& pre)
398398
{
399399
switch (pre)
400400
{

0 commit comments

Comments
 (0)