LibCppBinary
Modern cross-platform C++ binary parsing library
Loading...
Searching...
No Matches
Binary::IntField< IntType, IntSize > Class Template Reference

Represents an integer field in a binary file. More...

#include <IntField.h>

Inheritance diagram for Binary::IntField< IntType, IntSize >:
Binary::RawField Binary::DataField

Public Member Functions

 IntField ()
 Default constructor; creates a new instance of IntField.
 IntField (IntType value)
 Constructor; creates a new instance of IntField.
 IntField (FieldEndianness endianness)
 Constructor; creates a new instance of IntField.
 IntField (IntType value, FieldEndianness endianness)
 Constructor; creates a new instance of IntField.
IntType Value () const
 Gets the value of the field.
IntType MinValue () const
 Gets the minimum value this field can hold.
IntType MaxValue () const
 Gets the maximum value this field can hold.
FieldEndianness Endianness () const
 Gets the endianness of the field.
void SetValue (IntType value)
 Sets the value of the field.
void SetEndianness (FieldEndianness e)
 Sets the endianness of the field.
void SetToDefaultEndianness ()
 Sets the field to use the default endianness.
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.
IntField< IntType, IntSize > & operator= (const IntField< IntType, IntSize > &other)
 Assignment operator for IntField.
Public Member Functions inherited from Binary::RawField
 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.

Additional Inherited Members

Protected Member Functions inherited from Binary::RawField
std::string FormatData (StringFormat format) const
 Format's the field's data as a string in a specific format.
Protected Attributes inherited from Binary::RawField
size_t size
std::unique_ptr< char[]> rawData

Detailed Description

template<typename IntType, size_t IntSize>
class Binary::IntField< IntType, IntSize >

Represents an integer field in a binary file.

Binary files can sometimes contain integer fields of varying sizes, signed or unsigned, with little endian or big endian byte order. This class template allows for a number of different variations of the int fields. Use the specializations of this template (UInt8Field, Int8Field, UInt16Field, Int16Field, UInt24Field, Int24Field, UInt32Field, Int32Field, UInt64Field, Int64Field) to read or write these types of fields from and to a Binary::Stream, such as a Binary::StandardFileStream.

Invariant
Initial value is initialized either to 0 or a specified value.
Size of the underlying data is always equal to IntSize.
The int value is always stored in the current endianness.
The int value is between MinValue() and MaxValue() inclusive.

Constructor & Destructor Documentation

◆ IntField() [1/4]

template<typename IntType, size_t IntSize>
Binary::IntField< IntType, IntSize >::IntField ( )
inline

Default constructor; creates a new instance of IntField.

Postcondition
The field is initialized to 0.

◆ IntField() [2/4]

template<typename IntType, size_t IntSize>
Binary::IntField< IntType, IntSize >::IntField ( IntType value)
inline

Constructor; creates a new instance of IntField.

Parameters
valueThe value to initialize the field to.
Precondition
Value must be between MinValue() and MaxValue(), inclusive.
Postcondition
The field is initialized to the specified value.
Exceptions
std::out_of_rangeif value is outside of the valid range.

◆ IntField() [3/4]

template<typename IntType, size_t IntSize>
Binary::IntField< IntType, IntSize >::IntField ( FieldEndianness endianness)
inline

Constructor; creates a new instance of IntField.

Parameters
endiannessThe endianness the field should use.

◆ IntField() [4/4]

template<typename IntType, size_t IntSize>
Binary::IntField< IntType, IntSize >::IntField ( IntType value,
FieldEndianness endianness )
inline

Constructor; creates a new instance of IntField.

Parameters
valueThe value to initialize the field to.
endiannessThe endianness the field should use.
Precondition
Value must be between MinValue() and MaxValue(), inclusive.
Postcondition
The field is initialized to the specified value & endianness.
Exceptions
std::out_of_rangeif value is outside of the valid range.

Member Function Documentation

◆ Endianness()

template<typename IntType, size_t IntSize>
FieldEndianness Binary::IntField< IntType, IntSize >::Endianness ( ) const
inline

Gets the endianness of the field.

Returns
The endianness of the field.

◆ MaxValue()

template<typename IntType, size_t IntSize>
IntType Binary::IntField< IntType, IntSize >::MaxValue ( ) const
inline

Gets the maximum value this field can hold.

Returns
The maximum value the field can hold.

◆ MinValue()

template<typename IntType, size_t IntSize>
IntType Binary::IntField< IntType, IntSize >::MinValue ( ) const
inline

Gets the minimum value this field can hold.

Returns
The minimum value the field can hold.

◆ operator=()

template<typename IntType, size_t IntSize>
IntField< IntType, IntSize > & Binary::IntField< IntType, IntSize >::operator= ( const IntField< IntType, IntSize > & other)
inline

Assignment operator for IntField.

Copies the value and endianness from another IntField of the same type and size. Performs a self-assignment check. After assignment, this field will have the same value and endianness as the source, but remains a distinct object.

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

◆ SetEndianness()

template<typename IntType, size_t IntSize>
void Binary::IntField< IntType, IntSize >::SetEndianness ( FieldEndianness e)
inline

Sets the endianness of the field.

Parameters
eThe endianness to set the field to.
Postcondition
The field raw data bytes are swapped to match new endianness.

◆ SetToDefaultEndianness()

template<typename IntType, size_t IntSize>
void Binary::IntField< IntType, IntSize >::SetToDefaultEndianness ( )
inline

Sets the field to use the default endianness.

Postcondition
The field raw data bytes are swapped to match new endianness.

◆ SetValue()

template<typename IntType, size_t IntSize>
void Binary::IntField< IntType, IntSize >::SetValue ( IntType value)
inline

Sets the value of the field.

Parameters
valueThe value to set.
Precondition
Value must be between MinValue() and MaxValue(), inclusive.
Postcondition
The field's value is set to the specified value.
Exceptions
std::out_of_rangeif value is outside of the valid range.

◆ ToString() [1/2]

template<typename IntType, size_t IntSize>
std::string Binary::IntField< IntType, IntSize >::ToString ( ) const
inlineoverridevirtual

Converts the field's data to a string representation.

Returns
A string representation of the field's data.

Implements Binary::DataField.

◆ ToString() [2/2]

template<typename IntType, size_t IntSize>
std::string Binary::IntField< IntType, IntSize >::ToString ( StringFormat format) const
inlineoverridevirtual

Converts the field's data to a string representation.

Returns
A string representation of the field's data.

Implements Binary::DataField.

◆ Value()

template<typename IntType, size_t IntSize>
IntType Binary::IntField< IntType, IntSize >::Value ( ) const
inline

Gets the value of the field.

Returns
The value of the field.

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