g4tools  5.4.0
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
tools::rroot::rbuf Class Reference
Inheritance diagram for tools::rroot::rbuf:
Inheritance graph
[legend]

Public Member Functions

 rbuf (std::ostream &a_out, bool a_byte_swap, const char *a_eob, char *&a_pos)
 
virtual ~rbuf ()
 
 rbuf (const rbuf &a_from)
 
rbufoperator= (const rbuf &a_from)
 
std::ostream & out () const
 
void skip (unsigned int a_num)
 
void set_eob (const char *a_eob)
 
char *& pos ()
 
const char * eob () const
 
void set_byte_swap (bool a_value)
 
bool read (unsigned char &a_x)
 
bool read (unsigned short &a_x)
 
bool read (unsigned int &a_x)
 
bool read (uint64 &a_x)
 
bool read (float &a_x)
 
bool read (double &a_x)
 
bool read (char &a_x)
 
bool read (short &a_x)
 
bool read (int &a_x)
 
bool read (int64 &a_x)
 
bool read (std::string &a_x)
 
bool read (bool &x)
 
bool read (std::vector< std::string > &a_a)
 
bool read_fast_array (bool *b, uint32 n)
 
bool read_fast_array (char *c, uint32 n)
 
bool read_fast_array (unsigned char *c, uint32 n)
 
template<class T >
bool read_fast_array (T *a_a, uint32 a_n)
 
template<class T >
bool read_array (uint32 a_sz, T *&a_a, uint32 &a_n)
 
template<class T >
bool read_array (std::vector< T > &a_v)
 
template<class T >
bool read_array2 (std::vector< std::vector< T > > &a_v)
 
bool check_eob (uint32 n)
 

Protected Member Functions

template<class T >
bool _check_eob (T &a_x)
 

Protected Attributes

std::ostream & m_out
 
bool m_byte_swap
 
const char * m_eob
 
char *& m_pos
 
r_2_func m_r_2_func
 
r_4_func m_r_4_func
 
r_8_func m_r_8_func
 

Detailed Description

Definition at line 22 of file rbuf.

Constructor & Destructor Documentation

◆ rbuf() [1/2]

tools::rroot::rbuf::rbuf ( std::ostream &  a_out,
bool  a_byte_swap,
const char *  a_eob,
char *&  a_pos 
)
inline

Definition at line 80 of file rbuf.

81  :m_out(a_out)
82  ,m_byte_swap(a_byte_swap)
83  ,m_eob(a_eob)
84  ,m_pos(a_pos)
85 
86  ,m_r_2_func(0)
87  ,m_r_4_func(0)
88  ,m_r_8_func(0)
89  {
90 #ifdef TOOLS_MEM
91  mem::increment(s_class().c_str());
92 #endif
93  set_byte_swap(a_byte_swap);
94  }

◆ ~rbuf()

virtual tools::rroot::rbuf::~rbuf ( )
inlinevirtual

Definition at line 95 of file rbuf.

95  {
96 #ifdef TOOLS_MEM
97  mem::decrement(s_class().c_str());
98 #endif
99  }

◆ rbuf() [2/2]

tools::rroot::rbuf::rbuf ( const rbuf a_from)
inline

Definition at line 101 of file rbuf.

102  :m_out(a_from.m_out)
103  ,m_byte_swap(a_from.m_byte_swap)
104  ,m_eob(a_from.m_eob)
105  ,m_pos(a_from.m_pos)
106  ,m_r_2_func(a_from.m_r_2_func)
107  ,m_r_4_func(a_from.m_r_4_func)
108  ,m_r_8_func(a_from.m_r_8_func)
109  {
110 #ifdef TOOLS_MEM
111  mem::increment(s_class().c_str());
112 #endif
113  set_byte_swap(a_from.m_byte_swap);
114  }

Member Function Documentation

◆ _check_eob()

template<class T >
bool tools::rroot::rbuf::_check_eob ( T &  a_x)
inlineprotected

Definition at line 387 of file rbuf.

387  {
388  if((m_pos+sizeof(T))>m_eob) {
389  a_x = T();
390  m_out << s_class() << " : " << stype(T()) << " : "
391  << " try to access out of buffer " << long_out(sizeof(T)) << " bytes"
392  << " (pos=" << charp_out(m_pos)
393  << ", eob=" << charp_out(m_eob) << ")." << std::endl;
394  return false;
395  }
396  return true;
397  }

◆ check_eob()

bool tools::rroot::rbuf::check_eob ( uint32  n)
inline

Definition at line 375 of file rbuf.

375  {
376  if((m_pos+n)>m_eob) {
377  m_out << "tools::rroot::rbuf::check_eob :"
378  << " try to access out of buffer " << n << " bytes."
379  << std::endl;
380  return false;
381  }
382  return true;
383  }

◆ eob()

const char* tools::rroot::rbuf::eob ( ) const
inline

Definition at line 131 of file rbuf.

131 {return m_eob;}

◆ operator=()

rbuf& tools::rroot::rbuf::operator= ( const rbuf a_from)
inline

Definition at line 115 of file rbuf.

115  {
116  set_byte_swap(a_from.m_byte_swap);
117  m_eob = a_from.m_eob;
118  //m_pos is a ref.
119  m_r_2_func = a_from.m_r_2_func;
120  m_r_4_func = a_from.m_r_4_func;
121  m_r_8_func = a_from.m_r_8_func;
122  return *this;
123  }

◆ out()

std::ostream& tools::rroot::rbuf::out ( ) const
inline

Definition at line 125 of file rbuf.

125 {return m_out;}

◆ pos()

char*& tools::rroot::rbuf::pos ( )
inline

Definition at line 130 of file rbuf.

130 {return m_pos;}

◆ read() [1/13]

bool tools::rroot::rbuf::read ( bool &  x)
inline

Definition at line 241 of file rbuf.

241  {
242  unsigned char uc = 0;
243  bool status = read(uc);
244  x = uc?true:false;
245  return status;
246  }

◆ read() [2/13]

bool tools::rroot::rbuf::read ( char &  a_x)
inline

Definition at line 186 of file rbuf.

186  {
187  if(!_check_eob<char>(a_x)) return false;
188  a_x = *m_pos;m_pos++;
189  return true;
190  }

◆ read() [3/13]

bool tools::rroot::rbuf::read ( double &  a_x)
inline

Definition at line 179 of file rbuf.

179  {
180  if(!_check_eob<double>(a_x)) return false;
181  m_r_8_func(m_pos,(char*)&a_x);
182  m_pos += sizeof(double);
183  return true;
184  }

◆ read() [4/13]

bool tools::rroot::rbuf::read ( float &  a_x)
inline

Definition at line 172 of file rbuf.

172  {
173  if(!_check_eob<float>(a_x)) return false;
174  m_r_4_func(m_pos,(char*)&a_x);
175  m_pos += sizeof(float);
176  return true;
177  }

◆ read() [5/13]

bool tools::rroot::rbuf::read ( int &  a_x)
inline

Definition at line 198 of file rbuf.

198  {
199  if(!_check_eob<int>(a_x)) return false;
200  m_r_4_func(m_pos,(char*)&a_x);
201  m_pos += sizeof(int);
202  return true;
203  }

◆ read() [6/13]

bool tools::rroot::rbuf::read ( int64 a_x)
inline

Definition at line 205 of file rbuf.

205  {
206  if(!_check_eob<int64>(a_x)) return false;
207  m_r_8_func(m_pos,(char*)&a_x);
208  m_pos += 8;
209  return true;
210  }

◆ read() [7/13]

bool tools::rroot::rbuf::read ( short &  a_x)
inline

Definition at line 191 of file rbuf.

191  {
192  if(!_check_eob<short>(a_x)) return false;
193  m_r_2_func(m_pos,(char*)&a_x);
194  m_pos += sizeof(short);
195  return true;
196  }

◆ read() [8/13]

bool tools::rroot::rbuf::read ( std::string &  a_x)
inline

Definition at line 212 of file rbuf.

212  {
213  unsigned char nwh;
214  if(!read(nwh)) {a_x.clear();return false;}
215  int nchars;
216  if(nwh == 255) {
217  if(!read(nchars)) {a_x.clear();return false;}
218  } else {
219  nchars = nwh;
220  }
221  if(nchars<0) {
222  m_out << s_class() << "::read(string) :"
223  << " negative char number " << nchars << "." << std::endl;
224  a_x.clear();
225  return false;
226  }
227  if((m_pos+nchars)>m_eob) {
228  m_out << s_class() << "::read(string) :"
229  << " try to access out of buffer " << long_out(nchars) << " bytes "
230  << " (pos=" << charp_out(m_pos)
231  << ", eob=" << charp_out(m_eob) << ")." << std::endl;
232  a_x.clear();
233  return false;
234  }
235  a_x.resize(nchars);
236  ::memcpy((char*)a_x.c_str(),m_pos,nchars);
237  m_pos += nchars;
238  return true;
239  }

◆ read() [9/13]

bool tools::rroot::rbuf::read ( std::vector< std::string > &  a_a)
inline

Definition at line 247 of file rbuf.

247  {
248  int n;
249  if(!read(n)) {a_a.clear();return false;}
250  for(int index=0;index<n;index++) {
251  std::string s;
252  if(!read(s)) {a_a.clear();return false;}
253  a_a.push_back(s);
254  }
255  return true;
256  }

◆ read() [10/13]

bool tools::rroot::rbuf::read ( uint64 a_x)
inline

Definition at line 165 of file rbuf.

165  {
166  if(!_check_eob<uint64>(a_x)) return false;
167  m_r_8_func(m_pos,(char*)&a_x);
168  m_pos += 8;
169  return true;
170  }

◆ read() [11/13]

bool tools::rroot::rbuf::read ( unsigned char &  a_x)
inline

Definition at line 146 of file rbuf.

146  {
147  if(!_check_eob<unsigned char>(a_x)) return false;
148  a_x = *m_pos;m_pos++;
149  return true;
150  }

◆ read() [12/13]

bool tools::rroot::rbuf::read ( unsigned int &  a_x)
inline

Definition at line 158 of file rbuf.

158  {
159  if(!_check_eob<unsigned int>(a_x)) return false;
160  m_r_4_func(m_pos,(char*)&a_x);
161  m_pos += sizeof(unsigned int);
162  return true;
163  }

◆ read() [13/13]

bool tools::rroot::rbuf::read ( unsigned short &  a_x)
inline

Definition at line 151 of file rbuf.

151  {
152  if(!_check_eob<unsigned short>(a_x)) return false;
153  m_r_2_func(m_pos,(char*)&a_x);
154  m_pos += sizeof(unsigned short);
155  return true;
156  }

◆ read_array() [1/2]

template<class T >
bool tools::rroot::rbuf::read_array ( std::vector< T > &  a_v)
inline

Definition at line 351 of file rbuf.

351  {
352  T* buffer = 0;
353  uint32 n;
354  if(!read_array(0,buffer,n)) {a_v.clear();return false;}
355  if(!buffer) {a_v.clear();return true;}
356  a_v.resize(n);
357  for(uint32 index=0;index<n;index++) {
358  a_v[index] = buffer[index];
359  }
360  delete [] buffer;
361  return true;
362  }

◆ read_array() [2/2]

template<class T >
bool tools::rroot::rbuf::read_array ( uint32  a_sz,
T *&  a_a,
uint32 a_n 
)
inline

Definition at line 314 of file rbuf.

314  {
315  a_n = 0;
316  {int n;
317  if(!read(n)) {a_n = 0;return false;}
318  a_n = n;}
319 
320  if(!a_n) return true;
321 
322  uint32 l = a_n * sizeof(T);
323  if(!check_eob(l)) return false;
324 
325  bool owner = false;
326  if(!a_a) {
327  //ignore a_sz
328  a_a = new T[a_n];
329  if(!a_a) {a_n=0;return false;}
330  owner = true;
331  } else {
332  if(a_n>a_sz) return false;
333  }
334 
335  if(m_byte_swap) {
336  for(uint32 i=0;i<a_n;i++) {
337  if(!read(*(a_a+i))) {
338  if(owner) {delete [] a_a;a_a = 0;}
339  a_n = 0;
340  return false;
341  }
342  }
343  } else {
344  ::memcpy(a_a,m_pos,l);
345  m_pos += l;
346  }
347  return true;
348  }

◆ read_array2()

template<class T >
bool tools::rroot::rbuf::read_array2 ( std::vector< std::vector< T > > &  a_v)
inline

Definition at line 365 of file rbuf.

365  {
366  int n;
367  if(!read(n)) {a_v.clear();return false;}
368  a_v.resize(n);
369  for(int index=0;index<n;index++) {
370  if(!read_array(a_v[index])) return false;
371  }
372  return true;
373  }

◆ read_fast_array() [1/4]

bool tools::rroot::rbuf::read_fast_array ( bool *  b,
uint32  n 
)
inline

Definition at line 261 of file rbuf.

261  {
262  if(!n) return true;
263  uint32 l = n * sizeof(unsigned char);
264  if(!check_eob(l)) return false;
265  for(uint32 i = 0; i < n; i++) {
266  unsigned char uc;
267  if(!read(uc)) return false;
268  b[i] = uc?true:false;
269  }
270  return true;
271  }

◆ read_fast_array() [2/4]

bool tools::rroot::rbuf::read_fast_array ( char *  c,
uint32  n 
)
inline

Definition at line 272 of file rbuf.

272  {
273  if(!n) return true;
274  uint32 l = n * sizeof(char);
275  if(!check_eob(l)) return false;
276  ::memcpy(c,m_pos,l);
277  m_pos += l;
278  return true;
279  }

