Name the enums, and use them in the function declarations.

Old variant was a little bit too much "C with classes" for my taste.
This commit is contained in:
Jacob 2024-05-15 10:57:47 +02:00
parent d7107de41b
commit fa107fcb64
2 changed files with 53 additions and 35 deletions

View file

@ -47,7 +47,10 @@ namespace docpp {
* @brief A namespace to represent HTML elements and documents * @brief A namespace to represent HTML elements and documents
*/ */
namespace HTML { namespace HTML {
enum { /**
* @brief Enum for element tags.
*/
enum Tag {
ELEMENT_EMPTY, ELEMENT_EMPTY,
ELEMENT_ABBREVIATION, ELEMENT_ABBREVIATION,
ELEMENT_ABBR, ELEMENT_ABBR,
@ -190,11 +193,23 @@ namespace docpp {
ELEMENT_VIDEO, ELEMENT_VIDEO,
ELEMENT_WBR, ELEMENT_WBR,
ELEMENT_XMP, ELEMENT_XMP,
};
/**
* @brief Enum for element types.
*/
enum Type {
TYPE_SELF_CLOSING, TYPE_SELF_CLOSING,
TYPE_NON_SELF_CLOSING, TYPE_NON_SELF_CLOSING,
TYPE_NON_CLOSED, TYPE_NON_CLOSED,
TYPE_TEXT, TYPE_TEXT,
TYPE_TEXT_TAB, TYPE_TEXT_TAB,
};
/**
* @brief Enum for formatting options.
*/
enum Formatting {
FORMATTING_NONE, FORMATTING_NONE,
FORMATTING_PRETTY, FORMATTING_PRETTY,
FORMATTING_NEWLINE, FORMATTING_NEWLINE,
@ -438,7 +453,7 @@ namespace docpp {
private: private:
std::string tag{}; std::string tag{};
std::string data{}; std::string data{};
int type{TYPE_NON_SELF_CLOSING}; Type type{TYPE_NON_SELF_CLOSING};
Properties properties{}; Properties properties{};
protected: protected:
public: public:
@ -454,14 +469,14 @@ namespace docpp {
* @param properties The properties of the element * @param properties The properties of the element
* @param data The data of the element * @param data The data of the element
*/ */
Element(const std::string& tag, const Properties& properties = {}, const std::string& data = {}, const int type = TYPE_NON_SELF_CLOSING); Element(const std::string& tag, const Properties& properties = {}, const std::string& data = {}, const Type = TYPE_NON_SELF_CLOSING);
/** /**
* @brief Construct a new Element object * @brief Construct a new Element object
* @param tag The tag of the element * @param tag The tag of the element
* @param properties The properties of the element * @param properties The properties of the element
* @param data The data of the element * @param data The data of the element
*/ */
Element(const int tag, const Properties& properties = {}, const std::string& data = {}, const int type = TYPE_NON_SELF_CLOSING); Element(const Tag tag, const Properties& properties = {}, const std::string& data = {}, const Type = TYPE_NON_SELF_CLOSING);
/** /**
* @brief Construct a new Element object * @brief Construct a new Element object
*/ */
@ -472,18 +487,18 @@ namespace docpp {
* @param id The id of the element * @param id The id of the element
* @param classes The classes of the element * @param classes The classes of the element
*/ */
void set(const std::string& tag, const Properties& properties = {}, const std::string& data = {}, const int type = TYPE_NON_SELF_CLOSING); void set(const std::string& tag, const Properties& properties = {}, const std::string& data = {}, const Type = TYPE_NON_SELF_CLOSING);
/** /**
* @brief Get the element in the form of an HTML tag. * @brief Get the element in the form of an HTML tag.
* @return std::string The tag of the element * @return std::string The tag of the element
*/ */
std::string get(const int formatting = FORMATTING_NONE, const int tabc = 0) const; std::string get(const Formatting formatting = FORMATTING_NONE, const int tabc = 0) const;
/** /**
* @brief Get the element in the form of a specific type. * @brief Get the element in the form of a specific type.
* @return T The element in the form of a specific type * @return T The element in the form of a specific type
*/ */
template <typename T> T get(const int formatting = FORMATTING_NONE, const int tabc = 0) const; template <typename T> T get(const Formatting formatting = FORMATTING_NONE, const int tabc = 0) const;
/** /**
* @brief Get the tag of the element * @brief Get the tag of the element
@ -667,7 +682,7 @@ namespace docpp {
* @param tag The tag of the section * @param tag The tag of the section
* @param properties The properties of the section * @param properties The properties of the section
*/ */
Section(const int tag, const Properties& properties = {}); Section(const Tag tag, const Properties& properties = {});
/** /**
* @brief Construct a new Section object * @brief Construct a new Section object
*/ */
@ -685,7 +700,7 @@ namespace docpp {
* @param id The id of the section * @param id The id of the section
* @param classes The classes of the section * @param classes The classes of the section
*/ */
void set(const int tag, const Properties& properties = {}); void set(const Tag tag, const Properties& properties = {});
/** /**
* @brief Swap two elements in the section * @brief Swap two elements in the section
* @param index1 The index of the first element * @param index1 The index of the first element
@ -719,12 +734,12 @@ namespace docpp {
* @brief Dump the entire section. * @brief Dump the entire section.
* @return std::string The section * @return std::string The section
*/ */
std::string get(const int formatting = FORMATTING_NONE, const int tabc = 0) const; std::string get(const Formatting formatting = FORMATTING_NONE, const int tabc = 0) const;
/** /**
* @brief Get the element in the form of a specific type. * @brief Get the element in the form of a specific type.
* @return T The element in the form of a specific type * @return T The element in the form of a specific type
*/ */
template <typename T> T get(const int formatting = FORMATTING_NONE, const int tabc = 0) const; template <typename T> T get(const Formatting formatting = FORMATTING_NONE, const int tabc = 0) const;
Section operator=(const Section& section); Section operator=(const Section& section);
void operator+=(const Element& element); void operator+=(const Element& element);
@ -760,12 +775,12 @@ namespace docpp {
* @param std::string The type to return * @param std::string The type to return
* @return std::string The document * @return std::string The document
*/ */
std::string get(const int formatting = FORMATTING_NONE, const int tabc = 0) const; std::string get(const Formatting formatting = FORMATTING_NONE, const int tabc = 0) const;
/** /**
* @brief Get the document in the form of a specific type. * @brief Get the document in the form of a specific type.
* @return T The document in the form of a specific type * @return T The document in the form of a specific type
*/ */
template <typename T> T get(const int formatting = FORMATTING_NONE, const int tabc = 0) const; template <typename T> T get(const Formatting formatting = FORMATTING_NONE, const int tabc = 0) const;
/** /**
* @brief Get the section * @brief Get the section
@ -812,14 +827,17 @@ namespace docpp {
* @brief Resolve a tag. * @brief Resolve a tag.
* @param tag The tag to resolve * @param tag The tag to resolve
*/ */
template <typename T> T resolve_tag(const int tag); template <typename T> T resolve_tag(const Tag tag);
} // namespace HTML } // namespace HTML
/** /**
* @brief A namespace to represent CSS elements and documents * @brief A namespace to represent CSS elements and documents
*/ */
namespace CSS { namespace CSS {
enum { /**
* @brief Enum for formatting options.
*/
enum Formatting {
FORMATTING_NONE, FORMATTING_NONE,
FORMATTING_PRETTY, FORMATTING_PRETTY,
FORMATTING_NEWLINE, FORMATTING_NEWLINE,
@ -1061,12 +1079,12 @@ namespace docpp {
* @brief Get the element * @brief Get the element
* @return std::pair<std::string, std::vector<Property>> The element * @return std::pair<std::string, std::vector<Property>> The element
*/ */
std::string get(const int formatting = FORMATTING_NONE, const int tabc = 0) const; std::string get(const Formatting formatting = FORMATTING_NONE, const int tabc = 0) const;
/** /**
* @brief Get the element in the form of a specific type. * @brief Get the element in the form of a specific type.
* @return T The element in the form of a specific type * @return T The element in the form of a specific type
*/ */
template <typename T> T get(const int formatting = FORMATTING_NONE, const int tabc = 0) const; template <typename T> T get(const Formatting formatting = FORMATTING_NONE, const int tabc = 0) const;
/** /**
* @brief Get the tag of the element * @brief Get the tag of the element
* @return std::string The tag of the element * @return std::string The tag of the element
@ -1236,12 +1254,12 @@ namespace docpp {
* @brief Get the stylesheet * @brief Get the stylesheet
* @return std::string The stylesheet * @return std::string The stylesheet
*/ */
std::string get(const int formatting = FORMATTING_NONE, const int tabc = 0) const; std::string get(const Formatting formatting = FORMATTING_NONE, const int tabc = 0) const;
/** /**
* @brief Get the stylesheet in the form of a specific type. * @brief Get the stylesheet in the form of a specific type.
* @return T The stylesheet in the form of a specific type * @return T The stylesheet in the form of a specific type
*/ */
template <typename T> T get(const int formatting = FORMATTING_NONE, const int tabc = 0) const; template <typename T> T get(const Formatting formatting = FORMATTING_NONE, const int tabc = 0) const;
Stylesheet operator=(const Stylesheet& stylesheet); Stylesheet operator=(const Stylesheet& stylesheet);
void operator+=(const Element& element); void operator+=(const Element& element);

View file

@ -175,11 +175,11 @@ void docpp::HTML::Properties::swap(const docpp::HTML::Property& property1, const
this->swap(this->find(property1), this->find(property2)); this->swap(this->find(property1), this->find(property2));
} }
docpp::HTML::Element::Element(const std::string& tag, const Properties& properties, const std::string& data, const int type) { docpp::HTML::Element::Element(const std::string& tag, const Properties& properties, const std::string& data, const Type type) {
this->set(tag, properties, data, type); this->set(tag, properties, data, type);
} }
docpp::HTML::Element::Element(const int tag, const Properties& properties, const std::string& data, const int type) { docpp::HTML::Element::Element(const Tag tag, const Properties& properties, const std::string& data, const Type type) {
this->set(resolve_tag<std::string>(tag), properties, data, type); this->set(resolve_tag<std::string>(tag), properties, data, type);
} }
@ -192,14 +192,14 @@ void docpp::HTML::Element::operator+=(const std::string& data) {
this->data += data; this->data += data;
} }
void docpp::HTML::Element::set(const std::string& tag, const Properties& properties, const std::string& data, const int type) { void docpp::HTML::Element::set(const std::string& tag, const Properties& properties, const std::string& data, const Type type) {
this->tag = tag; this->tag = tag;
this->data = data; this->data = data;
this->properties = properties; this->properties = properties;
this->type = type; this->type = type;
} }
std::string docpp::HTML::Element::get(const int formatting, const int tabc) const { std::string docpp::HTML::Element::get(const Formatting formatting, const int tabc) const {
std::string ret{}; std::string ret{};
if (this->type == docpp::HTML::TYPE_TEXT) { if (this->type == docpp::HTML::TYPE_TEXT) {
@ -244,7 +244,7 @@ std::string docpp::HTML::Element::get(const int formatting, const int tabc) cons
return std::move(ret); return std::move(ret);
} }
template <typename T> T docpp::HTML::Element::get(const int formatting, const int tabc) const { template <typename T> T docpp::HTML::Element::get(const Formatting formatting, const int tabc) const {
return T(this->get(formatting, tabc)); return T(this->get(formatting, tabc));
} }
@ -291,7 +291,7 @@ docpp::HTML::Element docpp::HTML::Section::operator[](const int& index) const {
return this->at(index); return this->at(index);
} }
docpp::HTML::Section::Section(const int tag, const Properties& properties) { docpp::HTML::Section::Section(const Tag tag, const Properties& properties) {
this->set(tag, properties); this->set(tag, properties);
} }
@ -300,7 +300,7 @@ void docpp::HTML::Section::set(const std::string& tag, const Properties& propert
this->properties = properties; this->properties = properties;
} }
template <typename T> T docpp::HTML::resolve_tag(const int tag) { template <typename T> T docpp::HTML::resolve_tag(const Tag tag) {
switch (tag) { switch (tag) {
case ELEMENT_EMPTY: case ELEMENT_EMPTY:
return ""; return "";
@ -590,7 +590,7 @@ template <typename T> T docpp::HTML::resolve_tag(const int tag) {
return ""; return "";
} }
void docpp::HTML::Section::set(const int tag, const Properties& properties) { void docpp::HTML::Section::set(const Tag tag, const Properties& properties) {
this->tag = resolve_tag<std::string>(tag); this->tag = resolve_tag<std::string>(tag);
this->properties = properties; this->properties = properties;
} }
@ -798,7 +798,7 @@ std::vector<docpp::HTML::Section> docpp::HTML::Section::get_sections() const {
return std::move(ret); return std::move(ret);
} }
std::string docpp::HTML::Section::get(const int formatting, const int tabc) const { std::string docpp::HTML::Section::get(const Formatting formatting, const int tabc) const {
std::string ret{}; std::string ret{};
int tabcount{tabc}; int tabcount{tabc};
@ -856,7 +856,7 @@ std::string docpp::HTML::Section::get(const int formatting, const int tabc) cons
return std::move(ret); return std::move(ret);
} }
template <typename T> T docpp::HTML::Section::get(const int formatting, const int tabc) const { template <typename T> T docpp::HTML::Section::get(const Formatting formatting, const int tabc) const {
return T(this->get(formatting, tabc)); return T(this->get(formatting, tabc));
} }
@ -878,11 +878,11 @@ void docpp::HTML::Section::swap(const Section& section1, const Section& section2
this->swap(this->find(section1), this->find(section2)); this->swap(this->find(section1), this->find(section2));
} }
std::string docpp::HTML::Document::get(const int formatting, const int tabc) const { std::string docpp::HTML::Document::get(const Formatting formatting, const int tabc) const {
return this->doctype + (formatting == FORMATTING_PRETTY ? "\n" : formatting == FORMATTING_NEWLINE ? "\n" : "") + this->document.get(formatting, tabc); return this->doctype + (formatting == FORMATTING_PRETTY ? "\n" : formatting == FORMATTING_NEWLINE ? "\n" : "") + this->document.get(formatting, tabc);
} }
template <typename T> T docpp::HTML::Document::get(const int formatting, const int tabc) const { template <typename T> T docpp::HTML::Document::get(const Formatting formatting, const int tabc) const {
return T(this->get(formatting, tabc)); return T(this->get(formatting, tabc));
} }
@ -1086,7 +1086,7 @@ void docpp::CSS::Element::swap(const Property& property1, const Property& proper
this->swap(this->find(property1), this->find(property2)); this->swap(this->find(property1), this->find(property2));
} }
std::string docpp::CSS::Element::get(const int formatting, const int tabc) const { std::string docpp::CSS::Element::get(const Formatting formatting, const int tabc) const {
std::string ret{}; std::string ret{};
if (this->element.first.compare("")) { if (this->element.first.compare("")) {
@ -1123,7 +1123,7 @@ std::string docpp::CSS::Element::get(const int formatting, const int tabc) const
return std::move(ret); return std::move(ret);
} }
template <typename T> T docpp::CSS::Element::get(const int formatting, const int tabc) const { template <typename T> T docpp::CSS::Element::get(const Formatting formatting, const int tabc) const {
return T(this->get(formatting, tabc)); return T(this->get(formatting, tabc));
} }
@ -1240,7 +1240,7 @@ std::vector<docpp::CSS::Element> docpp::CSS::Stylesheet::get_elements() const {
return this->elements; return this->elements;
} }
std::string docpp::CSS::Stylesheet::get(const int formatting, const int tabc) const { std::string docpp::CSS::Stylesheet::get(const Formatting formatting, const int tabc) const {
std::string ret{}; std::string ret{};
for (const Element& it : this->elements) { for (const Element& it : this->elements) {
@ -1250,6 +1250,6 @@ std::string docpp::CSS::Stylesheet::get(const int formatting, const int tabc) co
return std::move(ret); return std::move(ret);
} }
template <typename T> T docpp::CSS::Stylesheet::get(const int formatting, const int tabc) const { template <typename T> T docpp::CSS::Stylesheet::get(const Formatting formatting, const int tabc) const {
return T(this->get(formatting, tabc)); return T(this->get(formatting, tabc));
} }