Collage  1.7.0
High-performance C++ library for developing object-oriented distributed applications.
dataOStream.ipp
1 
2 /* Copyright (c) 2012-2017, Daniel Nachbaur <danielnachbaur@gmail.com>
3  * Stefan.Eilemann@epfl.ch
4  *
5  * This library is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Lesser General Public License version 2.1 as published
7  * by the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #include <co/object.h>
20 #include <co/objectVersion.h>
21 #include <servus/serializable.h> // used inline
22 
23 namespace co
24 {
28 template <>
29 inline DataOStream& DataOStream::operator<<(const std::string& str)
30 {
31  const uint64_t nElems = str.length();
32  _write(&nElems, sizeof(nElems));
33  if (nElems > 0)
34  _write(str.c_str(), nElems);
35 
36  return *this;
37 }
38 
40 template <>
41 inline DataOStream& DataOStream::operator<<(const Object* const& object)
42 {
43  LBASSERT(!object || object->isAttached());
44  (*this) << ObjectVersion(object);
45  return *this;
46 }
47 
49 template <class T>
50 void DataOStream::_writeSerializable(const T& object, const boost::true_type&)
51 {
52  const auto& data = object.toBinary();
53  (*this) << data.size << Array<const void>(data.ptr.get(), data.size);
54 }
55 
56 template <>
57 inline void DataOStream::_writeArray(const Array<void> array,
58  const boost::false_type&)
59 {
60  _write(array.data, array.getNumBytes());
61 }
62 
63 template <>
64 inline void DataOStream::_writeArray(const Array<const void> array,
65  const boost::false_type&)
66 {
67  _write(array.data, array.getNumBytes());
68 }
69 
71 template <class T>
72 inline DataOStream& DataOStream::operator<<(const lunchbox::RefPtr<T>& ptr)
73 {
74  return *this << ptr.get();
75 }
76 
77 template <class T>
78 inline DataOStream& DataOStream::operator<<(const lunchbox::Buffer<T>& buffer)
79 {
80  return (*this) << buffer.getSize()
81  << Array<const T>(buffer.getData(), buffer.getSize());
82 }
83 
84 template <class T>
85 inline DataOStream& DataOStream::operator<<(const std::vector<T>& value)
86 {
87  const uint64_t nElems = value.size();
88  *this << nElems;
89  for (uint64_t i = 0; i < nElems; ++i)
90  *this << value[i];
91  return *this;
92 }
93 
94 template <class K, class V>
95 inline DataOStream& DataOStream::operator<<(const std::map<K, V>& value)
96 {
97  const uint64_t nElems = value.size();
98  *this << nElems;
99  for (typename std::map<K, V>::const_iterator it = value.begin();
100  it != value.end(); ++it)
101  {
102  *this << it->first << it->second;
103  }
104  return *this;
105 }
106 
107 template <class T>
108 inline DataOStream& DataOStream::operator<<(const std::set<T>& value)
109 {
110  const uint64_t nElems = value.size();
111  *this << nElems;
112  for (typename std::set<T>::const_iterator it = value.begin();
113  it != value.end(); ++it)
114  {
115  *this << *it;
116  }
117  return *this;
118 }
119 
120 template <class K, class V>
122  const std::unordered_map<K, V>& value)
123 {
124  const uint64_t nElems = value.size();
125  *this << nElems;
126  for (const auto& elem : value)
127  *this << elem.first << elem.second;
128  return *this;
129 }
130 
131 template <class T>
132 inline DataOStream& DataOStream::operator<<(const std::unordered_set<T>& value)
133 {
134  const uint64_t nElems = value.size();
135  *this << nElems;
136  for (const auto& elem : value)
137  *this << elem;
138  return *this;
139 }
140 
141 template <typename C>
142 inline void DataOStream::serializeChildren(const std::vector<C*>& children)
143 {
144  const uint64_t nElems = children.size();
145  (*this) << nElems;
146 
147  for (typename std::vector<C*>::const_iterator i = children.begin();
148  i != children.end(); ++i)
149  {
150  C* child = *i;
151  (*this) << ObjectVersion(child);
152  LBASSERTINFO(!child || child->isAttached(),
153  "Found unmapped object during serialization");
154  }
155 }
159 template <>
160 inline DataOStream& DataOStream::operator<<(const std::vector<uint8_t>& value)
161 {
162  return _writeFlatVector(value);
163 }
164 
166 template <>
167 inline DataOStream& DataOStream::operator<<(const std::vector<uint16_t>& value)
168 {
169  return _writeFlatVector(value);
170 }
171 
173 template <>
174 inline DataOStream& DataOStream::operator<<(const std::vector<int16_t>& value)
175 {
176  return _writeFlatVector(value);
177 }
178 
180 template <>
181 inline DataOStream& DataOStream::operator<<(const std::vector<uint32_t>& value)
182 {
183  return _writeFlatVector(value);
184 }
185 
187 template <>
188 inline DataOStream& DataOStream::operator<<(const std::vector<int32_t>& value)
189 {
190  return _writeFlatVector(value);
191 }
192 
194 template <>
195 inline DataOStream& DataOStream::operator<<(const std::vector<uint64_t>& value)
196 {
197  return _writeFlatVector(value);
198 }
199 
201 template <>
202 inline DataOStream& DataOStream::operator<<(const std::vector<int64_t>& value)
203 {
204  return _writeFlatVector(value);
205 }
206 
208 template <>
209 inline DataOStream& DataOStream::operator<<(const std::vector<float>& value)
210 {
211  return _writeFlatVector(value);
212 }
213 
215 template <>
216 inline DataOStream& DataOStream::operator<<(const std::vector<double>& value)
217 {
218  return _writeFlatVector(value);
219 }
220 
222 template <>
224  const std::vector<ObjectVersion>& value)
225 {
226  return _writeFlatVector(value);
227 }
229 }
DataOStream & operator<<(const T &value)
Write a plain data item by copying it to the stream.
Definition: dataOStream.h:91
A distributed object.
Definition: object.h:47
A helper struct bundling an object identifier and version.
Definition: objectVersion.h:46
CO_API bool isAttached() const
A std::ostream-like interface for object serialization.
Definition: dataOStream.h:56
Object-oriented network library.
Definition: barrier.h:27