Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
ab_compare.hpp
Go to the documentation of this file.
1#pragma once
2
3// A/B compare.
4//
5// Two-slot state snapshot component backed by pulp::state::StateStore's
6// serialize/deserialize contract. Plugin UIs wire an ABCompare over their
7// StateStore to support the workflow every mature audio developer expects:
8// edit preset A, snap to B, tweak B separately, toggle A/B to hear the
9// difference, copy one slot over the other.
10//
11// This component holds the raw serialized bytes per slot and drives the
12// StateStore through its public API; it does not touch individual
13// ParamValue atomics, so the audio thread is never stalled.
14
15#include <pulp/state/store.hpp>
16
17#include <array>
18#include <cstdint>
19#include <span>
20#include <vector>
21
22namespace pulp::view {
23
24class ABCompare {
25public:
26 enum class Slot { A = 0, B = 1 };
27
28 explicit ABCompare(state::StateStore* store) : store_(store) {}
29
32 Slot current() const { return current_; }
33
35 void save_to(Slot slot) {
36 if (!store_) return;
37 buffer_for(slot) = store_->serialize();
38 }
39
43 bool load_from(Slot slot) {
44 if (!store_) return false;
45 const auto& buf = buffer_for(slot);
46 if (buf.empty()) return false;
47 if (!store_->deserialize(std::span<const uint8_t>(buf.data(), buf.size())))
48 return false;
49 current_ = slot;
50 return true;
51 }
52
54 bool toggle() {
55 return load_from(current_ == Slot::A ? Slot::B : Slot::A);
56 }
57
60 void copy(Slot src, Slot dst) {
61 if (src == dst) return;
62 buffer_for(dst) = buffer_for(src);
63 }
64
66 void swap() {
67 std::swap(buffer_for(Slot::A), buffer_for(Slot::B));
68 }
69
71 bool has(Slot slot) const { return !buffer_for(slot).empty(); }
72
74 void clear(Slot slot) { buffer_for(slot).clear(); }
75
78 std::span<const uint8_t> snapshot(Slot slot) const {
79 const auto& b = buffer_for(slot);
80 return {b.data(), b.size()};
81 }
82
83private:
84 std::vector<uint8_t>& buffer_for(Slot slot) {
85 return slots_[static_cast<size_t>(slot)];
86 }
87 const std::vector<uint8_t>& buffer_for(Slot slot) const {
88 return slots_[static_cast<size_t>(slot)];
89 }
90
91 state::StateStore* store_ = nullptr;
92 std::array<std::vector<uint8_t>, 2> slots_;
93 Slot current_ = Slot::A;
94};
95
96} // namespace pulp::view
Centralized parameter storage with lock-free value access.
Definition store.hpp:164
void save_to(Slot slot)
Capture the StateStore's current state into slot.
Definition ab_compare.hpp:35
void swap()
Swap A and B.
Definition ab_compare.hpp:66
Slot
Definition ab_compare.hpp:26
@ A
Definition ab_compare.hpp:26
@ B
Definition ab_compare.hpp:26
void clear(Slot slot)
Drop any saved snapshot from slot.
Definition ab_compare.hpp:74
Slot current() const
Which slot is currently live (i.e.
Definition ab_compare.hpp:32
bool has(Slot slot) const
Does slot currently hold a snapshot?
Definition ab_compare.hpp:71
bool load_from(Slot slot)
Restore the StateStore from slot.
Definition ab_compare.hpp:43
bool toggle()
Toggle between A and B. Fails if the target slot has no snapshot.
Definition ab_compare.hpp:54
void copy(Slot src, Slot dst)
Overwrite dst with a copy of src's snapshot.
Definition ab_compare.hpp:60
ABCompare(state::StateStore *store)
Definition ab_compare.hpp:28
std::span< const uint8_t > snapshot(Slot slot) const
Raw snapshot bytes (exposed for serialization of the A/B pair itself — e.g.
Definition ab_compare.hpp:78
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
@ b
Definition input_events.hpp:39