Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(534)

Side by Side Diff: mojo/public/tools/bindings/generators/cpp_templates/union_definition.tmpl

Issue 923033003: Implement unions as members of structs. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 {%- import "validation_macros.tmpl" as validation_macros %} 1 {%- import "validation_macros.tmpl" as validation_macros %}
2 {%- set class_name = union.name ~ "_Data" %} 2 {%- set class_name = union.name ~ "_Data" %}
3 {%- set enum_name = union.name ~ "_Tag" -%} 3 {%- set enum_name = union.name ~ "_Tag" -%}
4 4
5 // static 5 // static
6 {{class_name}}* {{class_name}}::New(mojo::internal::Buffer* buf) { 6 {{class_name}}* {{class_name}}::New(mojo::internal::Buffer* buf) {
7 return new (buf->Allocate(sizeof({{class_name}}))) {{class_name}}(); 7 return new (buf->Allocate(sizeof({{class_name}}))) {{class_name}}();
8 } 8 }
9 9
10 // static 10 // static
11 bool {{class_name}}::Validate(const void* data, 11 bool {{class_name}}::Validate(const void* data,
12 mojo::internal::BoundsChecker* bounds_checker) { 12 mojo::internal::BoundsChecker* bounds_checker,
13 bool inlined) {
13 if (!data) { 14 if (!data) {
14 return true; 15 return true;
15 } 16 }
16 17
17 if (!mojo::internal::IsAligned(data)) { 18 if (!mojo::internal::IsAligned(data)) {
18 ReportValidationError(mojo::internal::VALIDATION_ERROR_MISALIGNED_OBJECT); 19 ReportValidationError(mojo::internal::VALIDATION_ERROR_MISALIGNED_OBJECT);
19 return false; 20 return false;
20 } 21 }
21 22
22 if (!bounds_checker->ClaimMemory(data, sizeof({{class_name}}))) { 23 // If the union is inlined in another structure its memory was already claimed .
24 // This ONLY applies to the union itself, NOT anything which the union points
25 // to.
26 if (!inlined && !bounds_checker->ClaimMemory(data, sizeof({{class_name}}))) {
23 ReportValidationError( 27 ReportValidationError(
24 mojo::internal::VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE); 28 mojo::internal::VALIDATION_ERROR_ILLEGAL_MEMORY_RANGE);
25 return false; 29 return false;
26 } 30 }
27 const {{class_name}}* object = static_cast<const {{class_name}}*>(data); 31 const {{class_name}}* object = static_cast<const {{class_name}}*>(data);
28 MOJO_ALLOW_UNUSED_LOCAL(object); 32 MOJO_ALLOW_UNUSED_LOCAL(object);
29 33
30 switch (object->tag) { 34 switch (object->tag) {
31 {% for field in union.fields %} 35 {% for field in union.fields %}
32 case {{enum_name}}::{{field.name|upper}}: 36 case {{enum_name}}::{{field.name|upper}}:
33 {{ validation_macros.validate_union_field(field, union)|indent(6) }} 37 {{ validation_macros.validate_union_field(field, union)|indent(6) }}
34 {%- endfor %} 38 {%- endfor %}
35 default: 39 default:
36 ReportValidationError( 40 ReportValidationError(
37 mojo::internal::VALIDATION_ERROR_UNKNOWN_UNION_TAG, 41 mojo::internal::VALIDATION_ERROR_UNKNOWN_UNION_TAG,
38 "unknown tag in {{union.name}}"); 42 "unknown tag in {{union.name}}");
39 return false; 43 return false;
40 } 44 }
41 } 45 }
42 46
47 void {{class_name}}::set_null() {
48 size = 0U;
49 tag = static_cast<{{enum_name}}>(0);
50 data.unknown = 0U;
51 }
52
43 {{class_name}}::{{class_name}}() { 53 {{class_name}}::{{class_name}}() {
44 } 54 }
45 55
46 void {{class_name}}::EncodePointersAndHandles( 56 void {{class_name}}::EncodePointersAndHandles(
47 std::vector<mojo::Handle>* handles) { 57 std::vector<mojo::Handle>* handles) {
48 // TODO(azani): Implement pointers and handles. 58 // TODO(azani): Implement pointers and handles.
49 } 59 }
50 60
51 void {{class_name}}::DecodePointersAndHandles( 61 void {{class_name}}::DecodePointersAndHandles(
52 std::vector<mojo::Handle>* handles) { 62 std::vector<mojo::Handle>* handles) {
53 // TODO(azani): Implement pointers and handles. 63 // TODO(azani): Implement pointers and handles.
54 } 64 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698