-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
86 lines (67 loc) · 2.67 KB
/
util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#pragma once
#include <functional>
#include <string>
#include <ostream>
#include <array>
#include <QByteArray>
namespace util {
template<typename T>
constexpr T convert(const std::array<std::uint8_t, sizeof(T)>& bytes) {
T result{0};
for (int i = 0; i < bytes.size(); ++i) result |= static_cast<T>(bytes[i]) << (i * 8);
return result;
}
template<>
constexpr float convert(const std::array<std::uint8_t, sizeof(float)>& bytes) {
return std::bit_cast<float>(convert<std::uint32_t>(bytes));
}
template<>
constexpr double convert(const std::array<std::uint8_t, sizeof(double)>& bytes) {
return std::bit_cast<double>(convert<std::uint64_t>(bytes));
}
template<class T, size_t N>
std::string to_string(const std::array<T, N>& array, std::function<std::string(T)> to_string) {
std::string result = "[";
for (size_t i = 0; i < N - 1; ++i) {
result += to_string(array[i]) + ", ";
}
result += to_string(array[N - 1]) + "]";
return result;
}
template<class T, size_t N>
std::string to_string(const std::array<T, N>& array) {
std::string result = "[";
for (size_t i = 0; i < N - 1; ++i) {
result += std::to_string(array[i]) + ", ";
}
result += std::to_string(array[N - 1]) + "]";
return result;
}
template<typename T, size_t CURRENT_SIZE, size_t NEW_SIZE, size_t FROM = 0>
std::array<T, NEW_SIZE> copy_resize(const std::array<T, CURRENT_SIZE>& array) {
std::array<T, NEW_SIZE> result{};
std::copy_n(array.cbegin() + FROM, NEW_SIZE, result.begin());
return result;
}
template<size_t C>
std::array<char, C> convert_type(const std::array<std::uint8_t, C>& array) {
return (const std::array<char, 48>&) array;
}
template<typename T, size_t PACKET_SIZE, size_t DATA_SIZE, size_t RESULT_SIZE, size_t OFFSET>
std::array<T, RESULT_SIZE> batch_create(const std::array<std::uint8_t, PACKET_SIZE>& bytes) {
std::array<T, RESULT_SIZE> result{};
for (size_t i = 0; i < RESULT_SIZE; ++i) {
std::array<std::uint8_t, DATA_SIZE> xs{};
std::copy_n(bytes.cbegin() + i * DATA_SIZE + OFFSET, DATA_SIZE, xs.begin());
// result[i] = T{util::copy_resize<std::uint8_t, PACKET_SIZE, DATA_SIZE, i * DATA_SIZE + OFFSET>(bytes)};
result[i] = T{xs};
}
return result;
}
template<size_t N>
std::array<std::uint8_t, N> copy_to_array(const QByteArray& bytes) {
std::array<std::uint8_t, N> array{};
std::copy(bytes.begin(), bytes.end(), array.begin());
return array;
}
}