A class to hold a resizable block of raw data. More...
Public Member Functions | |
| MemoryBlock () noexcept | |
| Create an uninitialised block with 0 size. | |
| MemoryBlock (const size_t initialSize, bool initialiseToZero=false) | |
| Creates a memory block with a given initial size. | |
| MemoryBlock (const MemoryBlock &other) | |
| Creates a copy of another memory block. | |
| MemoryBlock (const void *dataToInitialiseFrom, size_t sizeInBytes) | |
| Creates a memory block using a copy of a block of data. | |
| ~MemoryBlock () noexcept | |
| Destructor. | |
| MemoryBlock & | operator= (const MemoryBlock &other) |
| Copies another memory block onto this one. | |
| bool | operator== (const MemoryBlock &other) const noexcept |
| Compares two memory blocks. | |
| bool | operator!= (const MemoryBlock &other) const noexcept |
| Compares two memory blocks. | |
| bool | matches (const void *data, size_t dataSize) const noexcept |
| Returns true if the data in this MemoryBlock matches the raw bytes passed-in. | |
| void * | getData () const noexcept |
| Returns a void pointer to the data. | |
| template<typename Type > | |
| char & | operator[] (const Type offset) const noexcept |
| Returns a byte from the memory block. | |
| size_t | getSize () const noexcept |
| Returns the block's current allocated size, in bytes. | |
| void | setSize (const size_t newSize, bool initialiseNewSpaceToZero=false) |
| Resizes the memory block. | |
| void | ensureSize (const size_t minimumSize, bool initialiseNewSpaceToZero=false) |
| Increases the block's size only if it's smaller than a given size. | |
| void | fillWith (uint8 valueToUse) noexcept |
| Fills the entire memory block with a repeated byte value. | |
| void | append (const void *data, size_t numBytes) |
| Adds another block of data to the end of this one. | |
| void | swapWith (MemoryBlock &other) noexcept |
| Exchanges the contents of this and another memory block. | |
| void | copyFrom (const void *srcData, int destinationOffset, size_t numBytes) noexcept |
| Copies data into this MemoryBlock from a memory address. | |
| void | copyTo (void *destData, int sourceOffset, size_t numBytes) const noexcept |
| Copies data from this MemoryBlock to a memory address. | |
| void | removeSection (size_t startByte, size_t numBytesToRemove) |
| Chops out a section of the block. | |
| String | toString () const |
| Attempts to parse the contents of the block as a zero-terminated UTF8 string. | |
| void | loadFromHexString (const String &sourceHexString) |
| Parses a string of hexadecimal numbers and writes this data into the memory block. | |
| void | setBitRange (size_t bitRangeStart, size_t numBits, int binaryNumberToApply) noexcept |
| Sets a number of bits in the memory block, treating it as a long binary sequence. | |
| int | getBitRange (size_t bitRangeStart, size_t numBitsToRead) const noexcept |
| Reads a number of bits from the memory block, treating it as one long binary sequence. | |
| String | toBase64Encoding () const |
| Returns a string of characters that represent the binary contents of this block. | |
| bool | fromBase64Encoding (const String &encodedString) |
| Takes a string of encoded characters and turns it into binary data. | |
A class to hold a resizable block of raw data.
| MemoryBlock::MemoryBlock | ( | ) |
Create an uninitialised block with 0 size.
| MemoryBlock::MemoryBlock | ( | const size_t | initialSize, |
| bool | initialiseToZero = false |
||
| ) |
Creates a memory block with a given initial size.
| initialSize | the size of block to create |
| initialiseToZero | whether to clear the memory or just leave it uninitialised |
| MemoryBlock::MemoryBlock | ( | const MemoryBlock & | other ) |
Creates a copy of another memory block.
| MemoryBlock::MemoryBlock | ( | const void * | dataToInitialiseFrom, |
| size_t | sizeInBytes | ||
| ) |
Creates a memory block using a copy of a block of data.
| dataToInitialiseFrom | some data to copy into this block |
| sizeInBytes | how much space to use |
| MemoryBlock::~MemoryBlock | ( | ) |
Destructor.
| MemoryBlock& MemoryBlock::operator= | ( | const MemoryBlock & | other ) |
Copies another memory block onto this one.
This block will be resized and copied to exactly match the other one.
| bool MemoryBlock::operator== | ( | const MemoryBlock & | other ) | const |
Compares two memory blocks.
| bool MemoryBlock::operator!= | ( | const MemoryBlock & | other ) | const |
Compares two memory blocks.
| bool MemoryBlock::matches | ( | const void * | data, |
| size_t | dataSize | ||
| ) | const |
Returns true if the data in this MemoryBlock matches the raw bytes passed-in.
| void* MemoryBlock::getData | ( | ) | const |
Returns a void pointer to the data.
Note that the pointer returned will probably become invalid when the block is resized.
| char& MemoryBlock::operator[] | ( | const Type | offset ) | const |
Returns a byte from the memory block.
This returns a reference, so you can also use it to set a byte.
| size_t MemoryBlock::getSize | ( | ) | const |
Returns the block's current allocated size, in bytes.
| void MemoryBlock::setSize | ( | const size_t | newSize, |
| bool | initialiseNewSpaceToZero = false |
||
| ) |
Resizes the memory block.
This will try to keep as much of the block's current content as it can, and can optionally be made to clear any new space that gets allocated at the end of the block.
| newSize | the new desired size for the block |
| initialiseNewSpaceToZero | if the block gets enlarged, this determines whether to clear the new section or just leave it uninitialised |
| void MemoryBlock::ensureSize | ( | const size_t | minimumSize, |
| bool | initialiseNewSpaceToZero = false |
||
| ) |
Increases the block's size only if it's smaller than a given size.
| minimumSize | if the block is already bigger than this size, no action will be taken; otherwise it will be increased to this size |
| initialiseNewSpaceToZero | if the block gets enlarged, this determines whether to clear the new section or just leave it uninitialised |
| void MemoryBlock::fillWith | ( | uint8 | valueToUse ) |
Fills the entire memory block with a repeated byte value.
This is handy for clearing a block of memory to zero.
| void MemoryBlock::append | ( | const void * | data, |
| size_t | numBytes | ||
| ) |
Adds another block of data to the end of this one.
This block's size will be increased accordingly.
| void MemoryBlock::swapWith | ( | MemoryBlock & | other ) |
Exchanges the contents of this and another memory block.
No actual copying is required for this, so it's very fast.
| void MemoryBlock::copyFrom | ( | const void * | srcData, |
| int | destinationOffset, | ||
| size_t | numBytes | ||
| ) |
Copies data into this MemoryBlock from a memory address.
| srcData | the memory location of the data to copy into this block |
| destinationOffset | the offset in this block at which the data being copied should begin |
| numBytes | how much to copy in (if this goes beyond the size of the memory block, it will be clipped so not to do anything nasty) |
| void MemoryBlock::copyTo | ( | void * | destData, |
| int | sourceOffset, | ||
| size_t | numBytes | ||
| ) | const |
Copies data from this MemoryBlock to a memory address.
| destData | the memory location to write to |
| sourceOffset | the offset within this block from which the copied data will be read |
| numBytes | how much to copy (if this extends beyond the limits of the memory block, zeros will be used for that portion of the data) |
| void MemoryBlock::removeSection | ( | size_t | startByte, |
| size_t | numBytesToRemove | ||
| ) |
Chops out a section of the block.
This will remove a section of the memory block and close the gap around it, shifting any subsequent data downwards and reducing the size of the block.
If the range specified goes beyond the size of the block, it will be clipped.
| String MemoryBlock::toString | ( | ) | const |
Attempts to parse the contents of the block as a zero-terminated UTF8 string.
| void MemoryBlock::loadFromHexString | ( | const String & | sourceHexString ) |
Parses a string of hexadecimal numbers and writes this data into the memory block.
The block will be resized to the number of valid bytes read from the string. Non-hex characters in the string will be ignored.
| void MemoryBlock::setBitRange | ( | size_t | bitRangeStart, |
| size_t | numBits, | ||
| int | binaryNumberToApply | ||
| ) |
Sets a number of bits in the memory block, treating it as a long binary sequence.
| int MemoryBlock::getBitRange | ( | size_t | bitRangeStart, |
| size_t | numBitsToRead | ||
| ) | const |
Reads a number of bits from the memory block, treating it as one long binary sequence.
| String MemoryBlock::toBase64Encoding | ( | ) | const |
Returns a string of characters that represent the binary contents of this block.
Uses a 64-bit encoding system to allow binary data to be turned into a string of simple non-extended characters, e.g. for storage in XML.
| bool MemoryBlock::fromBase64Encoding | ( | const String & | encodedString ) |
Takes a string of encoded characters and turns it into binary data.
The string passed in must have been created by to64BitEncoding(), and this block will be resized to recreate the original data block.