g4tools  5.4.0
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
tools::wroot::wbuf Class Reference

Public Member Functions

 wbuf (std::ostream &a_out, bool a_byte_swap, const char *a_eob, char *&a_pos)
 
virtual ~wbuf ()
 
 wbuf (const wbuf &a_from)
 
wbufoperator= (const wbuf &a_from)
 
void set_eob (const char *a_eob)
 
bool byte_swap () const
 
void set_byte_swap (bool a_value)
 
bool write (unsigned char a_x)
 
bool write (unsigned short a_x)
 
bool write (unsigned int a_x)
 
bool write (uint64 a_x)
 
bool write (float a_x)
 
bool write (double a_x)
 
bool write (char a_x)
 
bool write (short a_x)
 
bool write (int a_x)
 
bool write (int64 a_x)
 
bool write (const std::string &a_x)
 
template<class T >
bool write (const T *a_a, uint32 a_n)
 
template<class T >
bool write (const std::vector< T > &a_v)
 

Protected Member Functions

template<class T >
bool check_eob ()
 
bool check_eob (size_t a_n, const char *a_cmt)
 

Protected Attributes

std::ostream & m_out
 
bool m_byte_swap
 
const char * m_eob
 
char *& m_pos
 
w_2_func m_w_2_func
 
w_4_func m_w_4_func
 
w_8_func m_w_8_func
 

Detailed Description

Definition at line 22 of file wbuf.

Constructor & Destructor Documentation

◆ wbuf() [1/2]

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

Definition at line 74 of file wbuf.

75  :m_out(a_out)
76  ,m_byte_swap(a_byte_swap)
77  ,m_eob(a_eob)
78  ,m_pos(a_pos)
79 
80  ,m_w_2_func(0)
81  ,m_w_4_func(0)
82  ,m_w_8_func(0)
83  {
84 #ifdef TOOLS_MEM
85  mem::increment(s_class().c_str());
86 #endif
87  set_byte_swap(a_byte_swap);
88  }

◆ ~wbuf()

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

Definition at line 89 of file wbuf.

89  {
90 #ifdef TOOLS_MEM
91  mem::decrement(s_class().c_str());
92 #endif
93  }

◆ wbuf() [2/2]

tools::wroot::wbuf::wbuf ( const wbuf a_from)
inline

Definition at line 95 of file wbuf.

96  :m_out(a_from.m_out) //a ref.
97  ,m_byte_swap(a_from.m_byte_swap)
98  ,m_eob(a_from.m_eob)
99  ,m_pos(a_from.m_pos) //a ref.
100  ,m_w_2_func(a_from.m_w_2_func)
101  ,m_w_4_func(a_from.m_w_4_func)
102  ,m_w_8_func(a_from.m_w_8_func)
103  {
104 #ifdef TOOLS_MEM
105  mem::increment(s_class().c_str());
106 #endif
107  set_byte_swap(a_from.m_byte_swap);
108  }

Member Function Documentation

◆ byte_swap()

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

Definition at line 120 of file wbuf.

120 {return m_byte_swap;}

◆ check_eob() [1/2]

template<class T >
bool tools::wroot::wbuf::check_eob ( )
inlineprotected

Definition at line 230 of file wbuf.

230  {
231  if((m_pos+sizeof(T))>m_eob) {
232  m_out << s_class() << " : " << stype(T()) << " : "
233 // << " try to access out of buffer " << long_out(sizeof(T)) << " bytes"
234  << " try to access out of buffer " << sizeof(T) << " bytes"
235  << " (pos=" << charp_out(m_pos)
236  << ", eob=" << charp_out(m_eob) << ")." << std::endl;
237  return false;
238  }
239  return true;
240  }

◆ check_eob() [2/2]

bool tools::wroot::wbuf::check_eob ( size_t  a_n,
const char *  a_cmt 
)
inlineprotected

Definition at line 241 of file wbuf.

