g4tools  5.4.0
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
tools::wroot::buffer Class Reference
Inheritance diagram for tools::wroot::buffer:
Inheritance graph
[legend]
Collaboration diagram for tools::wroot::buffer:
Collaboration graph
[legend]

Public Member Functions

 buffer (std::ostream &a_out, bool a_byte_swap, uint32 a_size)
 
virtual ~buffer ()
 
bool byte_swap () const
 
std::ostream & out () const
 
char * buf ()
 
const char * buf () const
 
uint32 length () const
 
uint32 size () const
 
char *& pos ()
 
char * max_pos () const
 
template<class T >
bool write (T x)
 
bool write (bool x)
 
bool write (const std::string &x)
 
bool write_fast_array (const char *a_a, uint32 a_n)
 
bool write_cstring (const char *a_s)
 
template<class T >
bool write_fast_array (const T *a_a, uint32 a_n)
 
template<class T >
bool write_fast_array (const std::vector< T > &a_v)
 
template<class T >
bool write_array (const T *a_a, uint32 a_n)
 
template<class T >
bool write_array (const std::vector< T > a_v)
 
template<class T >
bool write_array2 (const std::vector< std::vector< T > > a_v)
 
bool write_version (short a_version)
 
bool write_version (short a_version, uint32 &a_pos)
 
bool set_byte_count (uint32 a_pos)
 
bool write_object (const ibo &a_obj)
 
bool expand2 (uint32 a_new_size)
 
bool expand (uint32 a_new_size)
 
size_t to_displace () const
 
bool displace_mapped (unsigned int a_num)
 
void reset_objs_map ()
 

Protected Member Functions

 buffer (const buffer &a_from)
 
bufferoperator= (const buffer &)
 
bool write_class (const std::string &a_cls)
 
bool set_byte_count_obj (uint32 a_pos)
 

Static Protected Member Functions

static short kMaxVersion ()
 
static uint32 kMaxMapCount ()
 
static short kByteCountVMask ()
 
static uint32 kNewClassTag ()
 
static int kMapOffset ()
 
static unsigned int kClassMask ()
 
static uint32 kByteCountMask ()
 

Protected Attributes

std::ostream & m_out
 
bool m_byte_swap
 
uint32 m_size
 
char * m_buffer
 
char * m_max
 
char * m_pos
 
wbuf m_wb
 
std::map< ibo *, uint32m_objs
 
std::vector< std::pair< uint32, uint32 > > m_obj_mapped
 
std::map< std::string, uint32m_clss
 
std::vector< std::pair< uint32, uint32 > > m_cls_mapped
 

Detailed Description

Definition at line 28 of file buffer.

Constructor & Destructor Documentation

◆ buffer() [1/2]

tools::wroot::buffer::buffer ( std::ostream &  a_out,
bool  a_byte_swap,
uint32  a_size 
)
inline

Definition at line 34 of file buffer.

35  :m_out(a_out)
36  ,m_byte_swap(a_byte_swap)
37  ,m_size(0)
38  ,m_buffer(0)
39  ,m_max(0)
40  ,m_pos(0)
41  ,m_wb(a_out,a_byte_swap,0,m_pos) //it holds a ref on m_pos.
42  {
43 #ifdef TOOLS_MEM
44  mem::increment(s_class().c_str());
45 #endif
46  m_size = a_size;
47  m_buffer = new char[m_size];
48  //if(!m_buffer) {}
50  m_pos = m_buffer;
52  }

◆ ~buffer()

virtual tools::wroot::buffer::~buffer ( )
inlinevirtual

Definition at line 54 of file buffer.

54  {
55  m_objs.clear();
56  m_obj_mapped.clear();
57 
58  m_clss.clear();
59  m_cls_mapped.clear();
60 
61  delete [] m_buffer;
62 #ifdef TOOLS_MEM
63  mem::decrement(s_class().c_str());
64 #endif
65  }

◆ buffer() [2/2]

tools::wroot::buffer::buffer ( const buffer a_from)
inlineprotected

Definition at line 67 of file buffer.

