UdonLibrary 1.0.0
機械システム研究部 C++ ライブラリ
読み取り中…
検索中…
一致する文字列を見つけられません
CanUtility.hpp
[詳解]
1#pragma once
2
4
5#include <algorithm>
6#include <cmath>
7
8namespace Udon
9{
10 namespace Impl
11 {
12
23 template <typename Function>
24 inline void Packetize(
25 Udon::ArrayView<const uint8_t>&& input, // _In_
26 Udon::ArrayView<uint8_t>&& output, // _Out_
27 size_t singlePacketSize, // _In_
28 Function func // _In_
29 )
30 {
31
32 if (input.size() > singlePacketSize)
33 {
34 // multi packetize
35 // packet: [index][data][data]...[data] <=8byte
36 const size_t dataSize = singlePacketSize - 1;
37 const size_t packetCount = static_cast<size_t>(std::ceil(
38 static_cast<double>(input.size()) / dataSize));
39
40 for (uint8_t index = 0; index < packetCount; ++index)
41 {
42 output.at(0) = index;
43
44 if (index == packetCount - 1)
45 {
46 // last packet
47 std::copy_n(
48 input.begin() + index * dataSize,
49 std::min(dataSize, input.size() - index * dataSize),
50 output.begin() + 1);
51 func(input.size() - index * dataSize); // callback
52 }
53 else
54 {
55 std::copy_n(
56 input.begin() + index * dataSize,
57 dataSize,
58 output.begin() + 1);
59 func(singlePacketSize); // callback
60 }
61 }
62 }
63 else
64 {
65 // single packetize
66 // packet: [data][data]...[data] <=8byte
67 std::copy_n(
68 input.begin(),
69 input.size(),
70 output.begin());
71 func(input.size()); // callback
72 }
73 }
74
83 inline void Unpacketize(
84 Udon::ArrayView<const uint8_t>&& input, // _In_
85 Udon::ArrayView<uint8_t>&& output, // _Out_
86 size_t singlePacketSize // _In_
87 )
88 {
89
90 if (singlePacketSize == 0)
91 {
92 return;
93 }
94
95 if (output.size() <= singlePacketSize)
96 {
97 // single packetize
98 // packet: [data][data]...[data] <=8byte
99 std::copy_n(
100 input.begin(),
101 std::min(input.size(), output.size()),
102 output.begin());
103 }
104 else
105 {
106 // multi packetize
107 // packet: [index][data][data]...[data] <=8byte
108
109 const auto index = input.at(0);
110
111 const size_t dataSize = singlePacketSize - 1; // -1: index
112 const size_t packetCount = static_cast<size_t>(std::ceil(
113 static_cast<double>(output.size()) / dataSize));
114
115 if (index >= packetCount)
116 {
117 // invalid index (buffer overflow)
118 return;
119 }
120 if (index == packetCount - 1)
121 {
122 // last packet (copy remain data)
123 std::copy_n(
124 input.begin() + 1, // skip index
125 output.size() - index * dataSize,
126 output.begin() + index * dataSize);
127 return;
128 }
129 else
130 {
131 std::copy_n(
132 input.begin() + 1, // skip index
133 dataSize,
134 output.begin() + index * dataSize);
135 }
136 }
137 }
138 } // namespace Impl
139
140} // namespace Udon
配列参照クラス
Definition ArrayView.hpp:27
void Packetize(Udon::ArrayView< const uint8_t > &&input, Udon::ArrayView< uint8_t > &&output, size_t singlePacketSize, Function func)
バイト列を複数のパケット、単一のパケットにパケット化する
Definition CanUtility.hpp:24
void Unpacketize(Udon::ArrayView< const uint8_t > &&input, Udon::ArrayView< uint8_t > &&output, size_t singlePacketSize)
複数のパケット、単一のパケットからバイト列にアンパケット化する
Definition CanUtility.hpp:83
Definition Bit.hpp:12