241  {
242  if((m_pos+a_n)>m_eob) {
243  m_out << s_class() << " : " << a_cmt << " : "
244 // << " try to access out of buffer " << long_out(a_n) << " bytes"
245  << " try to access out of buffer " << a_n << " bytes"
246  << " (pos=" << charp_out(m_pos)
247  << ", eob=" << charp_out(m_eob) << ")." << std::endl;
248  return false;
249  }
250  return true;
251  }

◆ operator=()

wbuf& tools::wroot::wbuf::operator= ( const wbuf a_from)
inline

Definition at line 109 of file wbuf.

109  {
110  set_byte_swap(a_from.m_byte_swap);
111  m_eob = a_from.m_eob;
112  //m_pos is a ref.
113  m_w_2_func = a_from.m_w_2_func;
114  m_w_4_func = a_from.m_w_4_func;
115  m_w_8_func = a_from.m_w_8_func;
116  return *this;
117  }

◆ set_byte_swap()

void tools::wroot::wbuf::set_byte_swap ( bool  a_value)
inline

Definition at line 121 of file wbuf.

121  {
122  m_byte_swap = a_value;
123  if(m_byte_swap) {
124  m_w_2_func = write_swap_2;
125  m_w_4_func = write_swap_4;
126  m_w_8_func = write_swap_8;
127  } else {
128  m_w_2_func = write_nswp_2;
129  m_w_4_func = write_nswp_4;
130  m_w_8_func = write_nswp_8;
131  }
132  }

◆ set_eob()

void tools::wroot::wbuf::set_eob ( const char *  a_eob)
inline

Definition at line 119 of file wbuf.

119 {m_eob = a_eob;}

◆ write() [1/13]

bool tools::wroot::wbuf::write ( char  a_x)
inline

Definition at line 175 of file wbuf.

175 {return write((unsigned char)a_x);}

◆ write() [2/13]

bool tools::wroot::wbuf::write ( const std::string &  a_x)
inline

Definition at line 180 of file wbuf.

180  {
181  unsigned char nwh;
182  unsigned int nchars = (unsigned int)a_x.size();
183  if(nchars>254) {
184  if(!check_eob(1+4,"std::string")) return false;
185  nwh = 255;
186  if(!write(nwh)) return false;
187  if(!write(nchars)) return false;
188  } else {
189  if(!check_eob(1,"std::string")) return false;
190  nwh = (unsigned char)nchars;
191  if(!write(nwh)) return false;
192  }
193  if(!check_eob(nchars,"std::string")) return false;
194  for (unsigned int i = 0; i < nchars; i++) m_pos[i] = a_x[i];
195  m_pos += nchars;
196  return true;
197  }

◆ write() [3/13]

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

Definition at line 217 of file wbuf.

217  {
218  if(a_v.empty()) return true;
219  uint32 n = uint32(a_v.size());
220  uint32 l = n * sizeof(T);
221  if(!check_eob(l,"array")) return false;
222  for(uint32 i=0;i<n;i++) {
223  if(!write(a_v[i])) return false;
224  }
225  return true;
226  }

◆ write() [4/13]

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

Definition at line 201 of file wbuf.

201  {
202  if(!a_n) return true;
203  uint32 l = a_n * sizeof(T);
204  if(!check_eob(l,"array")) return false;
205  if(m_byte_swap) {
206  for(uint32 i=0;i<a_n;i++) {
207  if(!write(a_a[i])) return false;
208  }
209  } else {
210  ::memcpy(m_pos,a_a,l);
211  m_pos += l;
212  }
213  return true;
214  }

◆ write() [5/13]

bool tools::wroot::wbuf::write ( double  a_x)
inline

Definition at line 168 of file wbuf.

168  {
169  if(!check_eob<double>()) return false;
170  m_w_8_func(m_pos,(char*)&a_x);
171  m_pos += sizeof(double);
172  return true;
173  }

◆ write() [6/13]

bool tools::wroot::wbuf::write ( float  a_x)
inline

Definition at line 161 of file wbuf.

161  {
162  if(!check_eob<float>()) return false;
163  m_w_4_func(m_pos,(char*)&a_x);
164  m_pos += sizeof(float);
165  return true;
166  }

