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

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

Issue 923033003: Implement unions as members of structs. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 9 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.
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 }
11 21
12 enum class {{enum_name}} : uint32_t { 22 enum class {{enum_name}} : uint32_t {
13 {% for field in union.fields %} 23 {% for field in union.fields %}
14 {{field.name|upper}}, 24 {{field.name|upper}},
15 {%- endfor %} 25 {%- endfor %}
16 }; 26 };
17 27
18 // A note on layout: 28 // A note on layout:
19 // "Each non-static data member is allocated as if it were the sole member of 29 // "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) 30 // a struct." - Section 9.5.2 ISO/IEC 14882:2011 (The C++ Spec)
21 union MOJO_ALIGNAS(8) Union_ { 31 union MOJO_ALIGNAS(8) Union_ {
22 {%- for field in union.fields %} 32 {%- for field in union.fields %}
23 {%- if field.kind|is_string_kind %} 33 {%- if field.kind|is_string_kind %}
24 uint64_t f_{{field.name}}; 34 uint64_t f_{{field.name}};
25 {%- elif field.kind.spec == 'b' %} 35 {%- elif field.kind.spec == 'b' %}
26 uint8_t f_{{field.name}} : 1; 36 uint8_t f_{{field.name}} : 1;
27 {%- elif field.kind|is_enum_kind %} 37 {%- elif field.kind|is_enum_kind %}
28 int32_t f_{{field.name}}; 38 int32_t f_{{field.name}};
29 {%- else %} 39 {%- else %}
30 {{field.kind|cpp_pod_type}} f_{{field.name}}; 40 {{field.kind|cpp_pod_type}} f_{{field.name}};
31 {%- endif %} 41 {%- endif %}
32 {%- endfor %} 42 {%- endfor %}
33 uint64_t unknown; 43 uint64_t unknown;
34 }; 44 };
35 45
36 uint32_t reserved; 46 uint32_t size;
37 {{enum_name}} tag; 47 {{enum_name}} tag;
38 Union_ data; 48 Union_ data;
39 49
40 void EncodePointersAndHandles(std::vector<mojo::Handle>* handles); 50 void EncodePointersAndHandles(std::vector<mojo::Handle>* handles);
41 void DecodePointersAndHandles(std::vector<mojo::Handle>* handles); 51 void DecodePointersAndHandles(std::vector<mojo::Handle>* handles);
42
43 private:
44 {{class_name}}();
45 ~{{class_name}}() = delete;
46 }; 52 };
47 static_assert(sizeof({{class_name}}) == 16, 53 static_assert(sizeof({{class_name}}) == 16,
48 "Bad sizeof({{class_name}})"); 54 "Bad sizeof({{class_name}})");
49 static_assert(sizeof({{class_name}}::Union_) == 8, 55 static_assert(sizeof({{class_name}}::Union_) == 8,
50 "Bad sizeof({{class_name}}::Union_)"); 56 "Bad sizeof({{class_name}}::Union_)");
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698