Write TPR body as opaque XDR data in big-endian format
[alexxy/gromacs.git] / src / gromacs / utility / inmemoryserializer.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2016,2017,2018,2019, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 #include "gmxpre.h"
36
37 #include "inmemoryserializer.h"
38
39 #include "config.h"
40
41 #include <algorithm>
42 #include <vector>
43
44 namespace gmx
45 {
46
47 namespace
48 {
49
50 template<typename T>
51 class CharBuffer
52 {
53 public:
54     static constexpr size_t ValueSize = sizeof(T);
55
56     explicit CharBuffer(T value) { u.v = value; }
57     explicit CharBuffer(const char buffer[]) { std::copy(buffer, buffer + ValueSize, u.c); }
58
59     T value() const { return u.v; }
60
61     void appendTo(std::vector<char>* buffer)
62     {
63         buffer->insert(buffer->end(), u.c, u.c + ValueSize);
64     }
65
66 private:
67     union {
68         char c[ValueSize];
69         T    v;
70     } u;
71 };
72
73 //! Return \c value with the byte order swapped.
74 template<typename T>
75 T swapEndian(const T& value)
76 {
77     union {
78         T                           value_;
79         std::array<char, sizeof(T)> valueAsCharArray_;
80     } endianessSwappedValue;
81
82     endianessSwappedValue.value_ = value;
83     int hiByte                   = sizeof(T) - 1;
84     for (int loByte = 0; hiByte > loByte; loByte++, hiByte--)
85     {
86         std::swap(endianessSwappedValue.valueAsCharArray_[loByte],
87                   endianessSwappedValue.valueAsCharArray_[hiByte]);
88     }
89
90     return endianessSwappedValue.value_;
91 }
92
93 //! \brief Change the host-dependent endian settings to either Swap or DoNotSwap.
94 //
95 // \param endianSwapBehavior input swap behavior, might depend on host.
96 //
97 // \return Host-independent setting, either Swap or DoNotSwap.
98 EndianSwapBehavior setEndianSwapBehaviorFromHost(EndianSwapBehavior endianSwapBehavior)
99 {
100     if (endianSwapBehavior == EndianSwapBehavior::SwapIfHostIsBigEndian)
101     {
102         return GMX_INTEGER_BIG_ENDIAN ? EndianSwapBehavior::Swap : EndianSwapBehavior::DoNotSwap;
103     }
104     else if (endianSwapBehavior == EndianSwapBehavior::SwapIfHostIsLittleEndian)
105     {
106         return GMX_INTEGER_BIG_ENDIAN ? EndianSwapBehavior::DoNotSwap : EndianSwapBehavior::Swap;
107     }
108     else
109     {
110         return endianSwapBehavior;
111     }
112 }
113
114 } // namespace
115
116 /********************************************************************
117  * InMemorySerializer
118  */
119
120 class InMemorySerializer::Impl
121 {
122 public:
123     Impl(EndianSwapBehavior endianSwapBehavior) :
124         endianSwapBehavior_(setEndianSwapBehaviorFromHost(endianSwapBehavior))
125     {
126     }
127
128     template<typename T>
129     void doValue(T value)
130     {
131         if (endianSwapBehavior_ == EndianSwapBehavior::Swap)
132         {
133             CharBuffer<T>(swapEndian(value)).appendTo(&buffer_);
134         }
135         else
136         {
137             CharBuffer<T>(value).appendTo(&buffer_);
138         }
139     }
140     void doString(const std::string& value)
141     {
142         doValue<uint64_t>(value.size());
143         buffer_.insert(buffer_.end(), value.begin(), value.end());
144     }
145     void doOpaque(const char* data, std::size_t size)
146     {
147         buffer_.insert(buffer_.end(), data, data + size);
148     }
149
150     std::vector<char>  buffer_;
151     EndianSwapBehavior endianSwapBehavior_;
152 };
153
154 InMemorySerializer::InMemorySerializer(EndianSwapBehavior endianSwapBehavior) :
155     impl_(new Impl(endianSwapBehavior))
156 {
157 }
158
159 InMemorySerializer::~InMemorySerializer() = default;
160
161 std::vector<char> InMemorySerializer::finishAndGetBuffer()
162 {
163     return std::move(impl_->buffer_);
164 }
165
166 void InMemorySerializer::doBool(bool* value)
167 {
168     impl_->doValue(*value);
169 }
170
171 void InMemorySerializer::doUChar(unsigned char* value)
172 {
173     impl_->doValue(*value);
174 }
175
176 void InMemorySerializer::doChar(char* value)
177 {
178     impl_->doValue(*value);
179 }
180
181 void InMemorySerializer::doUShort(unsigned short* value)
182 {
183     impl_->doValue(*value);
184 }
185
186 void InMemorySerializer::doInt(int* value)
187 {
188     impl_->doValue(*value);
189 }
190
191 void InMemorySerializer::doInt32(int32_t* value)
192 {
193     impl_->doValue(*value);
194 }
195
196 void InMemorySerializer::doInt64(int64_t* value)
197 {
198     impl_->doValue(*value);
199 }
200
201 void InMemorySerializer::doFloat(float* value)
202 {
203     impl_->doValue(*value);
204 }
205
206 void InMemorySerializer::doDouble(double* value)
207 {
208     impl_->doValue(*value);
209 }
210
211 void InMemorySerializer::doReal(real* value)
212 {
213     impl_->doValue(*value);
214 }
215
216 void InMemorySerializer::doRvec(rvec* value)
217 {
218     for (int d = 0; d < DIM; d++)
219     {
220         doReal(&(*value)[d]);
221     }
222 }
223
224 void InMemorySerializer::doIvec(ivec* value)
225 {
226     for (int d = 0; d < DIM; d++)
227     {
228         doInt(&(*value)[d]);
229     }
230 }
231
232 void InMemorySerializer::doString(std::string* value)
233 {
234     impl_->doString(*value);
235 }
236
237 void InMemorySerializer::doOpaque(char* data, std::size_t size)
238 {
239     impl_->doOpaque(data, size);
240 }
241
242 /********************************************************************
243  * InMemoryDeserializer
244  */
245
246 class InMemoryDeserializer::Impl
247 {
248 public:
249     explicit Impl(ArrayRef<const char> buffer, bool sourceIsDouble, EndianSwapBehavior endianSwapBehavior) :
250         buffer_(buffer),
251         sourceIsDouble_(sourceIsDouble),
252         pos_(0),
253         endianSwapBehavior_(setEndianSwapBehaviorFromHost(endianSwapBehavior))
254     {
255     }
256
257     template<typename T>
258     void doValue(T* value)
259     {
260         if (endianSwapBehavior_ == EndianSwapBehavior::Swap)
261         {
262             *value = swapEndian(CharBuffer<T>(&buffer_[pos_]).value());
263         }
264         else
265         {
266             *value = CharBuffer<T>(&buffer_[pos_]).value();
267         }
268         pos_ += CharBuffer<T>::ValueSize;
269     }
270     void doString(std::string* value)
271     {
272         uint64_t size;
273         doValue<uint64_t>(&size);
274         *value = std::string(&buffer_[pos_], size);
275         pos_ += size;
276     }
277     void doOpaque(char* data, std::size_t size)
278     {
279         std::copy(&buffer_[pos_], &buffer_[pos_ + size], data);
280         pos_ += size;
281     }
282
283     ArrayRef<const char> buffer_;
284     bool                 sourceIsDouble_;
285     size_t               pos_;
286     EndianSwapBehavior   endianSwapBehavior_;
287 };
288
289 InMemoryDeserializer::InMemoryDeserializer(ArrayRef<const char> buffer,
290                                            bool                 sourceIsDouble,
291                                            EndianSwapBehavior   endianSwapBehavior) :
292     impl_(new Impl(buffer, sourceIsDouble, endianSwapBehavior))
293 {
294 }
295
296 InMemoryDeserializer::~InMemoryDeserializer() = default;
297
298 bool InMemoryDeserializer::sourceIsDouble() const
299 {
300     return impl_->sourceIsDouble_;
301 }
302
303 void InMemoryDeserializer::doBool(bool* value)
304 {
305     impl_->doValue(value);
306 }
307
308 void InMemoryDeserializer::doUChar(unsigned char* value)
309 {
310     impl_->doValue(value);
311 }
312
313 void InMemoryDeserializer::doChar(char* value)
314 {
315     impl_->doValue(value);
316 }
317
318 void InMemoryDeserializer::doUShort(unsigned short* value)
319 {
320     impl_->doValue(value);
321 }
322
323 void InMemoryDeserializer::doInt(int* value)
324 {
325     impl_->doValue(value);
326 }
327
328 void InMemoryDeserializer::doInt32(int32_t* value)
329 {
330     impl_->doValue(value);
331 }
332
333 void InMemoryDeserializer::doInt64(int64_t* value)
334 {
335     impl_->doValue(value);
336 }
337
338 void InMemoryDeserializer::doFloat(float* value)
339 {
340     impl_->doValue(value);
341 }
342
343 void InMemoryDeserializer::doDouble(double* value)
344 {
345     impl_->doValue(value);
346 }
347
348 void InMemoryDeserializer::doReal(real* value)
349 {
350     if (sourceIsDouble())
351     {
352         double temp = 0.0;
353         doDouble(&temp);
354         *value = temp;
355     }
356     else
357     {
358         float temp = 0.0;
359         doFloat(&temp);
360         *value = temp;
361     }
362 }
363
364 void InMemoryDeserializer::doRvec(rvec* value)
365 {
366     for (int d = 0; d < DIM; d++)
367     {
368         doReal(&(*value)[d]);
369     }
370 }
371
372 void InMemoryDeserializer::doIvec(ivec* value)
373 {
374     for (int d = 0; d < DIM; d++)
375     {
376         doInt(&(*value)[d]);
377     }
378 }
379
380 void InMemoryDeserializer::doString(std::string* value)
381 {
382     impl_->doString(value);
383 }
384
385 void InMemoryDeserializer::doOpaque(char* data, std::size_t size)
386 {
387     impl_->doOpaque(data, size);
388 }
389
390 } // namespace gmx