Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
bundle.hpp
Go to the documentation of this file.
1#pragma once
2
3// OSC Bundle — timetag + nested messages/bundles per OSC 1.0 spec.
4
5#include <pulp/osc/osc.hpp>
6#include <vector>
7#include <cstdint>
8#include <memory>
9#include <optional>
10#include <string>
11#include <variant>
12
13namespace pulp::osc {
14
16struct TimeTag {
17 uint32_t seconds = 0;
18 uint32_t fraction = 0;
19
21 static TimeTag immediate() { return {0, 1}; }
22
24 static TimeTag from_unix(double unix_time);
25
27 double to_unix() const;
28
29 bool operator==(const TimeTag& o) const {
30 return seconds == o.seconds && fraction == o.fraction;
31 }
32};
33
34struct Bundle; // forward declaration
35
39 std::variant<Message, std::unique_ptr<Bundle>> content;
40
41 BundleElement() = default;
42
44 explicit BundleElement(Message msg) : content(std::move(msg)) {}
45
47 explicit BundleElement(std::unique_ptr<Bundle> b) : content(std::move(b)) {}
48
50 explicit BundleElement(Bundle b);
51
52 bool is_message() const { return std::holds_alternative<Message>(content); }
53 bool is_bundle() const { return std::holds_alternative<std::unique_ptr<Bundle>>(content); }
54
55 const Message& message() const { return std::get<Message>(content); }
56 const Bundle& bundle() const { return *std::get<std::unique_ptr<Bundle>>(content); }
57};
58
60struct Bundle {
62 std::vector<BundleElement> elements;
63
65 void add(Message msg) {
66 elements.emplace_back(std::move(msg));
67 }
68
70 void add(Bundle nested) {
71 elements.emplace_back(std::make_unique<Bundle>(std::move(nested)));
72 }
73
75 std::vector<uint8_t> serialize() const;
76
78 static std::optional<Bundle> deserialize(const uint8_t* data, size_t size);
79};
80
83 : content(std::make_unique<Bundle>(std::move(b))) {}
84
87bool address_matches(std::string_view pattern, std::string_view address);
88
89} // namespace pulp::osc
Definition bundle.hpp:13
bool address_matches(std::string_view pattern, std::string_view address)
OSC address pattern matching per OSC 1.0 spec.
STL namespace.
const Message & message() const
Definition bundle.hpp:55
BundleElement(std::unique_ptr< Bundle > b)
Construct from a nested bundle (takes ownership).
Definition bundle.hpp:47
std::variant< Message, std::unique_ptr< Bundle > > content
Definition bundle.hpp:39
bool is_bundle() const
Definition bundle.hpp:53
BundleElement(Message msg)
Construct from a message.
Definition bundle.hpp:44
bool is_message() const
Definition bundle.hpp:52
const Bundle & bundle() const
Definition bundle.hpp:56
OSC Bundle — contains a timetag and a list of elements.
Definition bundle.hpp:60
TimeTag timetag
Definition bundle.hpp:61
std::vector< uint8_t > serialize() const
Serialize to OSC binary format.
void add(Bundle nested)
Add a nested bundle.
Definition bundle.hpp:70
static std::optional< Bundle > deserialize(const uint8_t *data, size_t size)
Deserialize from OSC binary.
std::vector< BundleElement > elements
Definition bundle.hpp:62
void add(Message msg)
Add a message to the bundle.
Definition bundle.hpp:65
Definition osc.hpp:39
OSC timetag — NTP timestamp (seconds since 1900-01-01).
Definition bundle.hpp:16
static TimeTag immediate()
"Immediately" timetag (1, 0)
Definition bundle.hpp:21
static TimeTag from_unix(double unix_time)
From Unix epoch seconds.
double to_unix() const
To Unix epoch seconds.
bool operator==(const TimeTag &o) const
Definition bundle.hpp:29
uint32_t fraction
Definition bundle.hpp:18
uint32_t seconds
Definition bundle.hpp:17