26 #ifndef DOZERG_DATA_STREAM_IMPL_H_20081016 27 #define DOZERG_DATA_STREAM_IMPL_H_20081016 30 #include "../to_string.hh" 31 #include "../char_buffer.hh" 32 #include "../tools/net.hh" 33 #include "../tools/debug.hh" 34 #include "../template.hh" 53 void status(
int code){status_ = code;}
104 bool good()
const{
return (0 == status_);}
132 , hostByteOrder_(tools::HostByteOrder())
135 bool needReverse()
const{
return (hostByteOrder_ != dataByteOrder_);}
136 void pushStub(
size_t pos){
138 stubs_.push_back(pos);
140 bool popStub(
size_t * pos){
148 *pos = stubs_.back();
152 bool checkStub(
size_t pos){
157 if(pos > stubs_.back()){
163 size_t getStub(
size_t end)
const{
166 return std::min(end, stubs_.back());
174 std::string toString()
const{
176 oss<<
"{status_="<<status_
177 <<
", hostByteOrder_="<<(hostByteOrder_ ?
"LittleEndian" :
"BigEndian")
178 <<
", dataByteOrder_="<<(dataByteOrder_ ?
"LittleEndian" :
"BigEndian");
181 for(
size_t i = 0;i < stubs_.size();++i){
192 bool bo(
bool net){
return (net ?
false : hostByteOrder_);}
195 const bool hostByteOrder_;
201 std::vector<size_t> stubs_;
204 #ifndef DOX_UNDOCUMENT_FLAG 206 template<
typename Char>
207 inline void __buf_copy(Char * dst,
const Char * src,
size_t sz)
209 typedef std::char_traits<Char> __Traits;
212 __Traits::assign(*dst, *src);
214 __Traits::copy(dst, src, sz);
217 template<
typename Char>
218 inline void __buf_move(Char * dst,
const Char * src,
size_t sz)
220 typedef std::char_traits<Char> __Traits;
223 __Traits::assign(*dst, *src);
225 __Traits::move(dst, src, sz);
228 inline std::string dumpBufPart(
const char * buf,
size_t sz,
size_t part)
231 return std::string(
"(NULL)");
234 <<tools::DumpHex(buf, part,
' ',
false)
237 oss<<tools::DumpHex(buf + part, sz - part,
' ',
false);
246 typedef typename __Buf::value_type __Char;
248 typedef __buf_ref_data<__Buf> __Myt;
250 explicit __buf_ref_data(__Buf & buf)
255 __buf_ref_data(__Buf & buf,
size_t begin)
260 size_t cur()
const{
return cur_;}
261 __Char * data(
size_t i){
return &buf_[offset(i)];}
262 bool ensure(
size_t len){
263 const size_t old = buf_.size();
264 if(offset(cur_ + len) > old)
265 buf_.resize(old + (old >> 1) + len);
268 bool seek(
size_t pos){
269 if(pos > cur_ && !ensure(pos - cur_))
274 void append(
const __Char * buf,
size_t sz){
275 insert(cur_, buf, sz);
277 void append(__Char c){
278 assert(offset(cur_ + 1) <= buf_.size());
281 void insert(
size_t off,
const __Char * buf,
size_t sz){
283 assert(sz && offset(cur_ + sz) <= buf_.size());
285 const size_t c = cur_ - off;
287 __buf_move(&buf_[offset(off + sz)], data(off), c);
288 __buf_copy(data(off), buf, sz);
292 buf_.resize(offset(cur_));
296 template<
typename SizeT>
297 bool exportData(SizeT * sz){
304 bool exportData(__Buf & buf){
307 const size_t old = buf.size();
308 buf.resize(old + buf_.size());
309 __buf_copy(&buf[old], &buf_[0], buf_.size());
312 template<
typename CharT>
313 bool exportData(CharT * buf,
size_t * sz){
314 if(!buf || !sz || *sz < offset(cur_) || !exportData())
316 __buf_copy(buf, &buf_[0], buf_.size());
320 std::string toString()
const{
322 oss<<
"{begin_="<<begin_
323 <<
", buf_="<<dumpBufPart(&buf_[0], begin_ + cur_, begin_)
328 size_t offset(
size_t i)
const{
return begin_ + i;}
340 typedef typename __Buf::value_type __Char;
342 typedef __buf_ref_data<__Buf> __Ref;
343 typedef __buf_data<__Buf> __Myt;
345 explicit __buf_data(
size_t reserve)
349 size_t cur()
const{
return ref_.cur();}
350 __Char * data(
size_t i){
return ref_.data(i);}
351 bool ensure(
size_t len){
return ref_.ensure(len);}
352 bool seek(
size_t pos){
return ref_.seek(pos);}
353 void append(
const __Char * buf,
size_t sz){ref_.append(buf, sz);}
354 void append(__Char c){ref_.append(c);}
355 void insert(
size_t offset,
const __Char * buf,
size_t sz){ref_.insert(offset, buf, sz);}
356 bool exportData(__Buf & buf){
361 const size_t old = buf.size();
362 buf.resize(old + buf_.size());
363 __buf_copy(&buf[old], data(0), buf_.size());
367 template<
typename CharT>
368 bool exportData(CharT * buf,
size_t * sz){
369 if(!buf || !sz || *sz < cur() || !ref_.exportData())
371 __buf_copy(buf, data(0), buf_.size());
375 std::string toString()
const{
return ref_.toString();}
382 template<
typename Char>
387 typedef typename __Buf::value_type __Char;
389 typedef __buf_data<__Buf> __Myt;
391 explicit __buf_data(__Char * buf,
size_t sz)
395 size_t cur()
const{
return cur_;}
396 __Char * data(
size_t i){
return &buf_[i];}
397 const __Char * data(
size_t i)
const{
return &buf_[i];}
398 bool ensure(
size_t len){
return (cur_ + len <= buf_.size());}
399 bool seek(
size_t pos){
400 if(pos > buf_.size())
405 void append(
const __Char * buf,
size_t sz){
406 insert(cur_, buf, sz);
408 void append(__Char c){
409 assert(cur_ + 1 <= buf_.size());
412 void insert(
size_t off,
const __Char * buf,
size_t sz){
414 assert(sz && cur_ + sz <= buf_.size());
416 const size_t c = cur_ - off;
418 __buf_move(data(off + sz), data(off), c);
419 __buf_copy(data(off), buf, sz);
426 template<
typename SizeT>
427 bool exportData(SizeT * sz){
432 template<
typename CharT>
433 bool exportData(CharT * buf,
size_t * sz){
434 if(!buf || !sz || *sz < cur())
436 __buf_copy(buf, data(0), cur());
440 std::string toString()
const{
442 oss<<
"{capacity="<<buf_.capacity()
443 <<
", buf_="<<tools::DumpHex(data(0), cur_)
455 template<
typename LenT,
class T>
456 struct CManipulatorArrayPtr
461 CManipulatorArrayPtr(T * c,
size_t sz, LenT * p)
468 template<
typename LenT,
class T>
469 struct CManipulatorArrayCont
473 CManipulatorArrayCont(T & c, LenT max_size)
479 template<
typename LenT,
class Iter>
480 struct CManipulatorArrayRange
484 CManipulatorArrayRange(Iter first, Iter last, LenT * sz)
492 struct CManipulatorRawPtr
496 CManipulatorRawPtr(T * c,
size_t sz):c_(c),sz_(sz){}
500 struct CManipulatorRawCont
505 CManipulatorRawCont(T & c,
size_t sz,
size_t * sz2)
513 struct CManipulatorRawRange
517 CManipulatorRawRange(Iter first, Iter last,
size_t * sz)
525 struct CManipulatorValueByteOrder
530 CManipulatorValueByteOrder(T & c, __Fun f):c_(c),fun_(f){}
533 struct CManipulatorSeek
536 explicit CManipulatorSeek(
size_t p):pos_(p){}
539 struct CManipulatorSkip
542 explicit CManipulatorSkip(ssize_t off):off_(off){}
545 struct CManipulatorSkipFill
549 explicit CManipulatorSkipFill(ssize_t off,
int fill):off_(off),fill_(fill){}
553 struct CManipulatorSkipPtr
556 explicit CManipulatorSkipPtr(T * off):off_(off){}
560 struct CManipulatorSkipPtrFill
564 explicit CManipulatorSkipPtrFill(T * off,
int fill):off_(off),fill_(fill){}
568 struct CManipulatorOffsetValue
572 CManipulatorOffsetValue(
size_t p, T & c):c_(c),pos_(p){}
576 struct CManipulatorInsert
580 CManipulatorInsert(
size_t p,
const T & c):c_(c),pos_(p){}
584 struct CManipulatorProtobuf
589 CManipulatorProtobuf(__Msg & c,
size_t sz):c_(c),sz_(sz){}
593 class CManipulatorVarint
597 typedef typename __Traits::__Signed __Signed;
598 typedef typename __Traits::__Unsigned __Unsigned;
599 CManipulatorVarint(T & c):c_(c){}
600 __Unsigned toUnsigned()
const{
604 void fromUnsigned(__Unsigned v)
const{
609 static U trans(U v, U & t){
return (t = v);}
610 static __Unsigned trans(__Signed v, __Unsigned & t){
611 return (t = (v << 1) ^ (v >> (__Traits::kMaxBits - 1)));
613 static __Signed trans(__Unsigned v, __Signed & t){
614 return (t = (v >> 1) ^ (-(v & 1)));
619 struct CManipulatorStubPush
622 explicit CManipulatorStubPush(
size_t sz):sz_(sz){}
625 struct CManipulatorStubPop
628 CManipulatorStubPop(
bool align,
bool check):align_(align), check_(check){}
631 template<
class T,
class S>
632 struct CManipulatorEnd;
635 struct CManipulatorEnd<void, void>{};
637 template<
class SizeT>
638 struct CManipulatorEnd<SizeT *,
void>
641 explicit CManipulatorEnd(SizeT * s):sz_(s){}
644 template<
class CharT>
645 struct CManipulatorEnd<CharT *,
size_t *>
649 CManipulatorEnd(CharT * buf,
size_t * sz):buf_(buf),sz_(sz){}
653 struct CManipulatorEnd<BufT, void>
656 explicit CManipulatorEnd(BufT & buf):buf_(buf){}
659 #endif // DOX_UNDOCUMENT_FLAG bool littleEndian() const
Get byte order.
Definition: data_stream_impl.hh:128
Definition: to_string.hh:43
Provide interfaces similar to std::string for raw byte array.
Definition: char_buffer.hh:49
Definition: template.hh:23
void littleEndian(bool endian)
Set byte order.
Definition: data_stream_impl.hh:118
bool operator!() const
Test if current error status is NOT 0.
Definition: data_stream_impl.hh:73
void status(int code)
Set error status.
Definition: data_stream_impl.hh:53
static const bool kByteOrderDefault
Byte order of the underlying data buffer, default Net Byte Order.
Definition: data_stream_impl.hh:47
bool good() const
Test if current error status is 0, same as operator __SafeBool().
Definition: data_stream_impl.hh:104
void netByteOrder(bool nb)
Set byte order.
Definition: data_stream_impl.hh:111
NS_IMPL::CManipulatorEnd< void, void > end()
End operations for CInByteStream, COutByteStreamStrRef, COutByteStreamVecRef.
Definition: data_stream.hh:929
int status() const
Get error status.
Definition: data_stream_impl.hh:58
Base class of CInByteStream, COutByteStreamBasic.
Definition: data_stream_impl.hh:42