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.
56 lines
1.1 KiB
C++
56 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(getSelf());
|
|
|
|
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(getSelf());
|
|
getLink()->setParent(nullptr);
|
|
}
|
|
|
|
LinkPtr getLink() override {
|
|
lazyInit();
|
|
return link_;
|
|
}
|
|
|
|
~LazyLinkMixin() override {
|
|
std::cout << "--- Destructor called for: " << "LazyLinkMixin" << "\n";
|
|
}
|
|
|
|
protected:
|
|
virtual NodePtr getShared() = 0;
|
|
NodePtr getSelf() override { return getShared(); }
|
|
|
|
private:
|
|
void lazyInit() {
|
|
if (!link_) {
|
|
link_ = std::make_shared<TLink>(getSelf());
|
|
}
|
|
}
|
|
|
|
LinkPtr link_;
|
|
};
|