add MutableMemento and improve FileHolder

This commit is contained in:
falsycat 2022-08-26 23:13:17 +09:00
parent bc3143016c
commit 651bad7625
8 changed files with 45 additions and 22 deletions

View File

@ -82,6 +82,7 @@ target_sources(nf7
common/luajit_thread_lambda.hh
common/memento.hh
common/memento_recorder.hh
common/mutable_memento.hh
common/native_file.hh
common/node.hh
common/node_link_store.hh

View File

@ -64,6 +64,7 @@ void FileHolder::SetUp() noexcept {
auto ptag = std::exchange(tag_, mem->Save());
if (ptag != tag_) {
onChildMementoChange();
if (mem_) mem_->Commit(); // commit owner's memento
}
}
onChildUpdate();

View File

@ -16,6 +16,7 @@
#include "common/file_base.hh"
#include "common/generic_watcher.hh"
#include "common/memento.hh"
#include "common/mutable_memento.hh"
#include "common/yas_nf7.hh"
#include "common/yas_std_variant.hh"
@ -34,8 +35,13 @@ class FileHolder : public nf7::FileBase::Feature {
using Entity = std::variant<
std::monostate, nf7::File::Path, std::shared_ptr<nf7::File>>;
FileHolder(nf7::File& owner, std::string_view id) noexcept :
owner_(&owner), id_(id) {
FileHolder(nf7::File& owner, std::string_view id,
nf7::MutableMemento* mem = nullptr) noexcept :
owner_(&owner), mem_(mem), id_(id) {
}
FileHolder(nf7::File& owner, std::string_view id,
nf7::MutableMemento& mem) noexcept :
FileHolder(owner, id, &mem) {
}
FileHolder(const FileHolder&) = delete;
FileHolder(FileHolder&&) = delete;
@ -62,6 +68,7 @@ class FileHolder : public nf7::FileBase::Feature {
SetUp();
onEmplace();
if (mem_) mem_->Commit();
}
void Emplace(std::unique_ptr<nf7::File>&& f) noexcept {
TearDown();
@ -70,6 +77,7 @@ class FileHolder : public nf7::FileBase::Feature {
SetUp();
onEmplace();
if (mem_) mem_->Commit();
}
nf7::File& GetFileOrThrow() {
@ -108,7 +116,7 @@ class FileHolder : public nf7::FileBase::Feature {
return own()? nf7::File::Path {{id_}}: std::get<nf7::File::Path>(entity_);
}
// called when kUpdate event is caused by the child
// called when kUpdate event is happened on the child
std::function<void(void)> onChildUpdate = [](){};
// called when the child's memento tag id is changed
@ -118,7 +126,9 @@ class FileHolder : public nf7::FileBase::Feature {
std::function<void(void)> onEmplace = [](){};
private:
nf7::File* const owner_;
nf7::File* const owner_;
nf7::MutableMemento* const mem_;
const std::string id_;
Entity entity_;

View File

@ -7,13 +7,13 @@
#include "nf7.hh"
#include "common/memento.hh"
#include "common/mutable_memento.hh"
namespace nf7 {
template <typename T>
class GenericMemento : public nf7::Memento {
class GenericMemento : public nf7::MutableMemento {
public:
class CustomTag;
@ -45,12 +45,12 @@ class GenericMemento : public nf7::Memento {
onRestore();
if (file_) file_->Touch();
}
void Commit() noexcept {
void Commit() noexcept override {
tag_ = nullptr;
onCommit();
if (file_) file_->Touch();
}
void CommitAmend() noexcept {
void CommitAmend() noexcept override {
if (!tag_) return;
auto itr = map_.find(tag_->id());
assert(itr != map_.end());

20
common/mutable_memento.hh Normal file
View File

@ -0,0 +1,20 @@
#pragma once
#include "common/memento.hh"
namespace nf7 {
class MutableMemento : public nf7::Memento {
public:
MutableMemento() = default;
MutableMemento(const MutableMemento&) = delete;
MutableMemento(MutableMemento&&) = delete;
MutableMemento& operator=(const MutableMemento&) = delete;
MutableMemento& operator=(MutableMemento&&) = delete;
virtual void Commit() noexcept = 0;
virtual void CommitAmend() noexcept = 0;
};
} // namespace nf7

View File

@ -65,7 +65,7 @@ class Node final : public nf7::FileBase, public nf7::DirItem, public nf7::Node {
nf7::DirItem(nf7::DirItem::kTooltip | nf7::DirItem::kWidget),
life_(*this),
log_(std::make_shared<nf7::LoggerRef>(*this)),
obj_(*this, "obj_factory"),
obj_(*this, "obj_factory", mem_),
obj_editor_(obj_, [](auto& t) { return t.flags().contains("nf7::Node"); }),
mem_(std::move(data), *this) {
nf7::FileBase::Install(*log_);
@ -83,15 +83,12 @@ class Node final : public nf7::FileBase, public nf7::DirItem, public nf7::Node {
});
};
obj_.onChildUpdate = [this]() {
obj_.onEmplace = obj_.onChildUpdate = [this]() {
if (fu_) {
log_->Info("factory update detected, dropping cache");
}
fu_ = std::nullopt;
};
obj_.onChildMementoChange = [this]() { mem_.Commit(); };
obj_.onEmplace = [this]() { mem_.Commit(); };
}
Node(nf7::Deserializer& ar) : Node(ar.env()) {

View File

@ -69,15 +69,12 @@ class Adaptor final : public nf7::FileBase, public nf7::Sequencer {
Sequencer::kTooltip |
Sequencer::kParamPanel),
life_(*this),
target_(*this, "target"),
target_(*this, "target", mem_),
target_editor_(target_,
[](auto& t) { return t.flags().contains("nf7::Sequencer"); }),
mem_(std::move(data), *this) {
mem_.data().target.SetTarget(target_);
mem_.CommitAmend();
target_.onChildMementoChange = [this]() { mem_.Commit(); };
target_.onEmplace = [this]() { mem_.Commit(); };
}
Adaptor(nf7::Deserializer& ar) : Adaptor(ar.env()) {

View File

@ -54,15 +54,12 @@ class Call final : public nf7::FileBase, public nf7::Sequencer {
Sequencer::kTooltip |
Sequencer::kParamPanel),
life_(*this),
callee_(*this, "callee"),
callee_(*this, "callee", mem_),
callee_editor_(callee_,
[](auto& t) { return t.flags().contains("nf7::Node"); }),
mem_(std::move(data), *this){
mem_(std::move(data), *this) {
mem_.data().callee.SetTarget(callee_);
mem_.CommitAmend();
callee_.onChildMementoChange = [this]() { mem_.Commit(); };
callee_.onEmplace = [this]() { mem_.Commit(); };
}
Call(nf7::Deserializer& ar) : Call(ar.env()) {