From d6db5690b389fddb05825bd3628c55754f8b957c Mon Sep 17 00:00:00 2001 From: speedie Date: Thu, 16 May 2024 08:09:27 +0200 Subject: [PATCH] Optimize constructors by initializing variables rather than overriding the values using the appropriate set() methods. --- include/docpp.hpp | 47 +++++++++++++++++++------------------- src/docpp.cpp | 58 ----------------------------------------------- 2 files changed, 24 insertions(+), 81 deletions(-) diff --git a/include/docpp.hpp b/include/docpp.hpp index 48b87f3..808cb33 100644 --- a/include/docpp.hpp +++ b/include/docpp.hpp @@ -26,7 +26,7 @@ namespace docpp { return message; } out_of_range() = default; - out_of_range(const char* message) : message(message) {}; + explicit out_of_range(const char* message) : message(message) {}; }; /** @@ -40,7 +40,7 @@ namespace docpp { return message; } invalid_argument() = default; - invalid_argument(const char* message) : message(message) {}; + explicit invalid_argument(const char* message) : message(message) {}; }; /** @@ -217,6 +217,13 @@ namespace docpp { FORMATTING_NEWLINE, }; + /** + * @brief Resolve a tag to a string and type. + * @param tag The tag to resolve + * @return std::pair The resolved tag + */ + std::pair resolve_tag(const Tag tag); + /** * @brief A class to represent an HTML property */ @@ -237,12 +244,12 @@ namespace docpp { * @param key The key of the property * @param value The value of the property */ - Property(const std::string& key, const std::string& value); + Property(const std::string& key, const std::string& value) : property(std::make_pair(key, value)) {}; /** * @brief Construct a new Property object * @param property The property to set */ - Property(const std::pair& property); + explicit Property(const std::pair& property) : property(property) {}; Property() = default; /** @@ -432,12 +439,12 @@ namespace docpp { * @brief Construct a new Properties object * @param properties The properties to set */ - Properties(const std::vector& properties); + explicit Properties(const std::vector& properties); /** * @brief Construct a new Properties object * @param property The property to add */ - Properties(const Property& property); + explicit Properties(const Property& property) : properties({property}) {}; /** * @brief Construct a new Properties object */ @@ -470,15 +477,16 @@ namespace docpp { * @param tag The tag of the element * @param properties The properties of the element * @param data The data of the element + * @param type The close tag type. */ - Element(const std::string& tag, const Properties& properties = {}, const std::string& data = {}, const Type = TYPE_NON_SELF_CLOSING); + Element(const std::string& tag, const Properties& properties = {}, const std::string& data = {}, const Type type = TYPE_NON_SELF_CLOSING) : tag(tag), properties(properties), data(data), type(type) {}; /** * @brief Construct a new Element object * @param tag The tag of the element * @param properties The properties of the element * @param data The data of the element */ - Element(const Tag tag, const Properties& properties = {}, const std::string& data = {}); + Element(const Tag tag, const Properties& properties = {}, const std::string& data = {}) : tag(resolve_tag(tag).first), properties(properties), data(data), type(resolve_tag(tag).second) {}; /** * @brief Construct a new Element object */ @@ -678,13 +686,13 @@ namespace docpp { * @param tag The tag of the section * @param properties The properties of the section */ - Section(const std::string& tag, const Properties& properties = {}); + Section(const std::string& tag, const Properties& properties = {}) : tag(tag), properties(properties) {}; /** * @brief Construct a new Section object * @param tag The tag of the section * @param properties The properties of the section */ - Section(const Tag tag, const Properties& properties = {}); + Section(const Tag tag, const Properties& properties = {}) : tag(resolve_tag(tag).first), properties(properties) {}; /** * @brief Construct a new Section object */ @@ -819,18 +827,11 @@ namespace docpp { * @brief Construct a new Document object * @param document The document to set */ - Document(const Section& document, const std::string& doctype = ""); + Document(const Section& document, const std::string& doctype = "") : document(document), doctype(doctype) {}; Document operator=(const Document& document); Document operator=(const Section& section); }; - - /** - * @brief Resolve a tag to a string and type. - * @param tag The tag to resolve - * @return std::pair The resolved tag - */ - std::pair resolve_tag(const Tag tag); } // namespace HTML /** @@ -865,12 +866,12 @@ namespace docpp { * @param key The key of the property * @param value The value of the property */ - Property(const std::string& key, const std::string& value); + Property(const std::string& key, const std::string& value) : property(std::make_pair(key, value)) {}; /** * @brief Construct a new Property object * @param property The property to set */ - Property(const std::pair& property); + explicit Property(const std::pair& property) : property(property) {}; Property() = default; /** @@ -994,12 +995,12 @@ namespace docpp { * @param tag The tag of the element * @param properties The properties of the element */ - Element(const std::string& tag, const std::vector& properties); + Element(const std::string& tag, const std::vector& properties) : element(std::make_pair(tag, properties)) {}; /** * @brief Construct a new Element object * @param element The element to set */ - Element(const std::pair>& element); + explicit Element(const std::pair>& element) : element(element) {}; Element() = default; /** @@ -1174,7 +1175,7 @@ namespace docpp { * @brief Construct a new Stylesheet object * @param elements The elements to set */ - Stylesheet(const std::vector& elements); + explicit Stylesheet(const std::vector& elements) : elements(elements) {}; Stylesheet() = default; /** diff --git a/src/docpp.cpp b/src/docpp.cpp index 705835c..132d2f3 100644 --- a/src/docpp.cpp +++ b/src/docpp.cpp @@ -10,15 +10,6 @@ #include #include -docpp::HTML::Property::Property(const std::string& key, const std::string& value) { - this->set_key(key); - this->set_value(value); -} - -docpp::HTML::Property::Property(const std::pair& property) { - this->set(property); -} - std::string docpp::HTML::Property::get_key() const { return this->property.first; } @@ -59,10 +50,6 @@ docpp::HTML::Properties::Properties(const std::vector& pr this->set(properties); } -docpp::HTML::Properties::Properties(const docpp::HTML::Property& property) { - this->push_back(property); -} - docpp::HTML::Properties docpp::HTML::Properties::operator=(const docpp::HTML::Property& property) { this->properties = {property}; return *this; @@ -176,17 +163,6 @@ void docpp::HTML::Properties::swap(const docpp::HTML::Property& property1, const this->swap(this->find(property1), this->find(property2)); } -docpp::HTML::Element::Element(const std::string& tag, const Properties& properties, const std::string& data, const Type type) { - this->set(tag, properties, data, type); -} - -docpp::HTML::Element::Element(const Tag tag, const Properties& properties, const std::string& data) { - std::pair map = resolve_tag(tag); - - this->tag = map.first; - this->type = map.second; - this->properties = properties; -} docpp::HTML::Element docpp::HTML::Element::operator=(const docpp::HTML::Element& element) { this->set(element.get_tag(), element.properties, element.get_data(), element.type); @@ -269,11 +245,6 @@ template T docpp::HTML::Element::get_data() const { return T(this->data); } -docpp::HTML::Section::Section(const std::string& tag, const Properties& properties) { - this->tag = tag; - this->properties = properties; -} - docpp::HTML::Section docpp::HTML::Section::operator=(const docpp::HTML::Section& section) { this->tag = section.tag; this->properties = section.properties; @@ -296,10 +267,6 @@ docpp::HTML::Element docpp::HTML::Section::operator[](const int& index) const { return this->at(index); } -docpp::HTML::Section::Section(const Tag tag, const Properties& properties) { - this->set(tag, properties); -} - void docpp::HTML::Section::set(const std::string& tag, const Properties& properties) { this->tag = tag; this->properties = properties; @@ -781,11 +748,6 @@ docpp::HTML::Document docpp::HTML::Document::operator=(const docpp::HTML::Sectio return *this; } -docpp::HTML::Document::Document(const docpp::HTML::Section& document, const std::string& doctype) { - this->set(document); - this->set_doctype(doctype); -} - std::string docpp::HTML::Document::get_doctype() const { return this->doctype; } @@ -794,14 +756,6 @@ template T docpp::HTML::Document::get_doctype() const { return T(this->doctype); } -docpp::CSS::Property::Property(const std::string& key, const std::string& value) { - this->set(key, value); -} - -docpp::CSS::Property::Property(const std::pair& property) { - this->set(property); -} - std::string docpp::CSS::Property::get_key() const { return this->property.first; } @@ -852,14 +806,6 @@ docpp::CSS::Property docpp::CSS::Property::operator=(const std::pair& properties) { - this->set(tag, properties); -} - -docpp::CSS::Element::Element(const std::pair>& element) { - this->set(element); -} - docpp::CSS::Element docpp::CSS::Element::operator=(const docpp::CSS::Element& element) { this->set({element.get_tag(), element.get_properties()}); return *this; @@ -1011,10 +957,6 @@ std::vector docpp::CSS::Element::get_properties() const { return this->element.second; } -docpp::CSS::Stylesheet::Stylesheet(const std::vector& elements) { - this->set(elements); -} - void docpp::CSS::Stylesheet::set(const std::vector& elements) { this->elements = elements; }