Thea
Public Member Functions | Public Attributes | Static Public Attributes | List of all members
Codec::BlockHeader Struct Reference

A header preceding a data block serialized using a codec. More...

#include <Codec.hpp>

Public Member Functions

 BlockHeader (MagicString const &magic_=zeroMagic(), uint64 data_size_=0)
 Construct with a magic string and a data block size. More...
 
 BlockHeader (std::string const &magic_, uint64 data_size_=0)
 Construct with a magic string specified as a std::string, and a data block size. More...
 
 BlockHeader (BinaryInputStream &in)
 Construct by calling read() on an input stream. More...
 
void calcAndWrite (BinaryOutputStream &out)
 Write the header to an output stream, after calculating the data block size based on where the header was supposed to have been written (calculated using markAndSkip()) vs the current stream position, assumed to be at the end of the data block. More...
 
int64 markAndSkip (BinaryOutputStream &out)
 Internally save the current location in an output stream, while reserving room to write a block header at that position. More...
 
void read (BinaryInputStream &in)
 Read the header from an input stream. More...
 
void write (BinaryOutputStream &out) const
 Write the header to an output stream. More...
 

Public Attributes

uint64 custom
 Additional custom data that can be optionally stored in the header. More...
 
uint64 data_size
 The size of the data block in bytes. More...
 
MagicString magic
 A magic string identifying the codec used to serialize the data. More...
 

Static Public Attributes

static intx const SERIALIZED_LENGTH
 The length in bytes of a serialized block header. More...
 

Detailed Description

A header preceding a data block serialized using a codec.

The header contains the size of the data block, a magic string identifying the serialization codec, and an optional 64-bit field that can be used for additional information.

Definition at line 48 of file Codec.hpp.

Constructor & Destructor Documentation

BlockHeader ( MagicString const &  magic_ = zeroMagic(),
uint64  data_size_ = 0 
)

Construct with a magic string and a data block size.

Definition at line 62 of file Codec.hpp.

BlockHeader ( std::string const &  magic_,
uint64  data_size_ = 0 
)

Construct with a magic string specified as a std::string, and a data block size.

Definition at line 66 of file Codec.hpp.

Construct by calling read() on an input stream.

Definition at line 26 of file Codec.cpp.

Member Function Documentation

void calcAndWrite ( BinaryOutputStream out)

Write the header to an output stream, after calculating the data block size based on where the header was supposed to have been written (calculated using markAndSkip()) vs the current stream position, assumed to be at the end of the data block.

After writing the header, the next write position is moved back to the end of the data block. This is guaranteed to write exactly Codec::BlockHeader::SERIALIZED_LENGTH bytes starting at header_pos.

header.markAndSkip(out);
// write data block
header.calcAndWrite(out);
Note
This function updates data_size.
Warning
This function seeks backwards in the stream, which may fail if a very large file is being written. This is because of the current implementation of BinaryOutputStream::setPosition(), which should be fixed at some point.
See also
markAndSkip()

Definition at line 61 of file Codec.cpp.

int64 markAndSkip ( BinaryOutputStream out)

Internally save the current location in an output stream, while reserving room to write a block header at that position.

This function should be called before writing a data block, and the return value passed to calcAndWrite() after the data block is written.

header.markAndSkip(out);
// write data block
header.calcAndWrite(out);
Returns
The saved location.
See also
calcAndWrite()

Definition at line 53 of file Codec.cpp.

void read ( BinaryInputStream in)

Read the header from an input stream.

This is guaranteed to read exactly SERIALIZED_LENGTH bytes.

Definition at line 33 of file Codec.cpp.

void write ( BinaryOutputStream out) const

Write the header to an output stream.

This is guaranteed to write exactly SERIALIZED_LENGTH bytes.

Definition at line 42 of file Codec.cpp.

Member Data Documentation

uint64 custom

Additional custom data that can be optionally stored in the header.

Definition at line 59 of file Codec.hpp.

uint64 data_size

The size of the data block in bytes.

Definition at line 58 of file Codec.hpp.

MagicString magic

A magic string identifying the codec used to serialize the data.

Definition at line 57 of file Codec.hpp.

intx const SERIALIZED_LENGTH
static
Initial value:
+ 8
+ 8
+ 8

The length in bytes of a serialized block header.

Definition at line 52 of file Codec.hpp.


The documentation for this struct was generated from the following files: