Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: mojo/public/tools/bindings/generators/cpp_templates/union_declaration.tmpl

Issue 2226853002: Mojo C++ bindings: add support for UnionTraits. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@85_10_inline_more
Patch Set: . Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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. 7 // Used to identify Mojom Union Data Classes.
8 typedef void MojomUnionDataType; 8 typedef void MojomUnionDataType;
9 9
10 {{class_name}}() {}
11 // Do nothing in the destructor since it won't be called when it is a
12 // non-inlined union.
13 ~{{class_name}}() {}
14
10 static {{class_name}}* New(mojo::internal::Buffer* buf) { 15 static {{class_name}}* New(mojo::internal::Buffer* buf) {
11 return new (buf->Allocate(sizeof({{class_name}}))) {{class_name}}(); 16 return new (buf->Allocate(sizeof({{class_name}}))) {{class_name}}();
12 } 17 }
13 18
14 {{class_name}}();
15 // Do nothing in the destructor since it won't be called.
16 ~{{class_name}}() {}
17
18 static bool Validate(const void* data, 19 static bool Validate(const void* data,
19 mojo::internal::ValidationContext* validation_context, 20 mojo::internal::ValidationContext* validation_context,
20 bool inlined); 21 bool inlined);
21 22
22 bool is_null() const { 23 bool is_null() const { return size == 0; }
23 return size == 0; 24
25 void set_null() {
26 size = 0U;
27 tag = static_cast<{{enum_name}}>(0);
28 data.unknown = 0U;
24 } 29 }
25 30
26 void set_null();
27
28 enum class {{enum_name}} : uint32_t { 31 enum class {{enum_name}} : uint32_t {
29 {% for field in union.fields %} 32 {% for field in union.fields %}
30 {{field.name|upper}}, 33 {{field.name|upper}},
31 {%- endfor %} 34 {%- endfor %}
32 }; 35 };
33 36
34 // A note on layout: 37 // A note on layout:
35 // "Each non-static data member is allocated as if it were the sole member of 38 // "Each non-static data member is allocated as if it were the sole member of
36 // a struct." - Section 9.5.2 ISO/IEC 14882:2011 (The C++ Spec) 39 // a struct." - Section 9.5.2 ISO/IEC 14882:2011 (The C++ Spec)
37 union MOJO_ALIGNAS(8) Union_ { 40 union MOJO_ALIGNAS(8) Union_ {
38 {%- for field in union.fields %} 41 {%- for field in union.fields %}
39 {%- if field.kind.spec == 'b' %} 42 {%- if field.kind.spec == 'b' %}
40 uint8_t f_{{field.name}} : 1; 43 uint8_t f_{{field.name}} : 1;
41 {%- else %} 44 {%- else %}
42 {{field.kind|cpp_union_field_type}} f_{{field.name}}; 45 {{field.kind|cpp_union_field_type}} f_{{field.name}};
43 {%- endif %} 46 {%- endif %}
44 {%- endfor %} 47 {%- endfor %}
45 uint64_t unknown; 48 uint64_t unknown;
46 }; 49 };
47 50
48 uint32_t size; 51 uint32_t size;
49 {{enum_name}} tag; 52 {{enum_name}} tag;
50 Union_ data; 53 Union_ data;
51 }; 54 };
52 static_assert(sizeof({{class_name}}) == mojo::internal::kUnionDataSize, 55 static_assert(sizeof({{class_name}}) == mojo::internal::kUnionDataSize,
53 "Bad sizeof({{class_name}})"); 56 "Bad sizeof({{class_name}})");
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698