Optimize constructors by initializing variables rather than overriding the

values using the appropriate set() methods.
This commit is contained in:
Jacob 2024-05-16 08:09:27 +02:00
parent 6d7cb9af86
commit d6db5690b3
2 changed files with 24 additions and 81 deletions

View file

@ -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<std::string, Type> The resolved tag
*/
std::pair<std::string, Type> 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<std::string, std::string>& property);
explicit Property(const std::pair<std::string, std::string>& 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<Property>& properties);
explicit Properties(const std::vector<Property>& 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 = "<!DOCTYPE html>");
Document(const Section& document, const std::string& doctype = "<!DOCTYPE html>") : 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<std::string, Type> The resolved tag
*/
std::pair<std::string, Type> 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<std::string, std::string>& property);
explicit Property(const std::pair<std::string, std::string>& 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<Property>& properties);
Element(const std::string& tag, const std::vector<Property>& properties) : element(std::make_pair(tag, properties)) {};
/**
* @brief Construct a new Element object
* @param element The element to set
*/
Element(const std::pair<std::string, std::vector<Property>>& element);
explicit Element(const std::pair<std::string, std::vector<Property>>& 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<Element>& elements);
explicit Stylesheet(const std::vector<Element>& elements) : elements(elements) {};
Stylesheet() = default;
/**

View file

@ -10,15 +10,6 @@
#include <unordered_map>
#include <vector>
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<std::string, std::string>& 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<docpp::HTML::Property>& 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<std::string, Type> 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 <typename T> 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 <typename T> 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<std::string, std::string>& 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<std::string
return *this;
}
docpp::CSS::Element::Element(const std::string& tag, const std::vector<Property>& properties) {
this->set(tag, properties);
}
docpp::CSS::Element::Element(const std::pair<std::string, std::vector<Property>>& 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::Property> docpp::CSS::Element::get_properties() const {
return this->element.second;
}
docpp::CSS::Stylesheet::Stylesheet(const std::vector<Element>& elements) {
this->set(elements);
}
void docpp::CSS::Stylesheet::set(const std::vector<Element>& elements) {
this->elements = elements;
}