WhisperCom/include/Whisper/Tree/Node.hpp

108 lines
2.9 KiB
C++

#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 <memory>
#include <list>
#include <string>
/**
* @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<Whisper::Tree::Node>
{
private:
/// the parent node of the node within the tree
std::shared_ptr<Whisper::Tree::Node> parent_;
/// all the children of this node
std::shared_ptr<std::list<std::shared_ptr<Whisper::Tree::Node>>> children_;
public:
/**
* @brief Constructor for just initializing the Node
*/
Node() : parent_(nullptr),
children_(nullptr)
{
children_=std::make_shared<std::list<std::shared_ptr<Whisper::Tree::Node>>>();
}
/**
* @brief set the parent of this node
*
* @param parent - the parent node
*/
void parent(std::shared_ptr<Whisper::Tree::Node> parent) {parent_=parent;}
/**
* @brief Returns the current set parent of the node
*
* @return the parent node
*/
std::shared_ptr<Whisper::Tree::Node> parent() {return parent_;}
/**
* @brief append a child node to the list of children
*
* @param child - the child to append
*/
void appendChild(std::shared_ptr<Whisper::Tree::Node> 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<Whisper::Tree::Node> 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<std::list<std::shared_ptr<Whisper::Tree::Node>>> 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