OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
osc_1_0_policy.hpp
1#pragma once
2#include <ossia/network/osc/detail/osc_common_policy.hpp>
3#include <ossia/network/osc/detail/osc_utils.hpp>
4#include <ossia/network/value/value.hpp>
5
6#include <boost/endian/conversion.hpp>
7
8#include <oscpack/osc/OscOutboundPacketStream.h>
9#include <oscpack/osc/OscTypes.h>
10
11namespace ossia::net
12{
13// This class is an implementation detail. It is used to send OSC 1.0 things.
14// Used for the dynamic std::vector<ossia::value> case
15struct osc_1_0_outbound_dynamic_policy : osc_common_outbound_dynamic_policy
16{
17 using osc_common_outbound_dynamic_policy::operator();
18 // Note: infinitum is not in OSC 1.0.
19 // But then we have no way to represent an array of impulses, which is
20 // *technically* possible in ossia
21 void operator()(impulse) const { p << int32_t(0); }
22
23 void operator()(bool b) const { p << int32_t(b); }
24
25 void operator()(char c) const { p << int32_t(c); }
26
27 // Arrays are flattened
28 void operator()(const std::vector<value>& t) const
29 {
30 for(const auto& val : t)
31 {
32 val.apply(*this);
33 }
34 }
35
36 void operator()(const value_map_type& t) const
37 {
38 for(const auto& [k, v] : t)
39 {
40 (*this)(k);
41 v.apply(*this);
42 }
43 }
44};
45
46struct osc_1_0_outbound_stream_visitor : osc_1_0_outbound_dynamic_policy
47{
48 using osc_1_0_outbound_dynamic_policy::operator();
49 void operator()(impulse) const { }
50
51 void operator()(const std::vector<value>& t) const
52 {
53 // We separate this case because an ossia::impulse type on its own
54 // should not have anything but a vector{ossia::impulse, ossia::impulse}
55 // should be [00] in osc 1.0
56 static_cast<const osc_1_0_outbound_dynamic_policy&>(*this)(t);
57 }
58};
59
60struct osc_1_0_outbound_static_policy : osc_common_outbound_static_policy
61{
62 using osc_common_outbound_static_policy::operator();
63 std::size_t operator()(char* buffer, ossia::impulse v, const auto&...) const noexcept
64 {
65 buffer[0] = ',';
66 buffer[1] = '\0';
67 buffer[2] = '\0';
68 buffer[3] = '\0';
69
70 return 4;
71 }
72
73 std::size_t operator()(char* buffer, bool v) const noexcept
74 {
75 return osc_common_outbound_static_policy::operator()(buffer, int32_t{v});
76 }
77
78 std::size_t operator()(char* buffer, char v) const noexcept
79 {
80 return osc_common_outbound_static_policy::operator()(buffer, int32_t{v});
81 }
82};
83
84struct osc_1_0_policy
85{
86 using static_policy = osc_1_0_outbound_static_policy;
87 using dynamic_policy = osc_1_0_outbound_dynamic_policy;
88};
89
90}