Loading...
Searching...
No Matches
binary_stream.tpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25namespace bitpit {
26
34template<typename T, std::size_t SIZE>
35IBinaryStream & operator>>(IBinaryStream &stream, std::array<T, SIZE> &data)
36{
37 for (T &value : data) {
38 stream >> value;
39 }
40
41 return stream;
42}
43
51template<typename T, std::size_t SIZE>
52OBinaryStream & operator<<(OBinaryStream &stream, const std::array<T, SIZE> &data)
53{
54 for (const T &value : data) {
55 stream << value;
56 }
57
58 return stream;
59}
60
71template<typename T>
72IBinaryStream & operator>>(IBinaryStream &stream, std::vector<T> &data)
73{
74 std::size_t size;
75 stream.read(reinterpret_cast<char *>(&size), sizeof(size));
76 data.resize(size);
77
78 for (T &value : data) {
79 stream >> value;
80 }
81
82 return stream;
83}
84
95template<typename T>
96OBinaryStream & operator<<(OBinaryStream &stream, const std::vector<T> &data)
97{
98 std::size_t size = data.size();
99 stream.write(reinterpret_cast<const char *>(&size), sizeof(size));
100
101 for (const T &value : data) {
102 stream << value;
103 }
104
105 return stream;
106}
107
115template<typename T1, typename T2>
116IBinaryStream & operator>>(IBinaryStream &stream, std::pair<T1, T2> &data)
117{
118 T1 first;
119 T2 second;
120 stream >> first;
121 stream >> second;
122 data = std::make_pair(std::move(first), std::move(second));
123
124 return stream;
125}
126
134template<typename T1, typename T2>
135OBinaryStream & operator<<(OBinaryStream &stream, const std::pair<T1, T2> &data)
136{
137 stream << data.first;
138 stream << data.second;
139
140 return stream;
141}
142
153template<typename K, typename T>
154IBinaryStream & operator>>(IBinaryStream &stream, std::unordered_map<K, T> &data)
155{
156 std::size_t size;
157 stream.read(reinterpret_cast<char *>(&size), sizeof(size));
158
159 data.clear();
160 data.reserve(size);
161 for (std::size_t i = 0; i < size; ++i) {
162 K key;
163 T value;
164 stream >> key;
165 stream >> value;
166 data[key] = std::move(value);
167 }
168
169 return stream;
170}
171
182template<typename K, typename T>
183OBinaryStream & operator<<(OBinaryStream &stream, const std::unordered_map<K, T> &data)
184{
185 std::size_t size = data.size();
186 stream.write(reinterpret_cast<const char *>(&size), sizeof(size));
187
188 for (const auto &entry : data) {
189 stream << entry.first;
190 stream << entry.second;
191 }
192
193 return stream;
194}
195
206template<typename K, typename T>
207IBinaryStream & operator>>(IBinaryStream &stream, std::map<K, T> &data)
208{
209 std::size_t size;
210 stream.read(reinterpret_cast<char *>(&size), sizeof(size));
211
212 data.clear();
213 for (std::size_t i = 0; i < size; ++i) {
214 K key;
215 T value;
216 stream >> key;
217 stream >> value;
218 data[key] = std::move(value);
219 }
220
221 return stream;
222}
223
234template<typename K, typename T>
235OBinaryStream & operator<<(OBinaryStream &stream, const std::map<K, T> &data)
236{
237 std::size_t size = data.size();
238 stream.write(reinterpret_cast<const char *>(&size), sizeof(size));
239
240 for (const auto &entry : data) {
241 stream << entry.first;
242 stream << entry.second;
243 }
244
245 return stream;
246}
247
258template<typename T>
259IBinaryStream & operator>>(IBinaryStream &stream, std::unordered_set<T> &data)
260{
261 std::size_t size;
262 stream.read(reinterpret_cast<char *>(&size), sizeof(size));
263
264 data.clear();
265 data.reserve(size);
266 for (std::size_t i = 0; i < size; ++i) {
267 T value;
268 stream >> value;
269 data.insert(std::move(value));
270 }
271
272 return stream;
273}
274
285template<typename T>
286OBinaryStream & operator<<(OBinaryStream &stream, const std::unordered_set<T> &data)
287{
288 std::size_t size = data.size();
289 stream.write(reinterpret_cast<const char *>(&size), sizeof(size));
290
291 for (const auto &value : data) {
292 stream << value;
293 }
294
295 return stream;
296}
297
305template<typename T>
306IBinaryStream & operator>>(IBinaryStream &stream, T &data)
307{
308 stream.read(reinterpret_cast<char *>(&data), sizeof(T));
309
310 return stream;
311}
312
320template<typename T>
321OBinaryStream & operator<<(OBinaryStream &stream, const T &data)
322{
323 stream.write(reinterpret_cast<const char *>(&data), sizeof(T));
324
325 return stream;
326}
327
328}
Logger & operator<<(Logger &logger, LoggerManipulator< T > &&m)
Definition logger.hpp:367
--- layout: doxygen_footer ---