From df56eb3462c87285476d5656ff545c0a616685e6 Mon Sep 17 00:00:00 2001 From: falsycat Date: Tue, 8 Nov 2022 11:40:14 +0900 Subject: [PATCH] add nf7::Node::Meta and improve nf7::Node interface --- common/node.hh | 25 +++++++++++++--- file/audio_device.cc | 13 ++++----- file/font_face.cc | 13 ++++----- file/gl_obj.cc | 7 ++--- file/luajit_node.cc | 7 ++--- file/node_imm.cc | 9 ++---- file/node_network.cc | 60 +++++++++++++++++--------------------- file/node_ref.cc | 12 ++++---- file/sequencer_call.cc | 3 +- file/sequencer_timeline.cc | 9 ++---- file/system_call.cc | 8 ++--- file/system_event.cc | 39 +++++++++---------------- file/system_nfile.cc | 10 ++----- file/value_curve.cc | 9 ++---- file/value_plot.cc | 8 ++--- 15 files changed, 94 insertions(+), 138 deletions(-) diff --git a/common/node.hh b/common/node.hh index c7983ca..005f1e4 100644 --- a/common/node.hh +++ b/common/node.hh @@ -28,6 +28,24 @@ class Node : public File::Interface { }; using Flags = uint8_t; + struct Meta final { + public: + Meta() = default; + Meta(std::vector&& i, std::vector&& o) noexcept : + inputs(std::move(i)), outputs(std::move(o)) { + } + Meta(const std::vector& i, const std::vector& o) noexcept : + inputs(i), outputs(o) { + } + + Meta(const Meta&) = default; + Meta(Meta&&) = default; + Meta& operator=(const Meta&) = default; + Meta& operator=(Meta&&) = default; + + std::vector inputs, outputs; + }; + static void ValidateSockets(std::span v) { for (auto itr = v.begin(); itr < v.end(); ++itr) { if (v.end() != std::find(itr+1, v.end(), *itr)) { @@ -50,13 +68,12 @@ class Node : public File::Interface { virtual void UpdateNode(Editor&) noexcept { } virtual void UpdateMenu(Editor&) noexcept { } - // The returned span is alive until next operation to the file. - virtual std::span GetInputs() const noexcept = 0; - virtual std::span GetOutputs() const noexcept = 0; + // don't call too often because causes heap allocation + virtual Meta GetMeta() const noexcept = 0; Flags flags() const noexcept { return flags_; } - protected: + private: Flags flags_; }; diff --git a/file/audio_device.cc b/file/audio_device.cc index 26ae60f..c162581 100644 --- a/file/audio_device.cc +++ b/file/audio_device.cc @@ -122,13 +122,12 @@ class Device final : public nf7::FileBase, std::shared_ptr CreateLambda( const std::shared_ptr&) noexcept override; - std::span GetInputs() const noexcept override { - static const std::vector kInputs = {"info", "mix", "peek"}; - return kInputs; - } - std::span GetOutputs() const noexcept override { - static const std::vector kOutputs = {"result"}; - return kOutputs; + nf7::Node::Meta GetMeta() const noexcept override { + static const std::vector kInputs = {}; + return { + {"info", "mix", "peek"}, + {"result"} + }; } nf7::Future> Build() noexcept; diff --git a/file/font_face.cc b/file/font_face.cc index c3c84e1..13e04ae 100644 --- a/file/font_face.cc +++ b/file/font_face.cc @@ -106,16 +106,13 @@ class FontFace final : public nf7::FileBase, return {std::current_exception()}; } - std::span GetInputs() const noexcept override { - static const std::vector kInputs = {"command"}; - return kInputs; - } - std::span GetOutputs() const noexcept override { - static const std::vector kOutputs = {"result"}; - return kOutputs; - } std::shared_ptr CreateLambda( const std::shared_ptr&) noexcept override; + nf7::Node::Meta GetMeta() const noexcept override { + return { + {"command"}, {"result"}, + }; + } void UpdateMenu() noexcept override; void UpdateTooltip() noexcept override; diff --git a/file/gl_obj.cc b/file/gl_obj.cc index 8ce6ead..13f5f73 100644 --- a/file/gl_obj.cc +++ b/file/gl_obj.cc @@ -140,11 +140,8 @@ class ObjBase : public nf7::FileBase, const std::shared_ptr& parent) noexcept override { return std::make_shared(*this, parent); } - std::span GetInputs() const noexcept override { - return T::kInputs; - } - std::span GetOutputs() const noexcept override { - return T::kOutputs; + nf7::Node::Meta GetMeta() const noexcept override { + return {T::kInputs, T::kOutputs}; } ResourceFuture Create() noexcept final { diff --git a/file/luajit_node.cc b/file/luajit_node.cc index 7cc130b..11c5e3b 100644 --- a/file/luajit_node.cc +++ b/file/luajit_node.cc @@ -90,11 +90,8 @@ class Node final : public nf7::FileBase, std::shared_ptr CreateLambda( const std::shared_ptr&) noexcept override; - std::span GetInputs() const noexcept override { - return mem_->inputs; - } - std::span GetOutputs() const noexcept override { - return mem_->outputs; + nf7::Node::Meta GetMeta() const noexcept override { + return nf7::Node::Meta {mem_->inputs, mem_->outputs}; } nf7::Future> Build() noexcept; diff --git a/file/node_imm.cc b/file/node_imm.cc index 2cda9e9..ee1cf59 100644 --- a/file/node_imm.cc +++ b/file/node_imm.cc @@ -65,13 +65,8 @@ class Imm final : public nf7::FileBase, std::shared_ptr CreateLambda( const std::shared_ptr&) noexcept override; - std::span GetInputs() const noexcept override { - static const std::vector kInputs = {"in"}; - return kInputs; - } - std::span GetOutputs() const noexcept override { - static const std::vector kOutputs = {"out"}; - return kOutputs; + nf7::Node::Meta GetMeta() const noexcept override { + return {{"in"}, {"out"}}; } void UpdateNode(nf7::Node::Editor&) noexcept override; diff --git a/file/node_network.cc b/file/node_network.cc index 71a4a40..cbb9ac1 100644 --- a/file/node_network.cc +++ b/file/node_network.cc @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -163,12 +164,8 @@ class Network final : public nf7::FileBase, std::shared_ptr CreateLambda( const std::shared_ptr&) noexcept override; - - std::span GetInputs() const noexcept override { - return mem_->inputs; - } - std::span GetOutputs() const noexcept override { - return mem_->outputs; + nf7::Node::Meta GetMeta() const noexcept override { + return {mem_->inputs, mem_->outputs}; } File::Interface* interface(const std::type_info& t) noexcept override { @@ -327,6 +324,7 @@ class Network::Item final { nf7::Env& env() const noexcept { return file_->env(); } nf7::File& file() const noexcept { return *file_; } nf7::Node& node() const noexcept { return *node_; } + const nf7::Node::Meta& meta() const noexcept { return meta_; } InternalNode* inode() const noexcept { return inode_; } InternalNode::Flags iflags() const noexcept { return inode_? inode_->flags(): 0; } @@ -335,8 +333,9 @@ class Network::Item final { ItemId id_; std::unique_ptr file_; - nf7::Node* node_; - InternalNode* inode_; + nf7::Node* node_; + InternalNode* inode_; + nf7::Node::Meta meta_; std::optional mem_; @@ -365,7 +364,9 @@ class Network::Item final { node_ = &file_->interfaceOrThrow(); mem_.emplace(file_->interface()); - inode_ = file_->interface(); + inode_ = file_->interface(); + meta_ = node_->GetMeta(); + prev_pos_ = pos_; } }; @@ -717,12 +718,8 @@ class Network::Initiator final : public nf7::File, }; return std::make_shared(*this, parent); } - std::span GetInputs() const noexcept { - return {}; - } - std::span GetOutputs() const noexcept { - static const std::vector kOutputs = {"out"}; - return kOutputs; + nf7::Node::Meta GetMeta() const noexcept { + return {{}, {"out"}}; } void UpdateNode(nf7::Node::Editor& ed) noexcept override; @@ -769,20 +766,14 @@ class Network::Terminal : public nf7::FileBase, const std::shared_ptr& parent) noexcept override { return std::make_shared(*this, parent); } - - std::span GetInputs() const noexcept override { - if (data().type == kOutput) { - static const std::vector kInputs = {"in"}; - return kInputs; + nf7::Node::Meta GetMeta() const noexcept override { + switch (data().type) { + case kInput: + return {{}, {"out"}}; + case kOutput: + return {{"in"}, {}}; } - return {}; - } - std::span GetOutputs() const noexcept override { - if (data().type == kInput) { - static const std::vector kInputs = {"out"}; - return kInputs; - } - return {}; + std::abort(); } void UpdateNode(nf7::Node::Editor&) noexcept override; @@ -867,7 +858,7 @@ void Network::Sanitize() { // remove expired links for (const auto& item : items_) { auto cmd = links_.CreateCommandToRemoveExpired( - item->id(), item->node().GetInputs(), item->node().GetOutputs()); + item->id(), item->meta().inputs, item->meta().outputs); if (cmd) { cmd->Apply(); } @@ -937,7 +928,6 @@ void Network::Item::Detach() noexcept { } void Network::Item::Watcher::Handle(const File::Event& ev) noexcept { auto& item = *owner_; - auto& node = item.node(); switch (ev.type) { case File::Event::kUpdate: @@ -945,8 +935,10 @@ void Network::Item::Watcher::Handle(const File::Event& ev) noexcept { auto& net = *item.owner_; net.Touch(); - const auto inputs = node.GetInputs(); - const auto outputs = node.GetOutputs(); + // update metadata + item.meta_ = item.node().GetMeta(); + const auto& inputs = item.meta().inputs; + const auto& outputs = item.meta().outputs; // check expired sockets if (auto cmd = net.links_.CreateCommandToRemoveExpired(item.id(), inputs, outputs)) { @@ -1195,9 +1187,9 @@ void Network::Item::UpdateNode(Node::Editor& ed) noexcept { node_->UpdateNode(ed); } else { ImGui::TextUnformatted(file_->type().name().c_str()); - nf7::gui::NodeInputSockets(node_->GetInputs()); + nf7::gui::NodeInputSockets(meta_.inputs); ImGui::SameLine(); - nf7::gui::NodeOutputSockets(node_->GetOutputs()); + nf7::gui::NodeOutputSockets(meta_.outputs); } } ImNodes::EndNode(); diff --git a/file/node_ref.cc b/file/node_ref.cc index ebfacae..eac988f 100644 --- a/file/node_ref.cc +++ b/file/node_ref.cc @@ -77,11 +77,8 @@ class Ref final : public nf7::FileBase, public nf7::Node { std::shared_ptr CreateLambda( const std::shared_ptr&) noexcept override; - std::span GetInputs() const noexcept override { - return mem_->inputs; - } - std::span GetOutputs() const noexcept override { - return mem_->outputs; + nf7::Node::Meta GetMeta() const noexcept override { + return {mem_->inputs, mem_->outputs}; } void UpdateNode(nf7::Node::Editor&) noexcept override; @@ -116,12 +113,13 @@ class Ref final : public nf7::FileBase, public nf7::Node { bool mod = false; try { auto& n = target().interfaceOrThrow(); + const auto meta = n.GetMeta(); - const auto srci = n.GetInputs(); + const auto& srci = meta.inputs; mod |= std::equal(dsti.begin(), dsti.end(), srci.begin(), srci.end()); dsti = std::vector{srci.begin(), srci.end()}; - const auto srco = n.GetOutputs(); + const auto& srco = meta.outputs; mod |= std::equal(dsto.begin(), dsto.end(), srco.begin(), srco.end()); dsto = std::vector{srco.begin(), srco.end()}; } catch (nf7::Exception& e) { diff --git a/file/sequencer_call.cc b/file/sequencer_call.cc index 0a43bcb..4831d1b 100644 --- a/file/sequencer_call.cc +++ b/file/sequencer_call.cc @@ -180,7 +180,8 @@ try { } ssla_->Listen(*file_, ss); - for (const auto& name : node.GetInputs()) { + const auto inputs = node.GetMeta().inputs; + for (const auto& name : inputs) { if (auto v = ss->Receive(name)) { la_->Handle(name, *v, ssla_); } diff --git a/file/sequencer_timeline.cc b/file/sequencer_timeline.cc index 41e4418..a343880 100644 --- a/file/sequencer_timeline.cc +++ b/file/sequencer_timeline.cc @@ -90,13 +90,8 @@ class TL final : public nf7::FileBase, public nf7::DirItem, public nf7::Node { std::shared_ptr CreateLambda( const std::shared_ptr&) noexcept override; - std::span GetInputs() const noexcept override { - static const std::vector kInputs = {"exec"}; - return kInputs; - } - std::span GetOutputs() const noexcept override { - static const std::vector kOutputs = {"result"}; - return kOutputs; + nf7::Node::Meta GetMeta() const noexcept override { + return {{"exec"}, {"result"}}; } void PostHandle(const nf7::File::Event& ev) noexcept; diff --git a/file/system_call.cc b/file/system_call.cc index 362875e..2695b83 100644 --- a/file/system_call.cc +++ b/file/system_call.cc @@ -51,12 +51,8 @@ class Call final : public nf7::File, public nf7::Node { std::shared_ptr CreateLambda( const std::shared_ptr&) noexcept override; - std::span GetInputs() const noexcept override { - static const std::vector kInputs = {"save", "exit", "abort", "panic"}; - return kInputs; - } - std::span GetOutputs() const noexcept override { - return {}; + nf7::Node::Meta GetMeta() const noexcept override { + return {{"save", "exit", "abort", "panic"}, {}}; } void UpdateNode(nf7::Node::Editor&) noexcept override; diff --git a/file/system_event.cc b/file/system_event.cc index c278b97..044deb2 100644 --- a/file/system_event.cc +++ b/file/system_event.cc @@ -92,12 +92,8 @@ class Event final : public nf7::FileBase, std::shared_ptr CreateLambda( const std::shared_ptr&) noexcept override; - std::span GetInputs() const noexcept override { - static const std::vector kInputs = {"value"}; - return kInputs; - } - std::span GetOutputs() const noexcept override { - return {}; + nf7::Node::Meta GetMeta() const noexcept override { + return {{"value"}, {}}; } void PostUpdate() noexcept override; @@ -121,12 +117,6 @@ class Event final : public nf7::FileBase, nf7::Node& GetHandler() const { return ResolveOrThrow(mem_->handler).interfaceOrThrow(); } - std::span GetHandlerInputs() const noexcept - try { - return GetHandler().GetInputs(); - } catch (nf7::Exception&) { - return {}; - } std::shared_ptr CreateLambdaIf() noexcept { try { if (!la_) { @@ -180,21 +170,18 @@ std::shared_ptr Event::CreateLambda( void Event::PostUpdate() noexcept { const auto& io = ImGui::GetIO(); - const auto in = GetHandlerInputs(); - if (in.end() != std::find(in.begin(), in.end(), "key")) { - for (size_t i = 0; i < ImGuiKey_KeysData_SIZE; ++i) { - const auto& key = io.KeysData[i]; - const char* event = nullptr; - if (key.DownDuration == 0) { - event = "down"; - } else if (key.DownDurationPrev >= 0 && !key.Down) { - event = "up"; - } - if (event) { - const auto k = static_cast(i); - TriggerKeyEvent(ImGui::GetKeyName(k), event); - } + for (size_t i = 0; i < ImGuiKey_KeysData_SIZE; ++i) { + const auto& key = io.KeysData[i]; + const char* event = nullptr; + if (key.DownDuration == 0) { + event = "down"; + } else if (key.DownDurationPrev >= 0 && !key.Down) { + event = "up"; + } + if (event) { + const auto k = static_cast(i); + TriggerKeyEvent(ImGui::GetKeyName(k), event); } } } diff --git a/file/system_nfile.cc b/file/system_nfile.cc index 92a7e92..e58eba9 100644 --- a/file/system_nfile.cc +++ b/file/system_nfile.cc @@ -143,14 +143,8 @@ class NFile final : public nf7::FileBase, std::shared_ptr CreateLambda( const std::shared_ptr&) noexcept override; - - std::span GetInputs() const noexcept override { - static const std::vector kInputs = {"command"}; - return kInputs; - } - std::span GetOutputs() const noexcept override { - static const std::vector kOutputs = {"result"}; - return kOutputs; + nf7::Node::Meta GetMeta() const noexcept override { + return {{"command"}, {"result"}}; } void UpdateTooltip() noexcept override; diff --git a/file/value_curve.cc b/file/value_curve.cc index f3db1cd..e61dce8 100644 --- a/file/value_curve.cc +++ b/file/value_curve.cc @@ -97,13 +97,8 @@ class Curve final : public nf7::FileBase, std::shared_ptr CreateLambda( const std::shared_ptr&) noexcept override; - std::span GetInputs() const noexcept override { - static const std::vector kInputs = {"x"}; - return kInputs; - } - std::span GetOutputs() const noexcept override { - static const std::vector kOutputs = {"y"}; - return kOutputs; + nf7::Node::Meta GetMeta() const noexcept override { + return {{"x"}, {"y"}}; } void UpdateItem(nf7::Sequencer::Editor&) noexcept override; diff --git a/file/value_plot.cc b/file/value_plot.cc index 7ad759b..152e67d 100644 --- a/file/value_plot.cc +++ b/file/value_plot.cc @@ -134,12 +134,8 @@ class Plot final : public nf7::FileBase, std::shared_ptr CreateLambda( const std::shared_ptr&) noexcept override; - - std::span GetInputs() const noexcept override { - return inputs_; - } - std::span GetOutputs() const noexcept override { - return {}; + nf7::Node::Meta GetMeta() const noexcept override { + return {inputs_, {}}; } void UpdateMenu() noexcept override;