DataType Manual

About

A maxon::DataType object represents a registered data type of the MAXON API. It is used to check the type of some given data at run time or to write generic template code.

Creation

The maxon::DataType object representing a specific data type can be accessed with maxon::GetDataType():

// This example gets the maxon::DataType for the given type.
const maxon::DataType dt = maxon::GetDataType<maxon::Int32>();
DiagnosticOutput("DataType: @", dt);
Definition: datatypebase.h:772
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:176
Note
Further utility functions can be found in the maxon::DataTypeLib.

Conversion

The maxon::DataType object is used to register and apply specific conversions from and to the defined data type.

Available conversion types are:

// This example adds a conversion for a custom data type and uses it to convert it into a maxon::Vector.
// add conversion
maxon::DataType::AddConversion<maxon::Vector, IntegerTriplet>(
{
dest.x = maxon::Float(src._a);
dest.y = maxon::Float(src._b);
dest.z = maxon::Float(src._c);
return maxon::OK;
// convert
// prepare data
IntegerTriplet triplet;
triplet._a = 10; triplet._b = 20; triplet._c = 30;
// get DataType
const maxon::DataType dt = maxon::GetDataType<maxon::Vector>();
if (dt)
{
// convert
maxon::Generic* const generic = reinterpret_cast<maxon::Generic*>(&vec);
dt.Convert(*generic, ptr, ignore) iferr_return;
DiagnosticOutput("Vector: @", vec);
}
[datatypes_datatype_declaration]
Definition: custom_datatype.h:25
maxon::Int _a
Definition: custom_datatype.h:29
Definition: datatypebase.h:1800
Result< void > Convert(Generic &dest, const ConstDataPtr &src, CONVERSION_FLAGS ignore=CONVERSION_FLAGS::NONE) const
PyObject * src
Definition: abstract.h:305
CONVERSION_FLAGS
Definition: datatypebase.h:62
Float64 Float
Definition: apibase.h:197
return OK
Definition: apibase.h:2690
@ WIDENING_LOSSY
The conversion contains a widening conversion which is always possible, but it might result in loss o...
@ NONE
When no other flags are set, the conversion is a one-to-one correspondence (such as from Float to Tim...
char ** ptr
Definition: pycore_dtoa.h:13
#define iferr_return
Definition: resultbase.h:1519
T y
Definition: vec.h:40
T x
Definition: vec.h:39
T z
Definition: vec.h:41

DataType Comparison

Two maxon:DataType objects can be compared using the "==" operator or with some more sophisticated functions:

Value Comparison

Using the maxon::DataType object it is also possible two compare two object of this data type:

// This example shows a template function that compares two values
// of the given type and prints the result to the debug console.
template <typename T> static maxon::Result<void> CompareAndPrint(T a, T b)
{
const maxon::DataType dt = maxon::GetDataType<T>();
if (!dt)
return maxon::UnexpectedError(MAXON_SOURCE_LOCATION, "Unknown data type."_s);
if (!dt->IsEqual(&a, &b, maxon::EQUALITY::DEEP))
{
const maxon::COMPARERESULT res = dt->Compare(&a, &b);
DiagnosticOutput("Compare result: @", res);
}
else
{
DiagnosticOutput("Values are equal.");
}
return maxon::OK;
}
Bool IsEqual(const void *s1, const void *s2, EQUALITY equality) const
Definition: datatypebase.h:547
COMPARERESULT Compare(const void *s1, const void *s2) const
Definition: datatypebase.h:555
Py_UCS4 * res
Definition: unicodeobject.h:1113
COMPARERESULT
Data type for comparison results.
Definition: compare.h:21
@ DEEP
A deep equality-test shall be done. For pointers or (non-copy-on-write) references the referenced obj...
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:67

Construct

The maxon::DataType object can be used to create new object instances of the define type:

  • maxon::DataType::Create(): Returns a new instance of the type.
  • maxon::DataType::Construct(): Creates multiple instances of the type.
  • maxon::DataType::MoveConstruct(): Moves constructs multiple instances of the type.
  • maxon::DataType::Destruct(): Destroys multiple instances of the type.
  • maxon::DataType::MoveFrom(): Moves multiple instances of the type to a target location.
  • maxon::DataType::CopyFrom(): Copies multiple instances of the type to the target location.
// This example shows a template function that will only work if used with a certain type.
template <typename T> static maxon::Result<void> FillIntNumbers(void* mem, maxon::Int count)
{
// get data type
const maxon::DataType dt = maxon::GetDataType<T>();
if (!dt)
return maxon::UnexpectedError(MAXON_SOURCE_LOCATION, "Unknown data type."_s);
// compare to legit data types
const maxon::DataType int_dt = maxon::GetDataType<maxon::Int>();
const maxon::DataType uint_dt = maxon::GetDataType<maxon::UInt>();
const maxon::Bool isIntDataType = dt == int_dt;
const maxon::Bool isUIntDataType = dt == uint_dt;
if (!(isIntDataType || isUIntDataType))
return maxon::UnexpectedError(MAXON_SOURCE_LOCATION, "Template function used with invalid type."_s);
// create numbers
dt->Construct(mem, 0, count);
return maxon::OK;
}
Py_ssize_t count
Definition: abstract.h:640
void Construct(void *dest) const
Definition: datatypebase.h:426
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:188
bool Bool
boolean type, possible values are only false/true, 8 bit
Definition: apibase.h:181

Interfaces

Also interfaces are registered as a maxon::DataType.

  • maxon::DataType::GetImplementation(): Returns the interface implementation of this type.
  • maxon::DataType::GetInterfaces(): Returns the interfaces of this object type.

Disc I/O

Data stored in an object of a given maxon::DataType can be written to a file. How this is done is described by a maxon::DataSerializeInterface object.

Properties

The maxon::DataType object gives access to various properties of the described data type:

// This example gets a maxon::String and a maxon::Id of the given maxon::DataType.
const maxon::String name = dt.ToString(nullptr);
const maxon::Id id = dt.GetId();
DiagnosticOutput("Data Type @, (@)", name, id);
const char const char * name
Definition: abstract.h:195
String ToString(const FormatStatement *formatStatement=nullptr) const
const Id & GetId() const
Definition: datatypebase.h:828
Definition: apibaseid.h:253
Definition: string.h:1235

The maxon::DataType object also provides information on the type of data:

// This example accesses basic information on the given maxon::DataType and prints it to the debug console.
const maxon::Int size = dt.GetSize();
DiagnosticOutput("Size @, Alignment @", size, alignment);
const maxon::VALUEKIND valueKind = dt->GetValueKind();
DiagnosticOutput("Type settings: @", valueKind);
Int GetAlignment() const
Definition: datatypebase.h:843
Int GetSize() const
Definition: datatypebase.h:837
VALUEKIND GetValueKind() const
Definition: datatypebase.h:267
Py_ssize_t size
Definition: bytesobject.h:86
Int alignment
Definition: apibase.h:738
VALUEKIND
Definition: apibase.h:2315

It is possible to register a data type using MAXON_DATATYPE_REGISTER_STRUCT. This will inform Cinema 4D about public members of the type. The maxon::DataType object can be used to list these public members.

See also maxon::TypeArguments and maxon::Member.

// This example accesses and loops through all registered named members
// of the given maxon::DataType.
// check if structure
{
const maxon::TupleDataType tupleData = dt.GetTupleType();
if (tupleData)
{
// loop through all members
for (maxon::Int i = 0; i < args.count; ++i)
{
const maxon::Member member = args.args[i];
const maxon::Id memberName = member.name;
const maxon::DataType memberType = member.type;
DiagnosticOutput("Member @ (@)", memberName, memberType);
}
}
}
Py_ssize_t i
Definition: abstract.h:645
PyObject * args
Definition: abstract.h:159
const TupleDataType & GetTupleType() const
Definition: datatypebase.h:969
VALUEKIND GetValueKind() const
Definition: datatypebase.h:850
const TypeArguments & GetTypeArguments() const
Definition: datatypebase.h:935
Definition: datatypelib.h:768
enum PyUnicode_Kind kind
Definition: unicodeobject.h:669
@ STRUCT
The data type has an underlying TupleDataType with named members.
Member represents a member of struct-like types or function signatures. A member is just a pair of a ...
Definition: datatypelib.h:353
InternedId name
The name of the member, this may be empty where anonymous members are allowed.
Definition: datatypelib.h:407
DataType type
The type of the member.
Definition: datatypelib.h:406
Definition: datatypelib.h:429

Further Reading