Ocean
Ocean::IO::InputBitstream Class Reference

This class implements an input bitstream. More...

Inheritance diagram for Ocean::IO::InputBitstream:

Public Member Functions

 InputBitstream (std::istream &stream)
 Creates a new bitstream object. More...
 
template<typename T >
bool read (T &value)
 Reads a value from the bitstream and moves the internal position inside the bitstream accordingly. More...
 
template<typename T >
readDefault (const T &defaultValue)
 Reads a value from the bitstream and moves the internal position inside the bitstream accordingly. More...
 
bool read (void *data, const size_t size)
 Reads a defined memory block from the stream into a given buffer and moves the internal position inside the bitstream accordingly. More...
 
template<typename T >
bool look (T &value)
 Reads a value from the bitstream but does not move the internal position inside the bitstream. More...
 
unsigned long long position () const
 Returns the current position inside the bitstream, in bytes counting from the beginning of the stream. More...
 
unsigned long long size () const
 Returns the current size of the bitstream, in bytes. More...
 
bool setPosition (const unsigned long long position)
 Sets the current position inside the bitstream explicitly. More...
 
 operator bool () const
 Returns whether this bitstream object is valid and can be used. More...
 

Protected Attributes

std::istream & inputStream
 The internal input stream object that this object encapsulates. More...
 

Detailed Description

This class implements an input bitstream.

The implementation of the input bitstream is not thread-safe, thus ensure that the internal input stream object is not used within several threads concurrently.
The following data types are supported:

Data type:          Number of bytes:      Description:
bool                1
char                1
wchar_t             4                     Depending on the platform wchar_t has 1, 2 or 4 bytes thus this stream object stores 4 bytes per wchar_t object (see also std::wstring)
unsigned char       1
short               2
unsigned short      2
int                 4
unsigned int        4
float               4
double              8
long long           8
unsigned long long  8
std::string         4 + 1 * size          Four bytes for the size of the string and 1 byte for each character
std::wstring        4 + 4 * size          Four bytes for the size of the string and 4 bytes for each character

The following data types are not supported as they have individual size on individual platforms and thus must not be used:

Data type:          Description:
size_t              size_t has 4 bytes on 32 bit platforms and 8 bytes on 64 bit platforms

Constructor & Destructor Documentation

◆ InputBitstream()

Ocean::IO::InputBitstream::InputBitstream ( std::istream &  stream)

Creates a new bitstream object.

Parameters
streamThe input stream object that is encapsulated by this object

Member Function Documentation

◆ look()

template<typename T >
bool Ocean::IO::InputBitstream::look ( T &  value)

Reads a value from the bitstream but does not move the internal position inside the bitstream.

Beware: It's recommended to provide the template argument explicitly to avoid type ambiguities.

Parameters
valueThe resulting value that is looked-up
Returns
True, if the value could be looked-up successfully
Template Parameters
TThe data type that is read, ensure that only supported data types are applied

◆ operator bool()

Ocean::IO::InputBitstream::operator bool ( ) const
inlineexplicit

Returns whether this bitstream object is valid and can be used.

Returns
True, if so

◆ position()

unsigned long long Ocean::IO::InputBitstream::position ( ) const

Returns the current position inside the bitstream, in bytes counting from the beginning of the stream.

Returns
The current stream position in bytes, -1 if the current position cannot be determined

◆ read() [1/2]

template<typename T >
bool Ocean::IO::InputBitstream::read ( T &  value)

Reads a value from the bitstream and moves the internal position inside the bitstream accordingly.

If the read process fails, the new position of the bitstream may be arbitrary.
Beware: It's recommended to provide the template argument explicitly to avoid type ambiguities.

Parameters
valueThe resulting value that is read
Returns
True, if the value could be read successfully
Template Parameters
TThe data type that is read, ensure that only supported data types are applied
See also
readDefault().

◆ read() [2/2]

bool Ocean::IO::InputBitstream::read ( void *  data,
const size_t  size 
)

Reads a defined memory block from the stream into a given buffer and moves the internal position inside the bitstream accordingly.

If the read process fails, the new position of the bitstream may be arbitrary.

Parameters
dataThe buffer that will receive the memory block, ensure that this buffer is large enough, may be nullptr if size is 0
sizeThe number of bytes that will be read into the memory block, with range [0, infinity)
Returns
True, if succeeded

◆ readDefault()

template<typename T >
T Ocean::IO::InputBitstream::readDefault ( const T &  defaultValue)

Reads a value from the bitstream and moves the internal position inside the bitstream accordingly.

If the read process fails, the new position of the bitstream may be arbitrary.
This function does not provide a success value but returns a given default value if the read process fails.
Beware: It's recommended to provide the template argument explicitly to avoid type ambiguities.

Parameters
defaultValueThe default value that is returned if the read process fails
Returns
The resulting value that has been read
Template Parameters
TThe data type that is read, ensure that only supported data types are applied
See also
read().

◆ setPosition()

bool Ocean::IO::InputBitstream::setPosition ( const unsigned long long  position)

Sets the current position inside the bitstream explicitly.

Parameters
positionThe new position inside the bitstream, in bytes counting from the beginning of the stream; with range [0, size())
Returns
True, if succeeded

◆ size()

unsigned long long Ocean::IO::InputBitstream::size ( ) const

Returns the current size of the bitstream, in bytes.

Returns
The current stream size in bytes, -1 if the current size cannot be determined

Field Documentation

◆ inputStream

std::istream& Ocean::IO::InputBitstream::inputStream
protected

The internal input stream object that this object encapsulates.


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