From e49535179e02a0182fc0710a8bd7984d8b2b770f Mon Sep 17 00:00:00 2001 From: speedie Date: Fri, 17 May 2024 13:02:26 +0200 Subject: [PATCH] Move template functions to header. Rather silly mistake. --- include/docpp.hpp | 108 +++++++++++++++++++++++++++++++++++++++------- src/docpp.cpp | 108 ---------------------------------------------- tests/test.cpp | 65 ++++++++++++++-------------- 3 files changed, 125 insertions(+), 156 deletions(-) diff --git a/include/docpp.hpp b/include/docpp.hpp index 5a69e7e..970b30e 100644 --- a/include/docpp.hpp +++ b/include/docpp.hpp @@ -12,6 +12,7 @@ #include #include #include +#include /** * @brief A namespace to represent HTML elements and documents @@ -263,7 +264,13 @@ namespace docpp { * @brief Get the key of the property in a specific type * @return T The key of the property */ - template T get_key() const; + template T get_key() const { + if (std::is_same::value) { + return this->property.first; + } + + return T(this->property.first); + }; /** * @brief Get the value of the property * @return std::string The value of the property @@ -273,7 +280,12 @@ namespace docpp { * @brief Get the value of the property in a specific type * @return T The value of the property */ - template T get_value() const; + template T get_value() const { + if (std::is_same::value) { + return this->property.second; + } + return T(this->property.second); + } /** * @brief Get the property. * @return std::pair The value of the property @@ -283,7 +295,13 @@ namespace docpp { * @brief Get the property in a specific type. * @return std::pair The value of the property */ - template std::pair get() const; + template std::pair get() const { + if (std::is_same::value) { + return this->property; + } + + return T(this->property); + } /** * @brief Set the key of the property. * @param key The key. @@ -510,7 +528,12 @@ namespace docpp { * @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 Formatting formatting = FORMATTING_NONE, const int tabc = 0) const; + template T get(const Formatting formatting = FORMATTING_NONE, const int tabc = 0) const { + if (std::is_same::value) { + return this->get(formatting, tabc); + } + return T(this->get(formatting, tabc)); + } /** * @brief Get the tag of the element @@ -521,7 +544,12 @@ namespace docpp { * @brief Get the tag of the element in a specific type * @return T The tag of the element */ - template T get_tag() const; + template T get_tag() const { + if (std::is_same::value) { + return this->tag; + } + return T(this->tag); + } /** * @brief Get the data of the element @@ -532,7 +560,12 @@ namespace docpp { * @brief Get the data of the element in a specific type * @return T The data of the element */ - template T get_data() const; + template T get_data() const { + if (std::is_same::value) { + return this->data; + } + return T(this->data); + } Element operator=(const Element& element); void operator+=(const std::string& data); @@ -751,7 +784,12 @@ namespace docpp { * @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 Formatting formatting = FORMATTING_NONE, const int tabc = 0) const; + template T get(const Formatting formatting = FORMATTING_NONE, const int tabc = 0) const { + if (std::is_same::value) { + return this->get(formatting, tabc); + } + return T(this->get(formatting, tabc)); + } Section operator=(const Section& section); void operator+=(const Element& element); @@ -792,7 +830,12 @@ namespace docpp { * @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 Formatting formatting = FORMATTING_NONE, const int tabc = 0) const; + template T get(const Formatting formatting, const int tabc) const { + if (std::is_same::value) { + return this->get(formatting, tabc); + } + return T(this->get(formatting, tabc)); + } /** * @brief Get the section @@ -809,7 +852,12 @@ namespace docpp { * @brief Get the doctype of the document in a specific type * @return T The doctype of the document */ - template T get_doctype() const; + template T get_doctype() const { + if (std::is_same::value) { + return this->doctype; + } + return T(this->doctype); + } /** * @brief Set the document @@ -885,7 +933,12 @@ namespace docpp { * @brief Get the key of the property in a specific type * @return T The key of the property */ - template T get_key() const; + template T get_key() const { + if (std::is_same::value) { + return this->property.first; + } + return T(this->property.first); + } /** * @brief Get the value of the property * @return std::string The value of the property @@ -895,7 +948,12 @@ namespace docpp { * @brief Get the value of the property in a specific type * @return T The value of the property */ - template T get_value() const; + template T get_value() const { + if (std::is_same::value) { + return this->property.second; + } + return T(this->property.second); + } /** * @brief Get the property. * @return std::pair The value of the property @@ -905,7 +963,12 @@ namespace docpp { * @brief Get the property in a specific type. * @return std::pair The value of the property */ - template std::pair get() const; + template std::pair get() const { + if (std::is_same::value) { + return std::make_pair(this->property.first, this->property.second); + } + return std::pair(this->property.first, this->property.second); + } /** * @brief Set the key of the property. * @param key The key. @@ -1090,7 +1153,12 @@ namespace docpp { * @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 Formatting formatting = FORMATTING_NONE, const int tabc = 0) const; + template T get(const Formatting formatting, const int tabc) const { + if (std::is_same::value) { + return this->get(formatting, tabc); + } + return T(this->get(formatting, tabc)); + } /** * @brief Get the tag of the element * @return std::string The tag of the element @@ -1100,7 +1168,12 @@ namespace docpp { * @brief Get the tag of the element in a specific type * @return T The tag of the element */ - template T get_tag() const; + template T get_tag() const { + if (std::is_same::value) { + return this->element.first; + } + return T(this->element.first); + } /** * @brief Get the properties of the element * @return std::vector The properties of the element @@ -1265,7 +1338,12 @@ namespace docpp { * @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 Formatting formatting = FORMATTING_NONE, const int tabc = 0) const; + template T get(const Formatting formatting = FORMATTING_NONE, const int tabc = 0) const { + if (std::is_same::value) { + return this->get(formatting, tabc); + } + return T(this->get(formatting, tabc)); + } Stylesheet operator=(const Stylesheet& stylesheet); void operator+=(const Element& element); diff --git a/src/docpp.cpp b/src/docpp.cpp index 8930728..12c0b3a 100644 --- a/src/docpp.cpp +++ b/src/docpp.cpp @@ -9,43 +9,19 @@ #include #include #include -#include std::string docpp::HTML::Property::get_key() const { return this->property.first; } -template T docpp::HTML::Property::get_key() const { - if (std::is_same::value) { - return this->property.first; - } - - return T(this->property.first); -} - std::string docpp::HTML::Property::get_value() const { return this->property.second; } -template T docpp::HTML::Property::get_value() const { - if (std::is_same::value) { - return this->property.second; - } - return T(this->property.second); -} - std::pair docpp::HTML::Property::get() const { return this->property; } -template std::pair docpp::HTML::Property::get() const { - if (std::is_same::value) { - return this->property; - } - - return T(this->property); -} - void docpp::HTML::Property::set_key(const std::string& key) { this->property.first = key; } @@ -233,35 +209,14 @@ std::string docpp::HTML::Element::get(const Formatting formatting, const int tab return std::move(ret); } -template T docpp::HTML::Element::get(const Formatting formatting, const int tabc) const { - if (std::is_same::value) { - return this->get(formatting, tabc); - } - return T(this->get(formatting, tabc)); -} - std::string docpp::HTML::Element::get_tag() const { return this->tag; } -template T docpp::HTML::Element::get_tag() const { - if (std::is_same::value) { - return this->tag; - } - return T(this->tag); -} - std::string docpp::HTML::Element::get_data() const { return this->data; } -template T docpp::HTML::Element::get_data() const { - if (std::is_same::value) { - return this->data; - } - return T(this->data); -} - docpp::HTML::Section docpp::HTML::Section::operator=(const docpp::HTML::Section& section) { this->tag = section.tag; this->properties = section.properties; @@ -711,13 +666,6 @@ std::string docpp::HTML::Section::get(const Formatting formatting, const int tab return std::move(ret); } -template T docpp::HTML::Section::get(const Formatting formatting, const int tabc) const { - if (std::is_same::value) { - return this->get(formatting, tabc); - } - 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]); @@ -740,13 +688,6 @@ std::string docpp::HTML::Document::get(const Formatting formatting, const int ta return this->doctype + (formatting == FORMATTING_PRETTY ? "\n" : formatting == FORMATTING_NEWLINE ? "\n" : "") + this->document.get(formatting, tabc); } -template T docpp::HTML::Document::get(const Formatting formatting, const int tabc) const { - if (std::is_same::value) { - return this->get(formatting, tabc); - } - return T(this->get(formatting, tabc)); -} - docpp::HTML::Section& docpp::HTML::Document::get_section() { return this->document; } @@ -774,46 +715,18 @@ std::string docpp::HTML::Document::get_doctype() const { return this->doctype; } -template T docpp::HTML::Document::get_doctype() const { - if (std::is_same::value) { - return this->doctype; - } - return T(this->doctype); -} - std::string docpp::CSS::Property::get_key() const { return this->property.first; } -template T docpp::CSS::Property::get_key() const { - if (std::is_same::value) { - return this->property.first; - } - return T(this->property.first); -} - std::string docpp::CSS::Property::get_value() const { return this->property.second; } -template T docpp::CSS::Property::get_value() const { - if (std::is_same::value) { - return this->property.second; - } - return T(this->property.second); -} - std::pair docpp::CSS::Property::get() const { return this->property; } -template std::pair docpp::CSS::Property::get() const { - if (std::is_same::value) { - return std::make_pair(this->property.first, this->property.second); - } - return std::pair(this->property.first, this->property.second); -} - void docpp::CSS::Property::set_key(const std::string& key) { this->property.first = key; } @@ -975,24 +888,10 @@ std::string docpp::CSS::Element::get(const Formatting formatting, const int tabc return std::move(ret); } -template T docpp::CSS::Element::get(const Formatting formatting, const int tabc) const { - if (std::is_same::value) { - return this->get(formatting, tabc); - } - return T(this->get(formatting, tabc)); -} - std::string docpp::CSS::Element::get_tag() const { return this->element.first; } -template T docpp::CSS::Element::get_tag() const { - if (std::is_same::value) { - return this->element.first; - } - return T(this->element.first); -} - std::vector docpp::CSS::Element::get_properties() const { return this->element.second; } @@ -1103,10 +1002,3 @@ std::string docpp::CSS::Stylesheet::get(const Formatting formatting, const int t return std::move(ret); } - -template T docpp::CSS::Stylesheet::get(const Formatting formatting, const int tabc) const { - if (std::is_same::value) { - return this->get(formatting, tabc); - } - return T(this->get(formatting, tabc)); -} diff --git a/tests/test.cpp b/tests/test.cpp index dee981a..0bd6d43 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -1,5 +1,4 @@ #include -#include #include #include @@ -40,8 +39,8 @@ SCENARIO("Test HTML", "[HTML]") { const std::string expected_html{"Test Title

