From 5a53d5c2cd51b83884a1f6fb0e9dbd60574142c6 Mon Sep 17 00:00:00 2001 From: Dominik Meyer Date: Thu, 26 Aug 2021 21:42:58 +0200 Subject: [PATCH] ADD: a base node for the payload tree --- CMakeLists.txt | 1 + include/Whisper/Tree/Node.hpp | 107 ++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 include/Whisper/Tree/Node.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 69d925f..e56197a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,7 @@ ENDIF() set(WHISPER_SOURCES include/Whisper/Data/Classification.hpp src/Whisper/Data/Classification.cpp + include/Whisper/Tree/Node.hpp ) set(WHISPER_REQUIRED_LIBRARIES loguru) diff --git a/include/Whisper/Tree/Node.hpp b/include/Whisper/Tree/Node.hpp new file mode 100644 index 0000000..dd8d6a8 --- /dev/null +++ b/include/Whisper/Tree/Node.hpp @@ -0,0 +1,107 @@ +#pragma once +/* This Source Code Form is subject to the terms of the Mozilla Public +* License, v. 2.0. If a copy of the MPL was not distributed with this +* file, You can obtain one at https://mozilla.org/MPL/2.0/. +*/ +/** @file */ +/** @copyright 2021 MPLv2 */ +#include +#include +#include + +/** +* @brief This is the main namespace for all whisper-com related datatypes and classed. +*/ +namespace Whisper +{ + + + /** + * @brief The namespace for all Tree related datatypes and classes + */ + namespace Tree + { + + /** + * @brief Base class for a Payload used by the Whisper::Serializer + * + * This class represents a node in a two way linked tree. + */ + class Node : public std::enable_shared_from_this + { + private: + + /// the parent node of the node within the tree + std::shared_ptr parent_; + + /// all the children of this node + std::shared_ptr>> children_; + + public: + + /** + * @brief Constructor for just initializing the Node + */ + Node() : parent_(nullptr), + children_(nullptr) + { + children_=std::make_shared>>(); + } + + /** + * @brief set the parent of this node + * + * @param parent - the parent node + */ + void parent(std::shared_ptr parent) {parent_=parent;} + + /** + * @brief Returns the current set parent of the node + * + * @return the parent node + */ + std::shared_ptr parent() {return parent_;} + + /** + * @brief append a child node to the list of children + * + * @param child - the child to append + */ + void appendChild(std::shared_ptr child) + { + children_->push_back(child); + child->parent(shared_from_this()); + } + + /** + * @brief prepend a child node to the list of children + * + * @param child - the child to prepend + */ + void prependChild(std::shared_ptr child) + { + children_->push_front(child); + child->parent(shared_from_this()); + } + + /** + * @brief return a pointer to the list of children + * + * @return shared_ptr of the list of children + */ + std::shared_ptr>> children() { return children_;} + + /** + * @brief Virtual method returning the type of the node when extended by a class + * + * @return type of the Node, in general it is expected to by of type Whisper::PayloadType + */ + virtual std::uint32_t type() const { return 0;}; + + }; // class Node + + + + }; // namespace Tree + +}; // namespace WHisper