mimmo_binary_stream.tpp
1 /*---------------------------------------------------------------------------*\
2  *
3  * mimmo
4  *
5  * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6  *
7  * -------------------------------------------------------------------------
8  * License
9  * This file is part of mimmo.
10  *
11  * mimmo 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  * mimmo 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 mimmo. If not, see <http://www.gnu.org/licenses/>.
22  *
23  \ *---------------------------------------------------------------------------*/
24 
25 //==============================================================//
26 // TEMPLATE BINARY STREAMS
27 //==============================================================//
28 
35 template<typename T>
36 mimmo::OBinaryStream& operator<<(mimmo::OBinaryStream &buffer, const std::vector<T> &var)
37 {
38  std::size_t nP = var.size();
39  buffer << nP;
40  for (std::size_t i = 0; i < nP; ++i) {
41  buffer << var[i];
42  }
43  return buffer;
44 }
45 
46 
53 template<typename T>
55 {
56  std::size_t nP;
57  buffer >> nP;
58  var.resize(nP);
59  for (std::size_t i = 0; i < nP; ++i) {
60  buffer >> var[i];
61  }
62  return buffer;
63 }
64 
65 
72 template <typename T, std::size_t d>
73 mimmo::OBinaryStream& operator<<(mimmo::OBinaryStream &buffer, const std::array<T,d> &var)
74 {
75  for(std::size_t i=0; i<d; ++i){
76  buffer << var[i];
77  }
78  return buffer;
79 }
80 
81 
88 template <typename T, std::size_t d>
89 mimmo::IBinaryStream& operator>>(mimmo::IBinaryStream &buffer, std::array<T,d> &var)
90 {
91  for(std::size_t i=0; i<d; ++i){
92  buffer >> var[i];
93  }
94  return buffer;
95 }
96 
103 template<typename T, typename Q>
104 mimmo::IBinaryStream& operator>>(mimmo::IBinaryStream &buffer, std::pair<T, Q>& element){
105 
106  T geo;
107  Q data;
108  buffer >> geo;
109  buffer >> data;
110  element = std::make_pair(geo, data);
111  return buffer;
112 };
113 
120 template<typename T, typename Q>
121 mimmo::OBinaryStream& operator<<(mimmo::OBinaryStream &buffer, const std::pair<T, Q>& element){
122 
123  buffer<<element.first;
124  buffer<<element.second;
125  return buffer;
126 };
127 
128 
135 template<typename T, typename Q>
136 mimmo::IBinaryStream& operator>>(mimmo::IBinaryStream &buffer,std::unordered_map<T, Q >& var){
137 
138  T key;
139  Q value;
140  std::size_t nP;
141  buffer >> nP;
142  for (std::size_t i = 0; i < nP; ++i) {
143  buffer >> key;
144  buffer >> value;
145  var[key] = value;
146  }
147  return buffer;
148 };
149 
156 template<typename T, typename Q>
157 mimmo::OBinaryStream& operator<<(mimmo::OBinaryStream &buffer, const std::unordered_map<T, Q>& var){
158 
159  std::size_t nP = var.size();
160  buffer << nP;
161  for (auto & ee : var) {
162  buffer << ee.first;
163  buffer <<ee.second;
164  }
165  return buffer;
166 };
167 
174 template<typename T, typename Q>
175 mimmo::IBinaryStream& operator>>(mimmo::IBinaryStream &buffer,std::map<T, Q >& var){
176 
177  T key;
178  Q value;
179  std::size_t nP;
180  buffer >> nP;
181  for (std::size_t i = 0; i < nP; ++i) {
182  buffer >> key;
183  buffer >> value;
184  var[key] = value;
185  }
186  return buffer;
187 };
188 
195 template<typename T, typename Q>
196 mimmo::OBinaryStream& operator<<(mimmo::OBinaryStream &buffer, const std::map<T, Q>& var){
197 
198  std::size_t nP = var.size();
199  buffer << nP;
200  for (auto & ee : var) {
201  buffer << ee.first;
202  buffer <<ee.second;
203  }
204  return buffer;
205 };
mimmo custom derivation of bitpit OBinaryStream (see relative doc)
mimmo::OBinaryStream & operator<<(mimmo::OBinaryStream &buf, const std::string &element)
mimmo::IBinaryStream & operator>>(mimmo::IBinaryStream &buffer, std::string &element)
mimmo custom derivation of bitpit IBinaryStream (see relative doc)