Test Header

Test Paragraph

Test Paragraph With ID

Test Paragraph In Div

Test Paragraph With ID And Class

"}; - REQUIRE(doc.get() == expected_html); - REQUIRE(doc.get(docpp::HTML::FORMATTING_NEWLINE) == "\n\n\nTest Title\n\n\n

Test Header

\n

Test Paragraph

\n

Test Paragraph With ID

\n
\n

Test Paragraph In Div

\n
\n

Test Paragraph With ID And Class

\n\n
\n
\n"); + REQUIRE(doc.get() == expected_html); + REQUIRE(doc.get(docpp::HTML::FORMATTING_NEWLINE) == "\n\n\nTest Title\n\n\n

Test Header

\n

Test Paragraph

\n

Test Paragraph With ID

\n
\n

Test Paragraph In Div

\n
\n

Test Paragraph With ID And Class

\n\n
\n
\n"); }; const auto test2 = []() { @@ -53,8 +52,8 @@ SCENARIO("Test HTML", "[HTML]") { section.erase(docpp::HTML::Element("p", {}, "Test 2")); - REQUIRE(section.get() == "

Test 1

Test 3

"); - REQUIRE(section.get(docpp::HTML::FORMATTING_NEWLINE) == "\n

Test 1

\n

Test 3

