#include "mapcore.h" MapCore::MapCore() { } std::shared_ptr MapCore::root() const{ return root_bind; } void MapCore::setRoot(std::shared_ptr root) { this->root_bind = root; } std::shared_ptr MapCore::refreshRoot(std::shared_ptr old, std::shared_ptr _new){ MapVisitor visitor(*this); auto links = visitor.findLink(old->itemPeer()); std::function(std::shared_ptr, std::shared_ptr)> replace_cascade = [&replace_cascade, this](std::shared_ptr current_up, std::shared_ptr current_new) -> std::shared_ptr { auto pinst_up = current_up->parent(); if(!pinst_up) return current_new; auto pinst = pinst_up->downwardPeers(); auto new_pinst = pinst->replace(current_up->downwardPeers(), current_new); return replace_cascade(pinst_up, new_pinst); }; this->root_bind = replace_cascade(links, _new); return this->root_bind; } MapVisitor::MapVisitor(MapCore &base) : core_bind(base) {} std::shared_ptr MapVisitor::findTarget(std::shared_ptr node) const{ return find_target(core_bind.root(), node); } std::shared_ptr MapVisitor::findLink(std::shared_ptr node) const { return this->find_link(nullptr, core_bind.root(), node); } QList > MapVisitor::collect(std::function)> proc) const{ return collect(core_bind.root(), proc); } std::shared_ptr MapVisitor::find_link(std::shared_ptr pinst, std::shared_ptr handle, std::shared_ptr node) const { if(handle->itemPeer() == node) return std::make_shared(pinst, handle); auto target_ptr = std::make_shared(pinst, handle); for(auto &hc : handle->members()){ auto result = find_link(target_ptr, hc, node); if(result) return result; } return nullptr; } std::shared_ptr MapVisitor::find_target(std::shared_ptr handle, std::shared_ptr node) const { if (handle->itemPeer() == node) { return handle; } for (auto &it : handle->members()) { auto result = find_target(it, node); if (result) return result; } return nullptr; } QList > MapVisitor::collect(std::shared_ptr handle, std::function)> proc) const { QList> retlist; if(proc(handle)) retlist.append(handle); for(auto &c : handle->members()) retlist.append(collect(c, proc)); return retlist; }