Chromium Code Reviews| Index: mojo/public/tools/bindings/generators/cpp_templates/wrapper_union_class_declaration.tmpl |
| diff --git a/mojo/public/tools/bindings/generators/cpp_templates/wrapper_union_class_declaration.tmpl b/mojo/public/tools/bindings/generators/cpp_templates/wrapper_union_class_declaration.tmpl |
| index 8b7cf9e6b1dc75485ff26d6184dd777a29cd0163..3ef166b49122a93a406182359e744ca80241bc17 100644 |
| --- a/mojo/public/tools/bindings/generators/cpp_templates/wrapper_union_class_declaration.tmpl |
| +++ b/mojo/public/tools/bindings/generators/cpp_templates/wrapper_union_class_declaration.tmpl |
| @@ -4,7 +4,21 @@ class {{export_attribute}} {{union.name}} { |
| using Data_ = internal::{{union.name}}_Data; |
| using Tag = Data_::{{union.name}}_Tag; |
| - static {{union.name}}Ptr New(); |
| + struct TagTypes_ { |
| +{%- for field in union.fields %} |
| + using {{field.name|under_to_camel}} = std::integral_constant<Tag, Tag::{{field.name|upper}}>; |
| +{%- endfor %} |
| + }; |
| + // Tag values that can be passed to the constructor to specify which field |
| + // should set at construction. |
| +{%- for field in union.fields %} |
| + static constexpr TagTypes_::{{field.name|under_to_camel}} {{field.name}} = {}; |
| +{%- endfor %} |
| + |
| + template <typename... Args> |
| + static {{union.name}}Ptr New(Args&&... args) { |
| + return {{union.name}}Ptr(base::in_place, std::forward<Args>(args)...); |
| + } |
| template <typename U> |
| static {{union.name}}Ptr From(const U& u) { |
| @@ -17,6 +31,20 @@ class {{export_attribute}} {{union.name}} { |
| } |
| {{union.name}}(); |
| + explicit {{union.name}}(Tag tag); |
|
yzshen1
2017/03/07 17:13:43
With the current constructors, users would do:
F
|
| +{% for field in union.fields %} |
| + |
| + // Construct an instance holding |{{field.name}}|. |
| + // Pass |{{union.name}}::{{field.name}}| to to match |TagTypes_::{{field.name|under_to_camel}}|. |
| +{%- if union|is_distinct_kind(field) %} |
| + explicit {{union.name}}({{field.kind|cpp_wrapper_param_type}} in) |
|
yzshen1
2017/03/07 17:13:43
I still think we should avoid this distinct-type-t
|
| + : {{union.name}}({{field.name}}, std::move(in)) {} |
| +{%- endif %} |
| +{%- if field.kind|is_object_kind %} |
| + {{union.name}}(TagTypes_::{{field.name|under_to_camel}}); |
|
yzshen1
2017/03/07 17:13:43
There is already a similar one on line 34. And it
|
| +{%- endif %} |
| + {{union.name}}(TagTypes_::{{field.name|under_to_camel}}, {{field.kind|cpp_wrapper_param_type}} in); |
| +{%- endfor %} |
| ~{{union.name}}(); |
| // Clone() is a template so it is only instantiated if it is used. Thus, the |
| @@ -58,7 +86,6 @@ class {{export_attribute}} {{union.name}} { |
| {%- endfor %} |
| private: |
| - friend class mojo::internal::UnionAccessor<{{union.name}}>; |
| union Union_ { |
| Union_() {} |
| ~Union_() {} |
| @@ -72,9 +99,7 @@ class {{export_attribute}} {{union.name}} { |
| {%- endif %} |
| {%- endfor %} |
| }; |
| - void SwitchActive(Tag new_active); |
| - void SetActive(Tag new_active); |
| void DestroyActive(); |
| - Tag tag_; |
| + Tag tag_ = static_cast<Tag>(-1); |
| Union_ data_; |
| }; |