68  :m_out(a_from.m_out)
69  ,m_byte_swap(a_from.m_byte_swap)
70  ,m_size(0)
71  ,m_buffer(0)
72  ,m_max(0)
73  ,m_pos(0)
74  ,m_wb(a_from.m_out,a_from.m_byte_swap,0,m_pos)
75  {
76 #ifdef TOOLS_MEM
77  mem::increment(s_class().c_str());
78 #endif
79  }

Member Function Documentation

◆ buf() [1/2]

char* tools::wroot::buffer::buf ( )
inline

Definition at line 87 of file buffer.

87 {return m_buffer;}

◆ buf() [2/2]

const char* tools::wroot::buffer::buf ( ) const
inline

Definition at line 88 of file buffer.

88 {return m_buffer;}

◆ byte_swap()

bool tools::wroot::buffer::byte_swap ( ) const
inline

Definition at line 82 of file buffer.

82 {return m_byte_swap;}

◆ displace_mapped()

bool tools::wroot::buffer::displace_mapped ( unsigned int  a_num)
inline

Definition at line 297 of file buffer.

297  {
298  char* opos = m_pos;
299 
300  //m_out << "tools::wroot::buffer::displace_mapped :"
301  // << " cls num " << m_cls_mapped.size()
302  // << std::endl;
303 
304  typedef std::pair<uint32,uint32> offset_id;
305 
306  {tools_vforcit(offset_id,m_cls_mapped,it) {
307  unsigned int offset = (*it).first;
308  unsigned int id = (*it).second;
309  //m_out << "displace " << offset << " " << id << std::endl;
310  m_pos = m_buffer+offset;
311  unsigned int clIdx = id+a_num;
312  if(!write(uint32(clIdx|kClassMask()))) {m_pos = opos;return false;}
313  }}
314 
315  //m_out << "tools::wroot::buffer::displace_mapped :"
316  // << " obj num " << m_obj_mapped.size()
317  // << std::endl;
318 
319  {tools_vforcit(offset_id,m_obj_mapped,it) {
320  uint32 offset = (*it).first;
321  uint32 id = (*it).second;
322  //m_out << "displace at " << offset
323  // << " the obj pos " << id
324  // << " by " << a_num
325  // << std::endl;
326  m_pos = m_buffer+offset;
327  unsigned int objIdx = id+a_num;
328  if(!write(objIdx)) {m_pos = opos;return false;}
329  }}
330 
331  m_pos = opos;
332  return true;
333  }

◆ expand()

bool tools::wroot::buffer::expand ( uint32  a_new_size)
inline

Definition at line 276 of file buffer.

276  {
278  if(!realloc<char>(m_buffer,a_new_size,m_size)) {
279  m_out << "tools::wroot::buffer::expand :"
280  << " can't realloc " << a_new_size << " bytes."
281  << std::endl;
282  m_size = 0;
283  m_max = 0;
284  m_pos = 0;
285  m_wb.set_eob(m_max);
286  return false;
287  }
288  m_size = a_new_size;
289  m_max = m_buffer + m_size;
290  m_pos = m_buffer + len;
291  m_wb.set_eob(m_max);
292  return true;
293  }

◆ expand2()

bool tools::wroot::buffer::expand2 ( uint32  a_new_size)
inline

Definition at line 274 of file buffer.

274 {return expand(mx<uint32>(2*m_size,a_new_size));} //CERN-ROOT logic.

◆ kByteCountMask()

static uint32 tools::wroot::buffer::kByteCountMask ( )
inlinestaticprotected

Definition at line 347 of file buffer.

347 {return 0x40000000;}

◆ kByteCountVMask()

static short tools::wroot::buffer::kByteCountVMask ( )
inlinestaticprotected

Definition at line 342 of file buffer.

342 {return 0x4000;}

◆ kClassMask()

static unsigned int tools::wroot::buffer::kClassMask ( )
inlinestaticprotected

Definition at line 346 of file buffer.

346 {return 0x80000000;}

◆ kMapOffset()

static int tools::wroot::buffer::kMapOffset ( )
inlinestaticprotected

