2 * This file is part of the GROMACS molecular simulation package.
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.
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.
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.
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.
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.
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.
37 #include "inmemoryserializer.h"
42 #include "gromacs/utility/gmxassert.h"
54 static const size_t ValueSize = sizeof(T);
56 explicit CharBuffer(T value) { u.v = value; }
57 explicit CharBuffer(const char buffer[]) { std::copy(buffer, buffer + ValueSize, u.c); }
59 T value() const { return u.v; }
61 void appendTo(std::vector<char>* buffer)
63 buffer->insert(buffer->end(), u.c, u.c + ValueSize);
75 /********************************************************************
79 class InMemorySerializer::Impl
85 CharBuffer<T>(value).appendTo(&buffer_);
87 void doString(const std::string& value)
89 doValue<uint64_t>(value.size());
90 buffer_.insert(buffer_.end(), value.begin(), value.end());
93 std::vector<char> buffer_;
96 InMemorySerializer::InMemorySerializer() : impl_(new Impl) {}
98 InMemorySerializer::~InMemorySerializer() {}
100 std::vector<char> InMemorySerializer::finishAndGetBuffer()
102 return std::move(impl_->buffer_);
105 void InMemorySerializer::doBool(bool* value)
107 impl_->doValue(*value);
110 void InMemorySerializer::doUChar(unsigned char* value)
112 impl_->doValue(*value);
115 void InMemorySerializer::doChar(char* value)
117 impl_->doValue(*value);
120 void InMemorySerializer::doUShort(unsigned short* value)
122 impl_->doValue(*value);
125 void InMemorySerializer::doInt(int* value)
127 impl_->doValue(*value);
130 void InMemorySerializer::doInt32(int32_t* value)
132 impl_->doValue(*value);
135 void InMemorySerializer::doInt64(int64_t* value)
137 impl_->doValue(*value);
140 void InMemorySerializer::doFloat(float* value)
142 impl_->doValue(*value);
145 void InMemorySerializer::doDouble(double* value)
147 impl_->doValue(*value);
150 void InMemorySerializer::doReal(real* value)
152 impl_->doValue(*value);
155 void InMemorySerializer::doRvec(rvec* value)
157 for (int d = 0; d < DIM; d++)
159 doReal(&(*value)[d]);
163 void InMemorySerializer::doIvec(ivec* value)
165 for (int d = 0; d < DIM; d++)
171 void InMemorySerializer::doString(std::string* value)
173 impl_->doString(*value);
176 /********************************************************************
177 * InMemoryDeserializer
180 class InMemoryDeserializer::Impl
183 explicit Impl(ArrayRef<const char> buffer, bool sourceIsDouble) :
185 sourceIsDouble_(sourceIsDouble),
191 void doValue(T* value)
193 *value = CharBuffer<T>(&buffer_[pos_]).value();
194 pos_ += CharBuffer<T>::ValueSize;
196 void doString(std::string* value)
199 doValue<uint64_t>(&size);
200 *value = std::string(&buffer_[pos_], size);
204 ArrayRef<const char> buffer_;
205 bool sourceIsDouble_;
209 InMemoryDeserializer::InMemoryDeserializer(ArrayRef<const char> buffer, bool sourceIsDouble) :
210 impl_(new Impl(buffer, sourceIsDouble))
214 InMemoryDeserializer::~InMemoryDeserializer() {}
216 bool InMemoryDeserializer::sourceIsDouble() const
218 return impl_->sourceIsDouble_;
221 void InMemoryDeserializer::doBool(bool* value)
223 impl_->doValue(value);
226 void InMemoryDeserializer::doUChar(unsigned char* value)
228 impl_->doValue(value);
231 void InMemoryDeserializer::doChar(char* value)
233 impl_->doValue(value);
236 void InMemoryDeserializer::doUShort(unsigned short* value)
238 impl_->doValue(value);
241 void InMemoryDeserializer::doInt(int* value)
243 impl_->doValue(value);
246 void InMemoryDeserializer::doInt32(int32_t* value)
248 impl_->doValue(value);
251 void InMemoryDeserializer::doInt64(int64_t* value)
253 impl_->doValue(value);
256 void InMemoryDeserializer::doFloat(float* value)
258 impl_->doValue(value);
261 void InMemoryDeserializer::doDouble(double* value)
263 impl_->doValue(value);
266 void InMemoryDeserializer::doReal(real* value)
268 if (sourceIsDouble())
282 void InMemoryDeserializer::doRvec(rvec* value)
284 for (int d = 0; d < DIM; d++)
286 doReal(&(*value)[d]);
290 void InMemoryDeserializer::doIvec(ivec* value)
292 for (int d = 0; d < DIM; d++)
298 void InMemoryDeserializer::doString(std::string* value)
300 impl_->doString(value);