Index: mojo/public/tools/bindings/generators/cpp_templates/wrapper_union_class_definition.tmpl |
diff --git a/mojo/public/tools/bindings/generators/cpp_templates/wrapper_union_class_definition.tmpl b/mojo/public/tools/bindings/generators/cpp_templates/wrapper_union_class_definition.tmpl |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bab528c65ff8b39f0addae2d7396857d964a6b45 |
--- /dev/null |
+++ b/mojo/public/tools/bindings/generators/cpp_templates/wrapper_union_class_definition.tmpl |
@@ -0,0 +1,96 @@ |
+// static |
+{{union.name}}Ptr {{union.name}}::New() { |
+ {{union.name}}Ptr rv; |
+ mojo::internal::StructHelper<{{union.name}}>::Initialize(&rv); |
+ return rv.Pass(); |
+} |
+ |
+{{union.name}}::{{union.name}}() { |
+ // TODO(azani): Implement default values here when/if we support them. |
+ // TODO(azani): Set to UNKNOWN when unknown is implemented. |
+ SetActive(static_cast<Tag>(0)); |
+} |
+ |
+{{union.name}}::~{{union.name}}() { |
+ DestroyActive(); |
+} |
+ |
+{% if union|is_cloneable_kind %} |
+{{union.name}}Ptr {{union.name}}::Clone() const { |
+ {{union.name}}Ptr rv(New()); |
+ switch (tag_) { |
+{% for field in union.fields %} |
+ case Tag::{{field.name|upper}}: |
+ rv->set_{{field.name}}(data_.{{field.name}}); |
+ break; |
+{%- endfor %} |
+ }; |
+ return rv.Pass(); |
+} |
+{%- endif %} |
+ |
+bool {{union.name}}::Equals(const {{union.name}}& other) const { |
+ if (tag_ != other.which()) { |
+ return false; |
+ } |
+ |
+ switch (tag_) { |
+{% for field in union.fields %} |
+ case Tag::{{field.name|upper}}: |
+ return mojo::internal::ValueTraits<{{field.kind|cpp_wrapper_type}}>::Equals(data_.{{field.name}}, other.get_{{field.name}}()); |
+{%- endfor %} |
+ }; |
+ |
+ return false; |
+} |
+ |
+{% for field in union.fields %} |
+bool {{union.name}}::is_{{field.name}}() const { |
+ return tag_ == Tag::{{field.name|upper}}; |
+} |
+ |
+{{field.kind|cpp_result_type}} {{union.name}}::get_{{field.name}}() const { |
+ MOJO_DCHECK(tag_ == Tag::{{field.name|upper}}); |
+ return data_.{{field.name}}; |
+} |
+ |
+void {{union.name}}::set_{{field.name}}({{field.kind|cpp_const_wrapper_type}} {{field.name}}) { |
+ SwitchActive(Tag::{{field.name|upper}}); |
+ data_.{{field.name}} = {{field.name}}; |
+} |
+{%- endfor %} |
+ |
+void {{union.name}}::SwitchActive(Tag new_active) { |
+ if (new_active == tag_) { |
+ return; |
+ } |
+ |
+ DestroyActive(); |
+ SetActive(new_active); |
+} |
+ |
+void {{union.name}}::SetActive(Tag new_active) { |
+ switch (new_active) { |
+{% for field in union.fields %} |
+ case Tag::{{field.name|upper}}: |
+{% if field.kind|is_string_kind %} |
+ new (&data_.{{field.name}}) String(); |
+{%- endif %} |
+ break; |
+{%- endfor %} |
+ } |
+ |
+ tag_ = new_active; |
+} |
+ |
+void {{union.name}}::DestroyActive() { |
+ switch (tag_) { |
+{% for field in union.fields %} |
+ case Tag::{{field.name|upper}}: |
+{% if field.kind|is_string_kind %} |
+ data_.{{field.name}}.~String(); |
+{%- endif %} |
+ break; |
+{%- endfor %} |
+ } |
+} |