Definition at line 345 of file buffer.

345 {return 2;}

◆ kMaxMapCount()

static uint32 tools::wroot::buffer::kMaxMapCount ( )
inlinestaticprotected

Definition at line 341 of file buffer.

341 {return 0x3FFFFFFE;}

◆ kMaxVersion()

static short tools::wroot::buffer::kMaxVersion ( )
inlinestaticprotected

Definition at line 340 of file buffer.

340 {return 0x3FFF;}

◆ kNewClassTag()

static uint32 tools::wroot::buffer::kNewClassTag ( )
inlinestaticprotected

Definition at line 343 of file buffer.

343 {return 0xFFFFFFFF;}

◆ length()

uint32 tools::wroot::buffer::length ( ) const
inline

Definition at line 89 of file buffer.

89 {return uint32(m_pos-m_buffer);}

◆ max_pos()

char* tools::wroot::buffer::max_pos ( ) const
inline

Definition at line 93 of file buffer.

93 {return m_max;} //used in basket.

◆ operator=()

buffer& tools::wroot::buffer::operator= ( const buffer )
inlineprotected

Definition at line 80 of file buffer.

80 {return *this;}

◆ out()

std::ostream& tools::wroot::buffer::out ( ) const
inline

Definition at line 83 of file buffer.

83 {return m_out;}

◆ pos()

char*& tools::wroot::buffer::pos ( )
inline

Definition at line 92 of file buffer.

92 {return m_pos;} //used in basket.

◆ reset_objs_map()

void tools::wroot::buffer::reset_objs_map ( )
inline

Definition at line 335 of file buffer.

335  {
336  m_objs.clear();
337  //m_clss.clear();
338  }

◆ set_byte_count()

bool tools::wroot::buffer::set_byte_count ( uint32  a_pos)
inline

Definition at line 199 of file buffer.

199  {
200  uint32 cnt = (uint32)(m_pos-m_buffer) - a_pos - sizeof(unsigned int);
201  if(cnt>=kMaxMapCount()) {
202  m_out << "tools::wroot::buffer::set_byte_count :"
203  << " bytecount too large (more than "
204  << kMaxMapCount() << ")."
205  << std::endl;
206  return false;
207  }
208 
209  union {
210  uint32 cnt;
211  short vers[2];
212  } v;
213  v.cnt = cnt;
214 
215  char* opos = m_pos;
216  m_pos = (char*)(m_buffer+a_pos);
217  if(m_byte_swap) {
218  if(!m_wb.write(short(v.vers[1]|kByteCountVMask())))
219  {m_pos = opos;return false;}
220  if(!m_wb.write(v.vers[0])) {m_pos = opos;return false;}
221  } else {
222  if(!m_wb.write(short(v.vers[0]|kByteCountVMask())))
223  {m_pos = opos;return false;}
224  if(!m_wb.write(v.vers[1])) {m_pos = opos;return false;}
225  }
226  m_pos = opos;
227 
228  return true;
229  }

◆ set_byte_count_obj()

bool tools::wroot::buffer::set_byte_count_obj ( uint32  a_pos)
inlineprotected

Definition at line 373 of file buffer.

373  {
374  uint32 cnt = (uint32)(m_pos-m_buffer) - a_pos - sizeof(unsigned int);
375  if(cnt>=kMaxMapCount()) {
376  m_out << "tools::wroot::buffer::set_byte_count_obj :"
377  << " bytecount too large (more than "
378  << kMaxMapCount() << ")."
379  << std::endl;
380  return false;
381  }
382  char* opos = m_pos;
383  m_pos = (char*)(m_buffer+a_pos);
384  if(!m_wb.write(uint32(cnt|kByteCountMask()))) {m_pos = opos;return false;}
385  m_pos = opos;
386  return true;
387  }

◆ size()

uint32 tools::wroot::buffer::size ( ) const
inline

Definition at line 90 of file buffer.

90 {return m_size;}

◆ to_displace()

size_t tools::wroot::buffer::to_displace ( ) const
inline

Definition at line 295 of file buffer.

