remove Lambda::Init()
because Lambda should independ from callers
This commit is contained in:
@@ -2,23 +2,22 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "nf7.hh"
|
||||
|
||||
#include "common/value.hh"
|
||||
|
||||
|
||||
namespace nf7 {
|
||||
|
||||
class Lambda : public nf7::Context {
|
||||
class Lambda {
|
||||
public:
|
||||
using nf7::Context::Context;
|
||||
|
||||
virtual void Init(const std::shared_ptr<Lambda>& parent) noexcept {
|
||||
(void) parent;
|
||||
}
|
||||
Lambda() = default;
|
||||
virtual ~Lambda() = default;
|
||||
Lambda(const Lambda&) = delete;
|
||||
Lambda(Lambda&&) = delete;
|
||||
Lambda& operator=(const Lambda&) = delete;
|
||||
Lambda& operator=(Lambda&&) = delete;
|
||||
|
||||
virtual void Handle(
|
||||
size_t idx, Value&&, const std::shared_ptr<Lambda>& sender) noexcept = 0;
|
||||
size_t idx, Value&&, const std::shared_ptr<Lambda>& recv) noexcept = 0;
|
||||
};
|
||||
|
||||
} // namespace nf7
|
||||
|
@@ -20,8 +20,6 @@ class Node : public File::Interface {
|
||||
public:
|
||||
class Editor;
|
||||
|
||||
using Id = uint64_t;
|
||||
|
||||
Node() = default;
|
||||
Node(const Node&) = default;
|
||||
Node(Node&&) = default;
|
||||
@@ -73,8 +71,6 @@ class Node::Editor {
|
||||
|
||||
virtual std::vector<std::pair<Node*, std::string>> GetSrcOf(Node&, std::string_view) const noexcept = 0;
|
||||
virtual std::vector<std::pair<Node*, std::string>> GetDstOf(Node&, std::string_view) const noexcept = 0;
|
||||
|
||||
virtual std::shared_ptr<nf7::Lambda> lambda() noexcept = 0;
|
||||
};
|
||||
|
||||
} // namespace nf7
|
||||
|
@@ -174,21 +174,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::Context, public nf7::Lambda,
|
||||
public std::enable_shared_from_this<Node::Lambda> {
|
||||
public:
|
||||
Lambda(Node& owner) noexcept : nf7::Lambda(owner),
|
||||
Lambda(Node& owner) noexcept :
|
||||
Context(owner),
|
||||
owner_(&owner), owner_id_(owner.id()),
|
||||
log_(owner.log_), handler_(owner.FetchHandler()) {
|
||||
}
|
||||
|
||||
void Init(const std::shared_ptr<nf7::Lambda>& parent) noexcept override {
|
||||
auto self = shared_from_this();
|
||||
handler_.ThenSub(self, [self, parent](auto) {
|
||||
self->CallHandler(std::nullopt, parent);
|
||||
});
|
||||
}
|
||||
void Handle(size_t idx, nf7::Value&& v, const std::shared_ptr<nf7::Lambda>& caller) noexcept {
|
||||
void Handle(size_t idx, nf7::Value&& v, const std::shared_ptr<nf7::Lambda>& caller) noexcept override {
|
||||
auto self = shared_from_this();
|
||||
handler_.ThenSub(self, [self, idx, v = std::move(v), caller](auto) mutable {
|
||||
self->CallHandler({{idx, std::move(v)}}, caller);
|
||||
|
@@ -110,20 +110,16 @@ class Imm final : public nf7::File, public nf7::DirItem, public nf7::Node {
|
||||
class Imm::Lambda final : public nf7::Lambda,
|
||||
public std::enable_shared_from_this<Imm::Lambda> {
|
||||
public:
|
||||
Lambda(Imm& owner) noexcept :
|
||||
nf7::Lambda(owner), value_(owner.mem_.data().value) {
|
||||
Lambda(Imm& owner) noexcept : value_(owner.mem_.data().value) {
|
||||
}
|
||||
|
||||
void Init(const std::shared_ptr<nf7::Lambda>& parent) noexcept override {
|
||||
parent->Handle(0, nf7::Value{value_}, shared_from_this());
|
||||
}
|
||||
void Handle(size_t, nf7::Value&&, const std::shared_ptr<nf7::Lambda>&) noexcept override {
|
||||
void Handle(size_t, nf7::Value&&, const std::shared_ptr<nf7::Lambda>& recv) noexcept override {
|
||||
recv->Handle(0, nf7::Value {value_}, shared_from_this());
|
||||
}
|
||||
|
||||
private:
|
||||
nf7::Value value_;
|
||||
};
|
||||
|
||||
std::shared_ptr<nf7::Lambda> Imm::CreateLambda() noexcept {
|
||||
return std::make_shared<Imm::Lambda>(*this);
|
||||
}
|
||||
|
@@ -51,7 +51,6 @@ class Network final : public nf7::File,
|
||||
|
||||
class Item;
|
||||
class ExecutionLambda;
|
||||
class DebugLambda;
|
||||
class CustomEditor;
|
||||
|
||||
class SocketSwapCommand;
|
||||
@@ -128,8 +127,6 @@ class Network final : public nf7::File,
|
||||
std::unordered_map<ItemId, Item*> item_map_;
|
||||
std::unordered_map<const Node*, Item*> node_map_;
|
||||
|
||||
std::shared_ptr<Network::DebugLambda> dctx_;
|
||||
|
||||
const char* popup_ = nullptr;
|
||||
|
||||
// persistent params
|
||||
@@ -278,23 +275,21 @@ class Network::Item::Watcher final : public nf7::Env::Watcher {
|
||||
void Handle(const nf7::File::Event&) noexcept override;
|
||||
};
|
||||
|
||||
class Network::ExecutionLambda : public nf7::Lambda,
|
||||
class Network::ExecutionLambda : public nf7::Context, public nf7::Lambda,
|
||||
public std::enable_shared_from_this<ExecutionLambda> {
|
||||
public:
|
||||
ExecutionLambda(Network& owner, const std::shared_ptr<nf7::Lambda>& outer = nullptr) noexcept :
|
||||
Lambda(owner), outer_(outer) {
|
||||
Context(owner), outer_(outer) {
|
||||
// create sub lambdas
|
||||
std::unordered_map<ItemId, std::shared_ptr<nf7::Lambda>> lmap;
|
||||
subs_.reserve(owner.items_.size());
|
||||
for (const auto& item : owner.items_) {
|
||||
if (auto inode = item->inode()) {
|
||||
if (auto lambda = inode->CreateLambdaForOuterInput()) {
|
||||
AddChild(lambda);
|
||||
input_handlers_.push_back(std::move(lambda));
|
||||
}
|
||||
}
|
||||
if (auto lambda = item->node().CreateLambda()) {
|
||||
AddChild(lambda);
|
||||
subs_[lambda.get()] = SubLambda {.lambda = lambda};
|
||||
lmap[item->id()] = std::move(lambda);
|
||||
}
|
||||
@@ -315,14 +310,6 @@ class Network::ExecutionLambda : public nf7::Lambda,
|
||||
}
|
||||
}
|
||||
|
||||
void Init(const std::shared_ptr<nf7::Lambda>&) noexcept override {
|
||||
for (const auto& lambda : input_handlers_) {
|
||||
lambda->Init(shared_from_this());
|
||||
}
|
||||
for (auto& sub : subs_) {
|
||||
sub.second.lambda->Init(shared_from_this());
|
||||
}
|
||||
}
|
||||
void Handle(size_t idx, Value&& v, const std::shared_ptr<nf7::Lambda>& sender) noexcept override {
|
||||
if(abort_) return;
|
||||
auto task = [this, self = shared_from_this(), idx, v = std::move(v), sender]() {
|
||||
@@ -347,12 +334,6 @@ class Network::ExecutionLambda : public nf7::Lambda,
|
||||
void CleanUp() noexcept override {
|
||||
}
|
||||
void Abort() noexcept override {
|
||||
for (const auto& lambda : input_handlers_) {
|
||||
lambda->Abort();
|
||||
}
|
||||
for (auto& sub : subs_) {
|
||||
sub.second.lambda->Abort();
|
||||
}
|
||||
abort_ = true;
|
||||
}
|
||||
|
||||
@@ -388,15 +369,6 @@ class Network::ExecutionLambda : public nf7::Lambda,
|
||||
std::atomic<bool> abort_ = false;
|
||||
};
|
||||
|
||||
class Network::DebugLambda final : public Network::ExecutionLambda {
|
||||
public:
|
||||
using ExecutionLambda::ExecutionLambda;
|
||||
|
||||
std::string GetDescription() const noexcept override {
|
||||
return "executing Node/Network in debug mode";
|
||||
}
|
||||
};
|
||||
|
||||
class Network::CustomEditor final : public nf7::Node::Editor {
|
||||
public:
|
||||
CustomEditor(Network& owner) noexcept : owner_(&owner) {
|
||||
@@ -466,13 +438,6 @@ class Network::CustomEditor final : public nf7::Node::Editor {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::shared_ptr<nf7::Lambda> lambda() noexcept override {
|
||||
if (!owner_->dctx_) {
|
||||
owner_->dctx_ = std::make_shared<Network::DebugLambda>(*owner_);
|
||||
}
|
||||
return owner_->dctx_;
|
||||
}
|
||||
|
||||
private:
|
||||
Network* const owner_;
|
||||
};
|
||||
@@ -702,21 +667,17 @@ class Network::Input final : public Network::InputOrOutput,
|
||||
class Emitter final : public nf7::Lambda,
|
||||
public std::enable_shared_from_this<Emitter> {
|
||||
public:
|
||||
Emitter(Input& owner, size_t idx) noexcept :
|
||||
Lambda(owner.env(), owner.id()), idx_(idx) {
|
||||
Emitter(size_t idx) noexcept : idx_(idx) {
|
||||
}
|
||||
void Handle(size_t idx, Value&& v, const std::shared_ptr<nf7::Lambda>& recv) noexcept override {
|
||||
if (idx != idx_) return;
|
||||
recv->Handle(0, std::move(v), shared_from_this());
|
||||
}
|
||||
std::string GetDescription() const noexcept override {
|
||||
return "initiates other nodes on Node/Network";
|
||||
}
|
||||
private:
|
||||
size_t idx_;
|
||||
};
|
||||
if (!owner()) return nullptr;
|
||||
return std::make_unique<Emitter>(*this, owner()->input(mem_.data()));
|
||||
return std::make_unique<Emitter>(owner()->input(mem_.data()));
|
||||
} catch (Exception&) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -761,8 +722,7 @@ class Network::Output final : public Network::InputOrOutput,
|
||||
try {
|
||||
class Emitter final : public nf7::Lambda {
|
||||
public:
|
||||
Emitter(Output& owner, size_t idx) noexcept :
|
||||
Lambda(owner.env(), owner.id()), idx_(idx) {
|
||||
Emitter(size_t idx) noexcept : idx_(idx) {
|
||||
}
|
||||
void Handle(size_t idx, Value&& v, const std::shared_ptr<nf7::Lambda>& recv) noexcept override {
|
||||
if (idx != 0) return;
|
||||
@@ -774,14 +734,11 @@ class Network::Output final : public Network::InputOrOutput,
|
||||
outer->Handle(idx_, std::move(v), exec);
|
||||
}
|
||||
}
|
||||
std::string GetDescription() const noexcept override {
|
||||
return "emits value passed by Node/Network/Output";
|
||||
}
|
||||
private:
|
||||
size_t idx_;
|
||||
};
|
||||
if (!owner()) return nullptr;
|
||||
return std::make_unique<Emitter>(*this, owner()->output(mem_.data()));
|
||||
return std::make_unique<Emitter>(owner()->output(mem_.data()));
|
||||
} catch (Exception&) {
|
||||
return nullptr;
|
||||
}
|
||||
|
@@ -154,8 +154,7 @@ class Ref final : public nf7::File, public nf7::Node {
|
||||
class Ref::Lambda final : public nf7::Lambda,
|
||||
public std::enable_shared_from_this<Ref::Lambda> {
|
||||
public:
|
||||
Lambda(Ref& owner, std::shared_ptr<nf7::Lambda>&& base) :
|
||||
nf7::Lambda(owner), base_(std::move(base)) {
|
||||
Lambda(Ref& owner, std::shared_ptr<nf7::Lambda>&& base) : base_(std::move(base)) {
|
||||
auto& n = owner.target();
|
||||
|
||||
// ref input index -> target input index
|
||||
@@ -179,10 +178,6 @@ class Ref::Lambda final : public nf7::Lambda,
|
||||
}
|
||||
}
|
||||
|
||||
void Init(const std::shared_ptr<nf7::Lambda>& parent) noexcept override {
|
||||
parent_ = parent;
|
||||
base_->Init(shared_from_this());
|
||||
}
|
||||
void Handle(size_t idx, Value&& v, const std::shared_ptr<nf7::Lambda>& caller) noexcept override {
|
||||
auto parent = parent_.lock();
|
||||
if (caller == parent) {
|
||||
|
Reference in New Issue
Block a user