MAXON Data Type


With the MAXON API, every class or structure can be registered as a data type. Objects of such a data type can be stored in a maxon::Data container or can be shared as published objects.

Declaration and Registration

A data type is declared in a header file using the attribute MAXON_DATATYPE:

// This example defines a custom class and declares it as a MAXON data type.
#include "maxon/datatype.h"
#include "maxon/string.h"
// ------------------------------
// Triplet of maxon::Int values.
// ------------------------------
IntegerTriplet() : _a(1), _b(1), _c(1) {}
maxon::String ToString(const maxon::FormatStatement* formatStatement) const
return FormatString("@, @, @", _a, _b, _c);
// register as MAXON data type
MAXON_DATATYPE(IntegerTriplet, "net.maxonexample.datatype.integertriplet");
Definition: custom_datatype.h:25
maxon::Int _a
Definition: custom_datatype.h:29
maxon::Int _c
Definition: custom_datatype.h:31
maxon::Int _b
Definition: custom_datatype.h:30
Definition: custom_datatype.h:27
maxon::String ToString(const maxon::FormatStatement *formatStatement) const
Definition: custom_datatype.h:33
Class to store formatting statements.
Definition: string.h:2030
Definition: string.h:1235
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:188
#define MAXON_DATATYPE(type, id)
Definition: datatype.h:295
#define FormatString(...)
Definition: string.h:2100

Additionally, the data type must also be registered in a source code file using one of these macros:

// This example registers the given class as a MAXON data type.
// Cinema is also informed about the public member variables.
Definition: datatype.h:411


An instance of a class registered as a MAXON API data type can be stored in a maxon::Data container:

// This example configures an object of the custom data type.
// The object is stored in a maxon::Data container.
// configure the object
IntegerTriplet triplet;
triplet._a = 1;
triplet._b = 4;
triplet._c = 9;
// print the content to the console by invoking ToString()
DiagnosticOutput("Triplet: @", triplet);
// storing the object in a maxon::Data container
data.Set(triplet) iferr_return;
Definition: datatypebase.h:1199
Result< void > Set(T &&data)
Definition: datatypebase.h:1393
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:176
#define iferr_return
Definition: resultbase.h:1465

The data type class can also be identified with a maxon::DataType object. This maxon::DataType can be obtained with maxon::GetDataType().

// This example accesses the maxon::DataType of the given class
// and uses it to initialize a maxon::Data object.
// get maxon::DataType
const maxon::DataType dataType = maxon::GetDataType<IntegerTriplet>();
// print data type
DiagnosticOutput("Data Type: @", dataType);
// init maxon::Data
data.Init(dataType) iferr_return;
// get object form maxon::Data
const IntegerTriplet triplet = data.Get<IntegerTriplet>() iferr_return;
DiagnosticOutput("@", triplet);
Result< typename std::conditional< GetCollectionKind< T >::value==COLLECTION_KIND::ARRAY, T, typename ByValueParam< T >::type >::type > Get() const
Definition: datatypebase.h:1404
Result< void > Init(const DataType &type)
Definition: datatypebase.h:1213
Definition: datatypebase.h:772


A custom data type class may define and implement these utility functions. These functions may be called by other parts of the MAXON API when using an instance of the data type:

To add generic functions see also Classes and Functions.

To copy internal data of a data type one has multiple options:

  • implementing a copy constructor and operator.
  • implementing a CopyFrom() function. This is recommended when copying can fail.
