Loading...
Searching...
No Matches
binaryUtils.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
25#ifndef __BITPIT_COMMON_BINARY_UTILS_TPP__
26#define __BITPIT_COMMON_BINARY_UTILS_TPP__
27
28namespace bitpit {
29
30namespace utils {
31
32namespace binary {
33
42template<typename T, typename std::enable_if<std::is_pod<T>::value>::type*>
43void write(std::ostream &stream, const std::vector<T> &container)
44{
45 size_t size = container.size();
46 write(stream, size);
47
48 write(stream, container.data(), size * sizeof(T));
49}
50
59template<typename T, std::size_t dim, typename std::enable_if<std::is_pod<T>::value>::type*>
60void write(std::ostream &stream, const std::array<T, dim> &container)
61{
62 write(stream, container.data(), dim * sizeof(T));
63}
64
73template<typename T, std::size_t dim, typename std::enable_if<!std::is_pod<T>::value>::type*>
74void write(std::ostream &stream, const std::array<T, dim> &container)
75{
76 for (std::size_t i = 0; i < dim; ++i) {
77 write(stream, container[i]);
78 }
79}
80
89template<typename T, typename std::enable_if<utils::is_iterable<T>::value>::type*>
90void write(std::ostream &stream, const T &value)
91{
92 write(stream, value.size());
93
94 for (const auto &item : value) {
95 write(stream, item);
96 }
97}
98
107template<typename T, typename std::enable_if<std::is_pod<T>::value>::type*>
108void write(std::ostream &stream, const T &value)
109{
110 write(stream, &value, sizeof(T));
111}
112
122template<typename T>
123void write(std::ostream &stream, const T &value, size_t size)
124{
125 stream.write(&value, size);
126}
127
137template<typename T>
138void write(std::ostream &stream, const T *value, size_t size)
139{
140 stream.write(reinterpret_cast<const char*>(value), size);
141}
142
152template<typename T>
153void write(std::ostream &stream, T *value, size_t size)
154{
155 stream.write(reinterpret_cast<char*>(value), size);
156}
157
166template<typename T, typename std::enable_if<std::is_pod<T>::value>::type*>
167void read(std::istream &stream, std::vector<T> &container)
168{
169 size_t size;
170 read(stream, size);
171
172 container.resize(size);
173 read(stream, container.data(), size * sizeof(T));
174}
175
184template<typename T, std::size_t dim, typename std::enable_if<std::is_pod<T>::value>::type*>
185void read(std::istream &stream, std::array<T, dim> &container)
186{
187 read(stream, container.data(), dim * sizeof(T));
188}
189
198template<typename T, std::size_t dim, typename std::enable_if<!std::is_pod<T>::value>::type*>
199void read(std::istream &stream, std::array<T, dim> &container)
200{
201 for (std::size_t i = 0; i < dim; ++i) {
202 read(stream, container[i]);
203 }
204}
205
214template<typename T, typename std::enable_if<utils::is_iterable<T>::value>::type*>
215void read(std::istream &stream, T &value)
216{
217 size_t size;
218 read(stream, size);
219
220 value.resize(size);
221 for (auto &item : value) {
222 read(stream, item);
223 }
224}
225
234template<typename T, typename std::enable_if<std::is_pod<T>::value>::type*>
235void read(std::istream &stream, T &value)
236{
237 read(stream, &value, sizeof(T));
238}
239
249template<typename T>
250void read(std::istream &stream, T &value, size_t size)
251{
252 read(stream, &value, size);
253}
254
264template<typename T>
265void read(std::istream &stream, T *value, size_t size)
266{
267 stream.read(reinterpret_cast<char*>(value), size);
268}
269
270}
271
272}
273
274}
275
276#endif
void write(std::ostream &stream, const std::vector< bool > &container)
void read(std::istream &stream, std::vector< bool > &container)
--- layout: doxygen_footer ---