Implement operator overloading for several classes.

This commit is contained in:
Jacob 2024-05-07 00:08:54 +02:00
parent aca33e80d5
commit 804309d323
3 changed files with 169 additions and 2 deletions

View file

@ -186,6 +186,10 @@ namespace docpp {
* @brief Construct a new HTMLElementProperties object
*/
HTMLElementProperties() = default;
HTMLElementProperties operator=(const HTMLElementProperties& properties);
HTMLElementProperties operator=(const std::vector<HTMLProperty>& properties);
HTMLElementProperties operator=(const HTMLProperty& property);
void operator+=(const HTMLProperty& property);
};
/**
@ -240,6 +244,8 @@ namespace docpp {
* @return std::string The data of the element
*/
std::string getData() const;
HTMLElement operator=(const HTMLElement& element);
void operator+=(const std::string& data);
};
/**
@ -408,6 +414,10 @@ namespace docpp {
*/
std::string get(const int formatting = FORMATTING_NONE) const;
HTMLSection operator=(const HTMLSection& section);
void operator+=(const HTMLElement& element);
void operator+=(const HTMLSection& section);
HTMLElement operator[](const int& index) const;
};
/**
@ -431,6 +441,12 @@ namespace docpp {
*/
std::string get(const int formatting = FORMATTING_NONE) const;
/**
* @brief Get the section
* @return HTMLSection The section
*/
HTMLSection& getSection();
/**
* @brief Get the doctype of the document
* @return std::string The doctype of the document
@ -456,6 +472,9 @@ namespace docpp {
* @param document The document to set
*/
HTMLDocument(const HTMLSection& document, const std::string& doctype = "<!DOCTYPE html>");
HTMLDocument operator=(const HTMLDocument& document);
HTMLDocument operator=(const HTMLSection& section);
};
} // namespace HTML
@ -524,6 +543,9 @@ namespace docpp {
* @param value The value of the property
*/
void set(const std::string& key, const std::string& value);
CSSProperty operator=(const CSSProperty& property);
CSSProperty operator=(const std::pair<std::string, std::string>& property);
};
class CSSElement {
@ -630,6 +652,11 @@ namespace docpp {
* @return std::vector<CSSProperty> The properties of the element
*/
std::vector<CSSProperty> getProperties() const;
CSSElement operator=(const CSSElement& element);
CSSElement operator=(const std::pair<std::string, std::vector<CSSProperty>>& element);
void operator+=(const CSSProperty& property);
CSSProperty operator[](const int& index) const;
};
/**
@ -723,6 +750,10 @@ namespace docpp {
* @return std::string The stylesheet
*/
std::string get(const int formatting = FORMATTING_NONE) const;
CSSStylesheet operator=(const CSSStylesheet& stylesheet);
void operator+=(const CSSElement& element);
CSSElement operator[](const int& index) const;
};
}
}

View file

