Marine Library  1.0
C++ library for Linux Networking Development
Public Member Functions | List of all members
CInByteStream Class Reference

Data unpacking interfaces. More...

#include <data_stream.hh>

Inheritance diagram for CInByteStream:
Inheritance graph
[legend]
Collaboration diagram for CInByteStream:
Collaboration graph
[legend]

Public Member Functions

 CInByteStream ()
 Construct an empty object. More...
 
 CInByteStream (const char *buf, size_t sz, bool net=kByteOrderDefault)
 Construct from a byte buffer. More...
 
 CInByteStream (const unsigned char *buf, size_t sz, bool net=kByteOrderDefault)
 Construct from a byte buffer. More...
 
 CInByteStream (const signed char *buf, size_t sz, bool net=kByteOrderDefault)
 Construct from a byte buffer. More...
 
 CInByteStream (const std::string &buf, bool net=kByteOrderDefault)
 Construct from a byte buffer. More...
 
void setSource (const char *buf, size_t sz, bool net=kByteOrderDefault)
 Initialize with a byte buffer. More...
 
void setSource (const unsigned char *buf, size_t sz, bool net=kByteOrderDefault)
 Initialize with a byte buffer. More...
 
void setSource (const signed char *buf, size_t sz, bool net=kByteOrderDefault)
 Initialize with a byte buffer. More...
 
void setSource (const std::string &buf, bool net=kByteOrderDefault)
 Initialize with a byte buffer. More...
 
__Mytbad (int code)
 Set status. More...
 
ssize_t seek (size_t pos)
 Set current pointer. More...
 
ssize_t skip (ssize_t off)
 Skip some data. More...
 
size_t cur () const
 Get current pointer. More...
 
const char * data () const
 Get a pointer to left data. More...
 
size_t left () const
 Get byte size of left data. More...
 
std::string toString () const
 Get a readable description of this object. More...
 
Unpack primitive types

These functions read sizeof c bytes from underlying data, and store result in c, with appropriate byte order transformation.

Parameters
[out]cAn integer to receive the result
Returns
Reference to self
__Mytoperator>> (char &c)
 
__Mytoperator>> (signed char &c)
 
__Mytoperator>> (unsigned char &c)
 
__Mytoperator>> (short &c)
 
__Mytoperator>> (unsigned short &c)
 
__Mytoperator>> (int &c)
 
__Mytoperator>> (unsigned int &c)
 
__Mytoperator>> (long &c)
 
__Mytoperator>> (unsigned long &c)
 
__Mytoperator>> (long long &c)
 
__Mytoperator>> (unsigned long long &c)
 
__Mytoperator>> (wchar_t &c)
 
Unpack compound types
__Mytoperator>> (std::string &c)
 Unpack a string. More...
 
__Mytoperator>> (std::wstring &c)
 Unpack a wide string. More...
 
template<class T , size_t N>
__Mytoperator>> (T(&c)[N])
 Unpack an array. More...
 
Manipulators

These functions operates on different manipulators to achieve special purpose.

Please see the corresponding manipulator generators in Manip for more information.

Parameters
mManipulator object
Returns
Reference of self
__Mytoperator>> (void(*m)(__MyBase &))
 
template<class T >
__Mytoperator>> (const NS_IMPL::CManipulatorRawPtr< T > &m)
 
template<class T >
__Mytoperator>> (const NS_IMPL::CManipulatorRawCont< T > &m)
 
template<class ForwardIter >
__Mytoperator>> (const NS_IMPL::CManipulatorRawRange< ForwardIter > &m)
 
template<typename LenT , class T >
__Mytoperator>> (const NS_IMPL::CManipulatorArrayPtr< LenT, T > &m)
 
template<typename LenT , class T >
__Mytoperator>> (const NS_IMPL::CManipulatorArrayCont< LenT, T > &m)
 
template<class T >
__Mytoperator>> (const NS_IMPL::CManipulatorValueByteOrder< T > &m)
 
__Mytoperator>> (const NS_IMPL::CManipulatorSeek &m)
 
__Mytoperator>> (const NS_IMPL::CManipulatorSkip &m)
 
template<typename T >
__Mytoperator>> (const NS_IMPL::CManipulatorSkipPtr< T > &m)
 
template<class T >
__Mytoperator>> (const NS_IMPL::CManipulatorOffsetValue< T > &m)
 
template<class T >
__Mytoperator>> (const NS_IMPL::CManipulatorProtobuf< T > &m)
 
template<typename T >
__Mytoperator>> (const NS_IMPL::CManipulatorVarint< T > &m)
 
__Mytoperator>> (const NS_IMPL::CManipulatorStubPush &m)
 
__Mytoperator>> (const NS_IMPL::CManipulatorStubPop &m)
 
template<class T , class S >
__Mytoperator>> (const NS_IMPL::CManipulatorEnd< T, S > &m)
 
__Mytoperator>> (NS_IMPL::CManipulatorEnd< void, void >(*m)())
 

Detailed Description

Data unpacking interfaces.

CInByteStream is super convenient for unpacking data. It receives a byte buffer containing serialized data, and tries to decode whatever you want from it.
CInByteStream manages data endianness automatically, so you don't need to call hton/ntoh family APIs manually.It also performs sufficient boundary checks so you won't end up with memory access violations.
What makes CInByteStream really powerful is that it integrates a number of manipulators, which makes unpacking complex structures a joy of life.
Sample code:

CInByteStream in(buf, sz); // initialize from a byte buffer
// Following are the data we want to unpack
uint32_t version; // a 32-bit integer
string name; // a string
vector<uint64_t> orders; // an array of 64-bit integers
// Then we extract all data in ONE line!
in >> version >> name >> Manip::array(orders);

