From ae4ecd46670f4abf4a42a8becacb2cdf80913a49 Mon Sep 17 00:00:00 2001 From: Ivan Morozov Date: Wed, 13 Jul 2022 21:19:06 +0300 Subject: [PATCH] advanced C++11. Add std::function, std::iterator --- Common/cppcf/RBTree/irbnode.h | 1 + Common/cppcf/RBTree/rbtree.cpp | 10 ++++++++++ Common/cppcf/RBTree/rbtree.h | 23 +++++++++++++++++++++-- Common/cppcf/cfstorage.cpp | 7 +++---- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Common/cppcf/RBTree/irbnode.h b/Common/cppcf/RBTree/irbnode.h index 913600c3e1..7c2622e79c 100644 --- a/Common/cppcf/RBTree/irbnode.h +++ b/Common/cppcf/RBTree/irbnode.h @@ -7,6 +7,7 @@ namespace RedBlackTree { class IRBNode; using PIRBNode = std::shared_ptr; +using WPIRBNode = std::weak_ptr; enum Color { diff --git a/Common/cppcf/RBTree/rbtree.cpp b/Common/cppcf/RBTree/rbtree.cpp index 8d326472c2..8df7084539 100644 --- a/Common/cppcf/RBTree/rbtree.cpp +++ b/Common/cppcf/RBTree/rbtree.cpp @@ -422,3 +422,13 @@ void RBTree::DoVisitTreeNodes(Action action, PIRBNode walker) DoVisitTreeNodes(action, walker->getRight()); } } + +RBTree::iterator::iterator(RBTree &tree, bool end) +{ + Action inserter = [&] (PIRBNode pNode) + { + heap.push_back(pNode); + }; + tree.VisitTreeNodes(inserter); + current = end ? heap.end() : heap.begin(); +} diff --git a/Common/cppcf/RBTree/rbtree.h b/Common/cppcf/RBTree/rbtree.h index a44039d5ca..520c1c1990 100644 --- a/Common/cppcf/RBTree/rbtree.h +++ b/Common/cppcf/RBTree/rbtree.h @@ -1,11 +1,13 @@ #pragma once #include "irbnode.h" #include +#include +#include namespace RedBlackTree { template -using Action = void(*)(T); +using Action = std::function; class RBTree @@ -49,7 +51,24 @@ private: void DoVisitTree(Action action, PIRBNode walker); void DoVisitTreeNodes(Action action, PIRBNode walker); - // TODO iterators +public: + // todo this from C# and it's weak realization + class iterator : public std::iterator + { + std::list heap; + std::list::iterator current; + public: + iterator(RBTree &tree, bool end = false); + inline iterator& operator++() {++current; return *this;} + inline iterator& operator--() {--current; return *this;} + inline bool operator==(iterator other) const {return current == other.current;} + inline bool operator!=(iterator other) const {return current != other.current;} + inline PIRBNode operator*() {return current->lock();} + inline void end() {current = heap.end();} + }; + + iterator begin() {return iterator(*this);} + iterator end()const; private: PIRBNode root; diff --git a/Common/cppcf/cfstorage.cpp b/Common/cppcf/cfstorage.cpp index 2f569c9252..bad4c5c47a 100644 --- a/Common/cppcf/cfstorage.cpp +++ b/Common/cppcf/cfstorage.cpp @@ -212,9 +212,8 @@ void CFStorage::VisitEntries(RedBlackTree::Action action, bool recursiv { SVector subStorages; - // TODO deligate - auto internalAction = - [subStorages] (RedBlackTree::PIRBNode targetNode) + RedBlackTree::Action internalAction = + [&] (RedBlackTree::PIRBNode targetNode) { auto d = std::dynamic_pointer_cast(targetNode); if (d->getStgType() == StgType::StgStream) @@ -266,7 +265,7 @@ void CFStorage::Delete(const std::wstring &entryName) for (const auto& de : *(temp->getChildren())) { auto ded = std::dynamic_pointer_cast(de); - temp->Delete(ded.Name); + temp->Delete(ded->GetEntryName()); } // ...then we need to rethread the root of siblings tree...