@ -59,6 +59,25 @@ docpp::HTML::HTMLElementProperties::HTMLElementProperties(const docpp::HTML::HTM
this->push_back(property);
}
docpp::HTML::HTMLElementProperties docpp::HTML::HTMLElementProperties::operator=(const docpp::HTML::HTMLProperty& property) {
this->properties = {property};
return *this;
}
docpp::HTML::HTMLElementProperties docpp::HTML::HTMLElementProperties::operator=(const docpp::HTML::HTMLElementProperties& properties) {
this->set(properties.getProperties());
return *this;
}
docpp::HTML::HTMLElementProperties docpp::HTML::HTMLElementProperties::operator=(const std::vector<docpp::HTML::HTMLProperty>& properties) {
this->set(properties);
return *this;
}
void docpp::HTML::HTMLElementProperties::operator+=(const docpp::HTML::HTMLProperty& property) {
this->push_back(property);
}
std::vector<docpp::HTML::HTMLProperty> docpp::HTML::HTMLElementProperties::getProperties() const {
return this->properties;
}
@ -149,6 +168,15 @@ docpp::HTML::HTMLElement::HTMLElement(const std::string& tag, const HTMLElementP
this->set(tag, properties, data, type);
}
docpp::HTML::HTMLElement docpp::HTML::HTMLElement::operator=(const docpp::HTML::HTMLElement& element) {
this->set(element.getTag(), element.properties, element.getData(), element.type);
return *this;
}
void docpp::HTML::HTMLElement::operator+=(const std::string& data) {
this->data += data;
}
void docpp::HTML::HTMLElement::set(const std::string& tag, const HTMLElementProperties& properties, const std::string& data, const int type) {
this->tag = tag;
this->data = data;
@ -198,6 +226,28 @@ docpp::HTML::HTMLSection::HTMLSection(const std::string& tag, const HTMLElementP
this->properties = properties;
}
docpp::HTML::HTMLSection docpp::HTML::HTMLSection::operator=(const docpp::HTML::HTMLSection& section) {
this->tag = section.tag;
this->properties = section.properties;
this->elements = section.elements;
this->sections = section.sections;
this->index = section.index;
return *this;
}
void docpp::HTML::HTMLSection::operator+=(const docpp::HTML::HTMLElement& element) {
this->push_back(element);
}
void docpp::HTML::HTMLSection::operator+=(const docpp::HTML::HTMLSection& section) {
this->push_back(section);
}
docpp::HTML::HTMLElement docpp::HTML::HTMLSection::operator[](const int& index) const {
return this->at(index);
}
docpp::HTML::HTMLSection::HTMLSection(const int tag, const HTMLElementProperties& properties) {
if (tag == docpp::HTML::SECTION_DIV) {
this->tag = "div";
@ -463,6 +513,10 @@ std::string docpp::HTML::HTMLDocument::get(const int formatting) const {
return this->doctype + (formatting == FORMATTING_PRETTY ? "\n" : "") + this->document.get(formatting);
}
docpp::HTML::HTMLSection& docpp::HTML::HTMLDocument::getSection() {
return this->document;
}
void docpp::HTML::HTMLDocument::set(const docpp::HTML::HTMLSection& document) {
this->document = document;
}
@ -471,6 +525,17 @@ void docpp::HTML::HTMLDocument::setDoctype(const std::string& doctype) {
this->doctype = doctype;
}
docpp::HTML::HTMLDocument docpp::HTML::HTMLDocument::operator=(const docpp::HTML::HTMLDocument& document) {
this->set(document.get());
this->setDoctype(document.getDoctype());
return *this;
}
docpp::HTML::HTMLDocument docpp::HTML::HTMLDocument::operator=(const docpp::HTML::HTMLSection& section) {
this->set(section);
return *this;
}
docpp::HTML::HTMLDocument::HTMLDocument(const docpp::HTML::HTMLSection& document, const std::string& doctype) {
this->set(document);
this->setDoctype(doctype);
@ -516,6 +581,16 @@ void docpp::CSS::CSSProperty::set(const std::string& key, const std::string& val
this->property = std::make_pair(key, value);
}
docpp::CSS::CSSProperty docpp::CSS::CSSProperty::operator=(const docpp::CSS::CSSProperty& property) {
this->set(property.get());
return *this;
}
docpp::CSS::CSSProperty docpp::CSS::CSSProperty::operator=(const std::pair<std::string, std::string>& property) {
this->set(property);
return *this;
}
docpp::CSS::CSSElement::CSSElement(const std::string& tag, const std::vector<CSSProperty>& properties) {
this->set(tag, properties);
}
@ -524,6 +599,19 @@ docpp::CSS::CSSElement::CSSElement(const std::pair<std::string, std::vector<CSSP
this->set(element);
}
docpp::CSS::CSSElement docpp::CSS::CSSElement::operator=(const docpp::CSS::CSSElement& element) {
this->set({element.getTag(), element.getProperties()});
return *this;
}
void docpp::CSS::CSSElement::operator+=(const CSSProperty& property) {
this->push_back(property);
}
docpp::CSS::CSSProperty docpp::CSS::CSSElement::operator[](const int& index) const {
return this->at(index);
}
void docpp::CSS::CSSElement::set(const std::string& tag, const std::vector<CSSProperty>& properties) {
this->element.first = tag;
this->element.second = properties;
@ -672,6 +760,19 @@ void docpp::CSS::CSSStylesheet::erase(const int index) {
this->elements.erase(this->elements.begin() + index);
}
docpp::CSS::CSSStylesheet docpp::CSS::CSSStylesheet::operator=(const docpp::CSS::CSSStylesheet& stylesheet) {
this->set(stylesheet.getElements());
return *this;
}
void docpp::CSS::CSSStylesheet::operator+=(const CSSElement& element) {
this->push_back(element);
}
docpp::CSS::CSSElement docpp::CSS::CSSStylesheet::operator[](const int& index) const {
return this->at(index);
}
docpp::CSS::CSSElement docpp::CSS::CSSStylesheet::at(const int index) const {
if (index < 0 || index >= this->elements.size()) {
throw std::out_of_range("Index out of range");

View file

@ -4,7 +4,7 @@
#include <catch2/catch_test_macros.hpp>
/**
* @brief Test cases for the DuvaHTML namespace.
* @brief Test cases for the docpp namespace.
*/
SCENARIO("Test HTML", "[HTML]") {
auto test1 = []() {
@ -266,7 +266,42 @@ SCENARIO("Test HTML", "[HTML]") {
REQUIRE(docpp::HTML::HTMLElement("p", prop, {}).get() == "<p id=\"test_id\"></p>");
};
std::vector<void (*)()> tests{test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12, test13, test14, test15};
auto test16 = []() {
docpp::HTML::HTMLDocument doc = docpp::HTML::HTMLSection({});
doc.getSection().push_back(docpp::HTML::HTMLElement("p", {}, "Test 1"));
doc.getSection().push_back(docpp::HTML::HTMLElement("p", {}, "Test 2"));
doc.getSection().push_back(docpp::HTML::HTMLElement("p", {}, "Test 3"));
doc.getSection() = docpp::HTML::HTMLSection(docpp::HTML::SECTION_HTML, {});
doc.getSection().push_back(docpp::HTML::HTMLElement("p", {}, "Test 4"));
doc.getSection().push_back(docpp::HTML::HTMLElement("p", {}, "Test 5"));
doc.getSection().push_back(docpp::HTML::HTMLElement("p", {}, "Test 6"));
doc.getSection() += docpp::HTML::HTMLElement("p", {}, "Test 7");
REQUIRE(doc.get() == "<!DOCTYPE html><html><p>Test 4</p><p>Test 5</p><p>Test 6</p><p>Test 7</p></html>");
};
std::vector<void (*)()> tests{
test1,
test2,
test3,
test4,
test5,
test6,
test7,
test8,
test9,
test10,
test11,
test12,
test13,
test14,
test15,
test16,
};
for (const auto& test : tests) {
test();