JUCE  v5.4.1-191-g0ab5e696f
JUCE API
Looking for a senior C++ dev?
I'm looking for work. Hire me!
juce::MemoryBlock Class Reference

A class to hold a resizable block of raw data. More...

#include <juce_MemoryBlock.h>

Collaboration diagram for juce::MemoryBlock:

Public Member Functions

 MemoryBlock () noexcept
 Create an uninitialised block with 0 size. More...
 
 MemoryBlock (const size_t initialSize, bool initialiseToZero=false)
 Creates a memory block with a given initial size. More...
 
 MemoryBlock (const MemoryBlock &)
 Creates a copy of another memory block. More...
 
 MemoryBlock (const void *dataToInitialiseFrom, size_t sizeInBytes)
 Creates a memory block using a copy of a block of data. More...
 
 MemoryBlock (MemoryBlock &&) noexcept
 Move constructor. More...
 
 ~MemoryBlock () noexcept
 Destructor. More...
 
void append (const void *data, size_t numBytes)
 Adds another block of data to the end of this one. More...
 
char * begin () const noexcept
 Returns an iterator for the data. More...
 
void copyFrom (const void *srcData, int destinationOffset, size_t numBytes) noexcept
 Copies data into this MemoryBlock from a memory address. More...
 
void copyTo (void *destData, int sourceOffset, size_t numBytes) const noexcept
 Copies data from this MemoryBlock to a memory address. More...
 
char * end () const noexcept
 Returns an end-iterator for the data. More...
 
void ensureSize (const size_t minimumSize, bool initialiseNewSpaceToZero=false)
 Increases the block's size only if it's smaller than a given size. More...
 
void fillWith (uint8 valueToUse) noexcept
 Fills the entire memory block with a repeated byte value. More...
 
bool fromBase64Encoding (StringRef encodedString)
 Takes a string created by MemoryBlock::toBase64Encoding() and extracts the original data. More...
 
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. More...
 
void * getData () const noexcept
 Returns a void pointer to the data. More...
 
size_t getSize () const noexcept
 Returns the block's current allocated size, in bytes. More...
 
void insert (const void *dataToInsert, size_t numBytesToInsert, size_t insertPosition)
 Inserts some data into the block. More...
 
void loadFromHexString (StringRef sourceHexString)
 Parses a string of hexadecimal numbers and writes this data into the memory block. More...
 
bool matches (const void *data, size_t dataSize) const noexcept
 Returns true if the data in this MemoryBlock matches the raw bytes passed-in. More...
 
bool operator!= (const MemoryBlock &other) const noexcept
 Compares two memory blocks. More...
 
MemoryBlockoperator= (const MemoryBlock &)
 Copies another memory block onto this one. More...
 
MemoryBlockoperator= (MemoryBlock &&) noexcept
 Move assignment operator. More...
 
bool operator== (const MemoryBlock &other) const noexcept
 Compares two memory blocks. More...
 
template<typename Type >
char & operator[] (const Type offset) const noexcept
 Returns a byte from the memory block. More...
 
void removeSection (size_t startByte, size_t numBytesToRemove)
 Chops out a section of the block. More...
 
void replaceWith (const void *data, size_t numBytes)
 Resizes this block to the given size and fills its contents from the supplied buffer. More...
 
void reset ()
 Frees all the blocks data, setting its size to 0. More...
 
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. More...
 
void setSize (const size_t newSize, bool initialiseNewSpaceToZero=false)
 Resizes the memory block. More...
 
void swapWith (MemoryBlock &other) noexcept
 Exchanges the contents of this and another memory block. More...
 
String toBase64Encoding () const
 Returns a string of characters in a JUCE-specific text encoding that represents the binary contents of this block. More...
 
String toString () const
 Attempts to parse the contents of the block as a zero-terminated UTF8 string. More...
 

Private Types

using HeapBlockType = HeapBlock< char, true >
 

Private Attributes

HeapBlockType data
 
size_t size = 0
 

Detailed Description

A class to hold a resizable block of raw data.

{Core}

Member Typedef Documentation

◆ HeapBlockType

using juce::MemoryBlock::HeapBlockType = HeapBlock<char, true>
private

