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.
poc-links/include/mixins/HierarchicalLinkMixin.h

52 lines
1.9 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/INode.h"
#include "mixins/LazyLinkMixin.h"
#include "links/OneToManyLink.h"
#include "links/OneToOneLink.h"
#include "Logger.h"
/// \brief Миксин для иерархических связей между элементами.
/// \tparam INode Тип дочернего элемента.
/// Автоматически выбирает тип связи (один-ко-многим или один-к-одному) в зависимости от типа
/// дочернего узла. Если тип дочернего узла совпадает с родителем, то используется связь
/// один-ко-многим. При попытке подключить узел отличного типа выбирается связь один-к-одному.
class HierarchicalLinkMixin : public LazyLinkMixin<OneToOneLink<INode>> {
using LinkPtr = std::shared_ptr<ILink<INode>>;
using ElemPtr = std::shared_ptr<INode>;
public:
~HierarchicalLinkMixin() override {
Logger::get("ConDes").dbg("--- Destructor called for: HierarchicalLinkMixin");
}
void linkChild(const ElemPtr& child) override {
hierarchicalInit(child);
LazyLinkMixin<OneToOneLink<INode>>::linkChild(child);
}
protected:
void hierarchicalInit(const ElemPtr& child) {
Logger::get("Mixin").dbg("--- hierarchicalInit called");
if (this->link_ && !this->link_->getChildren().empty())
return;
LinkPtr newLink;
if (typeid(*child) == typeid(*this)) {
Logger::get("Mixin").dbg("--- Mutate to OneToMany");
newLink = std::make_shared<OneToManyLink<INode>>(*this);
} else {
Logger::get("Mixin").dbg("--- Mutate to OneToOne");
newLink = std::make_shared<OneToOneLink<INode>>(*this);
}
if (newLink && this->link_)
newLink->setParent(this->link_->getParent());
this->link_ = newLink;
}
};