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.
49 lines
1.6 KiB
C++
49 lines
1.6 KiB
C++
#pragma once
|
|
#include <iostream>
|
|
#include "mixins/LazyLinkMixin.h"
|
|
#include <memory>
|
|
#include "links/OneToManyLink.h"
|
|
#include "links/OneToOneLink.h"
|
|
#include "Logger.h"
|
|
|
|
/// \brief Миксин для иерархических связей между элементами.
|
|
/// Автоматически выбирает тип связи (один-ко-многим или один-к-одному) в зависимости от типа дочернего элемента.
|
|
/// \tparam TElem Тип дочернего элемента.
|
|
template <class TElem>
|
|
class HierarchicalLinkMixin : public LazyLinkMixin<OneToOneLink<TElem>> {
|
|
using LinkPtr = std::shared_ptr<ILink<TElem>>;
|
|
using ElemPtr = std::shared_ptr<TElem>;
|
|
|
|
public:
|
|
~HierarchicalLinkMixin() override {
|
|
Logger::get("ConDes").dbg("--- Destructor called for: HierarchicalLinkMixin");
|
|
}
|
|
|
|
void linkChild(const ElemPtr& child) override {
|
|
hierarchicalInit(child);
|
|
LazyLinkMixin<OneToOneLink<TElem>>::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<TElem>>(this->getNode());
|
|
} else {
|
|
Logger::get("Mixin").dbg("--- Mutate to OneToOne");
|
|
newLink = std::make_shared<OneToOneLink<TElem>>(this->getNode());
|
|
}
|
|
|
|
if (newLink && this->link_)
|
|
newLink->setParent(this->link_->getParent());
|
|
|
|
this->link_ = newLink;
|
|
}
|
|
};
|