make nodes to provide nodes instead of mixins
This commit is contained in:
@@ -8,21 +8,23 @@
|
||||
template <class TElem>
|
||||
class BaseLink : public ILink<TElem> {
|
||||
public:
|
||||
BaseLink(std::shared_ptr<TElem> node) : owner_node_(node) {}
|
||||
std::shared_ptr<TElem> getNode() const override { return owner_node_.lock(); }
|
||||
std::shared_ptr<TElem> getParent() const override { return parent_.lock(); }
|
||||
void setParent(const std::shared_ptr<TElem>& parent) override { parent_ = parent; }
|
||||
const std::vector<std::shared_ptr<TElem>>& getChildren() const override { return children_; }
|
||||
using ElemPtr = std::shared_ptr<TElem>;
|
||||
|
||||
void addChild(const std::shared_ptr<TElem>& child) override {
|
||||
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 std::shared_ptr<TElem>& child) override {
|
||||
void removeChild(const ElemPtr& child) override {
|
||||
children_.erase(std::remove(children_.begin(), children_.end(), child), children_.end());
|
||||
}
|
||||
|
||||
void replaceChild(const std::shared_ptr<TElem>& oldChild, const std::shared_ptr<TElem>& newChild) override {
|
||||
void replaceChild(const ElemPtr& oldChild, const ElemPtr& newChild) override {
|
||||
auto it = std::find(children_.begin(), children_.end(), oldChild);
|
||||
if (it != children_.end()) {
|
||||
*it = newChild;
|
||||
@@ -33,7 +35,7 @@ public:
|
||||
Logger::get("Link").dbg("--- Destructor called for: BaseLink");
|
||||
}
|
||||
protected:
|
||||
std::vector<std::shared_ptr<TElem>> children_;
|
||||
std::vector<ElemPtr> children_;
|
||||
std::weak_ptr<TElem> owner_node_;
|
||||
std::weak_ptr<TElem> parent_;
|
||||
};
|
||||
|
||||
@@ -5,10 +5,14 @@
|
||||
template <class TElem>
|
||||
class OneToOneLink : public BaseLink<TElem> {
|
||||
public:
|
||||
OneToOneLink(std::shared_ptr<TElem> e) : BaseLink<TElem>(e) {}
|
||||
void addChild(const std::shared_ptr<TElem>& child) override {
|
||||
using ElemPtr = std::shared_ptr<TElem>;
|
||||
|
||||
OneToOneLink(ElemPtr e) : BaseLink<TElem>(e) {}
|
||||
|
||||
void addChild(const ElemPtr& child) override {
|
||||
if (!this->children_.empty())
|
||||
throw std::logic_error("OneToOneLink cannot have more than one child");
|
||||
throw std::logic_error("Too many children");
|
||||
|
||||
BaseLink<TElem>::addChild(child);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user