OSSIA
Open Scenario System for Interactive Application
osc_utils.hpp
1 #pragma once
2 #include <ossia/network/base/node.hpp>
3 #include <ossia/network/base/parameter.hpp>
4 #include <ossia/network/base/parameter_data.hpp>
5 
6 #include <oscpack/osc/OscTypes.h>
7 
8 #include <any>
9 #include <string_view>
10 
11 namespace ossia::net
12 {
13 // 0 -> 4 ; 1 -> 4; 2 -> 4; 3 -> 4;
14 // 4 -> 8 ; 5 -> 8; etc ...
15 // an optimized version of
16 //
17 // std::size_t pattern_size(std::size_t sz) noexcept
18 // {
19 // switch(sz % 4) {
20 // case 0:
21 // return sz + 4;
22 // case 1:
23 // return sz + 3;
24 // case 2:
25 // return sz + 2;
26 // case 3:
27 // return sz + 1;
28 // }
29 // return 0;
30 // }
31 
32 static inline constexpr std::size_t pattern_size(std::size_t sz) noexcept
33 {
34  return (sz & 0xFFFFFFFFFFFFFFFC) + 4;
35 }
36 
37 static inline std::size_t write_string(std::string_view str, char* buffer) noexcept
38 {
39  std::size_t i = 0;
40  for(; i < str.size(); i++)
41  {
42  buffer[i] = str[i];
43  }
44 
45  if(i % 4 == 0)
46  buffer[i++] = 0;
47 
48  for(; i % 4 != 0; i++)
49  {
50  buffer[i] = 0;
51  }
52  return i;
53 }
54 
55 static inline bool is_blob(const ossia::net::parameter_base& b) noexcept
56 {
57  using namespace std::literals;
58  auto& ext = b.get_node().get_extended_attributes();
59  auto it = ext.find("extended_type"sv);
60  if(it == ext.end())
61  return false;
62 
63  if(auto* str = std::any_cast<ossia::extended_type>(&it->second))
64  return (*str) == "buffer"sv; // TODO keep in sync with extended_types.cpp
65 
66  return false;
67 }
68 
69 static inline bool is_rgba(const ossia::net::parameter_base& b) noexcept
70 {
71  auto& u = b.get_unit();
72  return u == ossia::rgba8_u{};
73 }
74 
75 static inline bool is_blob(const ossia::net::full_parameter_data& b) noexcept
76 {
77  using namespace std::literals;
78  auto& ext = b.extended;
79  auto it = ext.find("extended_type"sv);
80  if(it == ext.end())
81  return false;
82 
83  if(auto* str = std::any_cast<ossia::extended_type>(&it->second))
84  return (*str) == "buffer"sv; // TODO keep in sync with extended_types.cpp
85 
86  return false;
87 }
88 
89 static inline bool is_rgba(const ossia::net::full_parameter_data& b) noexcept
90 {
91  return b.unit == ossia::rgba8_u{};
92 }
93 
94 static inline constexpr oscpack::RgbaColor to_osc_rgba(const ossia::rgba8& u) noexcept
95 {
96  uint32_t r = (uint32_t)u.dataspace_value[0] << 24;
97  uint32_t g = (uint32_t)u.dataspace_value[1] << 16;
98  uint32_t b = (uint32_t)u.dataspace_value[2] << 8;
99  uint32_t a = (uint32_t)u.dataspace_value[3];
100  return oscpack::RgbaColor(r + g + b + a);
101 }
102 }
The parameter_base class.
Definition: ossia/network/base/parameter.hpp:48
Full information about a parameter.
Definition: parameter_data.hpp:61