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