improve nf7::FileBase::Feature to install itself automatically by its constructor

This commit is contained in:
falsycat 2022-11-05 22:05:45 +09:00
parent 245884fae7
commit dd14217f5b
17 changed files with 33 additions and 45 deletions

View File

@ -1,5 +1,6 @@
#pragma once
#include <algorithm>
#include <string_view>
#include <vector>
@ -12,24 +13,22 @@ class FileBase : public nf7::File {
public:
class Feature {
public:
Feature() = default;
Feature() = delete;
Feature(nf7::FileBase& f) noexcept {
f.feats_.push_back(this);
}
virtual ~Feature() = default;
Feature(const Feature&) = delete;
Feature(Feature&&) = delete;
Feature& operator=(const Feature&) = delete;
Feature& operator=(Feature&&) = delete;
// Feature* is just for avoiding multi inheritance issues with Env::Watcher
virtual nf7::File* Find(std::string_view) const noexcept { return nullptr; }
virtual void Handle(const nf7::File::Event&) noexcept { }
virtual void Update() noexcept { }
};
FileBase(const nf7::File::TypeInfo& t,
nf7::Env& env,
std::vector<Feature*>&& feats = {}) noexcept :
nf7::File(t, env), feats_(std::move(feats)) {
}
using nf7::File::File;
nf7::File* Find(std::string_view name) const noexcept override {
for (auto feat : feats_) {
@ -50,11 +49,6 @@ class FileBase : public nf7::File {
}
}
protected:
void Install(Feature& f) noexcept {
feats_.push_back(&f);
}
private:
std::vector<Feature*> feats_;
};

View File

@ -35,11 +35,12 @@ 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,
FileHolder(nf7::FileBase& owner, std::string_view id,
nf7::MutableMemento* mem = nullptr) noexcept :
nf7::FileBase::Feature(owner),
owner_(&owner), mem_(mem), id_(id) {
}
FileHolder(nf7::File& owner, std::string_view id,
FileHolder(nf7::FileBase& owner, std::string_view id,
nf7::MutableMemento& mem) noexcept :
FileHolder(owner, id, &mem) {
}

View File

@ -55,7 +55,8 @@ class FileHolderEditor final : public nf7::FileBase::Feature {
kRef,
};
FileHolderEditor(nf7::FileHolder& h, FileFactory::Filter&& filter) noexcept :
FileHolderEditor(nf7::FileBase& f, nf7::FileHolder& h, FileFactory::Filter&& filter) noexcept :
nf7::FileBase::Feature(f),
holder_(&h), factory_(h.owner(), std::move(filter)) {
}
FileHolderEditor(const FileHolderEditor&) = delete;

View File

@ -18,8 +18,8 @@ namespace nf7 {
class LoggerRef final : public nf7::FileBase::Feature {
public:
LoggerRef(nf7::File& f, nf7::File::Path&& p = {"_logger"}) noexcept :
file_(&f), path_(std::move(p)) {
LoggerRef(nf7::FileBase& f, nf7::File::Path&& p = {"_logger"}) noexcept :
nf7::FileBase::Feature(f), file_(&f), path_(std::move(p)) {
}
LoggerRef(const LoggerRef&) = default;
LoggerRef(LoggerRef&&) = default;

View File

@ -12,7 +12,9 @@ namespace nf7 {
class NFileWatcher final : public nf7::FileBase::Feature {
public:
NFileWatcher() = default;
NFileWatcher() = delete;
NFileWatcher(nf7::FileBase& f) noexcept : nf7::FileBase::Feature(f) {
}
NFileWatcher(const NFileWatcher&) = delete;
NFileWatcher(NFileWatcher&&) = delete;
NFileWatcher& operator=(const NFileWatcher&) = delete;

View File

@ -100,7 +100,7 @@ class Device final : public nf7::FileBase, public nf7::DirItem, public nf7::Node
};
Device(nf7::Env& env, Data&& data = {}) noexcept :
nf7::FileBase(kType, env, {&log_}),
nf7::FileBase(kType, env),
nf7::DirItem(nf7::DirItem::kMenu | nf7::DirItem::kTooltip),
nf7::Node(nf7::Node::kNone),
life_(*this), log_(*this), mem_(std::move(data), *this) {

View File

@ -69,14 +69,14 @@ class FontFace final : public nf7::FileBase,
};
FontFace(nf7::Env& env, Data&& d = {}) noexcept :
nf7::FileBase(kType, env, {&nwatch_}),
nf7::FileBase(kType, env),
nf7::DirItem(nf7::DirItem::kMenu |
nf7::DirItem::kTooltip),
nf7::Node(nf7::Node::kNone),
life_(*this),
nwatch_(*this),
log_(std::make_shared<nf7::LoggerRef>(*this)),
mem_(std::move(d), *this) {
nf7::FileBase::Install(*log_);
mem_.onCommit = mem_.onRestore = nwatch_.onMod = [this]() {
cache_ = std::nullopt;
};

View File

@ -95,17 +95,14 @@ class ObjBase : public nf7::FileBase,
}
ObjBase(nf7::Env& env, T&& data = {}) noexcept :
nf7::FileBase(TypeInfo::kType, env, {}),
nf7::FileBase(TypeInfo::kType, env),
nf7::DirItem(nf7::DirItem::kMenu |
nf7::DirItem::kTooltip),
nf7::Node(nf7::Node::kNone),
life_(*this),
log_(std::make_shared<nf7::LoggerRef>(*this)),
nwatch_(std::make_shared<nf7::NFileWatcher>()),
nwatch_(std::make_shared<nf7::NFileWatcher>(*this)),
mem_(std::move(data), *this) {
nf7::FileBase::Install(*log_);
nf7::FileBase::Install(*nwatch_);
nwatch_->onMod = mem_.onRestore = mem_.onCommit = [this]() {
Drop();
};

View File

@ -62,15 +62,13 @@ class Node final : public nf7::FileBase, public nf7::DirItem, public nf7::Node {
};
Node(nf7::Env& env, Data&& data = {}) noexcept :
nf7::FileBase(kType, env, {}),
nf7::FileBase(kType, env),
nf7::DirItem(nf7::DirItem::kMenu | nf7::DirItem::kWidget),
nf7::Node(nf7::Node::kCustomNode),
life_(*this),
log_(std::make_shared<nf7::LoggerRef>(*this)),
mem_(std::move(data), *this),
importer_(std::make_shared<nf7::luajit::NFileImporter>(env.npath())) {
nf7::FileBase::Install(*log_);
mem_.onCommit = mem_.onRestore = [this]() {
cache_ = std::nullopt;
};

View File

@ -62,8 +62,6 @@ class Ref final : public nf7::FileBase, public nf7::Node {
life_(*this),
log_(std::make_shared<nf7::LoggerRef>(*this)),
mem_(std::move(data), *this) {
nf7::FileBase::Install(*log_);
mem_.onRestore = mem_.onCommit = [this]() { SetUpWatcher(); };
}

View File

@ -63,13 +63,13 @@ class Adaptor final : public nf7::FileBase, public nf7::Sequencer {
};
Adaptor(nf7::Env& env, Data&& data = {}) noexcept :
nf7::FileBase(kType, env, {&target_, &target_editor_}),
nf7::FileBase(kType, env),
Sequencer(Sequencer::kCustomItem |
Sequencer::kTooltip |
Sequencer::kParamPanel),
life_(*this),
target_(*this, "target", mem_),
target_editor_(target_,
target_editor_(*this, target_,
[](auto& t) { return t.flags().contains("nf7::Sequencer"); }),
mem_(std::move(data), *this) {
mem_.data().target.SetTarget(target_);

View File

@ -48,13 +48,13 @@ class Call final : public nf7::FileBase, public nf7::Sequencer {
};
Call(nf7::Env& env, Data&& data = {}) noexcept :
FileBase(kType, env, {&callee_, &callee_editor_}),
nf7::FileBase(kType, env),
Sequencer(Sequencer::kCustomItem |
Sequencer::kTooltip |
Sequencer::kParamPanel),
life_(*this),
callee_(*this, "callee", mem_),
callee_editor_(callee_,
callee_editor_(*this, callee_,
[](auto& t) { return t.flags().contains("nf7::Node"); }),
mem_(std::move(data), *this) {
mem_.data().callee.SetTarget(callee_);

View File

@ -69,7 +69,7 @@ class TL final : public nf7::FileBase, public nf7::DirItem, public nf7::Node {
std::vector<std::unique_ptr<Layer>>&& layers = {},
ItemId next = 1,
const nf7::gui::Window* win = nullptr) noexcept :
nf7::FileBase(kType, env, {&log_}),
nf7::FileBase(kType, env),
nf7::DirItem(nf7::DirItem::kMenu | nf7::DirItem::kWidget),
nf7::Node(nf7::Node::kMenu_DirItem),
life_(*this), log_(*this),

View File

@ -44,12 +44,12 @@ class Event final : public nf7::FileBase, public nf7::DirItem, public nf7::Node
};
Event(nf7::Env& env, Data&& data = {}) noexcept :
nf7::FileBase(kType, env, {&logger_, &handler_, &handler_editor_}),
nf7::FileBase(kType, env),
nf7::DirItem(nf7::DirItem::kMenu | nf7::DirItem::kWidget),
nf7::Node(nf7::Node::kMenu_DirItem),
life_(*this), logger_(*this),
handler_(*this, "handler", mem_),
handler_editor_(handler_,
handler_editor_(*this, handler_,
[](auto& t) { return t.flags().contains("nf7::Node"); }),
la_root_(std::make_shared<nf7::Node::Lambda>(*this)),
mem_(std::move(data)) {

View File

@ -17,7 +17,6 @@
#include "nf7.hh"
#include "common/dir_item.hh"
#include "common/file_base.hh"
#include "common/generic_context.hh"
#include "common/generic_memento.hh"
#include "common/generic_type_info.hh"
@ -111,7 +110,7 @@ class Logger final : public nf7::File,
};
Logger(nf7::Env& env, Data&& d = {}) noexcept :
File(kType, env), DirItem(DirItem::kMenu),
nf7::File(kType, env), DirItem(DirItem::kMenu),
mem_(std::move(d), *this), win_(*this, "Log View") {
win_.shown() = true;

View File

@ -112,16 +112,14 @@ class NFile final : public nf7::FileBase,
};
NFile(nf7::Env& env, Data&& data = {}) noexcept :
nf7::FileBase(kType, env, {&nwatch_}),
nf7::FileBase(kType, env),
nf7::DirItem(nf7::DirItem::kMenu |
nf7::DirItem::kTooltip),
nf7::Node(nf7::Node::kMenu_DirItem),
life_(*this),
life_(*this), nwatch_(*this),
shared_(std::make_shared<SharedData>(*this)),
th_(std::make_shared<Thread>(*this, Runner {shared_})),
mem_(std::move(data), *this) {
nf7::FileBase::Install(shared_->log);
mtx_.onLock = [this]() { SetUp(); };
mtx_.onUnlock = [this]() { shared_->nfile.reset(); };

View File

@ -110,7 +110,7 @@ class Plot final : public nf7::FileBase,
};
Plot(nf7::Env& env, const nf7::gui::Window* win = nullptr, Data&& data = {}) noexcept :
nf7::FileBase(kType, env, {&log_}),
nf7::FileBase(kType, env),
nf7::DirItem(nf7::DirItem::kMenu | nf7::DirItem::kWidget),
nf7::Node(nf7::Node::kNone),
life_(*this), log_(*this), win_(*this, "Plot", win), mem_(std::move(data)) {