(FEATURE): Implemented operator overloading. #33

Merged
shultzp1 merged 1 commits from feature/overloading into main 2025-10-31 19:23:58 -07:00
4 changed files with 27 additions and 12 deletions

View File

@ -19,3 +19,8 @@ void Node::Inspect(int indent) {
child->Inspect(indent + 1); child->Inspect(indent + 1);
} }
} }
std::ostream &operator<<(std::ostream &os, Node const &node) {
os << node.ToHtml();
return os;
}

View File

@ -19,12 +19,23 @@
/// Reference: https://www.youtube.com/watch?v=AmjoK55h68Y&t=166s /// Reference: https://www.youtube.com/watch?v=AmjoK55h68Y&t=166s
class Node { class Node {
/**
* @brief Override the '<<' operator to write the content of the node
* directly.
*
* This overridden operator will simply write the result of this->ToHtml to
* the stream.
*
* @author Hayden Hargreaves (hhargreaves2006@gmail.com)
*/
friend std::ostream &operator<<(std::ostream &os, Node const &node);
protected: protected:
/** /**
* @brief List of children nodes. * @brief List of children nodes.
* *
* Most nodes will not have children, but some may, therefore this class must * Most nodes will not have children, but some may, therefore this class
* have it. * must have it.
* *
* @author Hayden Hargreaves (hhargreaves2006@gmail.com) * @author Hayden Hargreaves (hhargreaves2006@gmail.com)
*/ */
@ -58,8 +69,8 @@ public:
/** /**
* @brief Return a read-only (const) list of children. * @brief Return a read-only (const) list of children.
* *
* Return our list of unique ptrs, they are const and therefore only have read * Return our list of unique ptrs, they are const and therefore only have
* access. * read access.
* *
* @author Hayden Hargreaves (hhargreaves2006@gmail.com) * @author Hayden Hargreaves (hhargreaves2006@gmail.com)
*/ */

View File

@ -15,7 +15,7 @@ string DocumentNode::ToHtml() const {
"head>\n\t<body>\n"; "head>\n\t<body>\n";
for (const auto &child : this->GetChilren()) { for (const auto &child : this->GetChilren()) {
ss << child->ToHtml(); ss << *child;
} }
ss << "\n\t</body>\n</html>"; ss << "\n\t</body>\n</html>";
@ -36,7 +36,7 @@ string HeadingNode::ToHtml() const {
ss << "<h" << size << ">"; ss << "<h" << size << ">";
for (const auto &child : this->GetChilren()) { for (const auto &child : this->GetChilren()) {
ss << child->ToHtml(); ss << *child;
} }
ss << "</h" << size << ">\n"; ss << "</h" << size << ">\n";
@ -48,7 +48,7 @@ string ParagraphNode::ToHtml() const {
ss << "<p>"; ss << "<p>";
for (const auto &child : this->GetChilren()) { for (const auto &child : this->GetChilren()) {
ss << child->ToHtml(); ss << *child;
} }
ss << "</p>\n"; ss << "</p>\n";
@ -61,7 +61,7 @@ string ListNode::ToHtml() const {
ss << (this->ordered ? "<ol>" : "<ul>") << "\n"; ss << (this->ordered ? "<ol>" : "<ul>") << "\n";
for (const auto &child : this->GetChilren()) { for (const auto &child : this->GetChilren()) {
ss << "<li>" << child->ToHtml() << "</li>" << "\n"; ss << "<li>" << *child << "</li>" << "\n";
} }
ss << (this->ordered ? "</ol>" : "</ul>") << "\n"; ss << (this->ordered ? "</ol>" : "</ul>") << "\n";
@ -74,7 +74,7 @@ string CodeBlockNode::ToHtml() const {
ss << "<code>\n"; ss << "<code>\n";
for (const auto &child : this->GetChilren()) { for (const auto &child : this->GetChilren()) {
ss << child->ToHtml() << "\n"; ss << *child << "\n";
} }
ss << "</code>\n"; ss << "</code>\n";

View File

@ -33,7 +33,7 @@ void test_nodes() {
root.AddChild(std::move(para)); root.AddChild(std::move(para));
root.AddChild(std::move(list)); root.AddChild(std::move(list));
std::cout << root.ToHtml() << std::endl; std::cout << root << std::endl;
} }
/** /**
@ -72,5 +72,4 @@ void test_document_converter() {
dc.ConvertWatcher(); dc.ConvertWatcher();
} }
int main(int argc, char **argv) { int main(int argc, char **argv) { test_document_converter(); }
test_document_converter(); }