Constructor & Destructor Documentation

◆ MemoryBlock() [1/5]

juce::MemoryBlock::MemoryBlock ( )
noexcept

Create an uninitialised block with 0 size.

◆ MemoryBlock() [2/5]

juce::MemoryBlock::MemoryBlock ( const size_t  initialSize,
bool  initialiseToZero = false 
)

Creates a memory block with a given initial size.

Parameters
initialSizethe size of block to create
initialiseToZerowhether to clear the memory or just leave it uninitialised

◆ MemoryBlock() [3/5]

juce::MemoryBlock::MemoryBlock ( const MemoryBlock )

Creates a copy of another memory block.

◆ MemoryBlock() [4/5]

juce::MemoryBlock::MemoryBlock ( const void *  dataToInitialiseFrom,
size_t  sizeInBytes 
)

Creates a memory block using a copy of a block of data.

Parameters
dataToInitialiseFromsome data to copy into this block
sizeInByteshow much space to use

◆ ~MemoryBlock()

juce::MemoryBlock::~MemoryBlock ( )
noexcept

Destructor.

◆ MemoryBlock() [5/5]

juce::MemoryBlock::MemoryBlock ( MemoryBlock &&  )
noexcept

Move constructor.

Member Function Documentation

◆ append()

void juce::MemoryBlock::append ( const void *  data,
size_t  numBytes 
)

Adds another block of data to the end of this one.

The data pointer must not be null. This block's size will be increased accordingly.

◆ begin()

char* juce::MemoryBlock::begin ( ) const
inlinenoexcept

Returns an iterator for the data.

◆ copyFrom()

void juce::MemoryBlock::copyFrom ( const void *  srcData,
int  destinationOffset,
size_t  numBytes 
)
noexcept

Copies data into this MemoryBlock from a memory address.

Parameters
srcDatathe memory location of the data to copy into this block
destinationOffsetthe offset in this block at which the data being copied should begin
numByteshow much to copy in (if this goes beyond the size of the memory block, it will be clipped so not to do anything nasty)

◆ copyTo()

void juce::MemoryBlock::copyTo ( void *  destData,
int  sourceOffset,
size_t  numBytes 
) const
noexcept

Copies data from this MemoryBlock to a memory address.

Parameters
destDatathe memory location to write to
sourceOffsetthe offset within this block from which the copied data will be read
numByteshow much to copy (if this extends beyond the limits of the memory block, zeros will be used for that portion of the data)

◆ end()

char* juce::MemoryBlock::end ( ) const
inlinenoexcept

Returns an end-iterator for the data.

◆ ensureSize()

void juce::MemoryBlock::ensureSize ( const size_t  minimumSize,
bool  initialiseNewSpaceToZero = false 
)

Increases the block's size only if it's smaller than a given size.

Parameters
minimumSizeif the block is already bigger than this size, no action will be taken; otherwise it will be increased to this size
initialiseNewSpaceToZeroif the block gets enlarged, this determines whether to clear the new section or just leave it uninitialised
See also
setSize

Referenced by juce::MidiDataConcatenator::processSysex().

◆ fillWith()

void juce::MemoryBlock::fillWith ( uint8  valueToUse)
noexcept

Fills the entire memory block with a repeated byte value.

This is handy for clearing a block of memory to zero.

◆ fromBase64Encoding()

bool juce::MemoryBlock::fromBase64Encoding ( StringRef  encodedString)

Takes a string created by MemoryBlock::toBase64Encoding() and extracts the original data.

The string passed in must have been created by to64BitEncoding(), and this block will be resized to recreate the original data block.

Note that these methods use a JUCE-specific (i.e. not standard!) 64-bit encoding system. You may prefer to use the Base64::convertFromBase64() method if you want to use the standard base-64 encoding.

See also
toBase64Encoding, Base64::convertFromBase64

Referenced by juce::StandalonePluginHolder::reloadPluginState().

◆ getBitRange()

int juce::MemoryBlock::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.

◆ getData()

void* juce::MemoryBlock::getData ( ) const
inlinenoexcept

Returns a void pointer to the data.

