#pragma once #include "task2.Element.hpp" // task2::Element #include // std::filesystem::path #include // std::ofstream #include // std::unique_ptr #include // std::endl #include // std::move #include // std::vector namespace task2 { /// @brief Simple SVG-Writer using the task2::Element interface class Image { public: /// @brief Constructs an Image from a root SVG element /// @param root Pointer to the root element Image(std::unique_ptr root) : root(std::move(root)), elements() {} /// @brief Adds a SVG element inside the root element /// @param elem Pointer to the element void add(std::unique_ptr elem) { elements.emplace_back(std::move(elem)); } /// @brief Writes the Image to a file /// @param filepath Filename of the image to be written void save(std::filesystem::path filepath) const { std::ofstream fs; fs.exceptions(std::ifstream::badbit); fs.open(filepath); fs << root->open() << std::endl; if (!root->text().empty()) fs << root->text() << std::endl; for (const auto& e : elements) { fs << " " << e->open() << std::endl; if (!e->text().empty()) fs << " " << e->text() << std::endl; if (!e->close().empty()) fs << " " << e->close() << std::endl; } if (!root->close().empty()) fs << root->close() << std::endl; } private: std::unique_ptr root; ///< root element containing all other elements std::vector> elements; ///< sequence of elements }; } // namespace task2