LibCppBinary
Modern cross-platform C++ binary parsing library
Loading...
Searching...
No Matches
Binary::RawField Class Reference

Represents a raw data field in a binary file. More...

#include <RawField.h>

Inheritance diagram for Binary::RawField:
Binary::DataField Binary::IntField< uint8_t, 1 > Binary::IntField< uint16_t, 2 > Binary::IntField< uint32_t, 3 > Binary::IntField< uint32_t, 4 > Binary::IntField< uint64_t, 8 > Binary::IntField< int8_t, 1 > Binary::IntField< int16_t, 2 > Binary::IntField< int32_t, 3 > Binary::IntField< int32_t, 4 > Binary::IntField< int64_t, 8 > Binary::BufferStream Binary::IntField< IntType, IntSize > Binary::StringField

Public Member Functions

 RawField (size_t size)
 Constructor; creates a new instance of Field.
 RawField (const RawField &other)
 Copy constructor; creates a deep copy of the field.
size_t Size () const override
 Gets the size of the data in the field.
char * RawData () override
 Provides access to the field's raw data via pointer.
const char * RawData () const override
 Provides access to the field's raw data via const pointer.
std::string ToString () const override
 Converts the field's data to a string representation.
std::string ToString (StringFormat format) const override
 Converts the field's data to a string representation.
RawFieldoperator= (const RawField &other)
 Assignment operator for RawField.
Public Member Functions inherited from Binary::DataField
virtual ~DataField ()=default
 Default destructor; properly destroys the instance.
virtual void CopyRawDataTo (DataField *other)
 Copies the raw data to another DataField.

Protected Member Functions

std::string FormatData (StringFormat format) const
 Format's the field's data as a string in a specific format.

Protected Attributes

size_t size
std::unique_ptr< char[]> rawData

Detailed Description

Represents a raw data field in a binary file.

Binary files can sometimes contain data fields that need to be read or written without respect to its interpretation. Use the RawField to read or write data without a specific interpretation from or to a Binary::Stream, such as a Binary::FileStream. More specialized field types should inherit from this class as it provides a lot of the basic functionality for managing the raw data.

Invariant
The size of the field is greater than 0.
Data is stored in char buffer equal to the size of the field.
Copying and assignment are deep copies of the field's data.

Constructor & Destructor Documentation

◆ RawField() [1/2]

Binary::RawField::RawField ( size_t size)

Constructor; creates a new instance of Field.

Parameters
sizeThe size of the field, in bytes.
Precondition
Size is > 0.

◆ RawField() [2/2]

Binary::RawField::RawField ( const RawField & other)

Copy constructor; creates a deep copy of the field.

Parameters
otherThe field to copy.

Member Function Documentation

◆ FormatData()

std::string Binary::RawField::FormatData ( StringFormat format) const
protected

Format's the field's data as a string in a specific format.

Parameters
formatThe format to use.
Returns
A formated string representation of the data.

◆ operator=()

RawField & Binary::RawField::operator= ( const RawField & other)

Assignment operator for RawField.

Copies the size and data from another RawField instance.

Parameters
otherThe RawField to copy from.
Returns
Reference to this RawField after assignment.

◆ RawData() [1/2]

const char * Binary::RawField::RawData ( ) const
inlineoverridevirtual

Provides access to the field's raw data via const pointer.

Provides read-only access to the raw data stored in the field. Note that this class should manage the lifecycle of the data, so do not attempt to deallocate the memory behind the pointer manually.

Returns
A raw pointer to the data.

Implements Binary::DataField.

◆ RawData() [2/2]

char * Binary::RawField::RawData ( )
inlineoverridevirtual

Provides access to the field's raw data via pointer.

Provides access to the raw data stored in the field. Note that this class should manage the lifecycle of the data, so do not attempt to deallocate the memory behind the pointer manually.

Returns
A raw pointer to the data.

Implements Binary::DataField.

◆ Size()

size_t Binary::RawField::Size ( ) const
inlineoverridevirtual

Gets the size of the data in the field.

Returns
The size of the data in the field, in bytes.

Implements Binary::DataField.

◆ ToString() [1/2]

std::string Binary::RawField::ToString ( ) const
overridevirtual

Converts the field's data to a string representation.

Returns
A string representation of the field's data.

Implements Binary::DataField.

Reimplemented in Binary::StringField.

◆ ToString() [2/2]

std::string Binary::RawField::ToString ( StringFormat format) const
overridevirtual

Converts the field's data to a string representation.

Parameters
formatThe format to use for the string conversion.
Returns
A string representation of the field's data.

Implements Binary::DataField.

Reimplemented in Binary::StringField.

Member Data Documentation

◆ rawData

std::unique_ptr<char[]> Binary::RawField::rawData
protected

◆ size

size_t Binary::RawField::size
protected

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