295 {return m_cls_mapped.size()+m_obj_mapped.size();}

◆ write() [1/3]

bool tools::wroot::buffer::write ( bool  x)
inline

Definition at line 104 of file buffer.

104 {return write<unsigned char>(x?1:0);}

◆ write() [2/3]

bool tools::wroot::buffer::write ( const std::string &  x)
inline

Definition at line 106 of file buffer.

106  {
107  uint32 sz = (uint32)(x.size() + sizeof(int) + 1);
108  if((m_pos+sz)>m_max) {
109  if(!expand2(m_size+sz)) return false;
110  }
111  return m_wb.write(x);
112  }

◆ write() [3/3]

template<class T >
bool tools::wroot::buffer::write ( x)
inline

Definition at line 97 of file buffer.

97  {
98  if(m_pos+sizeof(T)>m_max) {
99  if(!expand2(m_size+sizeof(T))) return false;
100  }
101  return m_wb.write(x);
102  }

◆ write_array() [1/2]

template<class T >
bool tools::wroot::buffer::write_array ( const std::vector< T >  a_v)
inline

Definition at line 154 of file buffer.

154  {
155  if(!write((uint32)a_v.size())) return false;
156  return write_fast_array(a_v);
157  }

◆ write_array() [2/2]

template<class T >
bool tools::wroot::buffer::write_array ( const T *  a_a,
uint32  a_n 
)
inline

Definition at line 148 of file buffer.

148  {
149  if(!write(a_n)) return false;
150  return write_fast_array(a_a,a_n);
151  }

◆ write_array2()

template<class T >
bool tools::wroot::buffer::write_array2 ( const std::vector< std::vector< T > >  a_v)
inline

Definition at line 160 of file buffer.

160  {
161  if(!write((uint32)a_v.size())) return false;
162  for(unsigned int index=0;index<a_v.size();index++) {
163  if(!write_array(a_v[index])) return false;
164  }
165  return true;
166  }

◆ write_class()

bool tools::wroot::buffer::write_class ( const std::string &  a_cls)
inlineprotected

Definition at line 349 of file buffer.

349  {
350 
351  std::map<std::string,uint32>::const_iterator it = m_clss.find(a_cls);
352  if(it!=m_clss.end()) {
353  uint32 clIdx = (*it).second;
354 
355  unsigned int offset = (unsigned int)(m_pos-m_buffer);
356 
357  // save index of already stored class
358  if(!write(uint32(clIdx|kClassMask()))) return false;
359 
360  m_cls_mapped.push_back(std::pair<uint32,uint32>(offset,clIdx));
361 
362  } else {
363 
364  unsigned int offset = (unsigned int)(m_pos-m_buffer);
365  if(!write(kNewClassTag())) return false;
366  if(!write_cstring(a_cls.c_str())) return false;
367  m_clss[a_cls] = offset + kMapOffset();
368 
369  }
370  return true;
371  }

◆ write_cstring()

bool tools::wroot::buffer::write_cstring ( const char *  a_s)
inline

Definition at line 125 of file buffer.

125 {return write_fast_array(a_s,(uint32(::strlen(a_s))+1)*sizeof(char));}

◆ write_fast_array() [1/3]

bool tools::wroot::buffer::write_fast_array ( const char *  a_a,
uint32  a_n 
)
inline

Definition at line 114 of file buffer.

114  {
115  if(!a_n) return true;
116  uint32 l = a_n * sizeof(char);
117  if((m_pos+l)>m_max) {
118  if(!expand2(m_size+l)) return false;
119  }
120  ::memcpy(m_pos,a_a,l);
121  m_pos += l;
122  return true;
123  }

◆ write_fast_array() [2/3]

template<class T >
bool tools::wroot::buffer::write_fast_array ( const std::vector< T > &  a_v)
inline

Definition at line 138 of file buffer.

138  {
139  if(a_v.empty()) return true;
140  uint32 l = uint32(a_v.size() * sizeof(T));
141  if((m_pos+l)>m_max) {
142  if(!expand2(m_size+l)) return false;
143  }
144  return m_wb.write<T>(a_v);
145  }

