OSSIA
Open Scenario System for Interactive Application
make_unit.hpp
1 #pragma once
2 #include <ossia/detail/config.hpp>
3 
4 #include <ossia/detail/for_each.hpp>
5 #include <ossia/detail/size.hpp>
6 #include <ossia/network/dataspace/dataspace.hpp>
7 #include <ossia/network/dataspace/detail/dataspace_parse.hpp>
8 
9 namespace ossia
10 {
11 class make_unit_helper
12 {
13 public:
14  make_unit_helper()
15  : indices(make_dataspace_index_array())
16  , units(make_unit_array())
17  {
18  }
19 
20  ossia::unit_t get_unit(uint64_t dataspace, uint64_t unit) const
21  {
22  // Position of the dataspace + position of the unit
23  // position of the dataspace is the sum of the n first in unit_sizes
24 
25  if(dataspace < indices.size())
26  {
27  auto idx = indices[dataspace];
28 
29  if(idx + unit < units.size())
30  return units[idx + unit];
31  }
32 
33  return {};
34  }
35 
36 private:
45  using indices_array = std::array<uint64_t, ossia::dataspace_count>;
46  using units_array = std::array<ossia::unit_t, ossia::unit_count>;
47 
48  const indices_array indices;
49  const units_array units;
50 
51  static indices_array make_dataspace_index_array()
52  {
53  indices_array arr;
54 
55  uint64_t i = 0;
56  uint64_t sum = 0;
57  ossia::for_each_tagged(ossia::dataspace_u_list{}, [&](auto t) {
58  using dataspace_type = typename decltype(t)::type;
59  arr[i] = sum;
60  sum += ossia::size<dataspace_type>::value;
61  i++;
62  });
63 
64  return arr;
65  }
66 
67  // Creation of an array where each value is the corresponding unit
68  static units_array make_unit_array()
69  {
70  units_array arr;
71 
72  uint64_t i = 0;
73  ossia::for_each_tagged(ossia::dataspace_u_list{}, [&](auto t) {
74  using dataspace_type = typename decltype(t)::type;
75  ossia::for_each_tagged(dataspace_type{}, [&](auto u) {
76  using unit_type = typename decltype(u)::type;
77  arr[i] = unit_type{};
78  i++;
79  });
80  });
81  return arr;
82  }
83 };
84 }
Definition: git_info.h:7
Definition: dataspace.hpp:24