Note that the pointer returned will probably become invalid when the block is resized.

Referenced by juce::StandalonePluginHolder::askUserToLoadState(), juce::StandalonePluginHolder::askUserToSaveState(), juce::MidiDataConcatenator::processSysex(), and juce::StandalonePluginHolder::reloadPluginState().

◆ getSize()

size_t juce::MemoryBlock::getSize ( ) const
inlinenoexcept

◆ insert()

void juce::MemoryBlock::insert ( const void *  dataToInsert,
size_t  numBytesToInsert,
size_t  insertPosition 
)

Inserts some data into the block.

The dataToInsert pointer must not be null. This block's size will be increased accordingly. If the insert position lies outside the valid range of the block, it will be clipped to within the range before being used.

◆ loadFromHexString()

void juce::MemoryBlock::loadFromHexString ( StringRef  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.

See also
String::toHexString()

◆ matches()

bool juce::MemoryBlock::matches ( const void *  data,
size_t  dataSize 
) const
noexcept

Returns true if the data in this MemoryBlock matches the raw bytes passed-in.

◆ operator!=()

bool juce::MemoryBlock::operator!= ( const MemoryBlock other) const
noexcept

Compares two memory blocks.

Returns
true if the two blocks are different sizes or have different contents.

◆ operator=() [1/2]

MemoryBlock& juce::MemoryBlock::operator= ( const MemoryBlock )

Copies another memory block onto this one.

This block will be resized and copied to exactly match the other one.

◆ operator=() [2/2]

MemoryBlock& juce::MemoryBlock::operator= ( MemoryBlock &&  )
noexcept

Move assignment operator.

◆ operator==()

bool juce::MemoryBlock::operator== ( const MemoryBlock other) const
noexcept

Compares two memory blocks.

Returns
true only if the two blocks are the same size and have identical contents.

◆ operator[]()

template<typename Type >
char& juce::MemoryBlock::operator[] ( const Type  offset) const
inlinenoexcept

Returns a byte from the memory block.

This returns a reference, so you can also use it to set a byte.

◆ removeSection()

void juce::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.

◆ replaceWith()

void juce::MemoryBlock::replaceWith ( const void *  data,
size_t  numBytes 
)

Resizes this block to the given size and fills its contents from the supplied buffer.

The data pointer must not be null.

◆ reset()

void juce::MemoryBlock::reset ( )

Frees all the blocks data, setting its size to 0.

◆ setBitRange()

void juce::MemoryBlock::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.

◆ setSize()

void juce::MemoryBlock::setSize ( const size_t  newSize,
bool  initialiseNewSpaceToZero = false 
)

Resizes the memory block.

Any data that is present in both the old and new sizes will be retained. When enlarging the block, the new space that is allocated at the end can either be cleared, or left uninitialised.

Parameters
newSizethe new desired size for the block
initialiseNewSpaceToZeroif the block gets enlarged, this determines whether to clear the new section or just leave it uninitialised
See also
ensureSize

◆ swapWith()

void juce::MemoryBlock::swapWith ( MemoryBlock other)
noexcept

Exchanges the contents of this and another memory block.

No actual copying is required for this, so it's very fast.

◆ toBase64Encoding()

String juce::MemoryBlock::toBase64Encoding ( ) const

Returns a string of characters in a JUCE-specific text encoding that represents the binary contents of this block.

This uses a JUCE-specific (i.e. not standard!) 64-bit encoding system to convert binary data into a string of ASCII characters for purposes like storage in XML. Note that this proprietary format is mainly kept here for backwards-compatibility, and you may prefer to use the Base64::toBase64() method if you want to use the standard base-64 encoding.

See also
fromBase64Encoding, Base64::toBase64, Base64::convertToBase64

Referenced by juce::StandalonePluginHolder::savePluginState().

◆ toString()

String juce::MemoryBlock::toString ( ) const

Attempts to parse the contents of the block as a zero-terminated UTF8 string.

Referenced by juce::URL::getPostData().

Member Data Documentation

◆ data

HeapBlockType juce::MemoryBlock::data
private

◆ size

size_t juce::MemoryBlock::size = 0
private

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