◆ write_fast_array() [3/3]

template<class T >
bool tools::wroot::buffer::write_fast_array ( const T *  a_a,
uint32  a_n 
)
inline

Definition at line 128 of file buffer.

128  {
129  if(!a_n) return true;
130  uint32 l = a_n * sizeof(T);
131  if((m_pos+l)>m_max) {
132  if(!expand2(m_size+l)) return false;
133  }
134  return m_wb.write<T>(a_a,a_n);
135  }

◆ write_object()

bool tools::wroot::buffer::write_object ( const ibo a_obj)
inline

Definition at line 231 of file buffer.

231  {
232  //GB : if adding a write map logic, think to have a displace_mapped()
233  // in basket::write_on_file().
234 
235  std::map<ibo*,uint32>::const_iterator it = m_objs.find((ibo*)&a_obj);
236  if(it!=m_objs.end()) {
237 
238  uint32 objIdx = (*it).second;
239 
240  unsigned int offset = (unsigned int)(m_pos-m_buffer);
241 
242  // save index of already stored object
243  if(!write(objIdx)) return false;
244 
245  m_obj_mapped.push_back(std::pair<uint32,uint32>(offset,objIdx));
246 
247  } else {
248 
249  // reserve space for leading byte count
250  uint32 cntpos = (unsigned int)(m_pos-m_buffer);
251 
252  //NOTE : the below test is lacking in CERN-ROOT !
253  if((m_pos+sizeof(unsigned int))>m_max) {
254  if(!expand2(m_size+sizeof(unsigned int))) return false;
255  }
256  m_pos += sizeof(unsigned int);
257 
258  // write class of object first
259  if(!write_class(a_obj.store_cls())) return false;
260 
261  // add to map before writing rest of object (to handle self reference)
262  // (+kMapOffset so it's != kNullTag)
263  m_objs[(ibo*)&a_obj] = cntpos + kMapOffset();
264 
265  // let the object write itself :
266  if(!a_obj.stream(*this)) return false;
267 
268  // write byte count
269  if(!set_byte_count_obj(cntpos)) return false;
270  }
271  return true;
272  }

◆ write_version() [1/2]

bool tools::wroot::buffer::write_version ( short  a_version)
inline

Definition at line 169 of file buffer.

169  {
170  if(a_version>kMaxVersion()) {
171  m_out << "tools::wroot::buffer::write_version :"
172  << " version number " << a_version
173  << " cannot be larger than " << kMaxVersion() << "."
174  << std::endl;
175  return false;
176  }
177  return write(a_version);
178  }

◆ write_version() [2/2]

bool tools::wroot::buffer::write_version ( short  a_version,
uint32 a_pos 
)
inline

Definition at line 179 of file buffer.

179  {
180  // reserve space for leading byte count
181  a_pos = (uint32)(m_pos-m_buffer);
182 
183  //NOTE : the below test is lacking in CERN-ROOT !
184  if((m_pos+sizeof(unsigned int))>m_max) {
185  if(!expand2(m_size+sizeof(unsigned int))) return false;
186  }
187  m_pos += sizeof(unsigned int);
188 
189  if(a_version>kMaxVersion()) {
190  m_out << "tools::wroot::buffer::write_version :"
191  << " version number " << a_version
192  << " cannot be larger than " << kMaxVersion() << "."
193  << std::endl;
194  return false;
195  }
196  return write(a_version);
197  }

Member Data Documentation

◆ m_buffer

char* tools::wroot::buffer::m_buffer
protected

Definition at line 393 of file buffer.

◆ m_byte_swap

bool tools::wroot::buffer::m_byte_swap
protected

Definition at line 391 of file buffer.

◆ m_cls_mapped

std::vector< std::pair<uint32,uint32> > tools::wroot::buffer::m_cls_mapped
protected

Definition at line 402 of file buffer.

◆ m_clss

std::map<std::string,uint32> tools::wroot::buffer::m_clss
protected

Definition at line 401 of file buffer.

◆ m_max

char* tools::wroot::buffer::m_max
protected