Constructor & Destructor Documentation

CInByteStream::CInByteStream ( )
inline

Construct an empty object.

You need to call setSource before you can use this object to unpack data.

See also
setSource
CInByteStream::CInByteStream ( const char *  buf,
size_t  sz,
bool  net = kByteOrderDefault 
)
inline

Construct from a byte buffer.

Parameters
bufPointer to a byte buffer containing data to be unpacked
szByte size of buf, if present
netByte order of the data in buf:
  • true: Net byte order, this is the default;
  • false: Host byte order;
CInByteStream::CInByteStream ( const unsigned char *  buf,
size_t  sz,
bool  net = kByteOrderDefault 
)
inline

Construct from a byte buffer.

Parameters
bufPointer to a byte buffer containing data to be unpacked
szByte size of buf, if present
netByte order of the data in buf:
  • true: Net byte order, this is the default;
  • false: Host byte order;
CInByteStream::CInByteStream ( const signed char *  buf,
size_t  sz,
bool  net = kByteOrderDefault 
)
inline

Construct from a byte buffer.

Parameters
bufPointer to a byte buffer containing data to be unpacked
szByte size of buf
netByte order of the data in buf:
  • true: Net byte order, this is the default;
  • false: Host byte order;
CInByteStream::CInByteStream ( const std::string &  buf,
bool  net = kByteOrderDefault 
)
inline

Construct from a byte buffer.

Parameters
bufA byte buffer containing data to be unpacked
netByte order of the data in buf:
  • true: Net byte order, this is the default;
  • false: Host byte order;

Member Function Documentation

__Myt& CInByteStream::bad ( int  code)
inline

Set status.

If status is not 0, all unpacking operations will fail.

Parameters
code
  • 0: Reset status.
  • Otherwise: A number indicating error status.
Returns
Reference of self
size_t CInByteStream::cur ( ) const
inline

Get current pointer.

Current pointer is the index of underlying data where next unpacking operation will take place. Every unpacking operation increases current pointer by the amount of data it reads.

Returns
Current pointer
const char* CInByteStream::data ( ) const
inline

Get a pointer to left data.

This function returns a pointer to the first unread byte, which is also indicated by current pointer.

Returns
A pointer to the unread data
See also
cur
size_t CInByteStream::left ( ) const
inline

Get byte size of left data.

If there are stubs, this function returns data size before current effective stub.

Returns
Bytes size of left data
See also
Manip::stub Mapip::stub_pop
__Myt& CInByteStream::operator>> ( std::string &  c)
inline

Unpack a string.

This function first reads a uint16_t as the length of the result string, then reads the whole string.

Parameters
[out]cA string to receive the result
Returns
Reference to self
__Myt& CInByteStream::operator>> ( std::wstring &  c)
inline

Unpack a wide string.

This function first reads a uint16_t as the length of the result wide string, then reads the whole wide string.

Parameters
[out]cA wide string to receive the result
Returns
Reference to self
template<class T , size_t N>
__Myt& CInByteStream::operator>> ( T(&)  c[N])
inline

Unpack an array.

This function reads N elements directly (without leading length field), and stores them in c.
T must be a type supported by operator >> of CInByteStream.

Template Parameters
TType of element in array
NNumber of elements in array
Parameters
[out]cAn array to receive the result
Returns
Reference to self
ssize_t CInByteStream::seek ( size_t  pos)
inline

Set current pointer.

If this function makes current pointer smaller, old data will be read again; And if current pointer becomes larger, some data will be skipped.

Parameters
posNew current pointer
Returns
  • Negative: Operation failed, status will be set to a nonzero value;
  • Otherwise: New current pointer;
void CInByteStream::setSource ( const char *  buf,
size_t  sz,
bool  net = kByteOrderDefault 
)
inline

Initialize with a byte buffer.

This function will also reset current pointer and status.

Parameters
bufPointer to a byte buffer containing data to be unpacked
szByte size of buf, if present
netByte order of the data in buf:
  • true: Net byte order, this is the default;
  • false: Host byte order;
void CInByteStream::setSource ( const unsigned char *  buf,
size_t  sz,
bool  net = kByteOrderDefault 
)
inline

Initialize with a byte buffer.

This function will also reset current pointer and status.

Parameters
bufPointer to a byte buffer containing data to be unpacked
szByte size of buf, if present
netByte order of the data in buf:
  • true: Net byte order, this is the default;
  • false: Host byte order;
void CInByteStream::setSource ( const signed char *  buf,
size_t  sz,
bool  net = kByteOrderDefault 
)
inline

Initialize with a byte buffer.

This function will also reset current pointer (current reading position) and status.

Parameters
bufPointer to a byte buffer containing data to be unpacked
szByte size of buf, if present
netByte order of the data in buf:
  • true: Net byte order, this is the default;
  • false: Host byte order;
void CInByteStream::setSource ( const std::string &  buf,
bool  net = kByteOrderDefault 
)
inline

Initialize with a byte buffer.

Parameters
bufA byte buffer containing data to be unpacked
netByte order of the data in buf:
  • true: Net byte order, this is the default;
  • false: Host byte order;
ssize_t CInByteStream::skip ( ssize_t  off)
inline

Skip some data.

This function is equivalent to seek(cur() + off).

Parameters
offBytes size of data to skip.
Returns
  • Negative: Operation failed, status will be set to a nonzero value;
  • Otherwise: New current pointer;
Note
You can also give a negative off to jump back to old data.
std::string CInByteStream::toString ( ) const
inline

Get a readable description of this object.

Returns
A readable description of this object

The documentation for this class was generated from the following file: