improve nf7::GenericMemento to make a commit automatically when nf7::File::Event::kAdd event
This commit is contained in:
@@ -7,22 +7,22 @@
|
||||
|
||||
#include "nf7.hh"
|
||||
|
||||
#include "common/file_base.hh"
|
||||
#include "common/generic_context.hh"
|
||||
#include "common/memento.hh"
|
||||
|
||||
|
||||
namespace nf7 {
|
||||
|
||||
template <typename T>
|
||||
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<void()> 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<nf7::Memento::Tag> tag_;
|
||||
std::shared_ptr<nf7::Memento::Tag> last_;
|
||||
|
||||
|
||||
void Handle(const nf7::File::Event& e) noexcept override {
|
||||
switch (e.type) {
|
||||
case nf7::File::Event::kAdd:
|
||||
file_.env().ExecMain(
|
||||
std::make_shared<nf7::GenericContext>(file_),
|
||||
[this]() { CommitQuiet(); });
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
|
||||
@@ -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; };
|
||||
}
|
||||
|
||||
|
||||
@@ -78,9 +78,10 @@ class FontFace final : public nf7::FileBase,
|
||||
life_(*this),
|
||||
nwatch_(*this),
|
||||
log_(std::make_shared<nf7::LoggerRef>(*this)),
|
||||
mem_(std::move(d), *this) {
|
||||
mem_(*this, std::move(d)) {
|
||||
mem_.onCommit = mem_.onRestore = nwatch_.onMod = [this]() {
|
||||
cache_ = std::nullopt;
|
||||
Touch();
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -104,9 +104,10 @@ class ObjBase : public nf7::FileBase,
|
||||
life_(*this),
|
||||
log_(std::make_shared<nf7::LoggerRef>(*this)),
|
||||
nwatch_(std::make_shared<nf7::NFileWatcher>(*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<T>) {
|
||||
@@ -239,12 +240,12 @@ class ObjBase : public nf7::FileBase,
|
||||
std::optional<nf7::gui::Window> win_;
|
||||
|
||||
|
||||
void Drop() noexcept {
|
||||
void Drop(bool quiet = false) noexcept {
|
||||
auto ctx = std::make_shared<nf7::GenericContext>(*this, "dropping OpenGL obj");
|
||||
mtx_.AcquireLock(ctx, true /* = exclusive */).
|
||||
ThenIf([this](auto&) {
|
||||
ThenIf([this, quiet](auto&) {
|
||||
fu_ = std::nullopt;
|
||||
Touch();
|
||||
if (!quiet) Touch();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -68,7 +68,7 @@ class Node final : public nf7::FileBase,
|
||||
nf7::Node(nf7::Node::kCustomNode),
|
||||
life_(*this),
|
||||
log_(std::make_shared<nf7::LoggerRef>(*this)),
|
||||
mem_(std::move(data), *this),
|
||||
mem_(*this, std::move(data)),
|
||||
importer_(std::make_shared<nf7::luajit::NFileImporter>(env.npath())) {
|
||||
mem_.onCommit = mem_.onRestore = [this]() {
|
||||
cache_ = std::nullopt;
|
||||
|
||||
@@ -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<Imm> 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()) {
|
||||
|
||||
@@ -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_);
|
||||
}
|
||||
|
||||
|
||||
@@ -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<nf7::LoggerRef>(*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<nf7::GenericContext>(*this),
|
||||
std::bind(&Ref::SetUpWatcher, this));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateNode(nf7::Node::Editor&) noexcept override;
|
||||
void UpdateMenu(nf7::Node::Editor&) noexcept override;
|
||||
|
||||
|
||||
@@ -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()) {
|
||||
|
||||
@@ -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()) {
|
||||
|
||||
@@ -77,7 +77,7 @@ class Event final : public nf7::FileBase,
|
||||
nf7::Node(nf7::Node::kNone),
|
||||
life_(*this), log_(*this),
|
||||
la_root_(std::make_shared<nf7::Node::Lambda>(*this)),
|
||||
mem_(std::move(d), *this) {
|
||||
mem_(*this, std::move(d)) {
|
||||
}
|
||||
|
||||
Event(nf7::Deserializer& ar) : Event(ar.env()) {
|
||||
|
||||
@@ -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<ImGui_> 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()) {
|
||||
|
||||
@@ -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<ItemStore>(*this);
|
||||
return;
|
||||
case Event::kRemove:
|
||||
store_ = nullptr;
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ class NFile final : public nf7::FileBase,
|
||||
life_(*this), nwatch_(*this),
|
||||
shared_(std::make_shared<SharedData>(*this)),
|
||||
th_(std::make_shared<Thread>(*this, Runner {shared_})),
|
||||
mem_(std::move(data), *this) {
|
||||
mem_(*this, std::move(data)) {
|
||||
mtx_.onLock = [this]() { SetUp(); };
|
||||
mtx_.onUnlock = [this]() { shared_->nfile.reset(); };
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user