Page MenuHomec4science

comm_buffer.hh
No OneTemporary

File Metadata

Created
Sun, Jan 26, 16:08

comm_buffer.hh

/**
* @file comm_buffer.hh
*
* @author Guillaume Anciaux <guillaume.anciaux@epfl.ch>
*
* @date Mon Oct 28 19:23:14 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_COMM_BUFFER_H__
#define __LIBMULTISCALE_COMM_BUFFER_H__
/* -------------------------------------------------------------------------- */
#include <vector>
/* -------------------------------------------------------------------------- */
__BEGIN_LIBMULTISCALE__
template <typename T>
class CommBuffer {
public:
CommBuffer():
buf(*new std::vector<T>()),
index(0),auto_allocated(true){
buf.reserve(2048);
};
CommBuffer(std::vector<T> & v):
buf(v),index(0),auto_allocated(false){};
~CommBuffer(){
if (auto_allocated){
delete &buf;
}
};
//! access current data and shift index
inline void operator = (CommBuffer<T> & buf);
//! access current data and shift index
inline void operator >> (T & data);
//! fill the current buffer
inline void operator << (T & data);
//! shift the index
inline void operator += (UInt shift);
//! rewind the index
inline void rewindIndex();
//! clear the buffer
inline void clear();
//! return actual size of the pack buffer
inline UInt size();
//! resize the pack buffer
inline void resize(UInt size);
//! append size new elements at the end of the buffer
inline void append(UInt size);
//! return the raw buffer
inline T * buffer();
//! return the raw buffer with an operator
inline T* operator *();
private:
std::vector<T> & buf;
UInt index;
bool auto_allocated;
};
/* -------------------------------------------------------------------------- */
template <typename T>
inline void CommBuffer<T>::clear(){
buf.resize(0);
index=0;
};
/* -------------------------------------------------------------------------- */
template <typename T>
inline UInt CommBuffer<T>::size(){
return buf.size();
};
/* -------------------------------------------------------------------------- */
template <typename T>
inline void CommBuffer<T>::resize(UInt size){
buf.resize(size);
};
/* -------------------------------------------------------------------------- */
template <typename T>
inline void CommBuffer<T>::append(UInt size){
UInt actual_size = buf.size();
buf.resize(actual_size+size);
};
/* -------------------------------------------------------------------------- */
template <typename T>
inline T * CommBuffer<T>::buffer(){
return &buf[index];
}
/* -------------------------------------------------------------------------- */
template <typename T>
inline void CommBuffer<T>::operator =(CommBuffer<T> & _buf){
buf = _buf.buf;
}
/* -------------------------------------------------------------------------- */
template <typename T>
T * CommBuffer<T>::operator *(){
return &buf[index];
}
/* -------------------------------------------------------------------------- */
template <typename T>
inline void CommBuffer<T>::operator >> (T & data){
LM_ASSERT(index < buf.size(),"overflow: no more data to read");
data = buf[index++];
}
/* -------------------------------------------------------------------------- */
template <typename T>
inline void CommBuffer<T>::operator << (T & data){
buf.push_back(data);
}
/* -------------------------------------------------------------------------- */
template <typename T>
inline void CommBuffer<T>::operator += (UInt shift){
index += shift;
}
__END_LIBMULTISCALE__
/* -------------------------------------------------------------------------- */
#endif /* __LIBMULTISCALE_COMM_BUFFER_H__ */

Event Timeline