diff --git a/include/docpp.hpp b/include/docpp.hpp index 4aa4a2b..760bf6a 100644 --- a/include/docpp.hpp +++ b/include/docpp.hpp @@ -321,6 +321,11 @@ namespace docpp { * @return std::string The tag of the element */ std::string get(const int formatting = FORMATTING_NONE, const int tabc = 0) const; + /** + * @brief Get the element in the form of a specific type. + * @return T The element in the form of a specific type + */ + template T get(const int formatting = FORMATTING_NONE, const int tabc = 0) const; /** * @brief Get the tag of the element @@ -546,6 +551,11 @@ namespace docpp { * @return std::string The section */ std::string get(const int formatting = FORMATTING_NONE, const int tabc = 0) const; + /** + * @brief Get the element in the form of a specific type. + * @return T The element in the form of a specific type + */ + template T get(const int formatting = FORMATTING_NONE, const int tabc = 0) const; Section operator=(const Section& section); void operator+=(const Element& element); @@ -582,6 +592,11 @@ namespace docpp { * @return std::string The document */ std::string get(const int formatting = FORMATTING_NONE, const int tabc = 0) const; + /** + * @brief Get the document in the form of a specific type. + * @return T The document in the form of a specific type + */ + template T get(const int formatting = FORMATTING_NONE, const int tabc = 0) const; /** * @brief Get the section @@ -698,6 +713,9 @@ namespace docpp { Property operator=(const std::pair& property); }; + /** + * @brief A class to represent the properties of a CSS element + */ class Element { private: std::pair> element{}; @@ -849,6 +867,11 @@ namespace docpp { * @return std::pair> The element */ std::string get(const int formatting = FORMATTING_NONE, const int tabc = 0) const; + /** + * @brief Get the element in the form of a specific type. + * @return T The element in the form of a specific type + */ + template T get(const int formatting = FORMATTING_NONE, const int tabc = 0) const; /** * @brief Get the tag of the element * @return std::string The tag of the element @@ -1014,6 +1037,11 @@ namespace docpp { * @return std::string The stylesheet */ std::string get(const int formatting = FORMATTING_NONE, const int tabc = 0) const; + /** + * @brief Get the stylesheet in the form of a specific type. + * @return T The stylesheet in the form of a specific type + */ + template T get(const int formatting = FORMATTING_NONE, const int tabc = 0) const; Stylesheet operator=(const Stylesheet& stylesheet); void operator+=(const Element& element); diff --git a/src/docpp.cpp b/src/docpp.cpp index c8dc879..922763e 100644 --- a/src/docpp.cpp +++ b/src/docpp.cpp @@ -204,7 +204,7 @@ std::string docpp::HTML::Element::get(const int formatting, const int tabc) cons ret += "<" + this->tag; - for (const auto& it : this->properties.getProperties()) { + for (const Property& it : this->properties.getProperties()) { if (!it.getKey().compare("")) continue; if (!it.getValue().compare("")) continue; @@ -228,6 +228,10 @@ std::string docpp::HTML::Element::get(const int formatting, const int tabc) cons return std::move(ret); } +template T docpp::HTML::Element::get(const int formatting, const int tabc) const { + return T(this->get(formatting, tabc)); +} + std::string docpp::HTML::Element::getTag() const { return this->tag; } @@ -332,7 +336,7 @@ void docpp::HTML::Section::erase(const size_type index) { void docpp::HTML::Section::erase(const Section& section) { for (size_type i{0}; i < this->size(); i++) { - const auto it = this->getSections().at(i); + const Section it = this->getSections().at(i); if (it.get() == section.get()) { this->erase(i); @@ -345,7 +349,7 @@ void docpp::HTML::Section::erase(const Section& section) { void docpp::HTML::Section::erase(const Element& element) { for (size_type i{0}; i < this->size(); i++) { - const auto it = this->getElements().at(i); + const Element it = this->getElements().at(i); if (it.get() == element.get()) { this->erase(i); @@ -389,7 +393,7 @@ docpp::HTML::Section docpp::HTML::Section::at_section(const size_type index) con docpp::HTML::Section::size_type docpp::HTML::Section::find(const Element& element) { for (size_type i{0}; i < this->size(); i++) { - const auto it = this->getElements().at(i); + const Element it = this->getElements().at(i); if (it.get() == element.get()) { return i; @@ -401,7 +405,7 @@ docpp::HTML::Section::size_type docpp::HTML::Section::find(const Element& elemen docpp::HTML::Section::size_type docpp::HTML::Section::find(const Section& section) { for (size_type i{0}; i < this->size(); i++) { - const auto it = this->getSections().at(i); + const Section it = this->getSections().at(i); if (it.get() == section.get()) { return i; @@ -412,7 +416,7 @@ docpp::HTML::Section::size_type docpp::HTML::Section::find(const Section& sectio } docpp::HTML::Section::size_type docpp::HTML::Section::find(const std::string& str) { - const auto elements = this->getElements(); + const std::vector elements = this->getElements(); for (size_type i{0}; i < elements.size(); i++) { if (!elements.at(i).get().compare(str)) { @@ -420,7 +424,7 @@ docpp::HTML::Section::size_type docpp::HTML::Section::find(const std::string& st } } - const auto sections = this->getSections(); + const std::vector
sections = this->getSections(); for (size_type i{0}; i < sections.size(); i++) { if (!sections.at(i).get().compare(str)) { @@ -512,7 +516,7 @@ std::string docpp::HTML::Section::get(const int formatting, const int tabc) cons if (this->tag.compare("")) { ret += "<" + this->tag; - for (const auto& it : this->properties.getProperties()) { + for (const Property& it : this->properties.getProperties()) { if (!it.getKey().compare("")) continue; if (!it.getValue().compare("")) continue; @@ -549,6 +553,10 @@ std::string docpp::HTML::Section::get(const int formatting, const int tabc) cons return std::move(ret); } +template T docpp::HTML::Section::get(const int formatting, const int tabc) const { + return T(this->get(formatting, tabc)); +} + void docpp::HTML::Section::swap(const size_type index1, const size_type index2) { if (this->elements.find(index1) != this->elements.end() && this->elements.find(index2) != this->elements.end()) { std::swap(this->elements[index1], this->elements[index2]); @@ -571,6 +579,10 @@ std::string docpp::HTML::Document::get(const int formatting, const int tabc) con return this->doctype + (formatting == FORMATTING_PRETTY ? "\n" : formatting == FORMATTING_NEWLINE ? "\n" : "") + this->document.get(formatting, tabc); } +template T docpp::HTML::Document::get(const int formatting, const int tabc) const { + return T(this->get(formatting, tabc)); +} + docpp::HTML::Section& docpp::HTML::Document::getSection() { return this->document; } @@ -765,7 +777,7 @@ std::string docpp::CSS::Element::get(const int formatting, const int tabc) const ret += "\n"; } - for (const auto& it : this->element.second) { + for (const Property& it : this->element.second) { if (!it.getKey().compare("")) continue; if (!it.getValue().compare("")) continue; @@ -792,6 +804,10 @@ std::string docpp::CSS::Element::get(const int formatting, const int tabc) const return std::move(ret); } +template T docpp::CSS::Element::get(const int formatting, const int tabc) const { + return T(this->get(formatting, tabc)); +} + std::string docpp::CSS::Element::getTag() const { return this->element.first; } @@ -904,9 +920,13 @@ std::vector docpp::CSS::Stylesheet::getElements() const { std::string docpp::CSS::Stylesheet::get(const int formatting, const int tabc) const { std::string ret{}; - for (const auto& it : this->elements) { + for (const Element& it : this->elements) { ret += it.get(formatting, tabc); } return std::move(ret); } + +template T docpp::CSS::Stylesheet::get(const int formatting, const int tabc) const { + return T(this->get(formatting, tabc)); +} diff --git a/tests/test.cpp b/tests/test.cpp index a8b9332..9691dfb 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -373,6 +373,16 @@ SCENARIO("Test HTML", "[HTML]") { REQUIRE(caught); }; + const auto test23 = []() { + docpp::HTML::Section sect{}; + + sect.push_back(docpp::HTML::Element("p", {}, "Test 1")); + sect.push_back(docpp::HTML::Element("p", {}, "Test 2")); + sect.push_back(docpp::HTML::Element("p", {}, "Test 3")); + + REQUIRE(sect.get() == "

Test 1

Test 2

Test 3

"); + }; + std::vector tests{ test1, test2, @@ -396,6 +406,7 @@ SCENARIO("Test HTML", "[HTML]") { test20, test21, test22, + test23, }; for (const auto& test : tests) {