1class {{export_attribute}} {{union.name}} { 2 public: 3 using DataView = {{union.name}}DataView; 4 using Data_ = internal::{{union.name}}_Data; 5 using Tag = Data_::{{union.name}}_Tag; 6 7 static {{union.name}}Ptr New() { 8 return {{union.name}}Ptr(base::in_place); 9 } 10 11{%- for field in union.fields %} 12 // Construct an instance holding |{{field.name}}|. 13 static {{union.name}}Ptr 14 New{{field.name|under_to_camel}}( 15 {{field.kind|cpp_wrapper_param_type_new}} {{field.name}}) { 16 auto result = {{union.name}}Ptr(base::in_place); 17 result->set_{{field.name}}(std::move({{field.name}})); 18 return result; 19 } 20{%- endfor %} 21 22 template <typename U> 23 static {{union.name}}Ptr From(const U& u) { 24 return mojo::TypeConverter<{{union.name}}Ptr, U>::Convert(u); 25 } 26 27 template <typename U> 28 U To() const { 29 return mojo::TypeConverter<U, {{union.name}}>::Convert(*this); 30 } 31 32 {{union.name}}(); 33 ~{{union.name}}(); 34 35 // Clone() is a template so it is only instantiated if it is used. Thus, the 36 // bindings generator does not need to know whether Clone() or copy 37 // constructor/assignment are available for members. 38 template <typename UnionPtrType = {{union.name}}Ptr> 39 {{union.name}}Ptr Clone() const; 40 41 // Equals() is a template so it is only instantiated if it is used. Thus, the 42 // bindings generator does not need to know whether Equals() or == operator 43 // are available for members. 44 template <typename T, 45 typename std::enable_if<std::is_same< 46 T, {{union.name}}>::value>::type* = nullptr> 47 bool Equals(const T& other) const; 48 49{%- if union|is_hashable %} 50 size_t Hash(size_t seed) const; 51{%- endif %} 52 53 Tag which() const { 54 return tag_; 55 } 56 57{% for field in union.fields %} 58 bool is_{{field.name}}() const { return tag_ == Tag::{{field.name|upper}}; } 59 60 {{field.kind|cpp_union_getter_return_type}} get_{{field.name}}() const { 61 DCHECK(tag_ == Tag::{{field.name|upper}}); 62{%- if field.kind|is_object_kind or 63 field.kind|is_any_handle_or_interface_kind %} 64 return *(data_.{{field.name}}); 65{%- else %} 66 return data_.{{field.name}}; 67{%- endif %} 68 } 69 70 void set_{{field.name}}( 71 {{field.kind|cpp_wrapper_param_type_new}} {{field.name}}); 72{%- endfor %} 73 74 template <typename UserType> 75 static mojo::Message SerializeAsMessage(UserType* input) { 76 return mojo::internal::SerializeAsMessageImpl< 77 {{union.name}}::DataView>(input); 78 } 79 80 template <typename UserType> 81 static bool DeserializeFromMessage(mojo::Message input, 82 UserType* output) { 83 return mojo::internal::DeserializeImpl<{{union.name}}::DataView>( 84 input.payload(), input.payload_num_bytes(), 85 std::move(*input.mutable_handles()), output, Validate); 86 } 87 88 private: 89 union Union_ { 90 Union_() {} 91 ~Union_() {} 92 93{%- for field in union.fields %} 94{%- if field.kind|is_object_kind or 95 field.kind|is_any_handle_or_interface_kind %} 96 {{field.kind|cpp_wrapper_type}}* {{field.name}}; 97{%- else %} 98 {{field.kind|cpp_wrapper_type}} {{field.name}}; 99{%- endif %} 100{%- endfor %} 101 }; 102 103 static bool Validate(const void* data, 104 mojo::internal::ValidationContext* validation_context); 105 106 void DestroyActive(); 107 Tag tag_; 108 Union_ data_; 109}; 110