From 4b82ca17acb8be2fc5bd6d61a58a869d1961c53f Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Thu, 14 Mar 2024 20:12:36 +0100 Subject: [PATCH 1/2] chore: add composite --- Composite/CMakeLists.txt | 21 +++++++++++++++++++++ Composite/bootstrap.sh | 1 + Composite/build.sh | 1 + Composite/main.cpp | 8 ++++++++ 4 files changed, 31 insertions(+) create mode 100644 Composite/CMakeLists.txt create mode 120000 Composite/bootstrap.sh create mode 120000 Composite/build.sh create mode 100644 Composite/main.cpp diff --git a/Composite/CMakeLists.txt b/Composite/CMakeLists.txt new file mode 100644 index 0000000..b6e1b44 --- /dev/null +++ b/Composite/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 3.20) + +project( + Composite + VERSION 0.1.0 + LANGUAGES CXX) + +list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/../cmake") + +include(ExportCompileCommands) +include(sccache) + +add_executable(Composite main.cpp) + +target_compile_features(Composite PUBLIC cxx_std_20) + +set_target_properties( + Composite + PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin" + RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin" + RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/bin") diff --git a/Composite/bootstrap.sh b/Composite/bootstrap.sh new file mode 120000 index 0000000..796ed13 --- /dev/null +++ b/Composite/bootstrap.sh @@ -0,0 +1 @@ +../scripts/bootstrap.sh \ No newline at end of file diff --git a/Composite/build.sh b/Composite/build.sh new file mode 120000 index 0000000..0eff50d --- /dev/null +++ b/Composite/build.sh @@ -0,0 +1 @@ +../scripts/build.sh \ No newline at end of file diff --git a/Composite/main.cpp b/Composite/main.cpp new file mode 100644 index 0000000..1e4fb84 --- /dev/null +++ b/Composite/main.cpp @@ -0,0 +1,8 @@ +#include + +int main(int argc, char *argv[]) { + + std::cout << "Composite" << std::endl; + + return EXIT_SUCCESS; +} From 68da0a4b6c723da0cb6f25a09a07906a4191291e Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Thu, 14 Mar 2024 20:25:58 +0100 Subject: [PATCH 2/2] feat: implement Composite --- Composite/main.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/Composite/main.cpp b/Composite/main.cpp index 1e4fb84..3e129f7 100644 --- a/Composite/main.cpp +++ b/Composite/main.cpp @@ -1,8 +1,60 @@ #include +#include +#include + +class Node { +public: + virtual ~Node() = default; + virtual void print() const = 0; +}; + +class InnerNode : public Node { +private: + std::string name_; + std::vector children_; + +public: + InnerNode(std::string_view name) : name_(name) {} + + void addNode(Node *node) { children_.push_back(node); } + + void print() const override { + std::cout << name_ << " (inner)" << std::endl; + + for (auto child : children_) { + child->print(); + } + } +}; + +class Leaf : public Node { +private: + std::string name_; + +public: + Leaf(std::string_view name) : name_(name) {} + + void print() const override { std::cout << name_ << " (leaf)" << std::endl; } +}; int main(int argc, char *argv[]) { std::cout << "Composite" << std::endl; + InnerNode tree("root"); + InnerNode left("left"), right("right"); + Leaf leaf1("leaf 1"), leaf2("leaf 2"), leaf3("leaf 3"), leaf4("leaf 4"); + + tree.addNode(&left); + tree.addNode(&right); + + left.addNode(&leaf1); + left.addNode(&leaf2); + + right.addNode(&leaf3); + right.addNode(&leaf4); + + tree.print(); + return EXIT_SUCCESS; }