\n"); + REQUIRE(section.get() == "

Test 1

Test 3

"); + REQUIRE(section.get(docpp::HTML::FORMATTING_NEWLINE) == "\n

Test 1

\n

Test 3

\n"); }; const auto test3 = []() { @@ -67,8 +66,8 @@ SCENARIO("Test HTML", "[HTML]") { std::size_t pos = section.find(docpp::HTML::Element("p", {}, "Test 2")); section.insert(pos, docpp::HTML::Element("p", {}, "Test 2.5")); - REQUIRE(section.get() == "

Test 1

Test 2.5

Test 3

"); - REQUIRE(section.get(docpp::HTML::FORMATTING_NEWLINE) == "\n

Test 1

\n

Test 2.5

\n

Test 3

\n"); + REQUIRE(section.get() == "

Test 1

Test 2.5

Test 3

"); + REQUIRE(section.get(docpp::HTML::FORMATTING_NEWLINE) == "\n

Test 1

\n

Test 2.5

\n

Test 3

\n"); }; const auto test4 = []() { @@ -82,8 +81,8 @@ SCENARIO("Test HTML", "[HTML]") { section.erase(pos); - REQUIRE(section.get() == "

Test 1

Test 3

"); - REQUIRE(section.get(docpp::HTML::FORMATTING_NEWLINE) == "\n

