Line data Source code
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 : {
25 : /** @name Specialized output operators */
26 : //@{
27 : /** Write a std::string. */
28 : template <>
29 11253 : inline DataOStream& DataOStream::operator<<(const std::string& str)
30 : {
31 11253 : const uint64_t nElems = str.length();
32 11253 : _write(&nElems, sizeof(nElems));
33 11253 : if (nElems > 0)
34 11097 : _write(str.c_str(), nElems);
35 :
36 11253 : return *this;
37 : }
38 :
39 : /** Write an object identifier and version. */
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 :
48 : /** Serialize an inline serializable object. */
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 1 : inline void DataOStream::_writeArray(const Array<void> array,
58 : const boost::false_type&)
59 : {
60 1 : _write(array.data, array.getNumBytes());
61 1 : }
62 :
63 : template <>
64 5 : inline void DataOStream::_writeArray(const Array<const void> array,
65 : const boost::false_type&)
66 : {
67 5 : _write(array.data, array.getNumBytes());
68 5 : }
69 :
70 : /** @cond IGNORE */
71 : template <class T>
72 33 : inline DataOStream& DataOStream::operator<<(const lunchbox::RefPtr<T>& ptr)
73 : {
74 33 : 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 2 : inline DataOStream& DataOStream::operator<<(const std::vector<T>& value)
86 : {
87 2 : const uint64_t nElems = value.size();
88 2 : *this << nElems;
89 5 : for (uint64_t i = 0; i < nElems; ++i)
90 3 : *this << value[i];
91 2 : 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>
121 : inline DataOStream& DataOStream::operator<<(
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 : }
156 : /** @endcond */
157 :
158 : /** Optimized specialization to write a std::vector of uint8_t. */
159 : template <>
160 : inline DataOStream& DataOStream::operator<<(const std::vector<uint8_t>& value)
161 : {
162 : return _writeFlatVector(value);
163 : }
164 :
165 : /** Optimized specialization to write a std::vector of uint16_t. */
166 : template <>
167 : inline DataOStream& DataOStream::operator<<(const std::vector<uint16_t>& value)
168 : {
169 : return _writeFlatVector(value);
170 : }
171 :
172 : /** Optimized specialization to write a std::vector of int16_t. */
173 : template <>
174 : inline DataOStream& DataOStream::operator<<(const std::vector<int16_t>& value)
175 : {
176 : return _writeFlatVector(value);
177 : }
178 :
179 : /** Optimized specialization to write a std::vector of uint32_t. */
180 : template <>
181 : inline DataOStream& DataOStream::operator<<(const std::vector<uint32_t>& value)
182 : {
183 : return _writeFlatVector(value);
184 : }
185 :
186 : /** Optimized specialization to write a std::vector of int32_t. */
187 : template <>
188 : inline DataOStream& DataOStream::operator<<(const std::vector<int32_t>& value)
189 : {
190 : return _writeFlatVector(value);
191 : }
192 :
193 : /** Optimized specialization to write a std::vector of uint64_t. */
194 : template <>
195 : inline DataOStream& DataOStream::operator<<(const std::vector<uint64_t>& value)
196 : {
197 : return _writeFlatVector(value);
198 : }
199 :
200 : /** Optimized specialization to write a std::vector of int64_t. */
201 : template <>
202 : inline DataOStream& DataOStream::operator<<(const std::vector<int64_t>& value)
203 : {
204 : return _writeFlatVector(value);
205 : }
206 :
207 : /** Optimized specialization to write a std::vector of float. */
208 : template <>
209 : inline DataOStream& DataOStream::operator<<(const std::vector<float>& value)
210 : {
211 : return _writeFlatVector(value);
212 : }
213 :
214 : /** Optimized specialization to write a std::vector of double. */
215 : template <>
216 1 : inline DataOStream& DataOStream::operator<<(const std::vector<double>& value)
217 : {
218 1 : return _writeFlatVector(value);
219 : }
220 :
221 : /** Optimized specialization to write a std::vector of ObjectVersion. */
222 : template <>
223 3 : inline DataOStream& DataOStream::operator<<(
224 : const std::vector<ObjectVersion>& value)
225 : {
226 3 : return _writeFlatVector(value);
227 : }
228 : //@}
229 : }
|