OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
osc_1_1_extended_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
11// This handles all the other cases: char, rgb color, etc... as well as arrays
12namespace ossia::net
13{
14struct osc_extended_outbound_dynamic_policy : osc_common_outbound_dynamic_policy
15{
16 using osc_common_outbound_dynamic_policy::operator();
17 mutable int m_depth = 0;
18 void operator()(impulse) const { p << oscpack::Infinitum(); }
19
20 void operator()(bool b) const { p << b; }
21
22 void operator()(char c) const { p << c; }
23
24 void operator()(vec2f vec) const
25 {
26 if(m_depth > 0)
27 {
28 p << oscpack::BeginArray();
29 }
30
31 p << vec[0] << vec[1];
32
33 if(m_depth > 0)
34 {
35 p << oscpack::EndArray();
36 }
37 }
38
39 void operator()(vec3f vec) const
40 {
41 if(m_depth > 0)
42 {
43 p << oscpack::BeginArray();
44 }
45
46 p << vec[0] << vec[1] << vec[2];
47
48 if(m_depth > 0)
49 {
50 p << oscpack::EndArray();
51 }
52 }
53
54 void operator()(vec4f vec) const
55 {
56 if(unit == ossia::rgba8_u{})
57 {
58 p << to_osc_rgba({vec});
59 }
60 else
61 {
62 if(m_depth > 0)
63 {
64 p << oscpack::BeginArray();
65 }
66
67 p << vec[0] << vec[1] << vec[2] << vec[3];
68
69 if(m_depth > 0)
70 {
71 p << oscpack::EndArray();
72 }
73 }
74 }
75
76 // Arrays are nested
77 void operator()(const std::vector<value>& t) const
78 {
79 if(m_depth > 0)
80 {
81 p << oscpack::BeginArray();
82 }
83
84 m_depth++;
85 for(const auto& val : t)
86 {
87 val.apply(*this);
88 }
89 m_depth--;
90
91 if(m_depth > 0)
92 {
93 p << oscpack::EndArray();
94 }
95 }
96
97 void operator()(const value_map_type& t) const { }
98};
99
100struct osc_extended_outbound_static_policy : osc_common_outbound_static_policy
101{
102 using osc_common_outbound_static_policy::operator();
103 std::size_t operator()(
104 char* buffer, ossia::impulse v, const ossia::extended_type& t) const noexcept
105 {
106 // NOTE: this is a change wrt the old ossia::oscquery::osc_outbound_visitor
107 buffer[0] = ',';
108 if(t.empty())
109 buffer[1] = oscpack::INFINITUM_TYPE_TAG;
110 else if(t == "nil")
111 buffer[1] = oscpack::NIL_TYPE_TAG;
112 else if(t == "empty")
113 buffer[1] = '\0';
114 buffer[2] = '\0';
115 buffer[3] = '\0';
116
117 return 4;
118 }
119
120 std::size_t operator()(char* buffer, ossia::impulse v) const noexcept
121 {
122 // NOTE: this is a change wrt the old ossia::oscquery::osc_outbound_visitor
123 buffer[0] = ',';
124 buffer[1] = '\0';
125 buffer[2] = '\0';
126 buffer[3] = '\0';
127
128 return 4;
129 }
130
131 std::size_t operator()(char* buffer, bool v) const noexcept
132 {
133 buffer[0] = ',';
134 buffer[1] = v ? oscpack::TRUE_TYPE_TAG : oscpack::FALSE_TYPE_TAG;
135 buffer[2] = '\0';
136 buffer[3] = '\0';
137
138 return 4;
139 }
140
141 std::size_t operator()(char* buffer, char v) const noexcept
142 {
143 buffer[0] = ',';
144 buffer[1] = oscpack::CHAR_TYPE_TAG;
145 buffer[2] = '\0';
146 buffer[3] = '\0';
147
148 boost::endian::endian_store<int32_t, 4, boost::endian::order::big>(
149 (unsigned char*)buffer + 4, (int32_t)v);
150
151 return 8;
152 }
153
154 std::size_t operator()(char* buffer, oscpack::RgbaColor v) const noexcept
155 {
156 buffer[0] = ',';
157 buffer[1] = oscpack::RGBA_COLOR_TYPE_TAG;
158 buffer[2] = '\0';
159 buffer[3] = '\0';
160
161 boost::endian::endian_store<uint32_t, 4, boost::endian::order::big>(
162 (unsigned char*)buffer + 4, v.value);
163 return 8;
164 }
165
166 std::size_t operator()(char* buffer, vec4f t) const noexcept
167 {
168 if(unit == ossia::rgba8_u{})
169 {
170 return (*this)(buffer, to_osc_rgba({t}));
171 }
172 else
173 {
174 return osc_common_outbound_static_policy::operator()(buffer, t);
175 }
176 }
177};
178
179struct osc_extended_policy
180{
181 using static_policy = osc_extended_outbound_static_policy;
182 using dynamic_policy = osc_extended_outbound_dynamic_policy;
183};
184
185}
std::string extended_type
How a low-level type should be interpreted.
Definition complex_type.hpp:9