Index: third_party/mojo/src/mojo/public/tools/bindings/generators/cpp_templates/struct_definition.tmpl |
diff --git a/third_party/mojo/src/mojo/public/tools/bindings/generators/cpp_templates/struct_definition.tmpl b/third_party/mojo/src/mojo/public/tools/bindings/generators/cpp_templates/struct_definition.tmpl |
deleted file mode 100644 |
index 77f2ab06f4a31c16ba0456bf5c783b5030687f66..0000000000000000000000000000000000000000 |
--- a/third_party/mojo/src/mojo/public/tools/bindings/generators/cpp_templates/struct_definition.tmpl |
+++ /dev/null |
@@ -1,211 +0,0 @@ |
-{%- set class_name = struct.name ~ "_Data" %} |
- |
-{#- TODO(yzshen): Consider eliminating _validate_object() and |
- _validate_handle(). #} |
- |
-{#- Validates the specified struct field, which is supposed to be an object |
- (struct/array/string/map/union). |
- This macro is expanded by the Validate() method. #} |
-{%- macro _validate_object(struct, packed_field) %} |
-{%- set name = packed_field.field.name %} |
-{%- set kind = packed_field.field.kind %} |
-{%- set wrapper_type = kind|cpp_wrapper_type %} |
-{%- if not kind|is_nullable_kind %} |
-{%- if kind|is_union_kind %} |
- if (object->{{name}}.is_null()) { |
-{%- else %} |
- if (!object->{{name}}.offset) { |
-{%- endif %} |
- ReportValidationError( |
- mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER, |
- "null {{name}} field in {{struct.name}} struct"); |
- return false; |
- } |
-{%- endif %} |
-{%- if not kind|is_union_kind %} |
- if (!mojo::internal::ValidateEncodedPointer(&object->{{name}}.offset)) { |
- ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_POINTER); |
- return false; |
- } |
-{%- endif %} |
-{%- if kind|is_array_kind or kind|is_string_kind %} |
- const mojo::internal::ArrayValidateParams {{name}}_validate_params( |
- {{kind|get_array_validate_params_ctor_args|indent(6)}}); |
- if (!{{wrapper_type}}::Data_::Validate( |
- mojo::internal::DecodePointerRaw(&object->{{name}}.offset), |
- bounds_checker, &{{name}}_validate_params)) { |
-{%- elif kind|is_map_kind %} |
- const mojo::internal::ArrayValidateParams {{name}}_validate_params( |
- {{kind.value_kind|get_map_validate_params_ctor_args|indent(6)}}); |
- if (!{{wrapper_type}}::Data_::Validate( |
- mojo::internal::DecodePointerRaw(&object->{{name}}.offset), |
- bounds_checker, &{{name}}_validate_params)) { |
-{%- elif kind|is_struct_kind %} |
- if (!{{kind|get_name_for_kind}}::Data_::Validate( |
- mojo::internal::DecodePointerRaw(&object->{{name}}.offset), |
- bounds_checker)) { |
-{%- elif kind|is_union_kind %} |
- if (!{{kind|get_name_for_kind}}::Data_::Validate( |
- &object->{{name}}, bounds_checker, true)) { |
-{%- else %} |
- if (!{{wrapper_type}}::Data_::Validate( |
- mojo::internal::DecodePointerRaw(&object->{{name}}.offset), |
- bounds_checker)) { |
-{%- endif %} |
- return false; |
- } |
-{%- endmacro %} |
- |
-{#- Validates the specified struct field, which is supposed to be a handle or |
- contain a handle (in the case of interfaces). |
- This macro is expanded by the Validate() method. #} |
-{%- macro _validate_handle(struct, packed_field) %} |
-{%- set name = packed_field.field.name %} |
-{%- set kind = packed_field.field.kind %} |
-{%- if kind|is_interface_kind %} |
- const mojo::Handle {{name}}_handle = object->{{name}}.handle; |
-{%- else %} |
- const mojo::Handle {{name}}_handle = object->{{name}}; |
-{%- endif %} |
-{%- if not kind|is_nullable_kind %} |
- if ({{name}}_handle.value() == mojo::internal::kEncodedInvalidHandleValue) { |
- ReportValidationError( |
- mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE, |
- "invalid {{name}} field in {{struct.name}} struct"); |
- return false; |
- } |
-{%- endif %} |
- if (!bounds_checker->ClaimHandle({{name}}_handle)) { |
- ReportValidationError(mojo::internal::VALIDATION_ERROR_ILLEGAL_HANDLE); |
- return false; |
- } |
-{%- endmacro %} |
- |
-{#- Validates the specified struct field, which is supposed to be an associated |
- interface or an associated interface request. |
- This macro is expanded by the Validate() method. #} |
-{%- macro _validate_associated(struct, packed_field) %} |
- // TODO(yzshen): add validation for associated kinds. |
-{%- endmacro %} |
- |
-// 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) { |
- if (!data) |
- return true; |
- |
- if (!ValidateStructHeaderAndClaimMemory(data, bounds_checker)) |
- return false; |
- |
- // NOTE: The memory backing |object| may be smaller than |sizeof(*object)| if |
- // the message comes from an older version. |
- const {{class_name}}* object = static_cast<const {{class_name}}*>(data); |
- |
- static const struct { |
- uint32_t version; |
- uint32_t num_bytes; |
- } kVersionSizes[] = { |
-{%- for version in struct.versions -%} |
- { {{version.version}}, {{version.num_bytes}} }{% if not loop.last %}, {% endif -%} |
-{%- endfor -%} |
- }; |
- |
- if (object->header_.version <= |
- kVersionSizes[MOJO_ARRAYSIZE(kVersionSizes) - 1].version) { |
- // Scan in reverse order to optimize for more recent versions. |
- for (int i = MOJO_ARRAYSIZE(kVersionSizes) - 1; i >= 0; --i) { |
- if (object->header_.version >= kVersionSizes[i].version) { |
- if (object->header_.num_bytes == kVersionSizes[i].num_bytes) |
- break; |
- |
- ReportValidationError( |
- mojo::internal::VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER); |
- return false; |
- } |
- } |
- } else if (object->header_.num_bytes < |
- kVersionSizes[MOJO_ARRAYSIZE(kVersionSizes) - 1].num_bytes) { |
- ReportValidationError( |
- mojo::internal::VALIDATION_ERROR_UNEXPECTED_STRUCT_HEADER); |
- return false; |
- } |
- |
-{#- Before validating fields introduced at a certain version, we need to add |
- a version check, which makes sure we skip further validation if |object| |
- is from an earlier version. |last_checked_version| records the last |
- version that we have added such version check. #} |
-{%- set last_checked_version = 0 %} |
-{%- for packed_field in struct.packed.packed_fields_in_ordinal_order %} |
-{%- set kind = packed_field.field.kind %} |
-{%- if kind|is_object_kind or kind|is_any_handle_kind or |
- kind|is_interface_kind or kind|is_associated_kind %} |
-{%- if packed_field.min_version > last_checked_version %} |
-{%- set last_checked_version = packed_field.min_version %} |
- if (object->header_.version < {{packed_field.min_version}}) |
- return true; |
-{%- endif %} |
-{%- if kind|is_object_kind %} |
-{{_validate_object(struct, packed_field)}} |
-{%- elif kind|is_any_handle_kind or kind|is_interface_kind %} |
-{{_validate_handle(struct, packed_field)}} |
-{%- else %} |
-{{_validate_associated(struct, packed_field)}} |
-{%- endif %} |
-{%- endif %} |
-{%- endfor %} |
- |
- return true; |
-} |
- |
-void {{class_name}}::EncodePointersAndHandles( |
- std::vector<mojo::Handle>* handles) { |
- MOJO_CHECK(header_.version == {{struct.versions[-1].version}}); |
-{%- for pf in struct.packed.packed_fields_in_ordinal_order %} |
-{%- if pf.field.kind|is_union_kind %} |
- {{pf.field.name}}.EncodePointersAndHandles(handles); |
-{%- elif pf.field.kind|is_object_kind %} |
- mojo::internal::Encode(&{{pf.field.name}}, handles); |
-{%- elif pf.field.kind|is_any_handle_kind or pf.field.kind|is_interface_kind %} |
- mojo::internal::EncodeHandle(&{{pf.field.name}}, handles); |
-{%- endif %} |
-{%- endfor %} |
-} |
- |
-void {{class_name}}::DecodePointersAndHandles( |
- std::vector<mojo::Handle>* handles) { |
- // NOTE: The memory backing |this| may has be smaller than |sizeof(*this)|, if |
- // the message comes from an older version. |
-{#- Before decoding fields introduced at a certain version, we need to add |
- a version check, which makes sure we skip further decoding if |this| |
- is from an earlier version. |last_checked_version| records the last |
- version that we have added such version check. #} |
-{%- set last_checked_version = 0 %} |
-{%- for pf in struct.packed.packed_fields_in_ordinal_order %} |
-{%- set name = pf.field.name %} |
-{%- set kind = pf.field.kind %} |
-{%- if kind|is_object_kind or kind|is_any_handle_kind or kind|is_interface_kind %} |
-{%- if pf.min_version > last_checked_version %} |
-{%- set last_checked_version = pf.min_version %} |
- if (header_.version < {{pf.min_version}}) |
- return; |
-{%- endif %} |
-{%- if kind|is_union_kind %} |
- {{name}}.DecodePointersAndHandles(handles); |
-{%- elif kind|is_object_kind %} |
- mojo::internal::Decode(&{{name}}, handles); |
-{%- else %} |
- mojo::internal::DecodeHandle(&{{name}}, handles); |
-{%- endif %} |
-{%- endif %} |
-{%- endfor %} |
-} |
- |
-{{class_name}}::{{class_name}}() { |
- header_.num_bytes = sizeof(*this); |
- header_.version = {{struct.versions[-1].version}}; |
-} |