From 7af9a944f002509b37910680ea7475e0a7d40491 Mon Sep 17 00:00:00 2001 From: fusiled Date: Tue, 23 Jun 2026 17:35:31 -0500 Subject: [PATCH] Allow nested modules Use create_child to get a pointer to a Module inside a Module and initialize it as usual with the initialize method. --- .gitignore | 7 +++++++ include/Simo/module/Module.h | 9 ++++++++- tests/module/ModuleTest.cc | 9 +++++++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index de30407..b97b08f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,16 @@ +# CLion folders /.idea /cmake-build-* + +# guided_examples products /docs/guided_examples/*/build* /docs/guided_examples/flake.lock + # Avoid adding symlinks /support/ides/clion/* +# Products of SimoSim +Simo.log +statistics.yaml *.DS_Store diff --git a/include/Simo/module/Module.h b/include/Simo/module/Module.h index edad6fe..0f193de 100644 --- a/include/Simo/module/Module.h +++ b/include/Simo/module/Module.h @@ -162,7 +162,6 @@ class SIMO_PUBLIC Module { Log::Logger& get_logger() { return logger; } - protected: /// Create a new statistic of type T template T& create_statistic(Args... args) { @@ -179,8 +178,16 @@ class SIMO_PUBLIC Module { return out_ref; } + template + T& create_child(Args... args) { + children.emplace_back(std::make_unique(std::forward(args)...)); + return *children.back(); + } + + protected: Statistics::StatStorage statistics; std::unordered_map> ports; + std::vector> children; Log::Logger logger; private: diff --git a/tests/module/ModuleTest.cc b/tests/module/ModuleTest.cc index 1706b09..60b8f3b 100644 --- a/tests/module/ModuleTest.cc +++ b/tests/module/ModuleTest.cc @@ -106,12 +106,17 @@ BOOST_AUTO_TEST_CASE(StdoutLogSetupEnablesLogger) { BOOST_CHECK_EQUAL(logger.enabled(), true); } -BOOST_AUTO_TEST_CASE(NameOfChild) { +BOOST_AUTO_TEST_CASE(ModuleChild) { Simo::Context ctx; Simo::Parameters p; Simo::Module m; p.name("root"); const auto status = m.initialize(ctx, p); + auto& child = m.create_child(); auto child_name = m.name_of_child("child"); - BOOST_CHECK_EQUAL(child_name, "root/child"); + Simo::Parameters p_child; + p_child.name(child_name); + auto child_status = child.initialize(ctx, p_child); + BOOST_CHECK_EQUAL(child_status.success(), true); + BOOST_CHECK_EQUAL(p_child.name(), "root/child"); }