MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
RelationSlot.hpp
Go to the documentation of this file.
1#pragma once
2
4
6
7/**
8 * @struct RelationSlot
9 * @brief One addressable participant of a RelationNetwork
10 *
11 * A plain record: an optional node, a level, an activity flag, and the last
12 * rendered block. Fields are public and operators read them directly; the
13 * few methods below exist only where a field needs more than a plain
14 * assignment (firing callbacks, enabling a node's hooks).
15 *
16 * @warning A slot's address is not stable: RelationNetwork::add_slot() may
17 * reallocate the whole slot vector. Never hold a pointer or
18 * reference to a RelationSlot past a process_batch() call; read a
19 * slot fresh through RelationNetwork::get_slot(index) or
20 * get_node_output(index) instead.
21 *
22 * Structural edits (assigning a node, registering callbacks) must happen
23 * outside process_batch().
24 *
25 * A plain aggregate: RelationNetwork::add_slot() takes one by value, so it
26 * can be built with a designated initializer instead of a set_node() call
27 * afterward.
28 *
29 * @code
30 * n->add_slot({ .name = "carrier", .node = vega.Sine(220.0, 1.0) });
31 * n->add_slot({ .name = "product" }); // no node
32 * @endcode
33 *
34 * @note .node set this way still gets its hooks enabled: add_slot()
35 * applies that regardless of whether the node arrived through
36 * set_node() or through the .node field directly.
37 */
39 uint32_t index {};
40 std::string name;
41 std::shared_ptr<Node> node;
42 double level { 0.0 };
43 bool active { true };
44 std::vector<double> block;
45
46 std::vector<std::function<void()>> on_enter;
47 std::vector<std::function<void()>> on_leave;
48
49 /**
50 * @brief Set this slot's node
51 * @param new_node Node whose output becomes this slot's signal. Owned by the slot.
52 *
53 * Enables the node's hooks (on_impulse, on_phase_wrap, on_tick, ...) so
54 * they fire while the slot is advanced. A node already belonging to
55 * another network stays silent until set_in_network(false) is called on
56 * it directly.
57 */
58 void set_node(std::shared_ptr<Node> new_node)
59 {
60 if (new_node) {
61 new_node->m_fire_events_during_snapshot = true;
62 }
63 node = std::move(new_node);
64 }
65
66 /**
67 * @brief Set this slot's activity, firing on_enter/on_leave on a real transition
68 */
69 void set_active(bool is_active)
70 {
71 if (active == is_active) {
72 return;
73 }
74 active = is_active;
75 for (const auto& callback : is_active ? on_enter : on_leave) {
76 if (callback) {
77 callback();
78 }
79 }
80 }
81
82 /**
83 * @brief Preallocate this slot's block storage without changing its size
84 */
85 void reserve_block(uint32_t block_frames)
86 {
87 block.reserve(block_frames);
88 }
89
90 /**
91 * @brief Size this slot's block for writing
92 * @return Writable block, zero filled. Does not allocate within reserved capacity.
93 */
94 std::span<double> prepare_block(uint32_t frames)
95 {
96 block.assign(frames, 0.0);
97 return block;
98 }
99};
100
101} // namespace MayaFlux::Nodes::Network
PickCallback callback
Definition Picker.cpp:22
void reserve_block(uint32_t block_frames)
Preallocate this slot's block storage without changing its size.
std::vector< std::function< void()> > on_leave
void set_active(bool is_active)
Set this slot's activity, firing on_enter/on_leave on a real transition.
std::vector< std::function< void()> > on_enter
std::span< double > prepare_block(uint32_t frames)
Size this slot's block for writing.
void set_node(std::shared_ptr< Node > new_node)
Set this slot's node.
One addressable participant of a RelationNetwork.