Test 1

\n

Test 3

\n"); + REQUIRE(section.get() == "

Test 1

Test 3

"); + REQUIRE(section.get(docpp::HTML::FORMATTING_NEWLINE) == "\n

Test 1

\n

Test 3

\n"); }; const auto test5 = []() { @@ -102,8 +101,8 @@ SCENARIO("Test HTML", "[HTML]") { docpp::HTML::Document doc{}; doc.set(section); - REQUIRE(doc.get() == "

Test 1

Test 2

"); - REQUIRE(doc.get(docpp::HTML::FORMATTING_NEWLINE) == "\n\n
\n

Test 1

\n
\n

Test 2

\n
\n
\n"); + REQUIRE(doc.get() == "

Test 1

Test 2

"); + REQUIRE(doc.get(docpp::HTML::FORMATTING_NEWLINE) == "\n\n
\n

Test 1

\n
\n

Test 2

\n
\n
\n"); }; const auto test6 = []() { @@ -112,8 +111,8 @@ SCENARIO("Test HTML", "[HTML]") { css.push_back(element); - REQUIRE(css.get() == "p {color: red;font-size: 16px;font-family: Arial;}"); - REQUIRE(css.get(docpp::CSS::FORMATTING_NEWLINE) == "p {\ncolor: red;\nfont-size: 16px;\nfont-family: Arial;\n}\n"); + REQUIRE(css.get() == "p {color: red;font-size: 16px;font-family: Arial;}"); + REQUIRE(css.get(docpp::CSS::FORMATTING_NEWLINE) == "p {\ncolor: red;\nfont-size: 16px;\nfont-family: Arial;\n}\n"); }; const auto test7 = []() { @@ -124,7 +123,7 @@ SCENARIO("Test HTML", "[HTML]") { css.push_back(element); css.push_front(element2); - REQUIRE(css.get() == "div {color: blue;font-size: 12px;font-family: Arial;}p {color: red;font-size: 16px;font-family: Arial;}"); + REQUIRE(css.get() == "div {color: blue;font-size: 12px;font-family: Arial;}p {color: red;font-size: 16px;font-family: Arial;}"); }; const auto test8 = []() { @@ -137,7 +136,7 @@ SCENARIO("Test HTML", "[HTML]") { css.erase(css.find(element2)); - REQUIRE(css.get() == "p {color: red;font-size: 16px;font-family: Arial;}"); + REQUIRE(css.get() == "p {color: red;font-size: 16px;font-family: Arial;}"); }; const auto test9 = []() { @@ -153,11 +152,11 @@ SCENARIO("Test HTML", "[HTML]") { css.swap(css.find(element), css.find(element2)); - REQUIRE(css.get() == "p {color: red;font-size: 16px;font-family: Arial;}div {color: blue;font-size: 12px;font-family: Arial;}"); + REQUIRE(css.get() == "p {color: red;font-size: 16px;font-family: Arial;}div {color: blue;font-size: 12px;font-family: Arial;}"); element.push_front(docpp::CSS::Property("font-weight", "bold")); - REQUIRE(element.get() == "p {font-weight: bold;color: red;font-size: 16px;font-family: Arial;}"); + REQUIRE(element.get() == "p {font-weight: bold;color: red;font-size: 16px;font-family: Arial;}"); }; const auto test10 = []() { @@ -169,7 +168,7 @@ SCENARIO("Test HTML", "[HTML]") { section.swap(section.find(docpp::HTML::Element("p", {}, "Test 2")), section.find(docpp::HTML::Element("p", {}, "Test 3"))); - REQUIRE(section.get() == "

Test 1

Test 3

Test 2

"); + REQUIRE(section.get() == "

Test 1

Test 3

Test 2

"); }; const auto test11 = []() { @@ -181,7 +180,7 @@ SCENARIO("Test HTML", "[HTML]") { section.swap(docpp::HTML::Element("p", {}, "Test 2"), docpp::HTML::Element("p", {}, "Test 3")); - REQUIRE(section.get() == "

Test 1

Test 3

Test 2

"); + REQUIRE(section.get() == "

Test 1

Test 3

Test 2

"); }; const auto test12 = []() { @@ -193,7 +192,7 @@ SCENARIO("Test HTML", "[HTML]") { section.push_front(docpp::HTML::Element("p", {}, "Test 0")); - REQUIRE(section.get() == "

Test 0

Test 1

Test 2

Test 3

"); + REQUIRE(section.get() == "

