Tatooine
data_array.h
Go to the documentation of this file.
1#ifndef TATOOINE_GEOMETRY_VTK_XML_DATA_ARRAY_H
2#define TATOOINE_GEOMETRY_VTK_XML_DATA_ARRAY_H
3//==============================================================================
7
8#include <cstdint>
9#include <optional>
10#include <cstring>
11#include <limits>
12#include <rapidxml.hpp>
13#include <string>
14#include <vector>
15//==============================================================================
16namespace tatooine::vtk::xml {
17//==============================================================================
18struct reader;
19struct data_array {
20 //==============================================================================
21 private:
23 std::optional<std::string> m_name = {};
24 std::size_t m_num_components = 1;
26 std::size_t m_offset = std::numeric_limits<std::size_t>::max();
27 reader* m_reader = nullptr;
28 rapidxml::xml_node<>* m_node = nullptr;
29 //==============================================================================
30 public:
31 data_array() = default;
32 data_array(reader& r, rapidxml::xml_node<>* node);
33 data_array(data_array const&) = default;
34 auto operator=(data_array const&) -> data_array& = default;
35 //----------------------------------------------------------------------------
36 [[nodiscard]] auto type() const { return m_type; }
37 [[nodiscard]] auto name() const { return m_name; }
38 [[nodiscard]] auto num_components() const { return m_num_components; }
39 [[nodiscard]] auto format() const { return m_format; }
40 [[nodiscard]] auto offset() const { return m_offset; }
41 //----------------------------------------------------------------------------
42 auto visit_data(auto&& f) const {
43 switch (m_type) {
44 case data_type::int8:
45 if constexpr (std::invocable<decltype(f), std::vector<std::int8_t>>) {
46 f(read<std::int8_t>());
47 }
48 break;
50 if constexpr (std::invocable<decltype(f), std::vector<std::uint8_t>>) {
51 f(read<std::uint8_t>());
52 }
53 break;
55 if constexpr (std::invocable<decltype(f), std::vector<std::int16_t>>) {
56 f(read<std::int16_t>());
57 }
58 break;
60 if constexpr (std::invocable<decltype(f), std::vector<std::uint16_t>>) {
61 f(read<std::uint16_t>());
62 }
63 break;
65 if constexpr (std::invocable<decltype(f), std::vector<std::int32_t>>) {
66 f(read<std::int32_t>());
67 }
68 break;
70 if constexpr (std::invocable<decltype(f), std::vector<std::uint32_t>>) {
71 f(read<std::uint32_t>());
72 }
73 break;
75 if constexpr (std::invocable<decltype(f), std::vector<std::int64_t>>) {
76 f(read<std::int64_t>());
77 }
78 break;
80 if constexpr (std::invocable<decltype(f), std::vector<std::uint64_t>>) {
81 f(read<std::uint64_t>());
82 }
83 break;
85 if constexpr (std::invocable<decltype(f), std::vector<float>>) {
86 f(read<float>());
87 }
88 break;
90 if constexpr (std::invocable<decltype(f), std::vector<double>>) {
91 f(read<double>());
92 }
93 break;
95 throw std::runtime_error {
96 "[vtk::xml::data_array] could not visit data because data_type is "
97 "unknown."
98 };
99 default:
100 throw std::runtime_error {
101 "[vtk::xml::data_array] could not visit data because no function "
102 "matches."
103 };
104 }
105 }
106 //----------------------------------------------------------------------------
107 template <typename T>
108 auto read() const -> std::vector<T> {
109 switch (format()) {
111 return read_data_ascii<T>();
113 return read_data_binary<T>();
115 return read_data_appended<T>();
117 default:
118 return {};
119 }
120 }
121 //----------------------------------------------------------------------------
122 private:
123 template <typename T>
124 auto read_data_ascii() const {
125 throw std::runtime_error{"[vtk::xml::data_array] cannot read ascii"};
126 auto data = std::vector<T>{};
127 return data;
128 }
129 //----------------------------------------------------------------------------
130 template <typename T>
131 auto read_data_binary() const {
132 throw std::runtime_error{"[vtk::xml::data_array] cannot read binary"};
133 auto data = std::vector<T>{};
134 return data;
135 }
136 //----------------------------------------------------------------------------
137 template <typename T>
138 auto read_data_appended() const {
139 auto const num_bytes = read_appended_data_size();
140 auto data = std::vector<T>(num_bytes / sizeof(T));
141 read_appended_data(reinterpret_cast<char*>(data.data()), num_bytes);
142 return data;
143 }
144 //----------------------------------------------------------------------------
145 template <typename T>
146 auto read_appended_data() const {
147 auto const num_bytes = read_appended_data_size();
148 auto data = std::vector<T>(num_bytes / sizeof(T));
149 read_appended_data(reinterpret_cast<char*>(data.data()), num_bytes);
150 return data;
151 }
152 auto read_appended_data(char* data, std::size_t num_bytes) const -> void;
153 auto read_appended_data_size() const -> std::size_t;
155 // template <typename T, std::size_t N, typename F>
156 // auto read_appended_data(std::uint8_t const* data_begin, F&& f) {
157 // if constexpr (N > 1) {
158 // f(reinterpret_cast<std::array<T, N> const*>(data_begin));
159 // } else {
160 // f(reinterpret_cast<T const*>(data_begin));
161 // }
162 // }
164 // template <typename T, typename F>
165 // auto read_appended_data(std::uint8_t const* data_begin,
166 // std::size_t const num_components, F&& f) {
167 // switch (num_components) {
168 // case 1:
169 // read_appended_data<T, 1>(data_begin, std::forward<F>(f));
170 // break;
171 // case 2:
172 // read_appended_data<T, 2>(data_begin, std::forward<F>(f));
173 // break;
174 // case 3:
175 // read_appended_data<T, 3>(data_begin, std::forward<F>(f));
176 // break;
177 // case 4:
178 // read_appended_data<T, 4>(data_begin, std::forward<F>(f));
179 // break;
180 // }
181 // }
183 // template <typename F>
184 // auto read_appended_data(std::uint8_t const* data_begin,
185 // data_array const& meta, F&& f) {
186 // switch (meta.type()) {
187 // case data_array::type_t::int8:
188 // read_appended_data<std::int8_t>(data_begin, meta.num_components(),
189 // std::forward<F>(f));
190 // break;
191 // case data_array::type_t::uint8:
192 // read_appended_data<std::uint8_t>(data_begin, meta.num_components(),
193 // std::forward<F>(f));
194 // break;
195 // case data_array::type_t::int16:
196 // read_appended_data<std::int16_t>(data_begin, meta.num_components(),
197 // std::forward<F>(f));
198 // break;
199 // case data_array::type_t::uint16:
200 // read_appended_data<std::uint16_t>(data_begin, meta.num_components(),
201 // std::forward<F>(f));
202 // break;
203 // case data_array::type_t::int32:
204 // read_appended_data<std::int32_t>(data_begin, meta.num_components(),
205 // std::forward<F>(f));
206 // break;
207 // case data_array::type_t::uint32:
208 // read_appended_data<std::uint32_t>(data_begin, meta.num_components(),
209 // std::forward<F>(f));
210 // break;
211 // case data_array::type_t::int64:
212 // read_appended_data<std::int64_t>(data_begin, meta.num_components(),
213 // std::forward<F>(f));
214 // break;
215 // case data_array::type_t::uint64:
216 // read_appended_data<std::uint64_t>(data_begin, meta.num_components(),
217 // std::forward<F>(f));
218 // break;
219 // case data_array::type_t::float32:
220 // read_appended_data<float>(data_begin, meta.num_components(),
221 // std::forward<F>(f));
222 // break;
223 // case data_array::type_t::float64:
224 // read_appended_data<double>(data_begin, meta.num_components(),
225 // std::forward<F>(f));
226 // break;
227 // case data_array::type_t::unknown:
228 // default:
229 // break;
230 // }
231 // }
232};
233//==============================================================================
234} // namespace tatooine::vtk::xml
235//==============================================================================
236#endif
Definition: byte_order.h:6
format
Definition: format.h:8
data_type
Definition: data_type.h:11
Definition: data_array.h:19
auto read_data_ascii() const
Definition: data_array.h:124
auto name() const
Definition: data_array.h:37
auto visit_data(auto &&f) const
Definition: data_array.h:42
auto read_data_appended() const
Definition: data_array.h:138
auto type() const
Definition: data_array.h:36
std::size_t m_num_components
Definition: data_array.h:24
auto read() const -> std::vector< T >
Definition: data_array.h:108
auto read_appended_data(char *data, std::size_t num_bytes) const -> void
auto read_appended_data() const
Definition: data_array.h:146
auto format() const
Definition: data_array.h:39
data_type m_type
Definition: data_array.h:22
xml::format m_format
Definition: data_array.h:25
auto read_data_binary() const
Definition: data_array.h:131
data_array(reader &r, rapidxml::xml_node<> *node)
std::size_t m_offset
Definition: data_array.h:26
auto offset() const
Definition: data_array.h:40
reader * m_reader
Definition: data_array.h:27
auto operator=(data_array const &) -> data_array &=default
std::optional< std::string > m_name
Definition: data_array.h:23
data_array(data_array const &)=default
rapidxml::xml_node * m_node
Definition: data_array.h:28
auto read_appended_data_size() const -> std::size_t
auto num_components() const
Definition: data_array.h:38
Definition: reader.h:25