◆ read_fast_array() [3/4]

template<class T >
bool tools::rroot::rbuf::read_fast_array ( T *  a_a,
uint32  a_n 
)
inline

Definition at line 290 of file rbuf.

290  {
291  if(!a_n) return true;
292 
293  uint32 l = a_n * sizeof(T);
294  if(!check_eob(l)) {
295  m_out << s_class() << "::read_fast_array :"
296  << " try to access out of buffer " << long_out(l) << " bytes "
297  << " (pos=" << charp_out(m_pos)
298  << ", eob=" << charp_out(m_eob) << ")." << std::endl;
299  return false;
300  }
301 
302  if(m_byte_swap) {
303  for(uint32 i=0;i<a_n;i++) {
304  if(!read(*(a_a+i))) return false;
305  }
306  } else {
307  ::memcpy(a_a,m_pos,l);
308  m_pos += l;
309  }
310  return true;
311  }

◆ read_fast_array() [4/4]

bool tools::rroot::rbuf::read_fast_array ( unsigned char *  c,
uint32  n 
)
inline

Definition at line 280 of file rbuf.

280  {
281  if(!n) return true;
282  uint32 l = n * sizeof(unsigned char);
283  if(!check_eob(l)) return false;
284  ::memcpy(c, m_pos, l);
285  m_pos += l;
286  return true;
287  }

◆ set_byte_swap()

void tools::rroot::rbuf::set_byte_swap ( bool  a_value)
inline

Definition at line 133 of file rbuf.

133  {
134  m_byte_swap = a_value;
135  if(m_byte_swap) {
136  m_r_2_func = read_swap_2;
137  m_r_4_func = read_swap_4;
138  m_r_8_func = read_swap_8;
139  } else {
140  m_r_2_func = read_nswp_2;
141  m_r_4_func = read_nswp_4;
142  m_r_8_func = read_nswp_8;
143  }
144  }

◆ set_eob()

void tools::rroot::rbuf::set_eob ( const char *  a_eob)
inline

Definition at line 129 of file rbuf.

129 {m_eob = a_eob;}

◆ skip()

void tools::rroot::rbuf::skip ( unsigned int  a_num)
inline

Definition at line 127 of file rbuf.

127 {m_pos += a_num;}

Member Data Documentation

◆ m_byte_swap

bool tools::rroot::rbuf::m_byte_swap
protected

Definition at line 401 of file rbuf.

◆ m_eob

const char* tools::rroot::rbuf::m_eob
protected

Definition at line 402 of file rbuf.

◆ m_out

std::ostream& tools::rroot::rbuf::m_out
protected

Definition at line 400 of file rbuf.

◆ m_pos

char*& tools::rroot::rbuf::m_pos
protected

Definition at line 403 of file rbuf.

◆ m_r_2_func

r_2_func tools::rroot::rbuf::m_r_2_func
protected

Definition at line 405 of file rbuf.

◆ m_r_4_func

r_4_func tools::rroot::rbuf::m_r_4_func
protected

Definition at line 406 of file rbuf.

◆ m_r_8_func

r_8_func tools::rroot::rbuf::m_r_8_func
protected

Definition at line 407 of file rbuf.


The documentation for this class was generated from the following file:
tools::rroot::rbuf::set_byte_swap
void set_byte_swap(bool a_value)
Definition: rbuf:133
tools::rroot::rbuf::m_r_8_func
r_8_func m_r_8_func
Definition: rbuf:407
tools::rroot::rbuf::read_array
bool read_array(uint32 a_sz, T *&a_a, uint32 &a_n)
Definition: rbuf:314
tools::rroot::rbuf::check_eob
bool check_eob(uint32 n)
Definition: rbuf:375
tools::rroot::rbuf::m_pos
char *& m_pos
Definition: rbuf:403
tools::rroot::rbuf::m_r_2_func
r_2_func m_r_2_func
Definition: rbuf:405
tools::rroot::rbuf::m_byte_swap
bool m_byte_swap
Definition: rbuf:401
tools::rroot::rbuf::m_eob
const char * m_eob
Definition: rbuf:402
tools::rroot::rbuf::read
bool read(unsigned char &a_x)
Definition: rbuf:146
tools::stype
const std::string & stype(const mat4f &)
Definition: mat4f:73
tools::uint32
unsigned int uint32
Definition: typedefs:71
tools::rroot::rbuf::m_out
std::ostream & m_out
Definition: rbuf:400
tools::rroot::rbuf::m_r_4_func
r_4_func m_r_4_func
Definition: rbuf:406