From 61865f4d26c1e4127a53d481fac6cd210f95cb12 Mon Sep 17 00:00:00 2001 From: falsycat Date: Sun, 6 Nov 2022 22:09:41 +0900 Subject: [PATCH] improve nf7::GenericMemento to make a commit automatically when `nf7::File::Event::kAdd` event --- common/generic_memento.hh | 41 ++++++++++++++++++++++++++++----------- file/audio_device.cc | 2 +- file/font_face.cc | 3 ++- file/gl_obj.cc | 11 ++++++----- file/luajit_node.cc | 2 +- file/node_imm.cc | 8 +++++--- file/node_network.cc | 10 +++++----- file/node_ref.cc | 15 +------------- file/sequencer_adaptor.cc | 2 +- file/sequencer_call.cc | 2 +- file/system_event.cc | 2 +- file/system_imgui.cc | 7 ++++--- file/system_logger.cc | 7 +++---- file/system_nfile.cc | 2 +- file/value_curve.cc | 7 ++++--- file/value_plot.cc | 3 ++- 16 files changed, 68 insertions(+), 56 deletions(-) diff --git a/common/generic_memento.hh b/common/generic_memento.hh index 813c1c7..69070e7 100644 --- a/common/generic_memento.hh +++ b/common/generic_memento.hh @@ -7,22 +7,22 @@ #include "nf7.hh" +#include "common/file_base.hh" +#include "common/generic_context.hh" #include "common/memento.hh" namespace nf7 { template -class GenericMemento : public nf7::Memento { +class GenericMemento : public nf7::FileBase::Feature, public nf7::Memento { public: class CustomTag; - GenericMemento(T&& data, nf7::File* f = nullptr) noexcept : + GenericMemento(nf7::FileBase& f, T&& data) noexcept : + nf7::FileBase::Feature(f), file_(f), initial_(T(data)), data_(std::move(data)) { } - GenericMemento(T&& data, nf7::File& f) noexcept : - GenericMemento(std::move(data), &f) { - } ~GenericMemento() noexcept { tag_ = nullptr; last_ = nullptr; @@ -50,20 +50,26 @@ class GenericMemento : public nf7::Memento { tag_ = tag; last_ = tag; onRestore(); - if (file_) file_->Touch(); + file_.Touch(); } - void Commit() noexcept { + void Commit(bool quiet = false) noexcept { tag_ = nullptr; onCommit(); - if (file_) file_->Touch(); + if (!quiet) file_.Touch(); } - void CommitAmend() noexcept { + void CommitQuiet() noexcept { + Commit(true); + } + void CommitAmend(bool quiet = false) noexcept { if (!tag_) return; auto itr = map_.find(tag_->id()); assert(itr != map_.end()); itr->second = data_; onCommit(); - if (file_) file_->Touch(); + if (!quiet) file_.Touch(); + } + void CommitAmendQuiet() noexcept { + CommitAmend(true); } T& data() noexcept { return data_; } @@ -81,7 +87,7 @@ class GenericMemento : public nf7::Memento { std::function onCommit = [](){}; private: - nf7::File* const file_; + nf7::File& file_; const T initial_; T data_; @@ -91,6 +97,19 @@ class GenericMemento : public nf7::Memento { std::shared_ptr tag_; std::shared_ptr last_; + + + void Handle(const nf7::File::Event& e) noexcept override { + switch (e.type) { + case nf7::File::Event::kAdd: + file_.env().ExecMain( + std::make_shared(file_), + [this]() { CommitQuiet(); }); + return; + default: + return; + } + } }; template diff --git a/file/audio_device.cc b/file/audio_device.cc index 7431e2f..26ae60f 100644 --- a/file/audio_device.cc +++ b/file/audio_device.cc @@ -106,7 +106,7 @@ class Device final : public nf7::FileBase, nf7::GenericConfig(mem_), nf7::DirItem(nf7::DirItem::kMenu | nf7::DirItem::kTooltip), nf7::Node(nf7::Node::kNone), - life_(*this), log_(*this), mem_(std::move(data), *this) { + life_(*this), log_(*this), mem_(*this, std::move(data)) { mem_.onCommit = mem_.onRestore = [this]() { cache_ = std::nullopt; }; } diff --git a/file/font_face.cc b/file/font_face.cc index 60c3236..c3c84e1 100644 --- a/file/font_face.cc +++ b/file/font_face.cc @@ -78,9 +78,10 @@ class FontFace final : public nf7::FileBase, life_(*this), nwatch_(*this), log_(std::make_shared(*this)), - mem_(std::move(d), *this) { + mem_(*this, std::move(d)) { mem_.onCommit = mem_.onRestore = nwatch_.onMod = [this]() { cache_ = std::nullopt; + Touch(); }; } diff --git a/file/gl_obj.cc b/file/gl_obj.cc index e0a4edd..8ce6ead 100644 --- a/file/gl_obj.cc +++ b/file/gl_obj.cc @@ -104,9 +104,10 @@ class ObjBase : public nf7::FileBase, life_(*this), log_(std::make_shared(*this)), nwatch_(std::make_shared(*this)), - mem_(std::move(data), *this) { + mem_(*this, std::move(data)) { nwatch_->onMod = mem_.onRestore = mem_.onCommit = [this]() { - Drop(); + Drop(true /* = quiet */); + Touch(); }; if constexpr (HasWindow) { @@ -239,12 +240,12 @@ class ObjBase : public nf7::FileBase, std::optional win_; - void Drop() noexcept { + void Drop(bool quiet = false) noexcept { auto ctx = std::make_shared(*this, "dropping OpenGL obj"); mtx_.AcquireLock(ctx, true /* = exclusive */). - ThenIf([this](auto&) { + ThenIf([this, quiet](auto&) { fu_ = std::nullopt; - Touch(); + if (!quiet) Touch(); }); } diff --git a/file/luajit_node.cc b/file/luajit_node.cc index 12dfad8..06af04e 100644 --- a/file/luajit_node.cc +++ b/file/luajit_node.cc @@ -68,7 +68,7 @@ class Node final : public nf7::FileBase, nf7::Node(nf7::Node::kCustomNode), life_(*this), log_(std::make_shared(*this)), - mem_(std::move(data), *this), + mem_(*this, std::move(data)), importer_(std::make_shared(env.npath())) { mem_.onCommit = mem_.onRestore = [this]() { cache_ = std::nullopt; diff --git a/file/node_imm.cc b/file/node_imm.cc index 7fffd03..2cda9e9 100644 --- a/file/node_imm.cc +++ b/file/node_imm.cc @@ -17,6 +17,7 @@ #include "nf7.hh" #include "common/dir_item.hh" +#include "common/file_base.hh" #include "common/generic_memento.hh" #include "common/generic_type_info.hh" #include "common/gui.hh" @@ -30,7 +31,8 @@ namespace nf7 { namespace { -class Imm final : public nf7::File, public nf7::DirItem, public nf7::Node { +class Imm final : public nf7::FileBase, + public nf7::DirItem, public nf7::Node { public: static inline const nf7::GenericTypeInfo kType = {"Node/Imm", {"nf7::DirItem", "nf7::Node"}}; @@ -45,10 +47,10 @@ class Imm final : public nf7::File, public nf7::DirItem, public nf7::Node { class Lambda; Imm(nf7::Env& env, nf7::gui::Value&& v = {}) noexcept : - nf7::File(kType, env), + nf7::FileBase(kType, env), nf7::DirItem(nf7::DirItem::kWidget), nf7::Node(nf7::Node::kCustomNode), - life_(*this), mem_(std::move(v), *this) { + life_(*this), mem_(*this, std::move(v)) { } Imm(nf7::Deserializer& ar) : Imm(ar.env()) { diff --git a/file/node_network.cc b/file/node_network.cc index 7c38b48..92bf063 100644 --- a/file/node_network.cc +++ b/file/node_network.cc @@ -121,7 +121,7 @@ class Network final : public nf7::FileBase, life_(*this), win_(*this, "Editor Node/Network"), items_(std::move(items)), links_(std::move(links)), - mem_(std::move(d), *this) { + mem_(*this, std::move(d)) { win_.onConfig = []() { const auto em = ImGui::GetFontSize(); ImGui::SetNextWindowSize({36*em, 36*em}, ImGuiCond_FirstUseEver); @@ -736,7 +736,7 @@ class Network::Initiator final : public nf7::File, }; // Node that emits/receives input or output. -class Network::Terminal : public nf7::File, +class Network::Terminal : public nf7::FileBase, public nf7::Node, public Network::InternalNode { public: @@ -750,9 +750,9 @@ class Network::Terminal : public nf7::File, }; Terminal(nf7::Env& env, Data&& data = {}) noexcept : - nf7::File(kType, env), + nf7::FileBase(kType, env), nf7::Node(nf7::Node::kCustomNode), - life_(*this), mem_(std::move(data), *this) { + life_(*this), mem_(*this, std::move(data)) { } Terminal(nf7::Deserializer& ar) : Terminal(ar.env()) { @@ -799,7 +799,7 @@ class Network::Terminal : public nf7::File, } } File::Interface* interface(const std::type_info& t) noexcept override { - return InterfaceSelector< + return nf7::InterfaceSelector< Network::InternalNode, nf7::Node, nf7::Memento>(t).Select(this, &mem_); } diff --git a/file/node_ref.cc b/file/node_ref.cc index b67979e..ebfacae 100644 --- a/file/node_ref.cc +++ b/file/node_ref.cc @@ -61,7 +61,7 @@ class Ref final : public nf7::FileBase, public nf7::Node { nf7::Node(nf7::Node::kCustomNode | nf7::Node::kMenu), life_(*this), log_(std::make_shared(*this)), - mem_(std::move(data), *this) { + mem_(*this, std::move(data)) { mem_.onRestore = mem_.onCommit = [this]() { SetUpWatcher(); }; } @@ -84,19 +84,6 @@ class Ref final : public nf7::FileBase, public nf7::Node { return mem_->outputs; } - void Handle(const nf7::File::Event& ev) noexcept { - nf7::FileBase::Handle(ev); - - switch (ev.type) { - case nf7::File::Event::kAdd: - env().ExecMain(std::make_shared(*this), - std::bind(&Ref::SetUpWatcher, this)); - break; - default: - break; - } - } - void UpdateNode(nf7::Node::Editor&) noexcept override; void UpdateMenu(nf7::Node::Editor&) noexcept override; diff --git a/file/sequencer_adaptor.cc b/file/sequencer_adaptor.cc index 6ca277f..e5784c5 100644 --- a/file/sequencer_adaptor.cc +++ b/file/sequencer_adaptor.cc @@ -70,7 +70,7 @@ class Adaptor final : public nf7::FileBase, nf7::Sequencer(Sequencer::kCustomItem | Sequencer::kTooltip | Sequencer::kParamPanel), - life_(*this), mem_(std::move(d), *this) { + life_(*this), mem_(*this, std::move(d)) { } Adaptor(nf7::Deserializer& ar) : Adaptor(ar.env()) { diff --git a/file/sequencer_call.cc b/file/sequencer_call.cc index 2626df6..0a43bcb 100644 --- a/file/sequencer_call.cc +++ b/file/sequencer_call.cc @@ -56,7 +56,7 @@ class Call final : public nf7::FileBase, public nf7::Sequencer { Sequencer::kTooltip | Sequencer::kParamPanel), life_(*this), - mem_(std::move(data), *this) { + mem_(*this, std::move(data)) { } Call(nf7::Deserializer& ar) : Call(ar.env()) { diff --git a/file/system_event.cc b/file/system_event.cc index 5dc144d..65aae9e 100644 --- a/file/system_event.cc +++ b/file/system_event.cc @@ -77,7 +77,7 @@ class Event final : public nf7::FileBase, nf7::Node(nf7::Node::kNone), life_(*this), log_(*this), la_root_(std::make_shared(*this)), - mem_(std::move(d), *this) { + mem_(*this, std::move(d)) { } Event(nf7::Deserializer& ar) : Event(ar.env()) { diff --git a/file/system_imgui.cc b/file/system_imgui.cc index 0f7b3c4..3520a86 100644 --- a/file/system_imgui.cc +++ b/file/system_imgui.cc @@ -18,6 +18,7 @@ #include "nf7.hh" #include "common/dir_item.hh" +#include "common/file_base.hh" #include "common/generic_config.hh" #include "common/generic_memento.hh" #include "common/generic_type_info.hh" @@ -33,7 +34,7 @@ using namespace std::literals; namespace nf7 { namespace { -class ImGui_ final : public nf7::File, +class ImGui_ final : public nf7::FileBase, public nf7::GenericConfig, public nf7::DirItem { public: static inline const nf7::GenericTypeInfo kType = {"System/ImGui", {}}; @@ -72,11 +73,11 @@ class ImGui_ final : public nf7::File, }; ImGui_(nf7::Env& env) noexcept : - nf7::File(kType, env), + nf7::FileBase(kType, env), nf7::GenericConfig(mem_), nf7::DirItem(nf7::DirItem::kMenu | nf7::DirItem::kEarlyUpdate), - mem_({}, *this) { + mem_(*this, {}) { } ImGui_(nf7::Deserializer& ar) : ImGui_(ar.env()) { diff --git a/file/system_logger.cc b/file/system_logger.cc index 9fcc88e..7464d58 100644 --- a/file/system_logger.cc +++ b/file/system_logger.cc @@ -115,7 +115,8 @@ class Logger final : public nf7::FileBase, nf7::FileBase(kType, env), nf7::GenericConfig(mem_), nf7::DirItem(DirItem::kMenu), - mem_(std::move(d), *this), win_(*this, "Log View") { + mem_(*this, std::move(d)), + win_(*this, "Log View") { mem_.onCommit = mem_.onRestore = [this]() { store_->param(mem_.data()); }; @@ -138,13 +139,11 @@ class Logger final : public nf7::FileBase, } void Handle(const nf7::File::Event& ev) noexcept override { + nf7::FileBase::Handle(ev); switch (ev.type) { case Event::kAdd: store_ = std::make_shared(*this); return; - case Event::kRemove: - store_ = nullptr; - return; default: return; } diff --git a/file/system_nfile.cc b/file/system_nfile.cc index ccd8453..92a7e92 100644 --- a/file/system_nfile.cc +++ b/file/system_nfile.cc @@ -120,7 +120,7 @@ class NFile final : public nf7::FileBase, life_(*this), nwatch_(*this), shared_(std::make_shared(*this)), th_(std::make_shared(*this, Runner {shared_})), - mem_(std::move(data), *this) { + mem_(*this, std::move(data)) { mtx_.onLock = [this]() { SetUp(); }; mtx_.onUnlock = [this]() { shared_->nfile.reset(); }; diff --git a/file/value_curve.cc b/file/value_curve.cc index 6bc694d..f3db1cd 100644 --- a/file/value_curve.cc +++ b/file/value_curve.cc @@ -15,6 +15,7 @@ #include "nf7.hh" #include "common/dir_item.hh" +#include "common/file_base.hh" #include "common/generic_memento.hh" #include "common/generic_type_info.hh" #include "common/gui.hh" @@ -29,7 +30,7 @@ namespace nf7 { namespace { -class Curve final : public nf7::File, +class Curve final : public nf7::FileBase, public nf7::DirItem, public nf7::Node, public nf7::Sequencer { @@ -69,12 +70,12 @@ class Curve final : public nf7::File, }; Curve(nf7::Env& env, Data&& data = {}) noexcept : - nf7::File(kType, env), + nf7::FileBase(kType, env), nf7::DirItem(nf7::DirItem::kWidget), nf7::Node(nf7::Node::kCustomNode), nf7::Sequencer(nf7::Sequencer::kCustomItem | nf7::Sequencer::kParamPanel), - life_(*this), mem_(std::move(data), *this) { + life_(*this), mem_(*this, std::move(data)) { AssignId(); Sanitize(); } diff --git a/file/value_plot.cc b/file/value_plot.cc index 6f9a2de..7ad759b 100644 --- a/file/value_plot.cc +++ b/file/value_plot.cc @@ -114,7 +114,8 @@ class Plot final : public nf7::FileBase, nf7::GenericConfig(mem_), nf7::DirItem(nf7::DirItem::kMenu), nf7::Node(nf7::Node::kNone), - life_(*this), log_(*this), win_(*this, "Plot"), mem_(std::move(data)) { + life_(*this), log_(*this), win_(*this, "Plot"), + mem_(*this, std::move(data)) { win_.onUpdate = [this]() { PlotGraph(); }; mem_.onRestore = mem_.onCommit = [this]() { BuildInputList(); }; Sanitize();