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

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

Issue 2694843003: Add field-initializing factory static methods to generated mojo unions. (Closed)
Patch Set: Created 3 years, 9 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 class {{export_attribute}} {{union.name}} { 1 class {{export_attribute}} {{union.name}} {
2 public: 2 public:
3 using DataView = {{union.name}}DataView; 3 using DataView = {{union.name}}DataView;
4 using Data_ = internal::{{union.name}}_Data; 4 using Data_ = internal::{{union.name}}_Data;
5 using Tag = Data_::{{union.name}}_Tag; 5 using Tag = Data_::{{union.name}}_Tag;
6 6
7 static {{union.name}}Ptr New(); 7 struct TagTypes_ {
8 {%- for field in union.fields %}
9 using {{field.name|under_to_camel}} = std::integral_constant<Tag, Tag::{{fie ld.name|upper}}>;
10 {%- endfor %}
11 };
12 // Tag values that can be passed to the constructor to specify which field
13 // should set at construction.
14 {%- for field in union.fields %}
15 static constexpr TagTypes_::{{field.name|under_to_camel}} {{field.name}} = {};
16 {%- endfor %}
17
18 template <typename... Args>
19 static {{union.name}}Ptr New(Args&&... args) {
20 return {{union.name}}Ptr(base::in_place, std::forward<Args>(args)...);
21 }
8 22
9 template <typename U> 23 template <typename U>
10 static {{union.name}}Ptr From(const U& u) { 24 static {{union.name}}Ptr From(const U& u) {
11 return mojo::TypeConverter<{{union.name}}Ptr, U>::Convert(u); 25 return mojo::TypeConverter<{{union.name}}Ptr, U>::Convert(u);
12 } 26 }
13 27
14 template <typename U> 28 template <typename U>
15 U To() const { 29 U To() const {
16 return mojo::TypeConverter<U, {{union.name}}>::Convert(*this); 30 return mojo::TypeConverter<U, {{union.name}}>::Convert(*this);
17 } 31 }
18 32
19 {{union.name}}(); 33 {{union.name}}();
34 explicit {{union.name}}(Tag tag);
yzshen1 2017/03/07 17:13:43 With the current constructors, users would do: F
35 {% for field in union.fields %}
36
37 // Construct an instance holding |{{field.name}}|.
38 // Pass |{{union.name}}::{{field.name}}| to to match |TagTypes_::{{field.name| under_to_camel}}|.
39 {%- if union|is_distinct_kind(field) %}
40 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
41 : {{union.name}}({{field.name}}, std::move(in)) {}
42 {%- endif %}
43 {%- if field.kind|is_object_kind %}
44 {{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
45 {%- endif %}
46 {{union.name}}(TagTypes_::{{field.name|under_to_camel}}, {{field.kind|cpp_wrap per_param_type}} in);
47 {%- endfor %}
20 ~{{union.name}}(); 48 ~{{union.name}}();
21 49
22 // Clone() is a template so it is only instantiated if it is used. Thus, the 50 // Clone() is a template so it is only instantiated if it is used. Thus, the
23 // bindings generator does not need to know whether Clone() or copy 51 // bindings generator does not need to know whether Clone() or copy
24 // constructor/assignment are available for members. 52 // constructor/assignment are available for members.
25 template <typename UnionPtrType = {{union.name}}Ptr> 53 template <typename UnionPtrType = {{union.name}}Ptr>
26 {{union.name}}Ptr Clone() const; 54 {{union.name}}Ptr Clone() const;
27 55
28 // Equals() is a template so it is only instantiated if it is used. Thus, the 56 // Equals() is a template so it is only instantiated if it is used. Thus, the
29 // bindings generator does not need to know whether Equals() or == operator 57 // bindings generator does not need to know whether Equals() or == operator
(...skipping 21 matching lines...) Expand all
51 return *(data_.{{field.name}}); 79 return *(data_.{{field.name}});
52 {%- else %} 80 {%- else %}
53 return data_.{{field.name}}; 81 return data_.{{field.name}};
54 {%- endif %} 82 {%- endif %}
55 } 83 }
56 84
57 void set_{{field.name}}({{field.kind|cpp_wrapper_param_type}} {{field.name}}); 85 void set_{{field.name}}({{field.kind|cpp_wrapper_param_type}} {{field.name}});
58 {%- endfor %} 86 {%- endfor %}
59 87
60 private: 88 private:
61 friend class mojo::internal::UnionAccessor<{{union.name}}>;
62 union Union_ { 89 union Union_ {
63 Union_() {} 90 Union_() {}
64 ~Union_() {} 91 ~Union_() {}
65 92
66 {%- for field in union.fields %} 93 {%- for field in union.fields %}
67 {%- if field.kind|is_object_kind or 94 {%- if field.kind|is_object_kind or
68 field.kind|is_any_handle_or_interface_kind %} 95 field.kind|is_any_handle_or_interface_kind %}
69 {{field.kind|cpp_wrapper_type}}* {{field.name}}; 96 {{field.kind|cpp_wrapper_type}}* {{field.name}};
70 {%- else %} 97 {%- else %}
71 {{field.kind|cpp_wrapper_type}} {{field.name}}; 98 {{field.kind|cpp_wrapper_type}} {{field.name}};
72 {%- endif %} 99 {%- endif %}
73 {%- endfor %} 100 {%- endfor %}
74 }; 101 };
75 void SwitchActive(Tag new_active);
76 void SetActive(Tag new_active);
77 void DestroyActive(); 102 void DestroyActive();
78 Tag tag_; 103 Tag tag_ = static_cast<Tag>(-1);
79 Union_ data_; 104 Union_ data_;
80 }; 105 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698