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:
parent
972f5ce0a1
commit
d3f245381b
@ -9,10 +9,9 @@
|
|||||||
|
|
||||||
namespace nf7 {
|
namespace nf7 {
|
||||||
|
|
||||||
template <typename T = History::Command>
|
class AggregateCommand : public nf7::History::Command {
|
||||||
class AggregateCommand : public T {
|
|
||||||
public:
|
public:
|
||||||
using CommandList = std::vector<std::unique_ptr<T>>;
|
using CommandList = std::vector<std::unique_ptr<Command>>;
|
||||||
|
|
||||||
AggregateCommand(CommandList&& commands) noexcept :
|
AggregateCommand(CommandList&& commands) noexcept :
|
||||||
commands_(std::move(commands)) {
|
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:
|
private:
|
||||||
CommandList commands_;
|
CommandList commands_;
|
||||||
|
@ -10,8 +10,7 @@
|
|||||||
|
|
||||||
namespace nf7 {
|
namespace nf7 {
|
||||||
|
|
||||||
template <typename T>
|
class GenericHistory : public nf7::History {
|
||||||
class GenericHistory : public History {
|
|
||||||
public:
|
public:
|
||||||
GenericHistory() = default;
|
GenericHistory() = default;
|
||||||
GenericHistory(const GenericHistory&) = delete;
|
GenericHistory(const GenericHistory&) = delete;
|
||||||
@ -19,7 +18,7 @@ class GenericHistory : public History {
|
|||||||
GenericHistory& operator=(const GenericHistory&) = delete;
|
GenericHistory& operator=(const GenericHistory&) = delete;
|
||||||
GenericHistory& operator=(GenericHistory&&) = default;
|
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_.erase(cmds_.begin()+static_cast<intmax_t>(cursor_), cmds_.end());
|
||||||
cmds_.push_back(std::move(cmd));
|
cmds_.push_back(std::move(cmd));
|
||||||
cursor_++;
|
cursor_++;
|
||||||
@ -40,15 +39,15 @@ class GenericHistory : public History {
|
|||||||
++cursor_;
|
++cursor_;
|
||||||
}
|
}
|
||||||
|
|
||||||
T* prev() const noexcept {
|
Command* prev() const noexcept {
|
||||||
return cursor_ > 0? cmds_[cursor_-1].get(): nullptr;
|
return cursor_ > 0? cmds_[cursor_-1].get(): nullptr;
|
||||||
}
|
}
|
||||||
T* next() const noexcept {
|
Command* next() const noexcept {
|
||||||
return cursor_ < cmds_.size()? cmds_[cursor_].get(): nullptr;
|
return cursor_ < cmds_.size()? cmds_[cursor_].get(): nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::unique_ptr<T>> cmds_;
|
std::vector<std::unique_ptr<Command>> cmds_;
|
||||||
|
|
||||||
size_t cursor_ = 0;
|
size_t cursor_ = 0;
|
||||||
};
|
};
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
#include "nf7.hh"
|
#include "nf7.hh"
|
||||||
|
|
||||||
|
|
||||||
namespace nf7 {
|
namespace nf7 {
|
||||||
|
|
||||||
class History {
|
class History {
|
||||||
@ -16,6 +19,8 @@ class History {
|
|||||||
History& operator=(const History&) = delete;
|
History& operator=(const History&) = delete;
|
||||||
History& operator=(History&&) = delete;
|
History& operator=(History&&) = delete;
|
||||||
|
|
||||||
|
virtual Command& Add(std::unique_ptr<Command>&&) noexcept = 0;
|
||||||
|
|
||||||
virtual void UnDo() = 0;
|
virtual void UnDo() = 0;
|
||||||
virtual void ReDo() = 0;
|
virtual void ReDo() = 0;
|
||||||
};
|
};
|
||||||
|
@ -63,7 +63,7 @@ class NodeLinkStore {
|
|||||||
links_.erase(std::remove(links_.begin(), links_.end(), lk), links_.end());
|
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;
|
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_; }
|
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 {
|
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_) {
|
for (const auto& lk : links_) {
|
||||||
const bool rm =
|
const bool rm =
|
||||||
(lk.src_id == id && std::find(out.begin(), out.end(), lk.src_name) == out.end()) ||
|
(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 (rm) cmds.push_back(SwapCommand::CreateToRemove(*this, Link(lk)));
|
||||||
}
|
}
|
||||||
if (cmds.empty()) return nullptr;
|
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
|
} // namespace nf7
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@ -10,8 +11,7 @@
|
|||||||
|
|
||||||
namespace nf7 {
|
namespace nf7 {
|
||||||
|
|
||||||
template <typename T>
|
class SquashedHistory : public nf7::GenericHistory {
|
||||||
class SquashedHistory : nf7::GenericHistory<T> {
|
|
||||||
public:
|
public:
|
||||||
SquashedHistory() = default;
|
SquashedHistory() = default;
|
||||||
SquashedHistory(const SquashedHistory&) = delete;
|
SquashedHistory(const SquashedHistory&) = delete;
|
||||||
@ -19,7 +19,7 @@ class SquashedHistory : nf7::GenericHistory<T> {
|
|||||||
SquashedHistory& operator=(const SquashedHistory&) = delete;
|
SquashedHistory& operator=(const SquashedHistory&) = delete;
|
||||||
SquashedHistory& operator=(SquashedHistory&&) = default;
|
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));
|
staged_.push_back(std::move(cmd));
|
||||||
return *staged_.back();
|
return *staged_.back();
|
||||||
}
|
}
|
||||||
@ -27,25 +27,27 @@ class SquashedHistory : nf7::GenericHistory<T> {
|
|||||||
if (staged_.size() == 0) {
|
if (staged_.size() == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
nf7::GenericHistory<T>::Add(
|
nf7::GenericHistory::Add(
|
||||||
std::make_unique<nf7::AggregateCommand<T>>(std::move(staged_)));
|
std::make_unique<nf7::AggregateCommand>(std::move(staged_)));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Clear() noexcept {
|
void Clear() noexcept {
|
||||||
nf7::GenericHistory<T>::Clear();
|
nf7::GenericHistory::Clear();
|
||||||
staged_.clear();
|
staged_.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: check if staged_ is empty before UnDo/ReDo
|
void UnDo() override {
|
||||||
using nf7::GenericHistory<T>::UnDo;
|
assert(staged_.size() == 0);
|
||||||
using nf7::GenericHistory<T>::ReDo;
|
GenericHistory::UnDo();
|
||||||
|
}
|
||||||
using nf7::GenericHistory<T>::prev;
|
void ReDo() override {
|
||||||
using nf7::GenericHistory<T>::next;
|
assert(staged_.size() == 0);
|
||||||
|
GenericHistory::ReDo();
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::unique_ptr<T>> staged_;
|
std::vector<std::unique_ptr<Command>> staged_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace nf7
|
} // namespace nf7
|
||||||
|
@ -130,7 +130,7 @@ class Network final : public nf7::File,
|
|||||||
private:
|
private:
|
||||||
ItemId next_ = 1;
|
ItemId next_ = 1;
|
||||||
|
|
||||||
nf7::SquashedHistory<nf7::History::Command> history_;
|
nf7::SquashedHistory history_;
|
||||||
|
|
||||||
std::unordered_map<ItemId, Item*> item_map_;
|
std::unordered_map<ItemId, Item*> item_map_;
|
||||||
std::unordered_map<const Node*, Item*> node_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:
|
private:
|
||||||
nf7::SquashedHistory<History::Command> history_;
|
nf7::SquashedHistory history_;
|
||||||
|
|
||||||
std::shared_ptr<TL::Lambda> lambda_;
|
std::shared_ptr<TL::Lambda> lambda_;
|
||||||
std::vector<std::weak_ptr<TL::Lambda>> lambdas_running_;
|
std::vector<std::weak_ptr<TL::Lambda>> lambdas_running_;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user