Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0622f5d

Browse files
committedJul 13, 2023
feature #603: add static method [std::string description()] to manifest
1 parent f3f13cd commit 0622f5d

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed
 

‎include/behaviortree_cpp/basic_types.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,19 @@ struct has_static_method_providedPorts<
413413
{
414414
};
415415

416+
template <typename T, typename = void>
417+
struct has_static_method_description : std::false_type
418+
{
419+
};
420+
421+
template <typename T>
422+
struct has_static_method_description<
423+
T, typename std::enable_if<
424+
std::is_same<decltype(T::description()), std::string>::value>::type>
425+
: std::true_type
426+
{
427+
};
428+
416429
template <typename T> [[nodiscard]]
417430
inline PortsList getProvidedPorts(enable_if<has_static_method_providedPorts<T>> = nullptr)
418431
{

‎include/behaviortree_cpp/bt_factory.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ template <typename T>
4444
inline TreeNodeManifest CreateManifest(const std::string& ID,
4545
PortsList portlist = getProvidedPorts<T>())
4646
{
47-
return {getType<T>(), ID, portlist, {}};
47+
if constexpr( has_static_method_description<T>::value)
48+
{
49+
return {getType<T>(), ID, portlist, T::description()};
50+
}
51+
else {
52+
return {getType<T>(), ID, portlist, {}};
53+
}
4854
}
4955

5056
#ifdef BT_PLUGIN_EXPORT

‎src/xml_parsing.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,9 @@ void addNodeModelToXML(const TreeNodeManifest& model,
909909

910910
if (!model.description.empty())
911911
{
912-
element->SetAttribute("description", model.registration_ID.c_str());
912+
auto description_element = doc.NewElement("description");
913+
description_element->SetText(model.description.c_str());
914+
element->InsertEndChild(description_element);
913915
}
914916

915917
model_root->InsertEndChild(element);

‎tests/gtest_factory.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include <gtest/gtest.h>
22
#include <filesystem>
3-
#include "action_test_node.h"
4-
#include "condition_test_node.h"
53
#include "behaviortree_cpp/xml_parsing.h"
64
#include "../sample_nodes/crossdoor_nodes.h"
75
#include "../sample_nodes/dummy_nodes.h"
@@ -372,3 +370,36 @@ TEST(BehaviorTreeReload, ReloadSameTree)
372370
ASSERT_EQ(NodeStatus::FAILURE, tree.tickWhileRunning());
373371
}
374372
}
373+
374+
class DescriptiveAction : public SyncActionNode
375+
{
376+
public:
377+
DescriptiveAction(const std::string& name, const NodeConfig& config):
378+
SyncActionNode(name, config) {}
379+
380+
BT::NodeStatus tick() override {
381+
return NodeStatus::SUCCESS;
382+
}
383+
384+
static PortsList providedPorts() {
385+
return {};
386+
}
387+
388+
static std::string description() {
389+
return "THE DESCRIPTION";
390+
}
391+
};
392+
393+
TEST(BehaviorTreeFactory, DescriptionMethod)
394+
{
395+
396+
BehaviorTreeFactory factory;
397+
factory.registerNodeType<DescriptiveAction>("DescriptiveAction");
398+
const auto& manifest = factory.manifests().at("DescriptiveAction");
399+
ASSERT_EQ(manifest.description, "THE DESCRIPTION");
400+
401+
auto xml = writeTreeNodesModelXML(factory, false);
402+
std::cout << xml << std::endl;
403+
404+
ASSERT_NE(xml.find( "<description>THE DESCRIPTION</description>"), std::string::npos);
405+
}

0 commit comments

Comments
 (0)
Please sign in to comment.