◆ write() [7/13]

bool tools::wroot::wbuf::write ( int  a_x)
inline

Definition at line 177 of file wbuf.

177 {return write((unsigned int)a_x);}

◆ write() [8/13]

bool tools::wroot::wbuf::write ( int64  a_x)
inline

Definition at line 178 of file wbuf.

178 {return write((uint64)a_x);}

◆ write() [9/13]

bool tools::wroot::wbuf::write ( short  a_x)
inline

Definition at line 176 of file wbuf.

176 {return write((unsigned short)a_x);}

◆ write() [10/13]

bool tools::wroot::wbuf::write ( uint64  a_x)
inline

Definition at line 154 of file wbuf.

154  {
155  if(!check_eob<uint64>()) return false;
156  m_w_8_func(m_pos,(char*)&a_x);
157  m_pos += 8;
158  return true;
159  }

◆ write() [11/13]

bool tools::wroot::wbuf::write ( unsigned char  a_x)
inline

Definition at line 134 of file wbuf.

134  {
135  if(!check_eob<unsigned char>()) return false;
136  *m_pos++ = a_x;
137  return true;
138  }

◆ write() [12/13]

bool tools::wroot::wbuf::write ( unsigned int  a_x)
inline

Definition at line 147 of file wbuf.

147  {
148  if(!check_eob<unsigned int>()) return false;
149  m_w_4_func(m_pos,(char*)&a_x);
150  m_pos += sizeof(unsigned int);
151  return true;
152  }

◆ write() [13/13]

bool tools::wroot::wbuf::write ( unsigned short  a_x)
inline

Definition at line 140 of file wbuf.

140  {
141  if(!check_eob<unsigned short>()) return false;
142  m_w_2_func(m_pos,(char*)&a_x);
143  m_pos += sizeof(unsigned short);
144  return true;
145  }

Member Data Documentation

◆ m_byte_swap

bool tools::wroot::wbuf::m_byte_swap
protected

Definition at line 255 of file wbuf.

◆ m_eob

const char* tools::wroot::wbuf::m_eob
protected

Definition at line 256 of file wbuf.

◆ m_out

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

Definition at line 254 of file wbuf.

◆ m_pos

char*& tools::wroot::wbuf::m_pos
protected

Definition at line 257 of file wbuf.

◆ m_w_2_func

w_2_func tools::wroot::wbuf::m_w_2_func
protected

Definition at line 259 of file wbuf.

◆ m_w_4_func

w_4_func tools::wroot::wbuf::m_w_4_func
protected

Definition at line 260 of file wbuf.

◆ m_w_8_func

w_8_func tools::wroot::wbuf::m_w_8_func
protected

Definition at line 261 of file wbuf.


The documentation for this class was generated from the following file:
tools::uint64
unsigned long long uint64
Definition: typedefs:72
tools::wroot::wbuf::m_eob
const char * m_eob
Definition: wbuf:256
tools::wroot::wbuf::set_byte_swap
void set_byte_swap(bool a_value)
Definition: wbuf:121
tools::wroot::wbuf::m_out
std::ostream & m_out
Definition: wbuf:254
tools::wroot::wbuf::check_eob
bool check_eob()
Definition: wbuf:230
tools::wroot::wbuf::m_w_8_func
w_8_func m_w_8_func
Definition: wbuf:261
tools::wroot::wbuf::m_byte_swap
bool m_byte_swap
Definition: wbuf:255
tools::wroot::wbuf::m_w_4_func
w_4_func m_w_4_func
Definition: wbuf:260
tools::stype
const std::string & stype(const mat4f &)
Definition: mat4f:73
tools::wroot::wbuf::m_w_2_func
w_2_func m_w_2_func
Definition: wbuf:259
tools::uint32
unsigned int uint32
Definition: typedefs:71
tools::wroot::wbuf::write
bool write(unsigned char a_x)
Definition: wbuf:134
tools::wroot::wbuf::m_pos
char *& m_pos
Definition: wbuf:257