// This example shows a custom data type implementing various default functions.
// ------------------------------
// Triplet of maxon::Int values.
// ------------------------------
MAXON_DISALLOW_COPY_AND_ASSIGN(AdvancedIntegerTriplet) // no copy constructor, no "=" operator
_a = 1;
_b = 1;
_c = 1;
_a = src._a;
_b = src._b;
_c = src._c;
return maxon::OK;
if (_a != other._a)
return false;
if (_b != other._b)
return false;
if (_c != other._c)
return false;
return true;
// create hash code based on the internal data
return crc.GetCrc();
maxon::String ToString(const maxon::FormatStatement* formatStatement) const
return FormatString("@, @, @", _a, _b, _c);
return maxon::OK;
MAXON_DATATYPE(AdvancedIntegerTriplet, "net.maxonexample.datatype.advancedintegertriplet");
Definition: custom_datatype.h:54
maxon::Int _a
Definition: custom_datatype.h:62
maxon::Int _c
Definition: custom_datatype.h:66
maxon::Result< void > CopyFrom(const AdvancedIntegerTriplet &src)
Definition: custom_datatype.h:71
maxon::Int _b
Definition: custom_datatype.h:65
static maxon::Result< void > DescribeIO(const maxon::DataSerializeInterface &stream)
Definition: custom_datatype.h:106
MAXON_DISALLOW_COPY_AND_ASSIGN(AdvancedIntegerTriplet) public
Definition: custom_datatype.h:55
maxon::Int GetMemorySize() const
Definition: custom_datatype.h:118
maxon::UInt GetHashCode() const
Definition: custom_datatype.h:91
AdvancedIntegerTriplet(AdvancedIntegerTriplet &&src)=default
maxon::Bool operator==(const AdvancedIntegerTriplet &other) const
Definition: custom_datatype.h:79
maxon::String ToString(const maxon::FormatStatement *formatStatement) const
Definition: custom_datatype.h:101
Definition: crc32c.h:27
UInt32 GetCrc() const
Definition: crc32c.h:66
Definition: crc32c.h:543
static constexpr ValueType NONE
Definition: dataserialize.h:136
Definition: dataserialize.h:207
PyObject * stream
Definition: codecs.h:186
PyObject * src
Definition: abstract.h:305
PyObject * other
Definition: dictobject.h:70
#define Describe(name, memberName, type, flags)
Definition: dataserialize.h:294
#define PrepareDescribe(streamClass, className)
Definition: dataserialize.h:268
bool Bool
boolean type, possible values are only false/true, 8 bit
Definition: apibase.h:181
UInt64 UInt
unsigned 32/64 bit int, size depends on the platform
Definition: apibase.h:189
return OK
Definition: apibase.h:2667
#define SIZEOF(...)
Calculates the size of a datatype or element.
Definition: apibasemath.h:205
#define iferr_scope
Definition: resultbase.h:1374
// This example uses a custom data type in order to invoke its default funtions.
triplet._a = 100;
triplet._b = 200;
triplet._c = 300;
// call ToString()
DiagnosticOutput("Triplet: @", triplet);
// call GetMemorySize()
tripletArray.Resize(1000) iferr_return;
const maxon::Int size = tripletArray.GetMemorySize();
DiagnosticOutput("Memory Usage: @", size);
// call CopyFrom()
const AdvancedIntegerTriplet newTriplet;
tripletArray.Append(newTriplet) iferr_return;
// call GetHashCode()
const maxon::UInt arrayHash = tripletArray.GetHashCode();
DiagnosticOutput("Hash Value: @", arrayHash);
// call DescribeIO()
// file URL
const maxon::Url url = (targetFolder + ""_s)iferr_return;
maxon::Id fileID("net.maxonexample.triplet");
// save to file
maxon::DataFormatWriterRef outputStream = maxon::DataFormatWriterFactories::Binary().Create(url) iferr_return;
outputStream.WriteDocument(fileID, maxon::ConstDataPtr(triplet)) iferr_return;
outputStream.CloseOutput() iferr_return;
// read from file
AdvancedIntegerTriplet loadedTriplet;
maxon::DataFormatReaderRef inputStream = maxon::DataFormatReaderFactories::Binary().Create(url) iferr_return;
inputStream.ReadDocument(fileID, maxon::DataPtr(loadedTriplet)) iferr_return;
inputStream.CloseInput() iferr_return;
DiagnosticOutput("@", loadedTriplet);
HashInt GetHashCode() const
Definition: collection.h:894
Definition: basearray.h:412
Int GetMemorySize() const
Definition: basearray.h:1623
Definition: basearray.h:677
Definition: basearray.h:1369
Definition: datatypebase.h:1800
Definition: datatypebase.h:2070
Definition: apibaseid.h:253
Definition: url.h:942
Py_ssize_t size
Definition: bytesobject.h:86

Further Reading