VTable#
Fully qualified name: carb::variant::VTable
Defined in carb/variant/VariantTypes.h
-
struct VTable#
A v-table definition for a variant type. Each registered type has a unique v-table pointer that is retrievable via IVariant::getVTable(). Each entry in the v-table is a function with a default behavior if
nullptr.Note
This class is applicable only to users of carb.variant.plugin that author a custom Translator class.
Warning
Functions in the v-table should not be called directly; the Variant wrapper class calls them through various traits functions.
Warning
All functions require that
self->vtable->[function]is equal to the function called.Public Members
-
uint32_t sizeOf#
A member used as version control.
This member should be set to
sizeof(VTable)for the version of the v-table that a module is built against.
-
RString typeName#
Indicates the type name of the v-table.
Once registered with IVariant::registerType(), this name can be used to look up the type with IVariant::getVTable().
Warning
This must be a unique name within the running process and may not match any of the built-in type names.
-
void (*Destructor)(VariantData *self) noexcept#
Used to destroy the VariantData::data member.
A
nullptrdestructor function indicates that no destruction needs to take place.- Param self:
The VariantData to destroy. Can assume that
self->vtable->Destructoris the same as the function called.
-
VariantData (*Copy)(const VariantData *self) noexcept#
Called to create a functional copy of the given VariantData.
A
nullptrfunction indicates that VariantData can be trivially copied.Note
The resulting VariantData need not have the same v-table as
self.- Param self:
The VariantData to copy. Can assume that
self->vtable->Copyis the same as the function called.- Return:
A VariantData that is a functional copy of
self. traits::equals() should betruefor*selfand the returned VariantData.
-
bool (*Equals)(const VariantData *self, const VariantData *other) noexcept#
Called to test equality of
*selfwith (possibly different type)*other.A
nullptrfunction indicates that a trivial comparison of the VariantData is performed (i.e.memcmp).Warning
Generally speaking, order should not matter: assuming that
lhsandrhsare bothconst VariantData*with non-nullEquals, it should hold thatlhs->vtable->Equals(lhs, rhs)should always equalrhs->vtable->Equals(rhs, lhs)regardless of their respective v-tables.- Param self:
The VariantData performing the compare. Can assume that
self->vtable->Equalsis the same as the function called.- Param other:
The same or a different VariantData to compare with
self. May have a differentvtablethanself.- Return:
trueif the types are equal;falseotherwise.
-
omni::string (*ToString)(const VariantData *self) noexcept#
Called to render the VariantData as a string.
A
nullptrfunction indicates that a string is produced that contains “<vtable pointer>:<data pointer>”.- Param self:
The VariantData to render. Can assume that
self->vtable->ToStringis the same as the function called.- Return:
A type-dependent printable string representing the type, useful for debugging.
-
bool (*ConvertTo)(const VariantData *self, const VTable *newtype, VariantData *out) noexcept#
Called to attempt to convert
selfto a different type.A
nullptrfunction is the same as returning false.Note
Generally speaking, Equals() and ConvertTo() should understand the same types.
Warning
If
falseis returned,outis in an undefined state. If and only iftrueis returned, traits::destruct() must be called at some later point on*out.- Param self:
The VariantData performing the conversion. Can assume that
self->vtable->ConvertTois the same asConvertTo.- Param newtype:
A v-table representing the type to attempt to convert to. If the function recognizes the given v-table (which should not be the same as
self->vtable) and can convert to that type then the function should write tooutand returntrue. If the v-table is not recognized, the function must returnfalse.- Param out:
If
trueis returned from the function, this must be a valid VariantData and must be later destroyed with traits::destruct(). Iffalseis returned then the state ofoutis undefined. There is no requirement thatout->vtablematchesnewtypeiftrueis returned; it must merely be valid.- Return:
trueif and only ifoutcontains a valid converted representation ofself;falseotherwise.
-
size_t (*Hash)(const VariantData *self) noexcept#
Computes a hash of
self.A
nullptrfunction castsself->datato asize_tfor use as a hash.- Param self:
The VariantData to hash. Can assume that
self->vtable->Hashis the same as the function called.- Return:
A value to use as a hash identifying
*self.
-
bool (*ConvertFrom)(const VTable *desired, VariantData *out, const VariantData *from) noexcept#
Called to attempt to convert to
desiredfromfrom.A
nullptrfunction is the same as returning false.If
ConvertFromrecognizes the v-tablefrom->vtableand can convert to that type, then the function should write tooutand returntrue.desiredshould never be the same asfrom->vtable.Note
Generally speaking, Equals() and ConvertFrom() should understand the same types.
Note
In the past, only ConvertTo was supported. This function is typically only called if ConvertTo fails. If two types are mutually convertible via their ConvertTo methods, they need not implement this function.
Warning
If
falseis returned,outis in an undefined state. If and only iftrueis returned, traits::destruct() must be called at some later point on*out.- Param desired:
A v-table representing the type to attempt to convert to.
ConvertFromis the same asdesired->ConvertFrom. Given this,desiredcan often be ignored.- Param out:
If
trueis returned from the function, this must be a valid VariantData and must be later destroyed with traits::destruct(). Iffalseis returned then the state ofoutis undefined. There is no requirement thatout->vtablematchesdesirediftrueis returned; it must merely be valid.- Param from:
The VariantData to convert from.
- Return:
trueif and only ifoutcontains a valid converted representation offrom;falseotherwise.
-
uint32_t sizeOf#