ls1-MarDyn
ls1-MarDyn molecular dynamics code
ParaStrm.h
1
7#ifndef PARASTRM_H_
8#define PARASTRM_H_
9
10#include <malloc.h>
11
12#include "utils/mardyn_assert.h"
13#include <cstddef>
14#include <cstdlib>
15#include <cstring>
16
21class ParaStrm {
22public:
23 //enum Eparatype {UNDEF=0,DOUBLE,INT};
24
26 ParaStrm() : m_size(0)/*, m_pos(0)*/, m_pstrm(NULL), m_readpos(NULL) { }
27
29 ParaStrm( const ParaStrm& param_stream ) : m_size( param_stream.m_size ), m_pstrm(NULL), m_readpos(NULL) {
30 m_pstrm = (char *) malloc(m_size);
31 mardyn_assert(m_pstrm);
32 memcpy( m_pstrm, param_stream.m_pstrm, m_size );
33 m_readpos = m_pstrm;
34 }
35
38 if (m_pstrm)
39 free(m_pstrm);
40 }
41
43 bool eos() const {
44 return m_readpos >= m_pstrm + m_size;
45 }
47 bool empty() const {
48 return m_size == 0;
49 }
51 void reset_read() {
52 m_readpos = m_pstrm; /*m_pos=0;*/
53 }
54
56 ParaStrm& operator=( const ParaStrm& param_stream ) {
57 m_size = param_stream.m_size;
58 m_pstrm = (char *) malloc(m_size);
59 mardyn_assert(m_pstrm);
60 memcpy( m_pstrm, param_stream.m_pstrm, m_size );
61 m_readpos = m_pstrm;
62 return *this;
63 }
64
66 template<class T> ParaStrm& operator <<(T p) {
67 ptrdiff_t oldsize = m_size;
68 // determine relative reading position
69 ptrdiff_t readpos = m_readpos - m_pstrm;
70 // enlarge vector
71 m_size += sizeof(p);
72 if (!m_pstrm)
73 m_pstrm = (char*) malloc(m_size);
74 else
75 m_pstrm = (char*) realloc(m_pstrm, m_size);
76 // realloc might change m_pstrm!
77 mardyn_assert(m_pstrm);
78 // save value
79 *((T*) (m_pstrm + oldsize)) = p;
80 //m_ptypes.push_back(UNDEF);
81 // adjust m_readpos to maybe changed m_pstrm
82 m_readpos = m_pstrm + readpos;
83 return *this;
84 }
85
87 template<class T> ParaStrm& operator >>(T& p) {
88 mardyn_assert(m_readpos+sizeof(T)<=m_pstrm+m_size);
89 // get value
90 p = *((T*) m_readpos);
91 // move reading position
92 m_readpos = (char*) ((T*) m_readpos + 1);
93 //++m_pos;
94 return *this;
95 }
96
97private:
98 size_t m_size;
99 char *m_pstrm;
100 char *m_readpos;
101 // we also could check for valid types...
102 //std::vector<Eparatype> m_ptypes;
103 //std::size_t m_pos;
104};
105
106#endif /*PARASTRM_H_*/
Definition: ParaStrm.h:21
ParaStrm & operator>>(T &p)
read value at actual stream position and move position
Definition: ParaStrm.h:87
bool eos() const
end of stream reached?
Definition: ParaStrm.h:43
~ParaStrm()
Destructor.
Definition: ParaStrm.h:37
ParaStrm(const ParaStrm &param_stream)
Constructor using an existing parameter stream (resets the reading "pointer" to the beginning of the ...
Definition: ParaStrm.h:29
ParaStrm()
Constructor generating a new, empty parameter stream.
Definition: ParaStrm.h:26
void reset_read()
reset reading "pointer" to the beginning of the stream
Definition: ParaStrm.h:51
ParaStrm & operator=(const ParaStrm &param_stream)
Copy parameter stream (resets the reading "pointer" to the beginning of the stream).
Definition: ParaStrm.h:56
ParaStrm & operator<<(T p)
add value to the (end of) stream
Definition: ParaStrm.h:66
bool empty() const
stream empty?
Definition: ParaStrm.h:47