2#include <ossia/network/value/value.hpp>
10template <
bool SourcePrecedence = true>
13 template <
typename Value_T>
14 static void merge_value(
ossia::value& dest, Value_T&& src)
23 if(dest.valid() && src.valid())
25 switch(src.get_type())
28 auto& src_vec = src.template get<std::vector<ossia::value>>();
29 switch(dest.get_type())
32 merge_list(dest.get<std::vector<ossia::value>>(), src_vec);
35 merge_list(dest.get<ossia::vec2f>(), src_vec);
38 merge_list(dest.get<ossia::vec3f>(), src_vec);
41 merge_list(dest.get<ossia::vec4f>(), src_vec);
49 auto& src_vec = src.template get<ossia::vec2f>();
50 switch(dest.get_type())
53 merge_list(dest.get<std::vector<ossia::value>>(), src_vec);
56 merge_list(dest.get<ossia::vec2f>(), src_vec);
59 merge_list(dest.get<ossia::vec3f>(), src_vec);
62 merge_list(dest.get<ossia::vec4f>(), src_vec);
70 auto& src_vec = src.template get<ossia::vec3f>();
71 switch(dest.get_type())
74 merge_list(dest.get<std::vector<ossia::value>>(), src_vec);
77 merge_list(dest.get<ossia::vec2f>(), src_vec);
80 merge_list(dest.get<ossia::vec3f>(), src_vec);
83 merge_list(dest.get<ossia::vec4f>(), src_vec);
91 auto& src_vec = src.template get<ossia::vec4f>();
92 switch(dest.get_type())
95 merge_list(dest.get<std::vector<ossia::value>>(), src_vec);
98 merge_list(dest.get<ossia::vec2f>(), src_vec);
101 merge_list(dest.get<ossia::vec3f>(), src_vec);
104 merge_list(dest.get<ossia::vec4f>(), src_vec);
112 switch(dest.get_type())
116 dest.get<std::vector<ossia::value>>(), std::forward<Value_T>(src));
119 set_first_value(dest.get<ossia::vec2f>(), std::forward<Value_T>(src));
122 set_first_value(dest.get<ossia::vec3f>(), std::forward<Value_T>(src));
125 set_first_value(dest.get<ossia::vec4f>(), std::forward<Value_T>(src));
130 dest = std::forward<Value_T>(src);
141 if(
auto dest_list_ptr = dest.target<std::vector<ossia::value>>())
144 set_first_value(*dest_list_ptr, std::forward<Value_T>(src));
147 else if(
auto src_list_ptr = src.template target<std::vector<ossia::value>>())
150 if(src_list_ptr->empty())
152 std::vector<ossia::value> t{dest};
156 else if(!(*src_list_ptr)[0].valid())
158 std::vector<ossia::value> t = *src_list_ptr;
166 if(src.valid() && SourcePrecedence)
167 dest = std::forward<Value_T>(src);
174 if(src.valid() && SourcePrecedence)
175 dest = std::forward<Value_T>(src);
182 template <
typename Value_T>
183 static void insert_in_list(
186 std::vector<ossia::value>* cur_ptr = &t;
187 for(
auto it = idx.begin(); it != idx.end();)
189 auto& cur = *cur_ptr;
190 std::size_t pos = *it;
191 if(cur.size() < pos + 1)
196 if(++it == idx.end())
200 merge_value(cur[pos], std::forward<Value_T>(v));
205 if(
auto sub_list = cur[pos].target<std::vector<ossia::value>>())
213 std::vector<ossia::value> sub{std::move(cur[pos])};
214 cur[pos] = std::move(sub);
217 cur_ptr = cur[pos].target<std::vector<ossia::value>>();
223 static void write_float(
const ossia::value& val,
float& f)
227 switch(val.get_type())
230 f = (float)val.template get<int32_t>();
232 case ossia::val_type::FLOAT:
233 f = (float)val.template get<float>();
236 f = (float)val.template get<bool>();
244 template <std::
size_t N,
typename Value_T>
245 static void set_first_value(std::array<float, N>& t, Value_T&& val)
247 write_float(val, t[0]);
250 template <
typename Value_T>
251 static void set_first_value(std::vector<ossia::value>& t, Value_T&& val)
255 t.push_back(std::forward<Value_T>(val));
259 merge_value(t[0], std::forward<Value_T>(val));
264 merge_list(std::vector<ossia::value>& lhs,
const std::vector<ossia::value>& rhs)
266 std::size_t n = rhs.size();
272 for(std::size_t i = 0u; i < n; i++)
274 merge_value(lhs[i], rhs[i]);
277 static void merge_list(std::vector<ossia::value>& lhs, std::vector<ossia::value>&& rhs)
279 std::size_t n = rhs.size();
285 for(std::size_t i = 0u; i < n; i++)
287 merge_value(lhs[i], std::move(rhs)[i]);
291 template <std::
size_t N>
292 static void merge_list(std::vector<ossia::value>& lhs,
const std::array<float, N>& rhs)
299 for(std::size_t i = 0u; i < N; i++)
305 template <std::
size_t N>
306 static void merge_list(std::array<float, N>& lhs,
const std::vector<ossia::value>& rhs)
308 const std::size_t n = std::min(N, rhs.size());
309 for(std::size_t i = 0u; i < n; i++)
311 write_float(rhs[i], lhs[i]);
315 template <std::
size_t N, std::
size_t M>
316 static void merge_list(std::array<float, N>& lhs,
const std::array<float, M>& rhs)
318 const std::size_t n = std::min(N, M);
319 for(std::size_t i = 0u; i < n; i++)
338 ossia::destination_index::const_iterator it;
340 ossia::value operator()(
const std::vector<ossia::value>& t)
342 if(it == index.end())
346 else if((int64_t)t.size() > *it)
350 return val.apply(*
this);
358 template <
typename T>
361 if(it == index.end())
371 template <std::
size_t N>
374 if(it == index.end())
378 else if((int64_t)t.size() > *it)
380 if(it + 1 == index.end())
381 return float{t[*it]};
The value class.
Definition value.hpp:173
ossia::small_vector< int32_t, 2 > destination_index
Definition destination_index.hpp:40
The destination_index_retriever struct Get the value associated with an index in a list....
Definition value_algorithms.hpp:336