OSSIA
Open Scenario System for Interactive Application
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 
11 namespace 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
15 struct 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 
39 struct osc_1_0_outbound_stream_visitor : osc_1_0_outbound_dynamic_policy
40 {
41  using osc_1_0_outbound_dynamic_policy::operator();
42  void operator()(impulse) const { }
43 
44  void operator()(const std::vector<value>& t) const
45  {
46  // We separate this case because an ossia::impulse type on its own
47  // should not have anything but a vector{ossia::impulse, ossia::impulse}
48  // should be [00] in osc 1.0
49  static_cast<const osc_1_0_outbound_dynamic_policy&>(*this)(t);
50  }
51 };
52 
53 struct osc_1_0_outbound_static_policy : osc_common_outbound_static_policy
54 {
55  using osc_common_outbound_static_policy::operator();
56  std::size_t operator()(char* buffer, ossia::impulse v, const auto&...) const noexcept
57  {
58  buffer[0] = ',';
59  buffer[1] = '\0';
60  buffer[2] = '\0';
61  buffer[3] = '\0';
62 
63  return 4;
64  }
65 
66  std::size_t operator()(char* buffer, bool v) const noexcept
67  {
68  return osc_common_outbound_static_policy::operator()(buffer, int32_t{v});
69  }
70 
71  std::size_t operator()(char* buffer, char v) const noexcept
72  {
73  return osc_common_outbound_static_policy::operator()(buffer, int32_t{v});
74  }
75 };
76 
77 struct osc_1_0_policy
78 {
79  using static_policy = osc_1_0_outbound_static_policy;
80  using dynamic_policy = osc_1_0_outbound_dynamic_policy;
81 };
82 
83 }