Multiscale Universal Interface  2.0
A Concurrent Framework for Coupling Heterogeneous Solvers
message.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 MESSAGE_H_
49 #define MESSAGE_H_
50 
51 #include <memory>
52 #include "../../general/util.h"
53 #include "../../storage/stream.h"
54 #include "../../storage/stream_string.h"
55 #include "../../storage/stream_vector.h"
56 #include "../../storage/stream_tuple.h"
57 
58 namespace mui {
59 
60 struct message
61 {
62 public:
63  using id_type = std::string;
64 private:
65  id_type id_;
66  std::size_t id_size_;
67  std::vector<char> data_;
68 public:
69  message() : id_(), id_size_(0u), data_() {}
70 
71  template<typename... types>
72  static message make( const id_type& id, types&&... data ) {
73  message msg;
74  msg.id_ = id;
75  msg.id_size_ = streamed_size(id);
76  std::size_t n = msg.id_size_ + streamed_size(data...);
77  msg.data_.resize(n);
78  auto stream = make_ostream(msg.data_.data());
79  stream << id << std::forward_as_tuple(data...);
80  return msg;
81  }
82  static message make( std::vector<char> data ){
83  message m;
84  m.data_.swap(data);
85  auto in = make_istream(m.data_.begin());
86  in >> m.id_;
87  m.id_size_ = streamed_size(m.id_);
88  return m;
89  }
90 
91  bool has_id() const { return !id_.empty(); }
92 
93  const id_type& id() const { return id_; }
94  const char* data() const { return data_.data() + id_size_; }
95  std::size_t size() const { return data_.size() - id_size_; }
96  std::vector<char> detach() {
97  std::vector<char> data;
98  data.swap(data_);
99  id_.clear();
100  id_size_ = 0;
101  return data;
102  }
103 private:
104  friend ostream& operator<<( ostream&, const message&);
105 };
106 
107 // serialization & deserialization method
108 inline ostream& operator<< ( ostream& stream, const message& m )
109 {
110  stream << m.data_;
111  return stream;
112 }
113 
114 inline istream& operator>> ( istream& stream, message& m )
115 {
116  std::vector<char> v;
117  stream >> v;
118  m = message::make(v);
119  return stream;
120 }
121 
122 }
123 
124 #endif /* MESSAGE_H_ */
Definition: stream.h:61
Definition: stream.h:67
u u m
Definition: dim.h:281
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
oitr_stream< OutputIterator > make_ostream(OutputIterator cur)
Definition: stream.h:142
std::size_t streamed_size()
Definition: stream.h:161
iitr_stream< ConstInputIterator > make_istream(ConstInputIterator begin)
Definition: stream.h:118
Definition: message.h:61
const id_type & id() const
Definition: message.h:93
static message make(std::vector< char > data)
Definition: message.h:82
friend ostream & operator<<(ostream &, const message &)
Definition: message.h:108
std::size_t size() const
Definition: message.h:95
bool has_id() const
Definition: message.h:91
const char * data() const
Definition: message.h:94
std::vector< char > detach()
Definition: message.h:96
static message make(const id_type &id, types &&... data)
Definition: message.h:72
message()
Definition: message.h:69
std::string id_type
Definition: message.h:63