OSSIA
Open Scenario System for Interactive Application
domain_conversion.hpp
1 #pragma once
2 #include <ossia/network/domain/domain.hpp>
3 
4 namespace ossia
5 {
6 template <typename U>
7 struct domain_conversion
8 {
9 
10  template <typename T>
11  OSSIA_INLINE domain operator()(const T&)
12  {
13  return U{};
14  }
15 
16  domain operator()(const domain_base<int32_t>& t)
17  {
18  U f;
19  if(t.min)
20  f.min = *t.min;
21  if(t.max)
22  f.max = *t.max;
23  if(!t.values.empty())
24  for(auto val : t.values)
25  f.values.push_back(val);
26  return f;
27  }
28 
29  domain operator()(const domain_base<float>& t)
30  {
31  U f;
32  if(t.min)
33  f.min = *t.min;
34  if(t.max)
35  f.max = *t.max;
36  if(!t.values.empty())
37  for(auto val : t.values)
38  f.values.push_back(val);
39  return f;
40  }
41 
42  domain operator()(const domain_base<bool>& t)
43  {
44  U f;
45  f.min = 0;
46  f.max = 1;
47  return f;
48  }
49 };
50 
51 template <>
52 struct domain_conversion<domain_base<impulse>>
53 {
54  template <typename T>
55  OSSIA_INLINE domain operator()(const T&)
56  {
57  return domain_base<impulse>{};
58  }
59 };
60 
61 template <>
62 struct domain_conversion<domain_base<bool>>
63 {
64  template <typename T>
65  OSSIA_INLINE domain operator()(const T&)
66  {
67  return domain_base<bool>{};
68  }
69 };
70 template <>
71 struct domain_conversion<vector_domain>
72 {
73  OSSIA_INLINE domain operator()(const vector_domain& src) { return src; }
74 
75  template <typename T>
76  OSSIA_INLINE domain operator()(const T&)
77  {
78  return vector_domain();
79  }
80 };
81 
82 template <std::size_t N>
83 struct domain_conversion<vecf_domain<N>>
84 {
85  OSSIA_INLINE domain operator()(const vecf_domain<N>& src) { return src; }
86  vecf_domain<N> list_func(const vector_domain& t)
87  {
88  vecf_domain<N> dom;
89  const std::size_t min_size = std::min(N, t.min.size());
90  for(std::size_t i = 0; i < min_size; i++)
91  {
92  dom.min[i] = ossia::convert<float>(t.min[i]);
93  }
94 
95  const std::size_t max_size = std::min(N, t.max.size());
96  for(std::size_t i = 0; i < max_size; i++)
97  {
98  dom.max[i] = ossia::convert<float>(t.max[i]);
99  }
100 
101  const std::size_t vals_size = std::min(N, t.values.size());
102  for(std::size_t i = 0; i < vals_size; i++)
103  {
104  dom.values[i].clear();
105  for(auto& val : t.values[i])
106  dom.values[i].insert(ossia::convert<float>(val));
107  }
108 
109  return dom;
110  }
111 
112  OSSIA_INLINE domain operator()(const vector_domain& t) { return list_func(t); }
113 
114  OSSIA_INLINE domain operator()(const domain_base<int32_t>& d)
115  {
116  domain_base<float> res;
117  if(d.min)
118  res.min = *d.min;
119  if(d.max)
120  res.max = *d.max;
121  for(auto& val : d.values)
122  res.values.push_back(val);
123  return res;
124  }
125 
126  OSSIA_INLINE domain operator()(const domain_base<bool>& d)
127  {
128  domain_base<float> res;
129  res.min = 0.f;
130  res.max = 1.f;
131  return res;
132  }
133 
134  OSSIA_INLINE domain operator()(const domain_base<float>& d) { return d; }
135 
136  template <typename T>
137  OSSIA_INLINE domain operator()(const T&)
138  {
139  return vecf_domain<N>();
140  }
141 };
142 
143 template <>
144 struct domain_conversion<domain_base<std::string>>
145 {
146  OSSIA_INLINE domain operator()(const domain_base<std::string>& src) { return src; }
147 
148  template <typename T>
149  OSSIA_INLINE domain operator()(const T&)
150  {
151  return domain_base<std::string>();
152  }
153 };
154 
155 // TODO handle the ossia::value case
156 
157 inline domain convert_domain(const domain& dom, ossia::val_type newtype)
158 {
159  switch(newtype)
160  {
161  case val_type::IMPULSE:
162  return ossia::apply_nonnull(domain_conversion<domain_base<impulse>>{}, dom);
163  case val_type::INT:
164  return ossia::apply_nonnull(domain_conversion<domain_base<int32_t>>{}, dom);
165  case val_type::FLOAT:
166  return ossia::apply_nonnull(domain_conversion<domain_base<float>>{}, dom);
167  case val_type::BOOL:
168  return ossia::apply_nonnull(domain_conversion<domain_base<bool>>{}, dom);
169  case val_type::STRING:
170  return ossia::apply_nonnull(domain_conversion<domain_base<std::string>>{}, dom);
171  case val_type::LIST:
172  return ossia::apply_nonnull(domain_conversion<vector_domain>{}, dom);
173  case val_type::VEC2F:
174  return ossia::apply_nonnull(domain_conversion<vecf_domain<2>>{}, dom);
175  case val_type::VEC3F:
176  return ossia::apply_nonnull(domain_conversion<vecf_domain<3>>{}, dom);
177  case val_type::VEC4F:
178  return ossia::apply_nonnull(domain_conversion<vecf_domain<4>>{}, dom);
179  default:
180  return domain{};
181  }
182 }
183 
185 {
186  constexpr const auto list_index = 6;
187  // eggs::variants::detail::checked_index_of<vector_domain,
188  // domain_base_variant>::count;
189 
190  // Converts domains but keeps compatible different domains.
191  // e.g. a float domain works for vec4f or list.
193  if(dom.which() < list_index)
194  {
195  dom = convert_domain(dom, newtype);
196  }
197  else if(dom.which() == list_index)
198  {
199  switch(newtype)
200  {
203  case ossia::val_type::VEC4F: {
204  dom = convert_domain(dom, newtype);
205  break;
206  }
207  default:
208  break;
209  }
210  }
211 }
212 }
Definition: git_info.h:7
val_type
Enum to represent the types that a value can take.
Definition: parameter_properties.hpp:16
@ IMPULSE
array<float, 4>
@ VEC3F
array<float, 2>
@ LIST
std::string
@ VEC4F
array<float, 3>
@ BOOL
ossia::impulse
constexpr OSSIA_INLINE auto min(const T a, const U b) noexcept -> typename std::conditional<(sizeof(T) > sizeof(U)), T, U >::type
min function tailored for values
Definition: math.hpp:125
void convert_compatible_domain(domain &dom, ossia::val_type newtype)
Definition: domain_conversion.hpp:184
domain A domain of values
Definition: domain_base.hpp:23