You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.4 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#pragma once
#include <iostream>
#include <memory>
#include "ifaces/ILinkMixin.h"
#include "ifaces/INode.h"
#include "Logger.h"
/// \brief Базовый миксин для реализации ILinkMixin для INode.
/// Предоставляет базовую реализацию методов для работы с дочерними узлами и родителем.
class BaseLinkMixin : public virtual ILinkMixin<INode>,
public std::enable_shared_from_this<ILinkMixin<INode>> {
using ElemPtr = std::shared_ptr<INode>;
public:
~BaseLinkMixin() override {
Logger::get("Mixin").dbg("--- Destructor called for: BaseLinkMixin");
}
void linkChild(const ElemPtr& child) override {
getLink()->addChild(child);
auto childLink = child->getLink();
childLink->setParent(getNode());
}
void unlinkParent() override {
auto link = getLink();
ElemPtr parent = link->getParent();
if (!parent)
throw std::logic_error("Have no parent!");
auto parentLink = parent->getLink();
parentLink->removeChild(getNode());
getLink()->setParent(nullptr);
}
ElemPtr parent() override {
auto link = getLink();
ElemPtr parent = link->getParent();
return parent;
}
const std::vector<ElemPtr>& children() override {
auto link = getLink();
return link->getChildren();
}
protected:
ElemPtr getNode() {
return std::dynamic_pointer_cast<INode>(shared_from_this());
}
};