Definition at line 394 of file buffer.

◆ m_obj_mapped

std::vector< std::pair<uint32,uint32> > tools::wroot::buffer::m_obj_mapped
protected

Definition at line 399 of file buffer.

◆ m_objs

std::map<ibo*,uint32> tools::wroot::buffer::m_objs
protected

Definition at line 398 of file buffer.

◆ m_out

std::ostream& tools::wroot::buffer::m_out
protected

Definition at line 390 of file buffer.

◆ m_pos

char* tools::wroot::buffer::m_pos
protected

Definition at line 395 of file buffer.

◆ m_size

uint32 tools::wroot::buffer::m_size
protected

Definition at line 392 of file buffer.

◆ m_wb

wbuf tools::wroot::buffer::m_wb
protected

Definition at line 396 of file buffer.


The documentation for this class was generated from the following file:
tools::wroot::buffer::expand
bool expand(uint32 a_new_size)
Definition: buffer:276
tools::wroot::buffer::set_byte_count_obj
bool set_byte_count_obj(uint32 a_pos)
Definition: buffer:373
tools::wroot::buffer::kMapOffset
static int kMapOffset()
Definition: buffer:345
tools::wroot::buffer::kByteCountVMask
static short kByteCountVMask()
Definition: buffer:342
tools::wroot::buffer::write_cstring
bool write_cstring(const char *a_s)
Definition: buffer:125
tools::wroot::buffer::kNewClassTag
static uint32 kNewClassTag()
Definition: buffer:343
tools::wroot::wbuf::set_eob
void set_eob(const char *a_eob)
Definition: wbuf:119
tools::wroot::buffer::m_pos
char * m_pos
Definition: buffer:395
tools::wroot::buffer::m_cls_mapped
std::vector< std::pair< uint32, uint32 > > m_cls_mapped
Definition: buffer:402
tools::wroot::buffer::m_byte_swap
bool m_byte_swap
Definition: buffer:391
tools::wroot::buffer::expand2
bool expand2(uint32 a_new_size)
Definition: buffer:274
tools::wroot::buffer::m_obj_mapped
std::vector< std::pair< uint32, uint32 > > m_obj_mapped
Definition: buffer:399
tools::wroot::buffer::write_array
bool write_array(const T *a_a, uint32 a_n)
Definition: buffer:148
tools::wroot::buffer::kMaxMapCount
static uint32 kMaxMapCount()
Definition: buffer:341
tools::wroot::buffer::write
bool write(T x)
Definition: buffer:97
tools::wroot::buffer::write_fast_array
bool write_fast_array(const char *a_a, uint32 a_n)
Definition: buffer:114
tools::diff_pointer_t
unsigned long diff_pointer_t
Definition: typedefs:76
tools::wroot::buffer::m_objs
std::map< ibo *, uint32 > m_objs
Definition: buffer:398
tools::wroot::buffer::kByteCountMask
static uint32 kByteCountMask()
Definition: buffer:347
tools::wroot::buffer::m_out
std::ostream & m_out
Definition: buffer:390
tools::wroot::buffer::write_class
bool write_class(const std::string &a_cls)
Definition: buffer:349
tools::wroot::buffer::kMaxVersion
static short kMaxVersion()
Definition: buffer:340
tools::wroot::buffer::m_clss
std::map< std::string, uint32 > m_clss
Definition: buffer:401
tools::wroot::buffer::m_wb
wbuf m_wb
Definition: buffer:396
tools::wroot::buffer::kClassMask
static unsigned int kClassMask()
Definition: buffer:346
tools::wroot::buffer::m_size
uint32 m_size
Definition: buffer:392
tools_vforcit
#define tools_vforcit(a__T, a__v, a__it)
Definition: forit:7
tools::wroot::buffer::m_max
char * m_max
Definition: buffer:394
tools::uint32
unsigned int uint32
Definition: typedefs:71
tools::wroot::buffer::m_buffer
char * m_buffer
Definition: buffer:393
tools::wroot::wbuf::write
bool write(unsigned char a_x)
Definition: wbuf:134