move nf7::Lambda to Node::Lambda
and move depth() field to nf7::Context
This commit is contained in:
parent
2156312009
commit
40112d224d
@ -67,7 +67,6 @@ target_sources(nf7
|
||||
common/gui_timeline.cc
|
||||
common/gui_window.hh
|
||||
common/history.hh
|
||||
common/lambda.hh
|
||||
common/lock.hh
|
||||
common/logger.hh
|
||||
common/logger_ref.hh
|
||||
|
@ -1,41 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string_view>
|
||||
|
||||
#include "nf7.hh"
|
||||
|
||||
#include "common/value.hh"
|
||||
|
||||
|
||||
namespace nf7 {
|
||||
|
||||
class Lambda : public nf7::Context {
|
||||
public:
|
||||
class Owner;
|
||||
|
||||
Lambda() = delete;
|
||||
Lambda(nf7::File& f, const std::shared_ptr<nf7::Lambda>& parent) noexcept :
|
||||
Lambda(f.env(), f.id(), parent) {
|
||||
}
|
||||
Lambda(Env& env, File::Id id, const std::shared_ptr<nf7::Lambda>& parent) noexcept :
|
||||
Context(env, id), depth_(parent? parent->depth()+1: 0), parent_(parent) {
|
||||
}
|
||||
virtual ~Lambda() = default;
|
||||
Lambda(const Lambda&) = delete;
|
||||
Lambda(Lambda&&) = delete;
|
||||
Lambda& operator=(const Lambda&) = delete;
|
||||
Lambda& operator=(Lambda&&) = delete;
|
||||
|
||||
virtual void Handle(std::string_view, const nf7::Value&, const std::shared_ptr<Lambda>&) noexcept { }
|
||||
|
||||
size_t depth() const noexcept { return depth_; }
|
||||
const std::weak_ptr<Lambda>& parent() const noexcept { return parent_; }
|
||||
|
||||
private:
|
||||
size_t depth_;
|
||||
|
||||
std::weak_ptr<Lambda> parent_;
|
||||
};
|
||||
|
||||
} // namespace nf7
|
@ -50,7 +50,7 @@ class Thread::Lambda final : public Thread::RegistryItem,
|
||||
|
||||
class Receiver;
|
||||
std::shared_ptr<Receiver> recv_;
|
||||
std::shared_ptr<nf7::Lambda> la_;
|
||||
std::shared_ptr<Node::Lambda> la_;
|
||||
|
||||
|
||||
std::shared_ptr<Thread> GetThread(lua_State* L) {
|
||||
@ -67,18 +67,18 @@ class Thread::Lambda final : public Thread::RegistryItem,
|
||||
|
||||
|
||||
// Receives an output from targetted lambda and Resumes the Thread.
|
||||
class Thread::Lambda::Receiver final : public nf7::Lambda,
|
||||
class Thread::Lambda::Receiver final : public Node::Lambda,
|
||||
public std::enable_shared_from_this<Thread::Lambda::Receiver> {
|
||||
public:
|
||||
static constexpr size_t kMaxQueue = 1024;
|
||||
|
||||
Receiver() = delete;
|
||||
Receiver(nf7::Env& env, nf7::File::Id id) noexcept :
|
||||
nf7::Lambda(env, id, nullptr) {
|
||||
Node::Lambda(env, id, nullptr) {
|
||||
}
|
||||
|
||||
void Handle(std::string_view name, const nf7::Value& v,
|
||||
const std::shared_ptr<nf7::Lambda>&) noexcept override {
|
||||
const std::shared_ptr<Node::Lambda>&) noexcept override {
|
||||
values_.emplace_back(name, v);
|
||||
if (values_.size() > kMaxQueue) {
|
||||
values_.pop_front();
|
||||
|
@ -11,7 +11,7 @@
|
||||
|
||||
#include "nf7.hh"
|
||||
|
||||
#include "common/lambda.hh"
|
||||
#include "common/value.hh"
|
||||
|
||||
|
||||
namespace nf7 {
|
||||
@ -19,6 +19,7 @@ namespace nf7 {
|
||||
class Node : public File::Interface {
|
||||
public:
|
||||
class Editor;
|
||||
class Lambda;
|
||||
|
||||
enum Flag : uint8_t {
|
||||
kUI = 1 << 0, // UpdateNode() is called to display node
|
||||
@ -32,9 +33,7 @@ class Node : public File::Interface {
|
||||
Node& operator=(const Node&) = default;
|
||||
Node& operator=(Node&&) = default;
|
||||
|
||||
// Node* is a dummy parameter to avoid issues of multi inheritance.
|
||||
virtual std::shared_ptr<nf7::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Lambda>&, Node* = nullptr) noexcept = 0;
|
||||
virtual std::shared_ptr<Lambda> CreateLambda(const std::shared_ptr<Lambda>&) noexcept = 0;
|
||||
|
||||
virtual void UpdateNode(Editor&) noexcept { }
|
||||
virtual void UpdateMenu(Editor&) noexcept { }
|
||||
@ -61,7 +60,7 @@ class Node::Editor {
|
||||
Editor& operator=(Editor&&) = delete;
|
||||
|
||||
virtual void Emit(Node&, std::string_view, nf7::Value&&) noexcept = 0;
|
||||
virtual std::shared_ptr<nf7::Lambda> GetLambda(Node& node) noexcept = 0;
|
||||
virtual std::shared_ptr<Lambda> GetLambda(Node& node) noexcept = 0;
|
||||
|
||||
virtual void AddLink(Node& src_node, std::string_view src_name,
|
||||
Node& dst_node, std::string_view dst_name) noexcept = 0;
|
||||
@ -72,4 +71,22 @@ class Node::Editor {
|
||||
virtual std::vector<std::pair<Node*, std::string>> GetDstOf(Node&, std::string_view) const noexcept = 0;
|
||||
};
|
||||
|
||||
class Node::Lambda : public nf7::Context {
|
||||
public:
|
||||
Lambda(nf7::File& f, const std::shared_ptr<Lambda>& parent = nullptr) noexcept :
|
||||
Lambda(f.env(), f.id(), parent) {
|
||||
}
|
||||
Lambda(nf7::Env& env, nf7::File::Id id, const std::shared_ptr<Lambda>& parent = nullptr) noexcept :
|
||||
Context(env, id, parent), parent_(parent) {
|
||||
}
|
||||
|
||||
virtual void Handle(
|
||||
std::string_view, const nf7::Value&, const std::shared_ptr<Lambda>&) noexcept = 0;
|
||||
|
||||
std::shared_ptr<Lambda> parent() const noexcept { return parent_.lock(); }
|
||||
|
||||
private:
|
||||
const std::weak_ptr<Lambda> parent_;
|
||||
};
|
||||
|
||||
} // namespace nf7
|
||||
|
@ -8,8 +8,6 @@
|
||||
|
||||
#include "nf7.hh"
|
||||
|
||||
#include "common/lambda.hh"
|
||||
|
||||
|
||||
namespace nf7 {
|
||||
|
||||
@ -36,8 +34,7 @@ class Sequencer : public nf7::File::Interface {
|
||||
Sequencer& operator=(Sequencer&&) = delete;
|
||||
|
||||
// Sequencer* is a dummy parameter to avoid issues of multi inheritance.
|
||||
virtual std::shared_ptr<nf7::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Lambda>&, Sequencer* = nullptr) noexcept = 0;
|
||||
virtual std::shared_ptr<Lambda> CreateLambda(const std::shared_ptr<Lambda>&) noexcept = 0;
|
||||
|
||||
virtual void UpdateItem(Editor&) noexcept { }
|
||||
virtual void UpdateTooltip(Editor&) noexcept { }
|
||||
|
@ -22,7 +22,6 @@
|
||||
#include "common/dir_item.hh"
|
||||
#include "common/generic_context.hh"
|
||||
#include "common/generic_type_info.hh"
|
||||
#include "common/lambda.hh"
|
||||
#include "common/logger_ref.hh"
|
||||
#include "common/node.hh"
|
||||
#include "common/ptr_selector.hh"
|
||||
@ -76,8 +75,7 @@ class Device final : public nf7::File, public nf7::DirItem, public nf7::Node {
|
||||
return std::make_unique<Device>(env, Selector {selector_}, cfg_);
|
||||
}
|
||||
|
||||
std::shared_ptr<nf7::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Lambda>&, nf7::Node*) noexcept override;
|
||||
std::shared_ptr<Lambda> CreateLambda(const std::shared_ptr<Lambda>&) noexcept override;
|
||||
|
||||
void Handle(const Event&) noexcept override;
|
||||
void Update() noexcept override;
|
||||
@ -286,19 +284,19 @@ class Device::Ring final {
|
||||
std::atomic<uint64_t> time_ = 0;
|
||||
};
|
||||
|
||||
class Device::PlaybackLambda final : public nf7::Lambda,
|
||||
class Device::PlaybackLambda final : public nf7::Node::Lambda,
|
||||
public std::enable_shared_from_this<Device::PlaybackLambda> {
|
||||
public:
|
||||
static inline const std::vector<std::string> kInputs = {"get_info", "mix"};
|
||||
static inline const std::vector<std::string> kOutputs = {"info", "mixed_size"};
|
||||
|
||||
PlaybackLambda() = delete;
|
||||
PlaybackLambda(Device& f, const std::shared_ptr<nf7::Lambda>& parent) noexcept :
|
||||
PlaybackLambda(Device& f, const std::shared_ptr<Lambda>& parent) noexcept :
|
||||
Lambda(f, parent), data_(f.data_), info_(f.infoTuple()) {
|
||||
}
|
||||
|
||||
void Handle(std::string_view name, const nf7::Value& v,
|
||||
const std::shared_ptr<nf7::Lambda>& caller) noexcept override {
|
||||
const std::shared_ptr<Lambda>& caller) noexcept override {
|
||||
if (name == "get_info") {
|
||||
caller->Handle("info", nf7::Value {info_}, shared_from_this());
|
||||
return;
|
||||
@ -325,19 +323,19 @@ class Device::PlaybackLambda final : public nf7::Lambda,
|
||||
|
||||
uint64_t time_ = 0;
|
||||
};
|
||||
class Device::CaptureLambda final : public nf7::Lambda,
|
||||
class Device::CaptureLambda final : public nf7::Node::Lambda,
|
||||
std::enable_shared_from_this<Device::CaptureLambda> {
|
||||
public:
|
||||
static inline const std::vector<std::string> kInputs = {"get_info", "peek"};
|
||||
static inline const std::vector<std::string> kOutputs = {"info", "samples"};
|
||||
|
||||
CaptureLambda() = delete;
|
||||
CaptureLambda(Device& f, const std::shared_ptr<nf7::Lambda>& parent) noexcept :
|
||||
CaptureLambda(Device& f, const std::shared_ptr<Lambda>& parent) noexcept :
|
||||
Lambda(f, parent), data_(f.data_), info_(f.infoTuple()) {
|
||||
}
|
||||
|
||||
void Handle(std::string_view name, const nf7::Value&,
|
||||
const std::shared_ptr<nf7::Lambda>& caller) noexcept override {
|
||||
const std::shared_ptr<Lambda>& caller) noexcept override {
|
||||
if (name == "get_info") {
|
||||
caller->Handle("info", nf7::Value {info_}, shared_from_this());
|
||||
return;
|
||||
@ -361,8 +359,7 @@ class Device::CaptureLambda final : public nf7::Lambda,
|
||||
|
||||
std::optional<uint64_t> time_;
|
||||
};
|
||||
std::shared_ptr<nf7::Lambda> Device::CreateLambda(
|
||||
const std::shared_ptr<nf7::Lambda>& parent, nf7::Node*) noexcept {
|
||||
std::shared_ptr<Node::Lambda> Device::CreateLambda(const std::shared_ptr<Lambda>& parent) noexcept {
|
||||
switch (cfg_.deviceType) {
|
||||
case ma_device_type_playback:
|
||||
return std::make_shared<Device::PlaybackLambda>(*this, parent);
|
||||
|
@ -20,7 +20,6 @@
|
||||
#include "common/generic_type_info.hh"
|
||||
#include "common/generic_watcher.hh"
|
||||
#include "common/gui_dnd.hh"
|
||||
#include "common/lambda.hh"
|
||||
#include "common/logger_ref.hh"
|
||||
#include "common/luajit_obj.hh"
|
||||
#include "common/luajit_queue.hh"
|
||||
@ -86,8 +85,8 @@ class Node final : public nf7::File, public nf7::DirItem, public nf7::Node {
|
||||
std::vector<std::string>(input_), std::vector<std::string>(output_));
|
||||
}
|
||||
|
||||
std::shared_ptr<nf7::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Lambda>&, nf7::Node*) noexcept override;
|
||||
std::shared_ptr<nf7::Node::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Node::Lambda>&) noexcept override;
|
||||
|
||||
void Handle(const Event&) noexcept override;
|
||||
void Update() noexcept override;
|
||||
@ -182,16 +181,16 @@ class Node::FetchTask final : public nf7::Task<std::shared_ptr<nf7::luajit::Ref>
|
||||
}
|
||||
};
|
||||
|
||||
class Node::Lambda final : public nf7::Lambda,
|
||||
class Node::Lambda final : public nf7::Node::Lambda,
|
||||
public std::enable_shared_from_this<Node::Lambda> {
|
||||
public:
|
||||
Lambda(Node& f, const std::shared_ptr<nf7::Lambda>& parent) noexcept :
|
||||
nf7::Lambda(f, parent),
|
||||
Lambda(Node& f, const std::shared_ptr<nf7::Node::Lambda>& parent) noexcept :
|
||||
nf7::Node::Lambda(f, parent),
|
||||
file_(&f), log_(f.log_), handler_(f.FetchHandler()) {
|
||||
}
|
||||
|
||||
void Handle(std::string_view name, const nf7::Value& v,
|
||||
const std::shared_ptr<nf7::Lambda>& caller) noexcept override {
|
||||
const std::shared_ptr<nf7::Node::Lambda>& caller) noexcept override {
|
||||
auto self = shared_from_this();
|
||||
handler_.ThenSub(self, [self, name = std::string(name), v, caller](auto) mutable {
|
||||
self->CallHandler({{std::move(name), std::move(v)}}, caller);
|
||||
@ -219,7 +218,7 @@ class Node::Lambda final : public nf7::Lambda,
|
||||
|
||||
|
||||
using Param = std::pair<std::string, nf7::Value>;
|
||||
void CallHandler(std::optional<Param>&& p, const std::shared_ptr<nf7::Lambda>& caller) noexcept
|
||||
void CallHandler(std::optional<Param>&& p, const std::shared_ptr<nf7::Node::Lambda>& caller) noexcept
|
||||
try {
|
||||
auto self = shared_from_this();
|
||||
th_.erase(
|
||||
@ -271,11 +270,11 @@ class Node::Lambda final : public nf7::Lambda,
|
||||
}
|
||||
}
|
||||
|
||||
void PushCaller(lua_State* L, const std::shared_ptr<nf7::Lambda>& caller) noexcept {
|
||||
void PushCaller(lua_State* L, const std::shared_ptr<nf7::Node::Lambda>& caller) noexcept {
|
||||
constexpr auto kTypeName = "nf7::File/LuaJIT/Node::Owner";
|
||||
struct D final {
|
||||
std::weak_ptr<nf7::Lambda> self;
|
||||
std::shared_ptr<nf7::Lambda> caller;
|
||||
std::weak_ptr<Lambda> self;
|
||||
std::shared_ptr<nf7::Node::Lambda> caller;
|
||||
};
|
||||
new (lua_newuserdata(L, sizeof(D))) D { .self = weak_from_this(), .caller = caller };
|
||||
|
||||
@ -314,9 +313,9 @@ class Node::Lambda final : public nf7::Lambda,
|
||||
};
|
||||
|
||||
|
||||
std::shared_ptr<nf7::Lambda> Node::CreateLambda(
|
||||
const std::shared_ptr<nf7::Lambda>& parent, nf7::Node*) noexcept {
|
||||
return std::make_shared<Node::Lambda>(*this, parent);
|
||||
std::shared_ptr<nf7::Node::Lambda> Node::CreateLambda(
|
||||
const std::shared_ptr<nf7::Node::Lambda>& parent) noexcept {
|
||||
return std::make_shared<Lambda>(*this, parent);
|
||||
}
|
||||
nf7::Future<std::shared_ptr<nf7::luajit::Ref>> Node::FetchHandler() noexcept {
|
||||
if (handler_) return handler_;
|
||||
|
@ -76,8 +76,7 @@ class Imm final : public nf7::File, public nf7::DirItem, public nf7::Node {
|
||||
return std::make_unique<Imm>(env, data.type, nf7::Value{data.value});
|
||||
}
|
||||
|
||||
std::shared_ptr<nf7::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Lambda>&, nf7::Node*) noexcept override;
|
||||
std::shared_ptr<Node::Lambda> CreateLambda(const std::shared_ptr<Node::Lambda>&) noexcept override;
|
||||
|
||||
void UpdateNode(Node::Editor&) noexcept override;
|
||||
|
||||
@ -116,15 +115,15 @@ class Imm final : public nf7::File, public nf7::DirItem, public nf7::Node {
|
||||
}
|
||||
};
|
||||
|
||||
class Imm::Lambda final : public nf7::Lambda,
|
||||
class Imm::Lambda final : public Node::Lambda,
|
||||
public std::enable_shared_from_this<Imm::Lambda> {
|
||||
public:
|
||||
Lambda(Imm& f, const std::shared_ptr<nf7::Lambda>& parent) noexcept :
|
||||
nf7::Lambda(f, parent), imm_(&f) {
|
||||
Lambda(Imm& f, const std::shared_ptr<Node::Lambda>& parent) noexcept :
|
||||
Node::Lambda(f, parent), imm_(&f) {
|
||||
}
|
||||
|
||||
void Handle(std::string_view name, const nf7::Value&,
|
||||
const std::shared_ptr<nf7::Lambda>& caller) noexcept override {
|
||||
const std::shared_ptr<Node::Lambda>& caller) noexcept override {
|
||||
if (name == "in") {
|
||||
if (!env().GetFile(initiator())) return;
|
||||
caller->Handle("out", imm_->mem_.data().value, shared_from_this());
|
||||
@ -135,8 +134,8 @@ class Imm::Lambda final : public nf7::Lambda,
|
||||
private:
|
||||
Imm* const imm_;
|
||||
};
|
||||
std::shared_ptr<nf7::Lambda> Imm::CreateLambda(
|
||||
const std::shared_ptr<nf7::Lambda>& parent, nf7::Node*) noexcept {
|
||||
std::shared_ptr<Node::Lambda> Imm::CreateLambda(
|
||||
const std::shared_ptr<Node::Lambda>& parent) noexcept {
|
||||
return std::make_shared<Imm::Lambda>(*this, parent);
|
||||
}
|
||||
|
||||
|
@ -25,7 +25,6 @@
|
||||
#include "common/gui_file.hh"
|
||||
#include "common/gui_node.hh"
|
||||
#include "common/gui_window.hh"
|
||||
#include "common/lambda.hh"
|
||||
#include "common/memento.hh"
|
||||
#include "common/node.hh"
|
||||
#include "common/node_link_store.hh"
|
||||
@ -121,8 +120,8 @@ class Network final : public nf7::File,
|
||||
void UpdateNode(Node::Editor&) noexcept override;
|
||||
void Handle(const Event& ev) noexcept override;
|
||||
|
||||
std::shared_ptr<nf7::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Lambda>&, nf7::Node*) noexcept override;
|
||||
std::shared_ptr<Node::Lambda> CreateLambda(
|
||||
const std::shared_ptr<Node::Lambda>&) noexcept override;
|
||||
|
||||
File::Interface* interface(const std::type_info& t) noexcept override {
|
||||
return InterfaceSelector<nf7::DirItem, nf7::Node>(t).Select(this);
|
||||
@ -332,21 +331,21 @@ class Network::Item::Watcher final : public nf7::Env::Watcher {
|
||||
// Builds and holds network information independently from Node/Network.
|
||||
// When it receives an input from outside or an output from Nodes in the network,
|
||||
// propagates it to appropriate Nodes.
|
||||
class Network::Lambda : public nf7::Lambda,
|
||||
public std::enable_shared_from_this<Lambda> {
|
||||
class Network::Lambda : public Node::Lambda,
|
||||
public std::enable_shared_from_this<Network::Lambda> {
|
||||
public:
|
||||
Lambda(Network& f, const std::shared_ptr<nf7::Lambda>& parent = nullptr) noexcept :
|
||||
nf7::Lambda(f, parent), net_(&f), root_(parent == nullptr) {
|
||||
Lambda(Network& f, const std::shared_ptr<Node::Lambda>& parent = nullptr) noexcept :
|
||||
Node::Lambda(f, parent), net_(&f), root_(parent == nullptr) {
|
||||
}
|
||||
|
||||
void Handle(std::string_view name, const Value& v,
|
||||
const std::shared_ptr<nf7::Lambda>& caller) noexcept override {
|
||||
const std::shared_ptr<Node::Lambda>& caller) noexcept override {
|
||||
env().ExecSub(shared_from_this(), [this, name = std::string(name), v, caller]() mutable {
|
||||
if (abort_) return;
|
||||
if (!env().GetFile(initiator())) {
|
||||
return; // net_ is expired
|
||||
}
|
||||
auto parent = this->parent().lock();
|
||||
auto parent = this->parent();
|
||||
|
||||
// send input from outer to input handlers
|
||||
if (caller == parent) {
|
||||
@ -390,25 +389,25 @@ class Network::Lambda : public nf7::Lambda,
|
||||
}
|
||||
|
||||
// Ensure that net_ is alive before calling
|
||||
const std::shared_ptr<nf7::Lambda>& FindOrCreateLambda(ItemId id) noexcept
|
||||
const std::shared_ptr<Node::Lambda>& FindOrCreateLambda(ItemId id) noexcept
|
||||
try {
|
||||
return FindLambda(id);
|
||||
} catch (nf7::Exception&) {
|
||||
return CreateLambda(net_->GetItem(id));
|
||||
}
|
||||
const std::shared_ptr<nf7::Lambda>& FindOrCreateLambda(const Item& item) noexcept
|
||||
const std::shared_ptr<Node::Lambda>& FindOrCreateLambda(const Item& item) noexcept
|
||||
try {
|
||||
return FindLambda(item.id());
|
||||
} catch (nf7::Exception&) {
|
||||
return CreateLambda(item);
|
||||
}
|
||||
const std::shared_ptr<nf7::Lambda>& CreateLambda(const Item& item) noexcept {
|
||||
const std::shared_ptr<Node::Lambda>& CreateLambda(const Item& item) noexcept {
|
||||
auto la = item.node().CreateLambda(shared_from_this());
|
||||
idmap_[la.get()] = item.id();
|
||||
auto [itr, added] = lamap_.emplace(item.id(), std::move(la));
|
||||
return itr->second;
|
||||
}
|
||||
const std::shared_ptr<nf7::Lambda>& FindLambda(ItemId id) {
|
||||
const std::shared_ptr<Node::Lambda>& FindLambda(ItemId id) {
|
||||
auto itr = lamap_.find(id);
|
||||
if (itr == lamap_.end()) {
|
||||
throw nf7::Exception {"lambda is not registered"};
|
||||
@ -434,8 +433,8 @@ class Network::Lambda : public nf7::Lambda,
|
||||
Network* const net_;
|
||||
bool root_;
|
||||
|
||||
std::unordered_map<ItemId, std::shared_ptr<nf7::Lambda>> lamap_;
|
||||
std::unordered_map<nf7::Lambda*, ItemId> idmap_;
|
||||
std::unordered_map<ItemId, std::shared_ptr<Node::Lambda>> lamap_;
|
||||
std::unordered_map<Node::Lambda*, ItemId> idmap_;
|
||||
|
||||
std::atomic<bool> abort_ = false;
|
||||
};
|
||||
@ -459,7 +458,7 @@ class Network::Editor final : public nf7::Node::Editor {
|
||||
sub->Handle(name, v, main);
|
||||
});
|
||||
}
|
||||
std::shared_ptr<nf7::Lambda> GetLambda(Node& node) noexcept override {
|
||||
std::shared_ptr<Node::Lambda> GetLambda(Node& node) noexcept override {
|
||||
try {
|
||||
const auto& la = lambda()->FindOrCreateLambda(owner_->GetItem(node));
|
||||
assert(la);
|
||||
@ -718,15 +717,15 @@ class Network::Initiator final : public Network::ChildNode,
|
||||
return std::make_unique<Initiator>(env, enable_auto_);
|
||||
}
|
||||
|
||||
std::shared_ptr<nf7::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Lambda>& parent, nf7::Node*) noexcept override {
|
||||
std::shared_ptr<Node::Lambda> CreateLambda(
|
||||
const std::shared_ptr<Node::Lambda>& parent) noexcept override {
|
||||
// TODO: use enable_auto_ value
|
||||
class Emitter final : public nf7::Lambda,
|
||||
class Emitter final : public Node::Lambda,
|
||||
public std::enable_shared_from_this<Emitter> {
|
||||
public:
|
||||
using Lambda::Lambda;
|
||||
using Node::Lambda::Lambda;
|
||||
void Handle(std::string_view, const Value&,
|
||||
const std::shared_ptr<nf7::Lambda>& caller) noexcept override {
|
||||
const std::shared_ptr<Node::Lambda>& caller) noexcept override {
|
||||
if (!std::exchange(done_, true)) {
|
||||
caller->Handle("out", nf7::Value::Pulse {}, shared_from_this());
|
||||
}
|
||||
@ -848,16 +847,16 @@ class Network::Input final : public Network::InputOrOutput,
|
||||
return std::make_unique<Input>(env, mem_.data());
|
||||
}
|
||||
|
||||
std::shared_ptr<nf7::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Lambda>& parent, nf7::Node*) noexcept override {
|
||||
class Emitter final : public nf7::Lambda,
|
||||
std::shared_ptr<Node::Lambda> CreateLambda(
|
||||
const std::shared_ptr<Node::Lambda>& parent) noexcept override {
|
||||
class Emitter final : public Node::Lambda,
|
||||
public std::enable_shared_from_this<Emitter> {
|
||||
public:
|
||||
Emitter(Input& f, const std::shared_ptr<nf7::Lambda>& parent, std::string_view name) noexcept :
|
||||
nf7::Lambda(f, parent), name_(name) {
|
||||
Emitter(Input& f, const std::shared_ptr<Node::Lambda>& parent, std::string_view name) noexcept :
|
||||
Node::Lambda(f, parent), name_(name) {
|
||||
}
|
||||
void Handle(std::string_view name, const Value& v,
|
||||
const std::shared_ptr<nf7::Lambda>& caller) noexcept override {
|
||||
const std::shared_ptr<Node::Lambda>& caller) noexcept override {
|
||||
if (name != name_) return;
|
||||
caller->Handle("out", v, shared_from_this());
|
||||
}
|
||||
@ -906,17 +905,17 @@ class Network::Output final : public Network::InputOrOutput,
|
||||
return std::make_unique<Output>(env, mem_.data());
|
||||
}
|
||||
|
||||
std::shared_ptr<nf7::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Lambda>& parent, nf7::Node*) noexcept override {
|
||||
class Emitter final : public nf7::Lambda,
|
||||
std::shared_ptr<Node::Lambda> CreateLambda(
|
||||
const std::shared_ptr<Node::Lambda>& parent) noexcept override {
|
||||
class Emitter final : public Node::Lambda,
|
||||
public std::enable_shared_from_this<Emitter> {
|
||||
public:
|
||||
Emitter(Output& f, const std::shared_ptr<Network::Lambda>& parent, std::string_view name) noexcept :
|
||||
Lambda(f, parent), name_(name) {
|
||||
Node::Lambda(f, parent), name_(name) {
|
||||
assert(parent);
|
||||
}
|
||||
void Handle(std::string_view name, const Value& v,
|
||||
const std::shared_ptr<nf7::Lambda>& caller) noexcept override {
|
||||
const std::shared_ptr<Node::Lambda>& caller) noexcept override {
|
||||
if (name != "in") return;
|
||||
caller->Handle(name_, std::move(v), shared_from_this());
|
||||
}
|
||||
@ -990,8 +989,8 @@ try {
|
||||
} catch (Exception&) {
|
||||
return nullptr;
|
||||
}
|
||||
std::shared_ptr<nf7::Lambda> Network::CreateLambda(
|
||||
const std::shared_ptr<nf7::Lambda>& parent, nf7::Node*) noexcept {
|
||||
std::shared_ptr<Node::Lambda> Network::CreateLambda(
|
||||
const std::shared_ptr<Node::Lambda>& parent) noexcept {
|
||||
auto ret = std::make_shared<Network::Lambda>(*this, parent);
|
||||
lambdas_running_.emplace_back(ret);
|
||||
return ret;
|
||||
@ -1355,7 +1354,7 @@ void Network::Update() noexcept {
|
||||
ImGui::BeginTooltip();
|
||||
ImGui::TextUnformatted("call stack:");
|
||||
ImGui::Indent();
|
||||
for (auto p = ptr->parent().lock(); p; p = p->parent().lock()) {
|
||||
for (auto p = ptr->parent(); p; p = p->parent()) {
|
||||
auto f = env().GetFile(p->initiator());
|
||||
|
||||
const auto path = f? f->abspath().Stringify(): "[missing file]";
|
||||
|
@ -20,7 +20,6 @@
|
||||
#include "common/generic_type_info.hh"
|
||||
#include "common/gui_dnd.hh"
|
||||
#include "common/gui_node.hh"
|
||||
#include "common/lambda.hh"
|
||||
#include "common/logger_ref.hh"
|
||||
#include "common/memento.hh"
|
||||
#include "common/node.hh"
|
||||
@ -69,7 +68,8 @@ class Ref final : public nf7::File, public nf7::Node {
|
||||
std::vector<std::string>{input_}, std::vector<std::string>{output_});
|
||||
}
|
||||
|
||||
std::shared_ptr<nf7::Lambda> CreateLambda(const std::shared_ptr<nf7::Lambda>&, nf7::Node*) noexcept override;
|
||||
std::shared_ptr<Node::Lambda> CreateLambda(
|
||||
const std::shared_ptr<Node::Lambda>&) noexcept override;
|
||||
|
||||
void Handle(const Event& ev) noexcept {
|
||||
const auto& d = mem_.data();
|
||||
@ -161,18 +161,18 @@ class Ref final : public nf7::File, public nf7::Node {
|
||||
}
|
||||
};
|
||||
|
||||
class Ref::Lambda final : public nf7::Lambda,
|
||||
class Ref::Lambda final : public Node::Lambda,
|
||||
public std::enable_shared_from_this<Ref::Lambda> {
|
||||
public:
|
||||
Lambda(Ref& f, const std::shared_ptr<nf7::Lambda>& parent) :
|
||||
nf7::Lambda(f, parent), ref_(&f), log_(f.log_) {
|
||||
Lambda(Ref& f, const std::shared_ptr<Node::Lambda>& parent) :
|
||||
Node::Lambda(f, parent), ref_(&f), log_(f.log_) {
|
||||
}
|
||||
|
||||
void Handle(std::string_view name, const Value& v,
|
||||
const std::shared_ptr<nf7::Lambda>& caller) noexcept override {
|
||||
const std::shared_ptr<Node::Lambda>& caller) noexcept override {
|
||||
if (!env().GetFile(initiator())) return;
|
||||
|
||||
auto parent = this->parent().lock();
|
||||
auto parent = this->parent();
|
||||
if (!parent) return;
|
||||
|
||||
if (caller == base_) {
|
||||
@ -190,11 +190,11 @@ class Ref::Lambda final : public nf7::Lambda,
|
||||
Ref* const ref_;
|
||||
std::shared_ptr<nf7::LoggerRef> log_;
|
||||
|
||||
std::shared_ptr<nf7::Lambda> base_;
|
||||
std::shared_ptr<Node::Lambda> base_;
|
||||
};
|
||||
|
||||
std::shared_ptr<nf7::Lambda> Ref::CreateLambda(
|
||||
const std::shared_ptr<nf7::Lambda>& parent, nf7::Node*) noexcept
|
||||
std::shared_ptr<Node::Lambda> Ref::CreateLambda(
|
||||
const std::shared_ptr<Node::Lambda>& parent) noexcept
|
||||
try {
|
||||
return std::make_shared<Ref::Lambda>(*this, parent);
|
||||
} catch (nf7::Exception& e) {
|
||||
|
@ -24,7 +24,6 @@
|
||||
#include "common/gui_popup.hh"
|
||||
#include "common/gui_timeline.hh"
|
||||
#include "common/gui_window.hh"
|
||||
#include "common/lambda.hh"
|
||||
#include "common/node.hh"
|
||||
#include "common/ptr_selector.hh"
|
||||
#include "common/sequencer.hh"
|
||||
@ -52,8 +51,8 @@ class Null final : public nf7::File, public nf7::Sequencer {
|
||||
return std::make_unique<Null>(env);
|
||||
}
|
||||
|
||||
std::shared_ptr<nf7::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Lambda>&, nf7::Sequencer*) noexcept override {
|
||||
std::shared_ptr<Sequencer::Lambda> CreateLambda(
|
||||
const std::shared_ptr<Sequencer::Lambda>&) noexcept override {
|
||||
return nullptr;
|
||||
}
|
||||
File::Interface* interface(const std::type_info& t) noexcept override {
|
||||
@ -101,8 +100,8 @@ class TL final : public nf7::File, public nf7::DirItem, public nf7::Node {
|
||||
}
|
||||
std::unique_ptr<File> Clone(Env& env) const noexcept override;
|
||||
|
||||
std::shared_ptr<nf7::Lambda> CreateLambda(
|
||||
const std::shared_ptr<nf7::Lambda>&, nf7::Node*) noexcept override;
|
||||
std::shared_ptr<Node::Lambda> CreateLambda(
|
||||
const std::shared_ptr<Node::Lambda>&) noexcept override;
|
||||
|
||||
void Handle(const Event& ev) noexcept;
|
||||
void Update() noexcept override;
|
||||
@ -477,15 +476,19 @@ class TL::Layer final {
|
||||
};
|
||||
|
||||
|
||||
class TL::Lambda final : public nf7::Lambda {
|
||||
class TL::Lambda final : public Node::Lambda {
|
||||
public:
|
||||
Lambda() = delete;
|
||||
Lambda(TL& f, const std::shared_ptr<nf7::Lambda>& parent) noexcept :
|
||||
nf7::Lambda(f, parent) {
|
||||
Lambda(TL& f, const std::shared_ptr<Node::Lambda>& parent) noexcept :
|
||||
Node::Lambda(f, parent) {
|
||||
}
|
||||
|
||||
void Handle(std::string_view, const nf7::Value&,
|
||||
const std::shared_ptr<Node::Lambda>&) noexcept override {
|
||||
}
|
||||
};
|
||||
std::shared_ptr<nf7::Lambda> TL::CreateLambda(
|
||||
const std::shared_ptr<nf7::Lambda>& parent, nf7::Node*) noexcept {
|
||||
std::shared_ptr<Node::Lambda> TL::CreateLambda(
|
||||
const std::shared_ptr<Node::Lambda>& parent) noexcept {
|
||||
return std::make_shared<TL::Lambda>(*this, parent);
|
||||
}
|
||||
|
||||
|
3
nf7.cc
3
nf7.cc
@ -216,7 +216,8 @@ Context::Context(File& f, const std::shared_ptr<Context>& parent) noexcept :
|
||||
Context(f.env(), f.id(), parent) {
|
||||
}
|
||||
Context::Context(Env& env, File::Id initiator, const std::shared_ptr<Context>& parent) noexcept :
|
||||
env_(&env), initiator_(initiator), parent_(parent) {
|
||||
env_(&env), initiator_(initiator),
|
||||
parent_(parent), depth_(parent? parent->depth()+1: 0) {
|
||||
}
|
||||
Context::~Context() noexcept {
|
||||
}
|
||||
|
3
nf7.hh
3
nf7.hh
@ -230,6 +230,7 @@ class Context {
|
||||
Env& env() const noexcept { return *env_; }
|
||||
File::Id initiator() const noexcept { return initiator_; }
|
||||
std::shared_ptr<Context> parent() const noexcept { return parent_.lock(); }
|
||||
size_t depth() const noexcept { return depth_; }
|
||||
|
||||
private:
|
||||
Env* const env_;
|
||||
@ -237,6 +238,8 @@ class Context {
|
||||
const File::Id initiator_;
|
||||
|
||||
const std::weak_ptr<Context> parent_;
|
||||
|
||||
const size_t depth_;
|
||||
};
|
||||
|
||||
class Env {
|
||||
|
Loading…
x
Reference in New Issue
Block a user