About

Using MemoryFileStruct one can read/write from/into memory using a file class.

Typical use cases:

A general example on writing into a HyperFile in memory:

// This example writes some data into memory and accesses the raw memory afterwards.
if (!hf || !mfs)
return false;
// By setting the write mode of the filename, it will point to a file in memory.
// Open a HyperFile in memory and write some data into it.
if (!hf->Open(0, fn, FILEOPEN_WRITE, FILEDIALOG_NONE))
return false;
hf->WriteInt32(123);
hf->WriteString("foo");
hf->WriteString("bar");
hf->WriteInt32(456);
hf->Close(); // Close() is explicitly needed here, as the completed file is accessed a few lines below by GetData().
// Get the raw data of the HyperFile and do something with it.
void* rawData = nullptr;
Int rawDataSize = 0;
mfs->GetData(rawData, rawDataSize, false);
// ... do something with rawData...

This is how one can easily transfer an object (or a complete hierarchy) via network:

// This example demonstrates how to send an object (or a hierarchy of objects) over a network connection.
BaseObject* const op = doc->GetActiveObject();
if (!op)
{
GePrint("Please select an object");
return false;
}
// By setting the write mode of the filename, it will point to a file in memory.
if (!mfs)
return false;
fn.SetMemoryWriteMode(mfs); // No actual name needs to be set, switching mode is enough.
// Write the object(s) into the HyperFile in memory.
if (FILEERROR_NONE != WriteHyperFile(doc, op, fn, 0))
return false;
// Now, get the pointer to the HyperFile in memory and its size.
void* rawData = nullptr;
Int rawDataSize = 0;
mfs->GetData(rawData, rawDataSize, false); // Note: Pay special attention to the last parameter, defining the ownership of the memory block.
// ... Send data (rawData, rawDataSize) via for example a NetworkIpConnection...
SendToNetwork(rawData, rawDataSize); // custom helper function
return true;
// This example demonstrates how to receive an object (or a hierarchy of objects) from a network connection.
// ... receive data from an NetworkIpConnection, we get a pointer to the data and the size of the data...
void* rawData = nullptr;
Int rawDataSize = 0;
ReceiveFromNetwork(rawData, rawDataSize); // custom helper function
if (!rawData || rawDataSize == 0)
return false; // nothing received
// By setting the read mode of the filename, it will point to a file in memory.
fn.SetMemoryReadMode(rawData, rawDataSize, false); // No actual name needs to be set, switching mode is enough.
// Read object(s) from the HyperFile in memory.
String error;
if (!op)
return false;
if (FILEERROR_NONE != ReadHyperFile(doc, op, fn, 0, &error))
return false;
// Insert the object(s) into the active document.
doc->InsertObject(op.Release(), nullptr, nullptr, false);
return true;

Allocation/Deallocation

MemoryFileStruct objects are created with the usual tools, see Entity Creation and Destruction Manual.

Writing Data to a File in Memory and Accessing the Raw Data

Reading Data From a File in Memory

Further Reading