blob: 3e6b0ade989afa0cc76c9ba643cedfaf8aca8521 [file] [log] [blame]
{%- import "validation_macros.tmpl" as validation_macros %}
{%- set class_name = union.name ~ "_Data" %}
{%- set enum_name = union.name ~ "_Tag" -%}
// static
{{class_name}}* {{class_name}}::New(mojo::internal::Buffer* buf) {
return new (buf->Allocate(sizeof({{class_name}}))) {{class_name}}();
}
// static
bool {{class_name}}::Validate(const void* data,
mojo::internal::BoundsChecker* bounds_checker,
bool inlined) {
if (!data) {
return true;
}
if (!mojo::internal::IsAligned(data)) {
ReportValidationError(mojo::internal::VALIDATION_ERROR_MISALIGNED_OBJECT);
return false;
}
// If the union is inlined in another structure its memory was already claimed.
// This ONLY applies to the union itself, NOT anything which the union points
// to.
if (!inlined && !bounds_checker->ClaimMemory(data, sizeof({{class_name}}))) {
ReportValidationError(
mojo::internal::VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE);
return false;
}
const {{class_name}}* object = static_cast<const {{class_name}}*>(data);
MOJO_ALLOW_UNUSED_LOCAL(object);
switch (object->tag) {
{% for field in union.fields %}
case {{enum_name}}::{{field.name|upper}}: {
{{ validation_macros.validate_union_field(field, union)|indent(8) }}
}
{%- endfor %}
default: {
ReportValidationError(
mojo::internal::VALIDATION_ERROR_UNKNOWN_UNION_TAG,
"unknown tag in {{union.name}}");
return false;
}
}
}
void {{class_name}}::set_null() {
size = 0U;
tag = static_cast<{{enum_name}}>(0);
data.unknown = 0U;
}
{{class_name}}::{{class_name}}() {
}
void {{class_name}}::EncodePointersAndHandles(
std::vector<mojo::Handle>* handles) {
// TODO(azani): Implement pointers and handles.
}
void {{class_name}}::DecodePointersAndHandles(
std::vector<mojo::Handle>* handles) {
// TODO(azani): Implement pointers and handles.
}