From 6de1c8dbe5483edb071af4810fa9050ffac662b1 Mon Sep 17 00:00:00 2001 From: Robert Haschke Date: Wed, 20 Feb 2019 11:14:50 +0100 Subject: [PATCH] ContainerBase::findChild() --- .../include/moveit/task_constructor/container.h | 1 + core/src/container.cpp | 15 +++++++++++++++ core/test/test_container.cpp | 17 +++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/core/include/moveit/task_constructor/container.h b/core/include/moveit/task_constructor/container.h index 7dd1ac4e..5076b49e 100644 --- a/core/include/moveit/task_constructor/container.h +++ b/core/include/moveit/task_constructor/container.h @@ -51,6 +51,7 @@ public: typedef std::unique_ptr pointer; size_t numChildren() const; + Stage* findChild(const std::string& name) const; typedef std::function StageCallback; /// traverse direct children of this container, calling the callback for each of them diff --git a/core/src/container.cpp b/core/src/container.cpp index 64115916..83dce6ae 100644 --- a/core/src/container.cpp +++ b/core/src/container.cpp @@ -168,6 +168,21 @@ size_t ContainerBase::numChildren() const return pimpl()->children().size(); } +Stage* ContainerBase::findChild(const std::string& name) const +{ + auto pos = name.find('/'); + const std::string first = name.substr(0, pos); + for (const Stage::pointer& child : pimpl()->children()) + if (child->name() == first) + { + if (pos == std::string::npos) + return child.get(); + else if (auto *parent = dynamic_cast(child.get())) + return parent->findChild(name.substr(pos+1)); + } + return nullptr; +} + bool ContainerBase::traverseChildren(const ContainerBase::StageCallback &processor) const { return pimpl()->traverseStages(processor, 0, 1); diff --git a/core/test/test_container.cpp b/core/test/test_container.cpp index 17403b25..b3bf51a7 100644 --- a/core/test/test_container.cpp +++ b/core/test/test_container.cpp @@ -100,6 +100,23 @@ TEST(ContainerBase, positionForInsert) { EXPECT_EQ(impl->childByIndex(-4, true), impl->children().end()); } +TEST(ContainerBase, findChild) { + SerialContainer s, *c2; + Stage *a, *b, *c1, *d; + s.insert(Stage::pointer(a=new NamedStage("a"))); + s.insert(Stage::pointer(b=new NamedStage("b"))); + s.insert(Stage::pointer(c1=new NamedStage("c"))); + auto sub = Stage::pointer(c2 = new SerialContainer("c")); + c2->insert(Stage::pointer(d=new NamedStage("d"))); + s.insert(std::move(sub)); + + EXPECT_EQ(s.findChild("a"), a); + EXPECT_EQ(s.findChild("b"), b); + EXPECT_EQ(s.findChild("c"), c1); + EXPECT_EQ(s.findChild("d"), nullptr); + EXPECT_EQ(s.findChild("c/d"), d); +} + template class InitTest : public ::testing::Test {