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..3e129f7 --- /dev/null +++ b/Composite/main.cpp @@ -0,0 +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; +}