IoMemoryInterface Class Reference

#include <iomemory.h>

Detailed Description

Helper class for Url of type maxon::URLSCHEME_MEMORY. It turns memory blocks into InputStreams or OutputStreams. This way a memory block can be handled like a real file. The following example shows how to write into a memory file.

IoMemoryRef mfh = IoMemoryRef::Create() iferr_return;
Url writemem = mfh.GetUrl();
OutputStreamRef outmem = writemem.OpenOutputStream() iferr_return;
outmem.Write("ABCDE", 5);
outmem.Write("FGHIJ", 5);
outmem.Close();
for (Int i = 0; i < mfh.GetSize(); i++)
{
}
IoMemoryRef mem = IoMemoryRef::Create() iferr_return;
Char* buffer = NewMem(Char, size) iferr_return;
mem.PrepareReadBuffer(ToBlock(buffer, size),
[](const Char*& buffer)
{
DeleteMem(buffer);

Public Types

using FreeBufferCallback = Delegate< void(const Char *&buffer)>
 

Public Member Functions

MAXON_METHOD Result< void > PrepareReadBuffer (const Block< const Char > &buffer, const FreeBufferCallback &freeBufferCallback)
 
MAXON_METHOD Result< void > Reset (Int size)
 
const MAXON_METHOD Charoperator[] (Int idx)
 
MAXON_METHOD Int GetSize () const
 
MAXON_METHOD Bool IsWritable () const
 
MAXON_METHOD Result< UrlGetUrl () const
 
MAXON_METHOD Result< void > ReadBytes (Int position, const Block< Byte > &data) const
 
MAXON_METHOD Result< IntReadBytesEOS (Int position, const Block< Byte > &data) const
 
MAXON_METHOD Result< void > WriteBytes (Int position, const Block< const Byte > &data)
 
MAXON_METHOD Result< InputStreamRef > OpenInputStream (OPENSTREAMFLAGS flags) const
 
MAXON_METHOD Result< OutputStreamRef > OpenOutputStream (OPENSTREAMFLAGS flags)
 
MAXON_METHOD Result< InOutputStreamRef > OpenInOutputStream (OPENSTREAMFLAGS flags)
 

Static Public Member Functions

static MAXON_METHOD IoMemoryInterfaceAlloc (MAXON_SOURCE_LOCATION_DECLARATION)
 
static MAXON_METHOD IoMemoryInterfaceAlloc (MAXON_SOURCE_LOCATION_DECLARATION, const IoMemoryInterface &src)
 

Private Member Functions

 MAXON_INTERFACE_NONVIRTUAL (IoMemoryInterface, MAXON_REFERENCE_NORMAL, "net.maxon.interface.iomemory")
 

Member Typedef Documentation

◆ FreeBufferCallback

using FreeBufferCallback = Delegate<void(const Char*& buffer)>

Argument type of PrepareReadBuffer().

Member Function Documentation

◆ MAXON_INTERFACE_NONVIRTUAL()

MAXON_INTERFACE_NONVIRTUAL ( IoMemoryInterface  ,
MAXON_REFERENCE_NORMAL  ,
"net.maxon.interface.iomemory"   
)
private

◆ Alloc() [1/2]

Allocators for common use.

Parameters
[in]allocLocationSource location.

◆ Alloc() [2/2]

◆ PrepareReadBuffer()

MAXON_METHOD Result<void> PrepareReadBuffer ( const Block< const Char > &  buffer,
const FreeBufferCallback freeBufferCallback 
)

Prepares a given memory block for reading. The ownership of the memory can be transfered to the object. The memory will be freed by calling DeleteMem().

Parameters
[in]bufferBlock with the available data to read from.
[in]freeBufferCallbackIf a callback/lambda is given the memory should be owned by the object and will be freed by calling the callback. Within the callback the memory should be freed and the pointer must be set to nullptr! If "nullptr" the memory needs to be freed by yourself and needs to be kept until this class is not used anymore.
Returns
OK on success.

◆ Reset()

MAXON_METHOD Result<void> Reset ( Int  size)

Resets the class and preallocate a given size of bytes. In read mode the memory can e.g. be filled with WriteBytes. Write mode only needs size 0 as the data grows dynamically.

Parameters
[in]sizeRequested size of bytes in the buffer. This memory can be accessed via operator[] or ReadBytes/WriteBytes.
Returns
OK on success.

◆ operator[]()

const MAXON_METHOD Char& operator[] ( Int  idx)

Accesses operator to the buffer.

Parameters
[in]idxIndex of the data to read.
Returns
Value of the position in the buffer.

◆ GetSize()

MAXON_METHOD Int GetSize ( ) const

Gets the current buffer size.

Returns
Size of the internal buffer.

◆ IsWritable()

MAXON_METHOD Bool IsWritable ( ) const

Returns whether the object is writable (true) or not (false)

Returns
The current mode.

◆ GetUrl()

MAXON_METHOD Result<Url> GetUrl ( ) const

Creates an Url for this IoMemoryRef class. This name can be used to call InputStreamRef and OutputStreamRef functions with memory files. The Url can be passed to any function that accepts Urls for reading/writing files. E.g. reading/writing images from a memory block etc. InOutputStreamRef is not supported.

Returns
Url for further operations.

◆ ReadBytes()

MAXON_METHOD Result<void> ReadBytes ( Int  position,
const Block< Byte > &  data 
) const

Copies the classes buffer from position to destData with the given length.

Parameters
[in]positionStart copying memory from this offset. The position is the offset in the classes buffer, not the destination buffer.
[in]dataDestination buffer.
Returns
Number of bytes copied.

◆ ReadBytesEOS()

MAXON_METHOD Result<Int> ReadBytesEOS ( Int  position,
const Block< Byte > &  data 
) const

Copies the classes buffer from position to destData with the given length.

Parameters
[in]positionStart copying memory from this offset. The position is the offset in the classes buffer, not the destination buffer.
[in]dataDestination buffer.
Returns
Number of bytes copied.

◆ WriteBytes()

MAXON_METHOD Result<void> WriteBytes ( Int  position,
const Block< const Byte > &  data 
)

Writes the given sourceData at position in the classes buffer with the given length.

Parameters
[in]positionStart copying memory to this offset. The position is the offset in the classes buffer, not the source data buffer.
[in]dataSource data buffer.
Returns
OK on success.

◆ OpenInputStream()

MAXON_METHOD Result<InputStreamRef> OpenInputStream ( OPENSTREAMFLAGS  flags) const

◆ OpenOutputStream()

MAXON_METHOD Result<OutputStreamRef> OpenOutputStream ( OPENSTREAMFLAGS  flags)

◆ OpenInOutputStream()

MAXON_METHOD Result<InOutputStreamRef> OpenInOutputStream ( OPENSTREAMFLAGS  flags)
maxon::IoMemoryInterface::OpenOutputStream
MAXON_METHOD Result< OutputStreamRef > OpenOutputStream(OPENSTREAMFLAGS flags)
Int
maxon::Int Int
Definition: ge_sys_math.h:49
OutputWithFlags
#define OutputWithFlags(flags, formatString,...)
Definition: debugdiagnostics.h:182
maxon::ToBlock
Block< T > ToBlock(T(&array)[N])
Definition: block.h:827
iferr_return
#define iferr_return
Definition: resultbase.h:1434
maxon::IoMemoryInterface::GetUrl
MAXON_METHOD Result< Url > GetUrl() const
maxon::OUTPUT::NOLINEBREAK
@ NOLINEBREAK
if set, no line break is added
maxon::IoMemoryInterface::GetSize
MAXON_METHOD Int GetSize() const
maxon::DeleteMem
void DeleteMem(T *&p)
Definition: defaultallocator.h:258
maxon::IoMemoryInterface::PrepareReadBuffer
MAXON_METHOD Result< void > PrepareReadBuffer(const Block< const Char > &buffer, const FreeBufferCallback &freeBufferCallback)
NewMem
#define NewMem(T, cnt)
Definition: defaultallocator.h:196
maxon::OUTPUT::DIAGNOSTIC
@ DIAGNOSTIC
diagnostic output, shows up if this group of output is activated. This is also the default
Char
maxon::Char Char
Definition: ge_sys_math.h:41