OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
osc_common_policy.hpp
1#pragma once
2#include <ossia/network/osc/detail/osc_utils.hpp>
3#include <ossia/network/value/value.hpp>
4
5#include <boost/endian/conversion.hpp>
6
7#include <oscpack/osc/OscOutboundPacketStream.h>
8#include <oscpack/osc/OscTypes.h>
9
10namespace ossia::net
11{
12
13template <typename T>
14struct osc_always_bundled_policy : T
15{
16 using bundled = std::true_type;
17};
18template <typename T>
19using osc_never_bundled_policy = T;
20// Handling for types compatible with all OSC version
21
22// This class is an implementation detail. It is used to send things that work
23// for all versions of OSC. Used for the dynamic std::vector<ossia::value>
24// case.
25struct osc_common_outbound_dynamic_policy
26{
27 oscpack::OutboundPacketStream& p;
28 const ossia::unit_t& unit;
29
30 void operator()(int32_t i) const { p << i; }
31
32 void operator()(float f) const { p << f; }
33
34 void operator()(const std::string& str) const { p << (std::string_view)str; }
35
36 void operator()(oscpack::Blob str) const { p << str; }
37
38 void operator()(vec2f vec) const { p << vec[0] << vec[1]; }
39
40 void operator()(vec3f vec) const { p << vec[0] << vec[1] << vec[2]; }
41
42 void operator()(vec4f vec) const { p << vec[0] << vec[1] << vec[2] << vec[3]; }
43
44 void operator()() const { }
45};
46
47// Since most of the time we have known types, we can optimize a fair bit the
48// writing of the osc messages.
49// Mirrors ossia::net::osc_outbound_array_visitor
50struct osc_common_outbound_static_policy
51{
52 const ossia::unit_t& unit;
53 std::size_t operator()(char* buffer, float v) const noexcept
54 {
55 buffer[0] = ',';
56 buffer[1] = oscpack::FLOAT_TYPE_TAG;
57 buffer[2] = '\0';
58 buffer[3] = '\0';
59
60 boost::endian::endian_store<float, 4, boost::endian::order::big>(
61 (unsigned char*)buffer + 4, v);
62 return 8;
63 }
64
65 std::size_t operator()(char* buffer, int32_t v) const noexcept
66 {
67 buffer[0] = ',';
68 buffer[1] = oscpack::INT32_TYPE_TAG;
69 buffer[2] = '\0';
70 buffer[3] = '\0';
71
72 boost::endian::endian_store<int32_t, 4, boost::endian::order::big>(
73 (unsigned char*)buffer + 4, v);
74 return 8;
75 }
76
77 std::size_t operator()(char* buffer, const std::string& v) const noexcept
78 {
79 buffer[0] = ',';
80 buffer[1] = oscpack::STRING_TYPE_TAG;
81 buffer[2] = '\0';
82 buffer[3] = '\0';
83
84 std::size_t written = write_string(v, buffer + 4);
85 return 4 + written;
86 }
87
88 std::size_t operator()(char* buffer, const oscpack::Blob& v) const noexcept
89 {
90 buffer[0] = ',';
91 buffer[1] = oscpack::BLOB_TYPE_TAG;
92 buffer[2] = '\0';
93 buffer[3] = '\0';
94
95 std::size_t written
96 = write_string(std::string_view((const char*)v.data, v.size), buffer + 4);
97 return 4 + written;
98 }
99
100 std::size_t operator()(char* buffer, vec2f v) const noexcept
101 {
102 buffer[0] = ',';
103 buffer[1] = oscpack::FLOAT_TYPE_TAG;
104 buffer[2] = oscpack::FLOAT_TYPE_TAG;
105 buffer[3] = '\0';
106
107 boost::endian::endian_store<float, 4, boost::endian::order::big>(
108 (unsigned char*)buffer + 4, v[0]);
109 boost::endian::endian_store<float, 4, boost::endian::order::big>(
110 (unsigned char*)buffer + 8, v[1]);
111
112 return 12;
113 }
114
115 std::size_t operator()(char* buffer, vec3f v) const noexcept
116 {
117 buffer[0] = ',';
118 buffer[1] = oscpack::FLOAT_TYPE_TAG;
119 buffer[2] = oscpack::FLOAT_TYPE_TAG;
120 buffer[3] = oscpack::FLOAT_TYPE_TAG;
121 buffer[4] = '\0';
122 buffer[5] = '\0';
123 buffer[6] = '\0';
124 buffer[7] = '\0';
125
126 boost::endian::endian_store<float, 4, boost::endian::order::big>(
127 (unsigned char*)buffer + 8, v[0]);
128 boost::endian::endian_store<float, 4, boost::endian::order::big>(
129 (unsigned char*)buffer + 12, v[1]);
130 boost::endian::endian_store<float, 4, boost::endian::order::big>(
131 (unsigned char*)buffer + 16, v[2]);
132
133 return 20;
134 }
135
136 std::size_t operator()(char* buffer, vec4f v) const noexcept
137 {
138 buffer[0] = ',';
139 buffer[1] = oscpack::FLOAT_TYPE_TAG;
140 buffer[2] = oscpack::FLOAT_TYPE_TAG;
141 buffer[3] = oscpack::FLOAT_TYPE_TAG;
142 buffer[4] = oscpack::FLOAT_TYPE_TAG;
143 buffer[5] = '\0';
144 buffer[6] = '\0';
145 buffer[7] = '\0';
146
147 boost::endian::endian_store<float, 4, boost::endian::order::big>(
148 (unsigned char*)buffer + 8, v[0]);
149 boost::endian::endian_store<float, 4, boost::endian::order::big>(
150 (unsigned char*)buffer + 12, v[1]);
151 boost::endian::endian_store<float, 4, boost::endian::order::big>(
152 (unsigned char*)buffer + 16, v[2]);
153 boost::endian::endian_store<float, 4, boost::endian::order::big>(
154 (unsigned char*)buffer + 20, v[3]);
155
156 return 24;
157 }
158};
159
160}
Definition dataspace.hpp:24