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.

55 lines
1.1 KiB
C++

#pragma once
#include <iostream>
#include "ifaces/INode.h"
#include "ifaces/ILinkMixin.h"
#include "links/LeafLink.h"
#include "links/OneToManyLink.h"
#include <memory>
template <class TLink>
class LazyLinkMixin : public virtual ILinkMixin {
public:
void linkChild(const NodePtr& childNode) override {
LinkPtr childLink = childNode->getLink();
childLink->setParent(getNode());
getLink()->addChild(childNode);
}
void unlinkParent() override {
/* No link -- no parent, who'll unlinked? */
if (!link_)
throw std::logic_error("Link isn't inited!");
NodePtr parent = link_->getParent();
if (!parent)
throw std::logic_error("Have no parent!");
LinkPtr parentLink = parent->getLink();
parentLink->removeChild(getNode());
getLink()->setParent(nullptr);
}
LinkPtr getLink() override {
lazyInit();
return link_;
}
~LazyLinkMixin() override {
std::cout << "--- Destructor called for: " << "LazyLinkMixin" << "\n";
}
protected:
virtual NodePtr getNode() = 0;
private:
void lazyInit() {
if (!link_) {
link_ = std::make_shared<TLink>(getNode());
}
}
LinkPtr link_;
};