OLD | NEW |
1 {%- set class_name = union.name ~ "_Data" -%} | 1 {%- set class_name = union.name ~ "_Data" -%} |
2 {%- set enum_name = union.name ~ "_Tag" -%} | 2 {%- set enum_name = union.name ~ "_Tag" -%} |
3 {%- import "struct_macros.tmpl" as struct_macros %} | 3 {%- import "struct_macros.tmpl" as struct_macros %} |
4 | 4 |
5 class {{class_name}} { | 5 class {{class_name}} { |
6 public: | 6 public: |
| 7 // Used to identify Mojom Union Data Classes. |
| 8 typedef void MojomUnionDataType; |
7 static {{class_name}}* New(mojo::internal::Buffer* buf); | 9 static {{class_name}}* New(mojo::internal::Buffer* buf); |
| 10 {{class_name}}(); |
| 11 // Do nothing in the destructor since it won't be called. |
| 12 ~{{class_name}}() {} |
8 | 13 |
9 static bool Validate(const void* data, | 14 static bool Validate(const void* data, |
10 mojo::internal::BoundsChecker* bounds_checker); | 15 mojo::internal::BoundsChecker* bounds_checker, |
| 16 bool inlined); |
| 17 |
| 18 bool is_null() const { |
| 19 return size == 0; |
| 20 } |
| 21 |
| 22 void set_null(); |
11 | 23 |
12 enum class {{enum_name}} : uint32_t { | 24 enum class {{enum_name}} : uint32_t { |
13 {% for field in union.fields %} | 25 {% for field in union.fields %} |
14 {{field.name|upper}}, | 26 {{field.name|upper}}, |
15 {%- endfor %} | 27 {%- endfor %} |
16 }; | 28 }; |
17 | 29 |
18 // A note on layout: | 30 // A note on layout: |
19 // "Each non-static data member is allocated as if it were the sole member of | 31 // "Each non-static data member is allocated as if it were the sole member of |
20 // a struct." - Section 9.5.2 ISO/IEC 14882:2011 (The C++ Spec) | 32 // a struct." - Section 9.5.2 ISO/IEC 14882:2011 (The C++ Spec) |
21 union MOJO_ALIGNAS(8) Union_ { | 33 union MOJO_ALIGNAS(8) Union_ { |
22 {%- for field in union.fields %} | 34 {%- for field in union.fields %} |
23 {%- if field.kind|is_string_kind %} | 35 {%- if field.kind|is_string_kind %} |
24 uint64_t f_{{field.name}}; | 36 uint64_t f_{{field.name}}; |
25 {%- elif field.kind.spec == 'b' %} | 37 {%- elif field.kind.spec == 'b' %} |
26 uint8_t f_{{field.name}} : 1; | 38 uint8_t f_{{field.name}} : 1; |
27 {%- elif field.kind|is_enum_kind %} | 39 {%- elif field.kind|is_enum_kind %} |
28 int32_t f_{{field.name}}; | 40 int32_t f_{{field.name}}; |
29 {%- else %} | 41 {%- else %} |
30 {{field.kind|cpp_pod_type}} f_{{field.name}}; | 42 {{field.kind|cpp_pod_type}} f_{{field.name}}; |
31 {%- endif %} | 43 {%- endif %} |
32 {%- endfor %} | 44 {%- endfor %} |
33 uint64_t unknown; | 45 uint64_t unknown; |
34 }; | 46 }; |
35 | 47 |
36 uint32_t reserved; | 48 uint32_t size; |
37 {{enum_name}} tag; | 49 {{enum_name}} tag; |
38 Union_ data; | 50 Union_ data; |
39 | 51 |
40 void EncodePointersAndHandles(std::vector<mojo::Handle>* handles); | 52 void EncodePointersAndHandles(std::vector<mojo::Handle>* handles); |
41 void DecodePointersAndHandles(std::vector<mojo::Handle>* handles); | 53 void DecodePointersAndHandles(std::vector<mojo::Handle>* handles); |
42 | |
43 private: | |
44 {{class_name}}(); | |
45 ~{{class_name}}() = delete; | |
46 }; | 54 }; |
47 static_assert(sizeof({{class_name}}) == 16, | 55 static_assert(sizeof({{class_name}}) == 16, |
48 "Bad sizeof({{class_name}})"); | 56 "Bad sizeof({{class_name}})"); |
49 static_assert(sizeof({{class_name}}::Union_) == 8, | 57 static_assert(sizeof({{class_name}}::Union_) == 8, |
50 "Bad sizeof({{class_name}}::Union_)"); | 58 "Bad sizeof({{class_name}}::Union_)"); |
OLD | NEW |