About
Using MemoryFileStruct one can read/write from/into memory using a file class.
- Note
- The Maxon API equivalent is maxon::IoMemoryInterface.
Typical use cases:
- Accessing memory like a file, basically a BaseFile or a HyperFile in memory.
- Compress or encrypt file content (e.g. keep an image in JPEG format in memory).
- Initialize a BaseBitmap from memory.
- Transfer arbitrary GeListNode derived entities (e.g. BaseObject, BaseMaterial, BaseContainer,...) via a network connection.
A general example on writing into a HyperFile in memory:
AutoAlloc<HyperFile> hf;
AutoAlloc<MemoryFileStruct> mfs;
if (hf == nullptr || mfs == nullptr)
Filename fn;
fn.SetMemoryWriteMode(mfs);
hf->WriteInt32(123);
hf->WriteString("foo"_s);
hf->WriteString("bar"_s);
hf->WriteInt32(456);
hf->Close();
void* rawData = nullptr;
mfs->GetData(rawData, rawDataSize, false);
NONE
Definition: asset_browser.h:1
WRITE
Problems writing the file.
Definition: ge_prepass.h:4
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:69
maxon::Int Int
Definition: ge_sys_math.h:55
This is how one can easily transfer an object (or a complete hierarchy) via network:
BaseObject*
const op =
doc->GetActiveObject();
Filename fn;
AutoAlloc<MemoryFileStruct> mfs;
if (mfs == nullptr)
fn.SetMemoryWriteMode(mfs);
void* rawData = nullptr;
mfs->GetData(rawData, rawDataSize, false);
FILEERROR WriteHyperFile(BaseDocument *doc, GeListNode *node, const Filename &filename, Int32 ident)
const char * doc
Definition: pyerrors.h:226
#define iferr_return
Definition: resultbase.h:1531
PyObject * op
Definition: object.h:520
void* rawData = nullptr;
ReceiveFromNetwork(rawData, rawDataSize);
if (!rawData || rawDataSize == 0)
Filename fn;
fn.SetMemoryReadMode(rawData, rawDataSize, false);
AutoAlloc<BaseObject>
op {
Onull };
doc->InsertObject(
op.Release(),
nullptr,
nullptr,
false);
PyObject * error
Definition: codecs.h:206
return OK
Definition: apibase.h:2740
#define Onull
Null.
Definition: ge_prepass.h:1077
FILEERROR ReadHyperFile(BaseDocument *doc, GeListNode *node, const Filename &filename, Int32 ident, maxon::String *warning_string)
Allocation/Deallocation
MemoryFileStruct objects are created with the usual tools, see Entity Creation and Destruction Manual (Cinema API).
- MemoryFileStruct::Alloc(): Allocates a memory file.
- MemoryFileStruct::Free(): Destructs memory files allocated with Alloc().
Writing Data to a File in Memory and Accessing the Raw Data
- Allocate a MemoryFileStruct.
- Set a Filename to memory write mode via Filename::SetMemoryWriteMode().
- DO NOT set an actual filename via functions like e.g. Filename::SetFile().
- Open the file as usual via BaseFile::Open() or HyperFile::Open() with the above Filename.
- Simply write into the file as usual.
- Close the file.
- Afterwards the raw data of the file can be accessed via MemoryFileStruct::GetData().
- MemoryFileStruct::GetData(): Gets the data previously written to the memory file.
Reading Data From a File in Memory
- Set a Filename to memory read mode via Filename::SetMemoryReadMode(), pointing it to data in memory.
- DO NOT set an actual filename via functions like e.g. Filename::SetFile().
- Open the file as usual via BaseFile::Open() or HyperFile::Open() with the above Filename.
- Simply read data from the file as usual.
- Close the file.
Further Reading