-
注意
-
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>(
maxon::CONVERSION_FLAGS::WIDENING_LOSSY
,
[](
maxon::Vector
& dest,
const
IntegerTriplet&
src
) ->
maxon::Result<void>
{
dest.
x
=
maxon::Float
(
src
._a);
dest.
y
=
maxon::Float
(
src
._b);
dest.
z
=
maxon::Float
(
src
._c);
return
maxon::OK
;
})
iferr_return
;
// 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::Vector
vec;
maxon::Generic*
const
generic
=
reinterpret_cast<
maxon::Generic*
>
(&vec);
const
maxon::ConstDataPtr
ptr =
maxon::ConstDataPtr
(triplet);
const
maxon::CONVERSION_FLAGS
ignore =
maxon::CONVERSION_FLAGS::NONE
;
dt.
转换
(*
generic
, ptr, ignore)
iferr_return
;
DiagnosticOutput
(
"Vector: @"
, vec);
}
DataType Comparison
Two maxon:DataType objects can be compared using the "==" operator or with some more sophisticated functions:
Value Comparison
使用
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->
比较
(&a, &b);
DiagnosticOutput
(
"Compare result: @"
, res);
}
else
{
DiagnosticOutput
(
"Values are equal."
);
}
return
maxon::OK
;
}
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
;
}
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
对象。
特性
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
);
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
();
const
maxon::Int
alignment
= dt.
GetAlignment
();
DiagnosticOutput
(
"Size @, Alignment @"
, size,
alignment
);
const
maxon::VALUEKIND
valueKind = dt->
GetValueKind
();
const
maxon::String
settings = maxon::ToString<maxon::FormatStatement>(valueKind,
nullptr
,
false
);
DiagnosticOutput
(
"Type settings: @"
, settings);
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.
另请参阅
maxon::TypeArguments
and
maxon::Member
.
// This example accesses and loops through all registered named members
// of the given maxon::DataType.
const
maxon::VALUEKIND
kind = dt.
GetValueKind
();
// check if structure
if
(kind &
maxon::VALUEKIND::STRUCT
)
{
const
maxon::TupleDataType
tupleData = dt.
GetTupleType
();
if
(tupleData)
{
const
maxon::TypeArguments
& args = tupleData.
GetTypeArguments
();
// 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);
}
}
}
延伸阅读
void Construct(void *dest) const
定义:
datatypebase.h:398
CONVERSION_FLAGS
定义:
datatypebase.h:59
const TupleDataType & GetTupleType() const
定义:
datatypebase.h:915
VALUEKIND GetValueKind() const
定义:
datatypebase.h:256
return OK
定义:
apibase.h:2532
bool Bool
boolean type, possible values are only false/true, 8 bit
定义:
apibase.h:177
Bool IsEqual(const void *s1, const void *s2, EQUALITY equality) const
定义:
datatypebase.h:519
Int count
The number of type arguments which are types.
定义:
datatypelib.h:337
#define iferr_return
定义:
resultbase.h:1434
#define MAXON_SOURCE_LOCATION
定义:
memoryallocationbase.h:66
Int GetSize() const
定义:
datatypebase.h:783
Float64 Float
定义:
apibase.h:193
#define DiagnosticOutput(formatString,...)
定义:
debugdiagnostics.h:166
VALUEKIND
定义:
apibase.h:2219
const T & src
定义:
apibase.h:2525
@ WIDENING_LOSSY
The conversion contains a widening conversion which is always possible, but it might result in loss o...
Result< void > Convert(Generic &dest, const ConstDataPtr &src, CONVERSION_FLAGS ignore=CONVERSION_FLAGS::NONE) const
Int64 Int
signed 32/64 bit int, size depends on the platform
定义:
apibase.h:184
const TypeArguments & GetTypeArguments() const
定义:
datatypebase.h:881
Int alignment
定义:
apibase.h:660
String ToString(const FormatStatement *formatStatement=nullptr) const
@ NONE
When no other flags are set, the conversion is a one-to-one correspondence (such as from Float to Tim...
COMPARERESULT Compare(const void *s1, const void *s2) const
定义:
datatypebase.h:527
Member represents a member of struct-like types or function signatures. A member is just a pair of a ...
定义:
datatypelib.h:272
DataType type
The type of the member.
定义:
datatypelib.h:302
Int GetAlignment() const
定义:
datatypebase.h:789
@ STRUCT
The data type has an underlying TupleDataType with named members.
InternedId name
The name of the member, this may be empty where anonymous members are allowed.
定义:
datatypelib.h:303
@ DEEP
A deep equality-test shall be done. For pointers or (non-copy-on-write) references the referenced obj...
VALUEKIND GetValueKind() const
定义:
datatypebase.h:796
const Id & GetId() const
定义:
datatypebase.h:774
Member args[MAXON_FLEXIBLE_ARRAY_LENGTH]
The type arguments which are (optionally named) types.
定义:
datatypelib.h:340
COMPARERESULT
Data type for comparison results.