Page MenuHomec4science

pack_buffer.hh
No OneTemporary

File Metadata

Created
Sun, Nov 3, 20:33

pack_buffer.hh

/**
* @file pack_buffer.hh
*
* @author Guillaume Anciaux <guillaume.anciaux@epfl.ch>
*
* @date Mon Oct 28 13:15:05 2013
*
* @brief The is a buffer useful in the inter/intra models
*
* @section LICENSE
*
* Copyright (©) 2010-2011 EPFL (Ecole Polytechnique Fédérale de Lausanne)
* Laboratory (LSMS - Laboratoire de Simulation en Mécanique des Solides)
*
* LibMultiScale is free software: you can redistribute it and/or modify it
* under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* LibMultiScale is distributed in the hope that it will be useful, but
* WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with LibMultiScale. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef __LIBMULTISCALE_PACK_BUFFER_H__
#define __LIBMULTISCALE_PACK_BUFFER_H__
/* -------------------------------------------------------------------------- */
#include <vector>
/* -------------------------------------------------------------------------- */
__BEGIN_LIBMULTISCALE__
class PackBuffer {
public:
PackBuffer() : read_position(0){};
//! add an object to the packed buffer
template <typename T> inline void operator<<(T &data);
//! add a vector of objects to the packed buffer
template <typename T> inline void operator<<(std::vector<T> &data);
//! pop out a packed object
template <typename T> inline void operator>>(T &data);
//! clear the pack buffer
inline void clear();
//! return actual size of the pack buffer
inline UInt size();
//! return what remains to read
inline UInt remainingSize();
//! resize the pack buffer (argument in bytes)
inline void resize(UInt size);
//! return the raw buffer
inline char *buffer();
private:
std::vector<char> buf;
UInt read_position;
};
/* -------------------------------------------------------------------------- */
template <typename T> void PackBuffer::operator<<(T &data) {
UInt size = buf.size();
buf.resize(size + sizeof(T));
T *ptr = (T *)&buf[size];
*ptr = data;
};
/* -------------------------------------------------------------------------- */
template <typename T> void PackBuffer::operator<<(std::vector<T> &data) {
UInt size = buf.size();
UInt add_size = data.size() * sizeof(T);
buf.resize(size + add_size);
memcpy(&buf[size], &data[0], add_size);
};
/* -------------------------------------------------------------------------- */
template <typename T> void PackBuffer::operator>>(T &data) {
#ifndef LM_OPTIMIZED
UInt size = buf.size();
#endif // LM_OPTIMIZED
LM_ASSERT(read_position < size, "no more data to read");
T *ptr = (T *)&buf[read_position];
data = *ptr;
read_position += sizeof(T);
};
/* -------------------------------------------------------------------------- */
void PackBuffer::clear() {
buf.clear();
read_position = 0;
};
/* -------------------------------------------------------------------------- */
UInt PackBuffer::size() { return buf.size() - read_position; };
/* -------------------------------------------------------------------------- */
UInt PackBuffer::remainingSize() { return buf.size(); };
/* -------------------------------------------------------------------------- */
void PackBuffer::resize(UInt size) { buf.resize(size); };
/* -------------------------------------------------------------------------- */
char *PackBuffer::buffer() { return &buf[0]; }
/* -------------------------------------------------------------------------- */
__END_LIBMULTISCALE__
#endif /* __LIBMULTISCALE_PACK_BUFFER_H__ */

Event Timeline