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.

44 lines
1.4 KiB
C++

#pragma once
#include <vector>
#include <algorithm>
#include "ifaces/ILink.h"
#include "Logger.h"
/// \brief Базовая реализация интерфейса ILink для хранения связей между элементами.
/// \tparam TElem Тип элемента, между которыми устанавливается связь.
template <class TElem>
class BaseLink : public ILink<TElem> {
public:
using ElemPtr = std::shared_ptr<TElem>;
BaseLink(ElemPtr node) : owner_node_(node) {}
ElemPtr getNode() const override { return owner_node_.lock(); }
ElemPtr getParent() const override { return parent_.lock(); }
void setParent(const ElemPtr& parent) override { parent_ = parent; }
const std::vector<ElemPtr>& getChildren() const override { return children_; }
void addChild(const ElemPtr& child) override {
this->children_.push_back(child);
}
void removeChild(const ElemPtr& child) override {
children_.erase(std::remove(children_.begin(), children_.end(), child), children_.end());
}
void replaceChild(const ElemPtr& oldChild, const ElemPtr& newChild) override {
auto it = std::find(children_.begin(), children_.end(), oldChild);
if (it != children_.end()) {
*it = newChild;
}
}
~BaseLink() override {
Logger::get("ConDes").dbg("--- Destructor called for: BaseLink");
}
protected:
std::vector<ElemPtr> children_;
std::weak_ptr<TElem> owner_node_;
std::weak_ptr<TElem> parent_;
};