Test 0

Test 1

Test 2

Test 3

"); }; const auto test13 = []() { @@ -215,7 +214,7 @@ SCENARIO("Test HTML", "[HTML]") { section.erase(pos); section.erase(pos2); - REQUIRE(section.get() == "

Test 1

Test 3

Test 4

Test 5

"); + REQUIRE(section.get() == "

Test 1

Test 3

Test 4

Test 5

"); }; const auto test14 = []() { @@ -231,11 +230,11 @@ SCENARIO("Test HTML", "[HTML]") { element.erase(red); - REQUIRE(element.get() == "p {font-size: 16px;font-family: Arial;}"); + REQUIRE(element.get() == "p {font-size: 16px;font-family: Arial;}"); element.insert(red, docpp::CSS::Property("color", "red")); - REQUIRE(element.get() == "p {color: red;font-size: 16px;font-family: Arial;}"); + REQUIRE(element.get() == "p {color: red;font-size: 16px;font-family: Arial;}"); }; const auto test15 = []() { @@ -244,7 +243,7 @@ SCENARIO("Test HTML", "[HTML]") { prop.push_back(docpp::HTML::Property(std::pair("id", "test_id"))); prop.push_back(docpp::HTML::Property(std::pair("class", "class1 class2 class3"))); - REQUIRE(docpp::HTML::Element("p", prop, {}).get() == "

"); + REQUIRE(docpp::HTML::Element("p", prop, {}).get() == "

"); const int pos = prop.find("class"); @@ -263,7 +262,7 @@ SCENARIO("Test HTML", "[HTML]") { prop.erase(pos); - REQUIRE(docpp::HTML::Element("p", prop, {}).get() == "

"); + REQUIRE(docpp::HTML::Element("p", prop, {}).get() == "

"); }; const auto test16 = []() { @@ -281,7 +280,7 @@ SCENARIO("Test HTML", "[HTML]") { doc.get_section() += docpp::HTML::Element("p", {}, "Test 7"); - REQUIRE(doc.get() == "

Test 4

Test 5

Test 6

Test 7

"); + REQUIRE(doc.get() == "

Test 4

Test 5

Test 6

Test 7

"); }; const auto test17 = []() { @@ -293,7 +292,7 @@ SCENARIO("Test HTML", "[HTML]") { docpp::HTML::Document doc{section}; - REQUIRE(doc.get(docpp::HTML::FORMATTING_PRETTY) == "\n\n\t

Test 1

\n\t

Test 2

\n\t

Test 3

\n"); + REQUIRE(doc.get(docpp::HTML::FORMATTING_PRETTY) == "\n\n\t

Test 1

\n\t

Test 2

\n\t

Test 3

\n"); }; const auto test18 = []() { @@ -303,7 +302,7 @@ SCENARIO("Test HTML", "[HTML]") { section.push_back(docpp::HTML::Element("p", {}, "Test 2")); section.push_back(docpp::HTML::Element("p", {}, "Test 3")); - REQUIRE(section.get() == "

Test 1

Test 2

Test 3

"); + REQUIRE(section.get() == "

Test 1

Test 2

Test 3

"); }; const auto test19 = []() { @@ -313,8 +312,8 @@ SCENARIO("Test HTML", "[HTML]") { section.push_back(docpp::HTML::Element("p", {}, "Test 2")); section.push_back(docpp::HTML::Element("p", {}, "Test 3")); - REQUIRE(section.front().get() == "

Test 1

"); - REQUIRE(section.back().get() == "

Test 3

"); + REQUIRE(section.front().get() == "

Test 1

"); + REQUIRE(section.back().get() == "

Test 3

"); }; const auto test20 = []() { @@ -345,13 +344,13 @@ SCENARIO("Test HTML", "[HTML]") { sect.push_back(docpp::HTML::Element("p", {}, "Test 3")); for (const docpp::HTML::Element& elem : sect) { - REQUIRE(elem.get() == "

Test 1

"); + REQUIRE(elem.get() == "

Test 1

"); break; } for (docpp::HTML::Section::iterator it = ++sect.begin(); it != sect.end(); ++it) { docpp::HTML::Element elem = *it; - REQUIRE(elem.get() == "

Test 2

"); + REQUIRE(elem.get() == "

Test 2

"); break; } }; @@ -380,7 +379,7 @@ SCENARIO("Test HTML", "[HTML]") { 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

"); + REQUIRE(sect.get() == "

Test 1

Test 2

Test 3

"); }; const auto test24 = []() {