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

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

Issue 2226853002: Mojo C++ bindings: add support for UnionTraits. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@85_10_inline_more
Patch Set: . Created 4 years, 4 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 {%- set mojom_type = union|get_qualified_name_for_kind %} 1 {%- set mojom_type = union|get_qualified_name_for_kind %}
2 {%- set data_type = union|get_qualified_name_for_kind(internal=True) %} 2 {%- set data_type = union|get_qualified_name_for_kind(internal=True) %}
3 3
4 template <>
5 struct {{export_attribute}} UnionTraits<{{mojom_type}}, {{mojom_type}}Ptr> {
6 static bool IsNull(const {{mojom_type}}Ptr& input) { return !input; }
7 static void SetToNull({{mojom_type}}Ptr* output) { output->reset(); }
8
9 static {{mojom_type}}::Tag GetTag(const {{mojom_type}}Ptr& input) {
10 return input->which();
11 }
12
13 {%- for field in union.fields %}
14 {%- set return_ref = field.kind|is_object_kind or
15 field.kind|is_any_handle_or_interface_kind %}
16 {%- if return_ref %}
17 static decltype(std::declval<{{mojom_type}}>().get_{{field.name}}())
18 {{field.name}}({{mojom_type}}Ptr& input) {
19 return input->get_{{field.name}}();
20 }
21 {%- else %}
22 static decltype(std::declval<{{mojom_type}}>().get_{{field.name}}())
23 {{field.name}}(const {{mojom_type}}Ptr& input) {
24 return input->get_{{field.name}}();
25 }
26 {%- endif %}
27 {%- endfor %}
28
29 static bool Read({{mojom_type}}DataView input, {{mojom_type}}Ptr* output);
30 };
31
4 namespace internal { 32 namespace internal {
5 33
6 template <typename MojomType> 34 template <typename MaybeConstUserType>
7 struct UnionSerializerImpl; 35 struct Serializer<{{mojom_type}}Ptr, MaybeConstUserType> {
36 using UserType = typename std::remove_const<MaybeConstUserType>::type;
37 using Traits = UnionTraits<{{mojom_type}}, UserType>;
8 38
9 template <> 39 static size_t PrepareToSerialize(MaybeConstUserType& input,
10 struct {{export_attribute}} UnionSerializerImpl<{{mojom_type}}Ptr> {
11 static size_t PrepareToSerialize({{mojom_type}}Ptr& input,
12 bool inlined, 40 bool inlined,
13 SerializationContext* context); 41 SerializationContext* context) {
42 size_t size = inlined ? 0 : sizeof({{data_type}});
43
44 if (CallIsNullIfExists<Traits>(input))
45 return size;
46
47 void* custom_context = CustomContextHelper<Traits>::SetUp(input, context);
48 ALLOW_UNUSED_LOCAL(custom_context);
49
50 switch (CallWithContext(Traits::GetTag, input, custom_context)) {
51 {%- for field in union.fields %}
52 {%- set name = field.name %}
53 case {{mojom_type}}::Tag::{{name|upper}}: {
54 {%- if field.kind|is_object_kind %}
55 {%- set kind = field.kind %}
56 {%- set serializer_type = kind|unmapped_type_for_serializer %}
57 decltype(CallWithContext(Traits::{{name}}, input, custom_context))
58 in_{{name}} = CallWithContext(Traits::{{name}}, input,
59 custom_context);
60 {%- if kind|is_union_kind %}
61 size += mojo::internal::PrepareToSerialize<{{serializer_type}}>(
62 in_{{name}}, false, context);
63 {%- else %}
64 size += mojo::internal::PrepareToSerialize<{{serializer_type}}>(
65 in_{{name}}, context);
66 {%- endif %}
67 {%- endif %}
68 break;
69 }
70 {%- endfor %}
71 }
72 return size;
73 }
14 74
15 static void Serialize({{mojom_type}}Ptr& input, 75 static void Serialize({{mojom_type}}Ptr& input,
16 Buffer* buffer, 76 Buffer* buffer,
17 {{data_type}}** output, 77 {{data_type}}** output,
18 bool inlined, 78 bool inlined,
19 SerializationContext* context); 79 SerializationContext* context) {
80 if (CallIsNullIfExists<Traits>(input)) {
81 if (inlined)
82 (*output)->set_null();
83 else
84 *output = nullptr;
85 return;
86 }
87
88 void* custom_context = CustomContextHelper<Traits>::GetNext(context);
89
90 if (!inlined)
91 *output = {{data_type}}::New(buffer);
92
93 {{data_type}}* result = *output;
94 ALLOW_UNUSED_LOCAL(result);
95 // TODO(azani): Handle unknown and objects.
96 // Set the not-null flag.
97 result->size = kUnionDataSize;
98 result->tag = CallWithContext(Traits::GetTag, input, custom_context);
99 switch (result->tag) {
100 {%- for field in union.fields %}
101 {%- set name = field.name %}
102 {%- set kind = field.kind %}
103 {%- set serializer_type = kind|unmapped_type_for_serializer %}
104 case {{mojom_type}}::Tag::{{field.name|upper}}: {
105 decltype(CallWithContext(Traits::{{name}}, input, custom_context))
106 in_{{name}} = CallWithContext(Traits::{{name}}, input,
107 custom_context);
108 {%- if kind|is_object_kind %}
109 typename decltype(result->data.f_{{name}})::BaseType* ptr;
110 {%- if kind|is_union_kind %}
111 mojo::internal::Serialize<{{serializer_type}}>(
112 in_{{name}}, buffer, &ptr, false, context);
113 {%- elif kind|is_array_kind or kind|is_map_kind %}
114 const ContainerValidateParams {{name}}_validate_params(
115 {{kind|get_container_validate_params_ctor_args|indent(16)}});
116 mojo::internal::Serialize<{{serializer_type}}>(
117 in_{{name}}, buffer, &ptr, &{{name}}_validate_params, context);
118 {%- else %}
119 mojo::internal::Serialize<{{serializer_type}}>(
120 in_{{name}}, buffer, &ptr, context);
121 {%- endif %}
122 result->data.f_{{name}}.Set(ptr);
123 {%- if not kind|is_nullable_kind %}
124 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(
125 !ptr, mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
126 "null {{name}} in {{union.name}} union");
127 {%- endif %}
128
129 {%- elif kind|is_any_handle_or_interface_kind %}
130 mojo::internal::Serialize<{{serializer_type}}>(
131 in_{{name}}, &result->data.f_{{name}}, context);
132 {%- if not kind|is_nullable_kind %}
133 MOJO_INTERNAL_DLOG_SERIALIZATION_WARNING(
134 !mojo::internal::IsHandleOrInterfaceValid(result->data.f_{{name}}),
135 {%- if kind|is_associated_kind %}
136 mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_INTERFACE_ID,
137 {%- else %}
138 mojo::internal::VALIDATION_ERROR_UNEXPECTED_INVALID_HANDLE,
139 {%- endif %}
140 "invalid {{name}} in {{union.name}} union");
141 {%- endif %}
142
143 {%- elif kind|is_enum_kind %}
144 mojo::internal::Serialize<{{serializer_type}}>(
145 in_{{name}}, &result->data.f_{{name}});
146
147 {%- else %}
148 result->data.f_{{name}} = in_{{name}};
149 {%- endif %}
150 break;
151 }
152 {%- endfor %}
153 }
154
155 CustomContextHelper<Traits>::TearDown(input, custom_context);
156 }
20 157
21 static bool Deserialize({{data_type}}* input, 158 static bool Deserialize({{data_type}}* input,
22 {{mojom_type}}Ptr* output, 159 {{mojom_type}}Ptr* output,
23 SerializationContext* context); 160 SerializationContext* context) {
24 }; 161 if (!input || input->is_null())
162 return CallSetToNullIfExists<Traits>(output);
25 163
26 template <typename MaybeConstUserType> 164 {{mojom_type}}DataView data_view(input, context);
27 struct {{export_attribute}} Serializer<{{mojom_type}}Ptr, MaybeConstUserType> 165 return Traits::Read(data_view, output);
28 : public UnionSerializerImpl<{{mojom_type}}Ptr> { 166 }
29 using UserType = typename std::remove_const<MaybeConstUserType>::type;
30
31 static_assert(std::is_same<MaybeConstUserType, UserType>::value,
32 "Only support serialization of non-const Unions.");
33 static_assert(std::is_same<UserType, {{mojom_type}}Ptr>::value,
34 "Custom mapping of mojom union is not supported.");
35 }; 167 };
36 168
37 } // namespace internal 169 } // namespace internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698