Skip to content

Commit

Permalink
[dynamic] More dynamic ports work
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Jul 28, 2024
1 parent e4dd667 commit 6fa1bee
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
22 changes: 8 additions & 14 deletions examples/Helpers/DynamicPort.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
#pragma once
#include <cmath>
#include <halp/controls.hpp>
#include <halp/dynamic_port.hpp>
#include <halp/meta.hpp>

#include <functional>
#include <vector>

/* SPDX-License-Identifier: GPL-3.0-or-later */

namespace examples
namespace examples::helpers
{
struct SumPorts
{
halp_meta(name, "Sum")
halp_meta(c_name, "avnd_sumports")
halp_meta(name, "Sum (helpers)")
halp_meta(c_name, "avnd_sumports_helpers")
halp_meta(author, "Jean-Michaël Celerier")
halp_meta(category, "Debug")
halp_meta(description, "Example of an object with dynamic number of inputs")
halp_meta(uuid, "48b57b3e-227a-4a55-adce-86c011dcf491")
halp_meta(uuid, "191e6e08-a6c0-47d5-bd7b-a0f188cd273c")

struct inputs
{
Expand All @@ -26,16 +24,12 @@ struct SumPorts
static std::function<void(SumPorts&, int)> on_controller_interaction()
{
return [](SumPorts& object, int value) {
object.inputs.in.request_port_resize(value);
object.inputs.in_i.request_port_resize(value);
};
}
} controller;

struct : halp::knob_f32<"Input {}">
{
std::vector<double> ports{};
std::function<void(int)> request_port_resize;
} in;
halp::dynamic_port<halp::knob_f32<"Input {}">> in_i;
} inputs;

struct
Expand All @@ -48,7 +42,7 @@ struct SumPorts
outputs.out.value = 0;
int k = 0;

for(auto val : inputs.in.ports)
for(auto val : inputs.in_i.ports)
outputs.out.value += std::pow(10, k++) * std::floor(10 * val);
}
};
Expand Down
13 changes: 7 additions & 6 deletions include/avnd/binding/ossia/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,27 +514,28 @@ class safe_node_base : public safe_node_base_base<T>
template <typename Val, std::size_t N>
void control_updated_from_ui(Val&& new_value, int dynamic_port)
{
// FIXME how to combine dynamic_ports and control_input
if constexpr(requires { avnd::effect_container<T>::multi_instance; })
{
for(const auto& state : this->impl.full_state())
{
// Replace the value in the field
auto& field
= avnd::control_input_introspection<T>::template field<N>(state.inputs);
auto& field = avnd::dynamic_ports_input_introspection<T>::template field<N>(
state.inputs);

// OPTIMIZEME we're loosing a few allocations here that should be gc'd
field.ports[dynamic_port] = new_value;
field.ports[dynamic_port].value = new_value;

if_possible(field.update(state.effect));
}
}
else
{
// Replace the value in the field
auto& field
= avnd::control_input_introspection<T>::template field<N>(this->impl.inputs());
auto& field = avnd::dynamic_ports_input_introspection<T>::template field<N>(
this->impl.inputs());

std::swap(field.ports[dynamic_port], new_value);
std::swap(field.ports[dynamic_port].value, new_value);

if_possible(field.update(this->impl.effect));
}
Expand Down
1 change: 1 addition & 0 deletions include/avnd/binding/ossia/port_run_preprocess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct process_before_run
void init_value(
Field& ctrl, ossia::value_inlet& port, avnd::field_index<Idx> idx) const noexcept
{
// FIXME ensure this is not called for a dynamic_ports
if(!port.data.get_data().empty())
{
auto& last = port.data.get_data().back().value;
Expand Down
7 changes: 5 additions & 2 deletions include/avnd/concepts/dynamic_ports.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@

namespace avnd
{

template <typename T>
concept dynamic_ports_port = requires(T t) {
t.ports;
t.ports[0];
t.request_port_resize;
};

template <typename T>
using dynamic_port_type =
typename std::decay_t<decltype(std::declval<T>().ports)>::value_type;
}
13 changes: 13 additions & 0 deletions include/halp/dynamic_port.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once
#include <functional>
#include <vector>

namespace halp
{
template <typename Port>
struct dynamic_port
{
std::vector<Port> ports{};
std::function<void(int)> request_port_resize;
};
}

0 comments on commit 6fa1bee

Please sign in to comment.