From 5fd5822b6bd063e8a5b8cb0e35c3682a93940ad1 Mon Sep 17 00:00:00 2001 From: Hayden Hargreaves Date: Tue, 14 Oct 2025 20:34:45 -0700 Subject: [PATCH] (FEAT): Began working on node implementations. Drew out an inheritance map to review with the team. --- lib/node.cpp | 21 ++++++++++++++++ lib/node.h | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 lib/node.cpp create mode 100644 lib/node.h diff --git a/lib/node.cpp b/lib/node.cpp new file mode 100644 index 0000000..0ed30f8 --- /dev/null +++ b/lib/node.cpp @@ -0,0 +1,21 @@ +#include "./node.h" + +#include +#include + +void Node::Inspect(int indent) { + + if (this->children.size() > 0) { + for (int i = 0; i <= indent; i++) { + std::cout << "\t"; + } + std::cout << "std::vector children: " << std::endl; + } + + for (const auto &child : this->children) { + for (int i = 0; i <= indent; i++) { + std::cout << "\t"; + } + child->Inspect(indent + 1); + } +} diff --git a/lib/node.h b/lib/node.h new file mode 100644 index 0000000..47a3918 --- /dev/null +++ b/lib/node.h @@ -0,0 +1,71 @@ +#ifndef NODE_H +#define NODE_H + +#include +#include +#include + +/// NOTE: What the heck are unique pointers (unique_ptrs) +/// They are basically an abstraction over the typical (raw) pointer. +/// They handle ownership and deletion of the pointer for us, to prevent memory +/// leaks. +/// They are pretty easy to use, and only need to exist in places where new ones +/// are created. i.e., functions should not accept unique_ptrs, instead they +/// should accept normal pointers or references. +/// When calling a function that accepts a raw pointer, the unique_ptrs.get() +/// method is required. When calling a function that accepts a reference, the +/// reference operator (*) works perfectly fine. Hence, in this project we will +/// try to avoid using raw pointers, and only use references when needed. + +// NOTE ABC +class Node { +protected: + /** + * @brief List of children nodes. + * + * Most nodes will not have children, but some may, therefore this class must + * have it. + * + * @author Hayden Hargreaves (hhargreaves2006@gmail.com) + */ + std::vector> children; + +public: + /** + * @brief Inspect (view) the contents of the Node. + * + * This is a recursive approach to allow for indentation for easier viewing. + * + * @author Hayden Hargreaves (hhargreaves2006@gmail.com) + */ + virtual void Inspect(int indent = 0); + + /** + * @brief Return the node as a string. + * + * In this ABC the content is just returned with no modifications. The child + * nodes are expected to modify this behavior. i.e. wrapping in HTML tags. + * + * @author Hayden Hargreaves (hhargreaves2006@gmail.com) + */ + virtual std::string ToHtml() const = 0; + + virtual void AddChild(std::unique_ptr child) { + // Move ownership from the existing owner, to this class + this->children.push_back(std::move(child)); + } + + /** + * @brief Return a read-only (const) list of children. + * + * Return our list of unique ptrs, they are const and therefore only have read + * access. + * + * @author Hayden Hargreaves (hhargreaves2006@gmail.com) + */ + virtual const std::vector> &GetChilren() const { + return this->children; + } +}; + +#endif