(FEATURE): Implemented operator overloading.
Overloaded '<<' in the Node class to allow simpler output.
This commit is contained in:
parent
df73ea5c9d
commit
95e9644149
@ -19,3 +19,8 @@ void Node::Inspect(int indent) {
|
||||
child->Inspect(indent + 1);
|
||||
}
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, Node const &node) {
|
||||
os << node.ToHtml();
|
||||
return os;
|
||||
}
|
||||
|
||||
19
lib/node.h
19
lib/node.h
@ -19,12 +19,23 @@
|
||||
/// Reference: https://www.youtube.com/watch?v=AmjoK55h68Y&t=166s
|
||||
|
||||
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:
|
||||
/**
|
||||
* @brief List of children nodes.
|
||||
*
|
||||
* Most nodes will not have children, but some may, therefore this class must
|
||||
* have it.
|
||||
* Most nodes will not have children, but some may, therefore this class
|
||||
* must have it.
|
||||
*
|
||||
* @author Hayden Hargreaves (hhargreaves2006@gmail.com)
|
||||
*/
|
||||
@ -58,8 +69,8 @@ public:
|
||||
/**
|
||||
* @brief Return a read-only (const) list of children.
|
||||
*
|
||||
* Return our list of unique ptrs, they are const and therefore only have read
|
||||
* access.
|
||||
* Return our list of unique ptrs, they are const and therefore only have
|
||||
* read access.
|
||||
*
|
||||
* @author Hayden Hargreaves (hhargreaves2006@gmail.com)
|
||||
*/
|
||||
|
||||
@ -15,7 +15,7 @@ string DocumentNode::ToHtml() const {
|
||||
"head>\n\t<body>\n";
|
||||
|
||||
for (const auto &child : this->GetChilren()) {
|
||||
ss << child->ToHtml();
|
||||
ss << *child;
|
||||
}
|
||||
|
||||
ss << "\n\t</body>\n</html>";
|
||||
@ -36,7 +36,7 @@ string HeadingNode::ToHtml() const {
|
||||
ss << "<h" << size << ">";
|
||||
|
||||
for (const auto &child : this->GetChilren()) {
|
||||
ss << child->ToHtml();
|
||||
ss << *child;
|
||||
}
|
||||
|
||||
ss << "</h" << size << ">\n";
|
||||
@ -48,7 +48,7 @@ string ParagraphNode::ToHtml() const {
|
||||
ss << "<p>";
|
||||
|
||||
for (const auto &child : this->GetChilren()) {
|
||||
ss << child->ToHtml();
|
||||
ss << *child;
|
||||
}
|
||||
|
||||
ss << "</p>\n";
|
||||
@ -61,7 +61,7 @@ string ListNode::ToHtml() const {
|
||||
ss << (this->ordered ? "<ol>" : "<ul>") << "\n";
|
||||
|
||||
for (const auto &child : this->GetChilren()) {
|
||||
ss << "<li>" << child->ToHtml() << "</li>" << "\n";
|
||||
ss << "<li>" << *child << "</li>" << "\n";
|
||||
}
|
||||
|
||||
ss << (this->ordered ? "</ol>" : "</ul>") << "\n";
|
||||
@ -74,7 +74,7 @@ string CodeBlockNode::ToHtml() const {
|
||||
ss << "<code>\n";
|
||||
|
||||
for (const auto &child : this->GetChilren()) {
|
||||
ss << child->ToHtml() << "\n";
|
||||
ss << *child << "\n";
|
||||
}
|
||||
|
||||
ss << "</code>\n";
|
||||
|
||||
@ -33,7 +33,7 @@ void test_nodes() {
|
||||
root.AddChild(std::move(para));
|
||||
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();
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
test_document_converter(); }
|
||||
int main(int argc, char **argv) { test_document_converter(); }
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user