chromium/mojo/public/tools/bindings/generators/cpp_templates/union_declaration.tmpl

{%- macro kythe_annotation(name) %}
{%- if enable_kythe_annotations %}
// @generated_from: {{name}}
{%- endif %}
{%- endmacro %}

{%- set class_name = union.name ~ "_Data" -%}
{%- set enum_name = union.name ~ "_Tag" -%}
{%- import "struct_macros.tmpl" as struct_macros %}

class {{export_attribute}} {{class_name}} {
 public:
  // Used to identify Mojom Union Data Classes.
  typedef void MojomUnionDataType;

  {{class_name}}() = default;
  // Do nothing in the destructor since it won't be called when it is a
  // non-inlined union.
  ~{{class_name}}() = default;

  static bool Validate(const void* data,
                       mojo::internal::ValidationContext* validation_context,
                       bool inlined);

  bool is_null() const { return size == 0; }

  void set_null() {
    size = 0U;
    tag = static_cast<{{enum_name}}>(0);
    data.unknown = 0U;
  }

  // TODO(crbug.com/40731316): SHOUTY_CASE values are being deprecated per C++ code style
  // guidelines (https://google.github.io/styleguide/cppguide.html#Enumerator_Names),
  // please use kCamelCase values instead.  Cleanup NULL_VALUE, BOOL_VALUE, INT_VALUE, etc.
  // generation once codebase is transitioned to kNullValue, kBoolValue, kIntValue, etc.
  enum class {{enum_name}} : uint32_t {
{%  for field in union.fields %}
    {{ kythe_annotation("%s.%s"|format(union|get_full_mojom_name_for_kind(), field.name)) }}
    k{{field.name|under_to_camel}},
{%- endfor %}
  };

  // A note on layout:
  // "Each non-static data member is allocated as if it were the sole member of
  // a struct." - Section 9.5.2 ISO/IEC 14882:2011 (The C++ Spec)
  union MOJO_ALIGNAS(8) Union_ {
    Union_() : unknown(0) {}

{%- for field in union.fields %}
{%-   if field.kind.spec == 'b' %}
    uint8_t f_{{field.name}} : 1;
{%-   else %}
    {{field.kind|cpp_union_field_type}} f_{{field.name}};
{%-   endif %}
{%- endfor %}
    uint64_t unknown;
  };

  uint32_t size;
  {{enum_name}} tag;
  Union_ data;
};
static_assert(sizeof({{class_name}}) == mojo::internal::kUnionDataSize,
              "Bad sizeof({{class_name}})");