degrade History and it's not template now
because there's no need to make command type a template parameter
This commit is contained in:
		| @@ -9,10 +9,9 @@ | ||||
|  | ||||
| namespace nf7 { | ||||
|  | ||||
| template <typename T = History::Command> | ||||
| class AggregateCommand : public T { | ||||
| class AggregateCommand : public nf7::History::Command { | ||||
|  public: | ||||
|   using CommandList = std::vector<std::unique_ptr<T>>; | ||||
|   using CommandList = std::vector<std::unique_ptr<Command>>; | ||||
|  | ||||
|   AggregateCommand(CommandList&& commands) noexcept : | ||||
|       commands_(std::move(commands)) { | ||||
| @@ -67,7 +66,7 @@ class AggregateCommand : public T { | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   std::span<const std::unique_ptr<T>> commands() const noexcept { return commands_; } | ||||
|   std::span<const std::unique_ptr<Command>> commands() const noexcept { return commands_; } | ||||
|  | ||||
|  private: | ||||
|   CommandList commands_; | ||||
|   | ||||
| @@ -10,8 +10,7 @@ | ||||
|  | ||||
| namespace nf7 { | ||||
|  | ||||
| template <typename T> | ||||
| class GenericHistory : public History { | ||||
| class GenericHistory : public nf7::History { | ||||
|  public: | ||||
|   GenericHistory() = default; | ||||
|   GenericHistory(const GenericHistory&) = delete; | ||||
| @@ -19,7 +18,7 @@ class GenericHistory : public History { | ||||
|   GenericHistory& operator=(const GenericHistory&) = delete; | ||||
|   GenericHistory& operator=(GenericHistory&&) = default; | ||||
|  | ||||
|   T& Add(std::unique_ptr<T>&& cmd) noexcept { | ||||
|   Command& Add(std::unique_ptr<Command>&& cmd) noexcept override { | ||||
|     cmds_.erase(cmds_.begin()+static_cast<intmax_t>(cursor_), cmds_.end()); | ||||
|     cmds_.push_back(std::move(cmd)); | ||||
|     cursor_++; | ||||
| @@ -40,15 +39,15 @@ class GenericHistory : public History { | ||||
|     ++cursor_; | ||||
|   } | ||||
|  | ||||
|   T* prev() const noexcept { | ||||
|   Command* prev() const noexcept { | ||||
|     return cursor_ > 0? cmds_[cursor_-1].get(): nullptr; | ||||
|   } | ||||
|   T* next() const noexcept { | ||||
|   Command* next() const noexcept { | ||||
|     return cursor_ < cmds_.size()? cmds_[cursor_].get(): nullptr; | ||||
|   } | ||||
|  | ||||
|  private: | ||||
|   std::vector<std::unique_ptr<T>> cmds_; | ||||
|   std::vector<std::unique_ptr<Command>> cmds_; | ||||
|  | ||||
|   size_t cursor_ = 0; | ||||
| }; | ||||
|   | ||||
| @@ -1,7 +1,10 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include <memory> | ||||
|  | ||||
| #include "nf7.hh" | ||||
|  | ||||
|  | ||||
| namespace nf7 { | ||||
|  | ||||
| class History { | ||||
| @@ -16,6 +19,8 @@ class History { | ||||
|   History& operator=(const History&) = delete; | ||||
|   History& operator=(History&&) = delete; | ||||
|  | ||||
|   virtual Command& Add(std::unique_ptr<Command>&&) noexcept = 0; | ||||
|  | ||||
|   virtual void UnDo() = 0; | ||||
|   virtual void ReDo() = 0; | ||||
| }; | ||||
|   | ||||
| @@ -63,7 +63,7 @@ class NodeLinkStore { | ||||
|     links_.erase(std::remove(links_.begin(), links_.end(), lk), links_.end()); | ||||
|   } | ||||
|  | ||||
|   inline std::unique_ptr<History::Command> CreateCommandToRemoveExpired( | ||||
|   inline std::unique_ptr<nf7::History::Command> CreateCommandToRemoveExpired( | ||||
|       uint64_t id, std::span<const std::string> in, std::span<const std::string> out) noexcept; | ||||
|  | ||||
|   std::span<const Link> items() const noexcept { return links_; } | ||||
| @@ -104,9 +104,9 @@ class NodeLinkStore::SwapCommand : public History::Command { | ||||
| }; | ||||
|  | ||||
|  | ||||
| std::unique_ptr<History::Command> NodeLinkStore::CreateCommandToRemoveExpired( | ||||
| std::unique_ptr<nf7::History::Command> NodeLinkStore::CreateCommandToRemoveExpired( | ||||
|     uint64_t id, std::span<const std::string> in, std::span<const std::string> out) noexcept { | ||||
|   std::vector<std::unique_ptr<History::Command>> cmds; | ||||
|   std::vector<std::unique_ptr<nf7::History::Command>> cmds; | ||||
|   for (const auto& lk : links_) { | ||||
|     const bool rm = | ||||
|         (lk.src_id == id && std::find(out.begin(), out.end(), lk.src_name) == out.end()) || | ||||
| @@ -114,7 +114,7 @@ std::unique_ptr<History::Command> NodeLinkStore::CreateCommandToRemoveExpired( | ||||
|     if (rm) cmds.push_back(SwapCommand::CreateToRemove(*this, Link(lk))); | ||||
|   } | ||||
|   if (cmds.empty()) return nullptr; | ||||
|   return std::make_unique<AggregateCommand<History::Command>>(std::move(cmds)); | ||||
|   return std::make_unique<nf7::AggregateCommand>(std::move(cmds)); | ||||
| } | ||||
|  | ||||
| }  // namespace nf7 | ||||
|   | ||||
| @@ -1,5 +1,6 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include <cassert> | ||||
| #include <memory> | ||||
| #include <utility> | ||||
| #include <vector> | ||||
| @@ -10,8 +11,7 @@ | ||||
|  | ||||
| namespace nf7 { | ||||
|  | ||||
| template <typename T> | ||||
| class SquashedHistory : nf7::GenericHistory<T> { | ||||
| class SquashedHistory : public nf7::GenericHistory { | ||||
|  public: | ||||
|   SquashedHistory() = default; | ||||
|   SquashedHistory(const SquashedHistory&) = delete; | ||||
| @@ -19,7 +19,7 @@ class SquashedHistory : nf7::GenericHistory<T> { | ||||
|   SquashedHistory& operator=(const SquashedHistory&) = delete; | ||||
|   SquashedHistory& operator=(SquashedHistory&&) = default; | ||||
|  | ||||
|   T& Add(std::unique_ptr<T>&& cmd) noexcept { | ||||
|   Command& Add(std::unique_ptr<Command>&& cmd) noexcept override { | ||||
|     staged_.push_back(std::move(cmd)); | ||||
|     return *staged_.back(); | ||||
|   } | ||||
| @@ -27,25 +27,27 @@ class SquashedHistory : nf7::GenericHistory<T> { | ||||
|     if (staged_.size() == 0) { | ||||
|       return false; | ||||
|     } | ||||
|     nf7::GenericHistory<T>::Add( | ||||
|         std::make_unique<nf7::AggregateCommand<T>>(std::move(staged_))); | ||||
|     nf7::GenericHistory::Add( | ||||
|         std::make_unique<nf7::AggregateCommand>(std::move(staged_))); | ||||
|     return true; | ||||
|   } | ||||
|  | ||||
|   void Clear() noexcept { | ||||
|     nf7::GenericHistory<T>::Clear(); | ||||
|     nf7::GenericHistory::Clear(); | ||||
|     staged_.clear(); | ||||
|   } | ||||
|  | ||||
|   // TODO: check if staged_ is empty before UnDo/ReDo | ||||
|   using nf7::GenericHistory<T>::UnDo; | ||||
|   using nf7::GenericHistory<T>::ReDo; | ||||
|  | ||||
|   using nf7::GenericHistory<T>::prev; | ||||
|   using nf7::GenericHistory<T>::next; | ||||
|   void UnDo() override { | ||||
|     assert(staged_.size() == 0); | ||||
|     GenericHistory::UnDo(); | ||||
|   } | ||||
|   void ReDo() override { | ||||
|     assert(staged_.size() == 0); | ||||
|     GenericHistory::ReDo(); | ||||
|   } | ||||
|  | ||||
|  private: | ||||
|   std::vector<std::unique_ptr<T>> staged_; | ||||
|   std::vector<std::unique_ptr<Command>> staged_; | ||||
| }; | ||||
|  | ||||
| }  // namespace nf7 | ||||
|   | ||||
| @@ -130,7 +130,7 @@ class Network final : public nf7::File, | ||||
|  private: | ||||
|   ItemId next_ = 1; | ||||
|  | ||||
|   nf7::SquashedHistory<nf7::History::Command> history_; | ||||
|   nf7::SquashedHistory history_; | ||||
|  | ||||
|   std::unordered_map<ItemId,      Item*> item_map_; | ||||
|   std::unordered_map<const Node*, Item*> node_map_; | ||||
|   | ||||
| @@ -103,7 +103,7 @@ class TL final : public nf7::File, public nf7::DirItem, public nf7::Node { | ||||
|   } | ||||
|  | ||||
|  private: | ||||
|   nf7::SquashedHistory<History::Command> history_; | ||||
|   nf7::SquashedHistory history_; | ||||
|  | ||||
|   std::shared_ptr<TL::Lambda> lambda_; | ||||
|   std::vector<std::weak_ptr<TL::Lambda>> lambdas_running_; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user