improve nf7::GenericMemento
This commit is contained in:
@@ -5,6 +5,8 @@
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
#include "nf7.hh"
|
||||
|
||||
#include "common/memento.hh"
|
||||
|
||||
|
||||
@@ -15,7 +17,11 @@ class GenericMemento : public nf7::Memento {
|
||||
public:
|
||||
class CustomTag;
|
||||
|
||||
GenericMemento(T&& data) noexcept : initial_(T(data)), data_(std::move(data)) {
|
||||
GenericMemento(T&& data, nf7::File* f = nullptr) noexcept :
|
||||
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;
|
||||
@@ -37,10 +43,12 @@ class GenericMemento : public nf7::Memento {
|
||||
tag_ = tag;
|
||||
last_ = tag;
|
||||
onRestore();
|
||||
if (file_) file_->Touch();
|
||||
}
|
||||
void Commit() noexcept {
|
||||
tag_ = nullptr;
|
||||
onCommit();
|
||||
if (file_) file_->Touch();
|
||||
}
|
||||
void CommitAmend() noexcept {
|
||||
if (!tag_) return;
|
||||
@@ -48,6 +56,7 @@ class GenericMemento : public nf7::Memento {
|
||||
assert(itr != map_.end());
|
||||
itr->second = data_;
|
||||
onCommit();
|
||||
if (file_) file_->Touch();
|
||||
}
|
||||
|
||||
T& data() noexcept { return data_; }
|
||||
@@ -65,6 +74,8 @@ class GenericMemento : public nf7::Memento {
|
||||
std::function<void()> onCommit = [](){};
|
||||
|
||||
private:
|
||||
nf7::File* const file_;
|
||||
|
||||
const T initial_;
|
||||
T data_;
|
||||
|
||||
|
@@ -61,12 +61,9 @@ class InlineNode final : public nf7::FileBase, public nf7::DirItem, public nf7::
|
||||
nf7::DirItem(nf7::DirItem::kWidget),
|
||||
life_(*this),
|
||||
log_(std::make_shared<nf7::LoggerRef>(*this)),
|
||||
mem_(std::move(data)) {
|
||||
mem_(std::move(data), *this) {
|
||||
nf7::FileBase::Install(*log_);
|
||||
|
||||
mem_.onRestore = [this]() { Touch(); };
|
||||
mem_.onCommit = [this]() { Touch(); };
|
||||
|
||||
socket_popup_.onSubmit = [this](auto&& i, auto&& o) {
|
||||
this->data().inputs = std::move(i);
|
||||
this->data().outputs = std::move(o);
|
||||
|
@@ -67,7 +67,7 @@ class Node final : public nf7::FileBase, public nf7::DirItem, public nf7::Node {
|
||||
log_(std::make_shared<nf7::LoggerRef>(*this)),
|
||||
obj_(*this, "obj_factory"),
|
||||
obj_editor_(obj_, [](auto& t) { return t.flags().contains("nf7::Node"); }),
|
||||
mem_(std::move(data)) {
|
||||
mem_(std::move(data), *this) {
|
||||
nf7::FileBase::Install(*log_);
|
||||
|
||||
mem_.data().obj.SetTarget(obj_);
|
||||
@@ -92,9 +92,6 @@ class Node final : public nf7::FileBase, public nf7::DirItem, public nf7::Node {
|
||||
|
||||
obj_.onChildMementoChange = [this]() { mem_.Commit(); };
|
||||
obj_.onEmplace = [this]() { mem_.Commit(); };
|
||||
|
||||
mem_.onRestore = [this]() { Touch(); };
|
||||
mem_.onCommit = [this]() { Touch(); };
|
||||
}
|
||||
|
||||
Node(nf7::Deserializer& ar) : Node(ar.env()) {
|
||||
|
@@ -46,9 +46,7 @@ class Imm final : public nf7::File, public nf7::DirItem, public nf7::Node {
|
||||
|
||||
Imm(nf7::Env& env, nf7::gui::Value&& v = {}) noexcept :
|
||||
nf7::File(kType, env), nf7::DirItem(DirItem::kNone),
|
||||
life_(*this), mem_(std::move(v)) {
|
||||
mem_.onRestore = [this]() { Touch(); };
|
||||
mem_.onCommit = [this]() { Touch(); };
|
||||
life_(*this), mem_(std::move(v), *this) {
|
||||
}
|
||||
|
||||
Imm(nf7::Deserializer& ar) : Imm(ar.env()) {
|
||||
|
@@ -762,9 +762,7 @@ class Network::Terminal : public nf7::File,
|
||||
|
||||
Terminal(nf7::Env& env, Data&& data = {}) noexcept :
|
||||
nf7::File(kType, env),
|
||||
life_(*this), mem_(std::move(data)) {
|
||||
mem_.onRestore = [this]() { Touch(); };
|
||||
mem_.onCommit = [this]() { Touch(); };
|
||||
life_(*this), mem_(std::move(data), *this) {
|
||||
}
|
||||
|
||||
Terminal(nf7::Deserializer& ar) : Terminal(ar.env()) {
|
||||
|
@@ -58,11 +58,8 @@ class Ref final : public nf7::FileBase, public nf7::Node {
|
||||
nf7::FileBase(kType, env),
|
||||
life_(*this),
|
||||
log_(std::make_shared<nf7::LoggerRef>(*this)),
|
||||
mem_(std::move(data)) {
|
||||
mem_(std::move(data), *this) {
|
||||
nf7::FileBase::Install(*log_);
|
||||
|
||||
mem_.onRestore = [this]() { Touch(); };
|
||||
mem_.onCommit = [this]() { Touch(); };
|
||||
}
|
||||
|
||||
Ref(nf7::Deserializer& ar) : Ref(ar.env()) {
|
||||
|
@@ -72,15 +72,12 @@ class Adaptor final : public nf7::FileBase, public nf7::Sequencer {
|
||||
target_(*this, "target"),
|
||||
target_editor_(target_,
|
||||
[](auto& t) { return t.flags().contains("nf7::Sequencer"); }),
|
||||
mem_(std::move(data)) {
|
||||
mem_(std::move(data), *this) {
|
||||
mem_.data().target.SetTarget(target_);
|
||||
mem_.CommitAmend();
|
||||
|
||||
target_.onChildMementoChange = [this]() { mem_.Commit(); };
|
||||
target_.onEmplace = [this]() { mem_.Commit(); };
|
||||
|
||||
mem_.onRestore = [this]() { Touch(); };
|
||||
mem_.onCommit = [this]() { Touch(); };
|
||||
}
|
||||
|
||||
Adaptor(nf7::Deserializer& ar) : Adaptor(ar.env()) {
|
||||
|
@@ -57,15 +57,12 @@ class Call final : public nf7::FileBase, public nf7::Sequencer {
|
||||
callee_(*this, "callee"),
|
||||
callee_editor_(callee_,
|
||||
[](auto& t) { return t.flags().contains("nf7::Node"); }),
|
||||
mem_(std::move(data)){
|
||||
mem_(std::move(data), *this){
|
||||
mem_.data().callee.SetTarget(callee_);
|
||||
mem_.CommitAmend();
|
||||
|
||||
callee_.onChildMementoChange = [this]() { mem_.Commit(); };
|
||||
callee_.onEmplace = [this]() { mem_.Commit(); };
|
||||
|
||||
mem_.onRestore = [this]() { Touch(); };
|
||||
mem_.onCommit = [this]() { Touch(); };
|
||||
}
|
||||
|
||||
Call(nf7::Deserializer& ar) : Call(ar.env()) {
|
||||
|
@@ -87,12 +87,12 @@ class NativeFile final : public nf7::FileBase,
|
||||
life_(*this),
|
||||
shared_(std::make_shared<SharedData>(*this)),
|
||||
th_(std::make_shared<Thread>(*this, Runner {shared_})),
|
||||
mem_(std::move(data)),
|
||||
mem_(std::move(data), *this),
|
||||
config_popup_(*this) {
|
||||
nf7::FileBase::Install(shared_->log);
|
||||
|
||||
mem_.onRestore = [this]() { Refresh(); Touch(); };
|
||||
mem_.onCommit = [this]() { Refresh(); Touch(); };
|
||||
mem_.onRestore = [this]() { Refresh(); };
|
||||
mem_.onCommit = [this]() { Refresh(); };
|
||||
}
|
||||
|
||||
NativeFile(nf7::Deserializer& ar) : NativeFile(ar.env()) {
|
||||
|
@@ -70,12 +70,9 @@ class Curve final : public nf7::File,
|
||||
nf7::DirItem(nf7::DirItem::kWidget),
|
||||
nf7::Sequencer(nf7::Sequencer::kCustomItem |
|
||||
nf7::Sequencer::kParamPanel),
|
||||
life_(*this), mem_(std::move(data)) {
|
||||
life_(*this), mem_(std::move(data), *this) {
|
||||
AssignId();
|
||||
Sanitize();
|
||||
|
||||
mem_.onRestore = [this]() { Touch(); };
|
||||
mem_.onCommit = [this]() { Touch(); };
|
||||
}
|
||||
|
||||
Curve(nf7::Deserializer& ar) : Curve(ar.env()) {
|
||||
|
15
nf7.cc
15
nf7.cc
@@ -101,6 +101,13 @@ void File::Isolate() noexcept {
|
||||
parent_ = nullptr;
|
||||
name_ = "";
|
||||
}
|
||||
void File::Touch() noexcept {
|
||||
env().ExecMain(std::make_shared<nf7::GenericContext>(*this), [this]() {
|
||||
if (id()) {
|
||||
env().Handle( {.id = id(), .type = Event::kUpdate});
|
||||
}
|
||||
});
|
||||
}
|
||||
File& File::FindOrThrow(std::string_view name) const {
|
||||
if (auto ret = Find(name)) return *ret;
|
||||
throw NotFoundException("missing child: "+std::string(name));
|
||||
@@ -164,10 +171,6 @@ File& File::ancestorOrThrow(size_t dist) const {
|
||||
if (!f) throw NotFoundException("cannot go up over the root");
|
||||
return const_cast<File&>(*f);
|
||||
}
|
||||
void File::Touch() noexcept {
|
||||
if (!id()) return;
|
||||
env().ExecHandle({.id = id(), .type = Event::kUpdate});
|
||||
}
|
||||
|
||||
File::TypeInfo::TypeInfo(const std::string& name,
|
||||
std::unordered_set<std::string>&& flags) noexcept :
|
||||
@@ -248,10 +251,6 @@ File& Env::GetFileOrThrow(File::Id id) const {
|
||||
if (auto ret = GetFile(id)) return *ret;
|
||||
throw ExpiredException("file ("+std::to_string(id)+") is expired");
|
||||
}
|
||||
void Env::ExecHandle(const File::Event& ev) noexcept {
|
||||
ExecMain(std::make_shared<nf7::GenericContext>(*this, ev.id),
|
||||
[this, ev]() { Handle(ev); });
|
||||
}
|
||||
|
||||
Env::Watcher::Watcher(Env& env) noexcept : env_(&env) {
|
||||
}
|
||||
|
6
nf7.hh
6
nf7.hh
@@ -94,6 +94,8 @@ class File {
|
||||
void MakeAsRoot() noexcept;
|
||||
void Isolate() noexcept;
|
||||
|
||||
void Touch() noexcept;
|
||||
|
||||
virtual void Update() noexcept { }
|
||||
virtual void Handle(const Event&) noexcept { }
|
||||
|
||||
@@ -122,9 +124,6 @@ class File {
|
||||
File* parent() const noexcept { return parent_; }
|
||||
const std::string& name() const noexcept { return name_; }
|
||||
|
||||
protected:
|
||||
void Touch() noexcept;
|
||||
|
||||
private:
|
||||
const TypeInfo* const type_;
|
||||
Env* const env_;
|
||||
@@ -273,7 +272,6 @@ class Env {
|
||||
virtual void ExecAsync(const std::shared_ptr<Context>&, Task&&, Time = {}) noexcept = 0;
|
||||
|
||||
virtual void Handle(const File::Event&) noexcept = 0;
|
||||
void ExecHandle(const File::Event&) noexcept;
|
||||
|
||||
virtual void Save() noexcept = 0;
|
||||
virtual void Throw(std::exception_ptr&&) noexcept = 0;
|
||||
|
Reference in New Issue
Block a user