MemoryFileStruct Manual

内容表

关于

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

注意
The MAXON API equivalent is maxon::IoMemoryInterface .

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.
AutoAlloc<HyperFile> hf; AutoAlloc<MemoryFileStruct> mfs; if (hf == nullptr || mfs == nullptr ) return maxon::OutOfMemoryError( MAXON_SOURCE_LOCATION );

// By setting the write mode of the filename, it will point to a file in memory. Filename fn; fn. SetMemoryWriteMode (mfs); // DO NOT use fn.SetFile() or fn.SetDirectory()

// Open a HyperFile in memory and write some data into it. if (!hf-> Open (0, fn, FILEOPEN::WRITE , FILEDIALOG::NONE )) return maxon::UnexpectedError( MAXON_SOURCE_LOCATION );

hf-> WriteInt32 (123); hf-> WriteString ( "foo" _s); hf-> WriteString ( "bar" _s); hf-> WriteInt32 (456); // Close() is explicitly needed here, as the completed file is accessed a few lines below by GetData(). hf-> 关闭 ();

// 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 == nullptr ) return maxon::IllegalArgumentError( MAXON_SOURCE_LOCATION );

// By setting the write mode of the filename, it will point to a file in memory. Filename fn; AutoAlloc<MemoryFileStruct> mfs; if (mfs == nullptr ) return maxon::OutOfMemoryError( MAXON_SOURCE_LOCATION );

// No actual name needs to be set, switching mode is enough. fn. SetMemoryWriteMode (mfs); // DO NOT use fn.SetFile() or fn.SetDirectory()

// Write the object(s) into the HyperFile in memory. if ( FILEERROR::NONE != WriteHyperFile (doc, op, fn, 0)) return maxon::UnexpectedError( MAXON_SOURCE_LOCATION );

// Now, get the pointer to the HyperFile in memory and its size. void * rawData = nullptr ; Int rawDataSize = 0; // Note: Pay special attention to the last parameter, defining the ownership of the memory block. mfs-> GetData (rawData, rawDataSize, false );

// ... Send data (rawData, rawDataSize) via for example a NetworkIpConnection... SendToNetwork(rawData, rawDataSize) iferr_return ;

// 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 maxon::OK ; // nothing received

// By setting the read mode of the filename, it will point to a file in memory. // No actual name needs to be set, switching mode is enough. Filename fn; fn. SetMemoryReadMode (rawData, rawDataSize, false ); // DO NOT use fn.SetFile() or fn.SetDirectory()

// Read object(s) from the HyperFile in memory. AutoAlloc<BaseObject> op { Onull }; if (op == nullptr ) return maxon::OutOfMemoryError( MAXON_SOURCE_LOCATION ); String error; if ( FILEERROR::NONE != ReadHyperFile (doc, op, fn, 0, &error)) return maxon::UnknownError( MAXON_SOURCE_LOCATION );

// Insert the object(s) into the active document. doc-> InsertObject (op.Release(), nullptr , nullptr , false );

Allocation/Deallocation

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

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

Reading Data From a File in Memory

延伸阅读

WriteHyperFile
FILEERROR WriteHyperFile(BaseDocument *doc, GeListNode *node, const Filename &filename, Int32 ident)
HyperFile::WriteString
Bool WriteString(const maxon::String &v)
BaseDocument::InsertObject
void InsertObject(BaseObject *op, BaseObject *parent, BaseObject *pred, Bool checknames=false)
FILEDIALOG::NONE
@ NONE
Never show an error dialog.
Int
maxon::Int Int
定义: ge_sys_math.h:62
BaseObject
定义: c4d_baseobject.h:224
Onull
#define Onull
Null.
定义: ge_prepass.h:1009
MemoryFileStruct::GetData
void GetData(void *&data, Int &size) const
Filename
Manages file and path names.
定义: c4d_file.h:93
maxon::OK
return OK
定义: apibase.h:2532
Filename::SetMemoryReadMode
void SetMemoryReadMode(void *adr, Int size=-1, Bool transferOwnership=false)
iferr_return
#define iferr_return
定义: resultbase.h:1434
MAXON_SOURCE_LOCATION
#define MAXON_SOURCE_LOCATION
定义: memoryallocationbase.h:66
HyperFile::WriteInt32
Bool WriteInt32(Int32 v)
HyperFile::Open
Bool Open(Int32 ident, const Filename &filename, FILEOPEN mode, FILEDIALOG error_dialog)
String
定义: c4d_string.h:38
FILEOPEN::WRITE
@ WRITE
FILEERROR::NONE
@ NONE
No error.
Filename::SetMemoryWriteMode
void SetMemoryWriteMode(MemoryFileStruct *mfs)
HyperFile::Close
Bool Close()
AutoAlloc
定义: ge_autoptr.h:36
BaseDocument::GetActiveObject
BaseObject * GetActiveObject(void)
ReadHyperFile
FILEERROR ReadHyperFile(BaseDocument *doc, GeListNode *node, const Filename &filename, Int32 ident, maxon::String *warning_string)

Copyright  © 2014-2025 乐数软件    

工业和信息化部: 粤ICP备14079481号-1