Multiscale Universal Interface  2.0
A Concurrent Framework for Coupling Heterogeneous Solvers
stream_vector.h
Go to the documentation of this file.
1 /*****************************************************************************
2 * Multiscale Universal Interface Code Coupling Library *
3 * *
4 * Copyright (C) 2019 Y. H. Tang, S. Kudo, X. Bian, Z. Li, G. E. Karniadakis *
5 * *
6 * This software is jointly licensed under the Apache License, Version 2.0 *
7 * and the GNU General Public License version 3, you may use it according *
8 * to either. *
9 * *
10 * ** Apache License, version 2.0 ** *
11 * *
12 * Licensed under the Apache License, Version 2.0 (the "License"); *
13 * you may not use this file except in compliance with the License. *
14 * You may obtain a copy of the License at *
15 * *
16 * http://www.apache.org/licenses/LICENSE-2.0 *
17 * *
18 * Unless required by applicable law or agreed to in writing, software *
19 * distributed under the License is distributed on an "AS IS" BASIS, *
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
21 * See the License for the specific language governing permissions and *
22 * limitations under the License. *
23 * *
24 * ** GNU General Public License, version 3 ** *
25 * *
26 * This program is free software: you can redistribute it and/or modify *
27 * it under the terms of the GNU General Public License as published by *
28 * the Free Software Foundation, either version 3 of the License, or *
29 * (at your option) any later version. *
30 * *
31 * This program is distributed in the hope that it will be useful, *
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
34 * GNU General Public License for more details. *
35 * *
36 * You should have received a copy of the GNU General Public License *
37 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
38 *****************************************************************************/
39 
48 #ifndef MUI_STREAM_VECTOR_H
49 #define MUI_STREAM_VECTOR_H
50 
51 #include <vector>
52 
53 #include "stream.h"
54 
55 namespace mui {
56 template<typename TYPE>
57 inline istream& operator>>(istream& stream, std::vector<TYPE>& ret)
58 {
59  std::size_t size;
60  stream >> size;
61  std::vector<TYPE> vec(size);
62  for( auto& a: vec ) stream >> a;
63  ret.swap(vec);
64  return stream;
65 }
66 // specialization is only for char because of endian problem
67 inline istream& operator>>(istream& stream, std::vector<char>& ret)
68 {
69  std::size_t size;
70  stream >> size;
71  std::vector<char> vec(size);
72  stream.read(vec.data(), size);
73  ret.swap(vec);
74  return stream;
75 }
76 
77 template<typename TYPE>
78 inline ostream& operator<<(ostream& stream, const std::vector<TYPE>& vec)
79 {
80  stream << vec.size();
81  for( const auto& a: vec ) stream << a;
82  return stream;
83 }
84 inline ostream& operator<<(ostream& stream, const std::vector<char>& vec)
85 {
86  stream << vec.size();
87  stream.write(vec.data(), vec.size());
88  return stream;
89 }
90 }
91 
92 #endif
Definition: stream.h:61
virtual void read(char *ptr, std::size_t size)=0
Definition: stream.h:67
virtual void write(const char *ptr, std::size_t size)=0
Definition: comm.h:54
istream & operator>>(istream &stream, smalluint &sml)
Definition: comm_tcp.h:103
ostream & operator<<(ostream &stream, const smalluint &sml)
Definition: comm_tcp.h:127
Defines base stream class container_stream and associated functors.