OLD | NEW |
| (Empty) |
1 {%- import "validation_macros.tmpl" as validation_macros %} | |
2 {%- set class_name = union.name ~ "_Data" %} | |
3 {%- set enum_name = union.name ~ "_Tag" -%} | |
4 | |
5 // static | |
6 {{class_name}}* {{class_name}}::New(mojo::internal::Buffer* buf) { | |
7 return new (buf->Allocate(sizeof({{class_name}}))) {{class_name}}(); | |
8 } | |
9 | |
10 {# TODO(vardhan): Set error messages here for the remaining validation | |
11 errors. #} | |
12 // static | |
13 mojo::internal::ValidationError {{class_name}}::Validate( | |
14 const void* data, | |
15 mojo::internal::BoundsChecker* bounds_checker, | |
16 bool inlined, | |
17 std::string* err) { | |
18 if (!data) | |
19 return mojo::internal::ValidationError::NONE; | |
20 | |
21 if (!mojo::internal::IsAligned(data)) { | |
22 MOJO_INTERNAL_DEBUG_SET_ERROR_MSG(err) << ""; | |
23 return mojo::internal::ValidationError::MISALIGNED_OBJECT; | |
24 } | |
25 | |
26 // If the union is inlined in another structure its memory was already claimed
. | |
27 // This ONLY applies to the union itself, NOT anything which the union points | |
28 // to. | |
29 if (!inlined && !bounds_checker->ClaimMemory(data, sizeof({{class_name}}))) { | |
30 MOJO_INTERNAL_DEBUG_SET_ERROR_MSG(err) << ""; | |
31 return mojo::internal::ValidationError::ILLEGAL_MEMORY_RANGE; | |
32 } | |
33 | |
34 const {{class_name}}* object = static_cast<const {{class_name}}*>(data); | |
35 MOJO_ALLOW_UNUSED_LOCAL(object); | |
36 | |
37 if (object->is_null()) | |
38 return mojo::internal::ValidationError::NONE; | |
39 | |
40 switch (object->tag) { | |
41 {% for field in union.fields %} | |
42 case {{enum_name}}::{{field.name|upper}}: { | |
43 {{ validation_macros.validate_union_field(field, union, "err")|indent(8) }} | |
44 } | |
45 {%- endfor %} | |
46 default: | |
47 // Unknown tags should not cause validation to fail. | |
48 break; | |
49 } | |
50 return mojo::internal::ValidationError::NONE; | |
51 } | |
52 | |
53 void {{class_name}}::set_null() { | |
54 size = 0U; | |
55 tag = static_cast<{{enum_name}}>(0); | |
56 data.unknown = 0U; | |
57 } | |
58 | |
59 {{class_name}}::{{class_name}}() { | |
60 } | |
61 | |
62 void {{class_name}}::EncodePointersAndHandles( | |
63 std::vector<mojo::Handle>* handles) { | |
64 switch (tag) { | |
65 {%- for field in union.fields %} | |
66 case {{enum_name}}::{{field.name|upper}}: { | |
67 {%- if field.kind|is_object_kind %} | |
68 mojo::internal::Encode(&data.f_{{field.name}}, handles); | |
69 {%- elif field.kind|is_any_handle_kind %} | |
70 mojo::internal::EncodeHandle(&data.f_{{field.name}}, handles); | |
71 {%- elif field.kind|is_interface_kind %} | |
72 mojo::internal::EncodeHandle( | |
73 reinterpret_cast<mojo::internal::Interface_Data*>( | |
74 &data.f_{{field.name}}), handles); | |
75 {%- endif %} | |
76 return; | |
77 } | |
78 {%- endfor %} | |
79 case {{enum_name}}::__UNKNOWN__: { | |
80 MOJO_DCHECK(false) << "No sane way to serialize a union with an unknown ta
g."; | |
81 break; | |
82 } | |
83 } | |
84 } | |
85 | |
86 void {{class_name}}::DecodePointersAndHandles( | |
87 std::vector<mojo::Handle>* handles) { | |
88 switch (tag) { | |
89 {%- for field in union.fields %} | |
90 case {{enum_name}}::{{field.name|upper}}: { | |
91 {%- if field.kind|is_object_kind %} | |
92 mojo::internal::Decode(&data.f_{{field.name}}, handles); | |
93 {%- elif field.kind|is_any_handle_kind %} | |
94 mojo::internal::DecodeHandle(&data.f_{{field.name}}, handles); | |
95 {%- elif field.kind|is_interface_kind %} | |
96 mojo::internal::DecodeHandle( | |
97 reinterpret_cast<mojo::internal::Interface_Data*>( | |
98 &data.f_{{field.name}}), handles); | |
99 {%- endif %} | |
100 return; | |
101 } | |
102 {%- endfor %} | |
103 default: | |
104 return; | |
105 } | |
106 } | |
OLD | NEW |