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

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, 10 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 static {{class_name}}* New(mojo::internal::Buffer* buf); 7 static {{class_name}}* New(mojo::internal::Buffer* buf);
8 {{class_name}}();
9 // Do nothing in the destructor since it won't be called.
10 ~{{class_name}}() {}
8 11
9 static bool Validate(const void* data, 12 static bool Validate(const void* data,
10 mojo::internal::BoundsChecker* bounds_checker); 13 mojo::internal::BoundsChecker* bounds_checker,
14 bool claim_memory);
yzshen1 2015/02/17 19:34:35 I think it seems nicer to name it |inlined| becaus
azani 2015/02/18 00:27:57 Done.
15
16 static bool Validate(const void* data,
17 mojo::internal::BoundsChecker* bounds_checker) {
yzshen1 2015/02/17 19:34:35 Maybe we should remove this, so that if we don't h
azani 2015/02/18 00:27:57 Done.
18 return Validate(data, bounds_checker, true);
19 }
20
21 bool is_null() const {
yzshen1 2015/02/17 19:34:35 This is not correct: when the union is non-inlined
azani 2015/02/18 00:27:57 I think I had a different understanding of the out
22 return (flags & 0x1) == 0;
23 }
11 24
12 enum class {{enum_name}} : uint32_t { 25 enum class {{enum_name}} : uint32_t {
13 {% for field in union.fields %} 26 {% for field in union.fields %}
14 {{field.name|upper}}, 27 {{field.name|upper}},
15 {%- endfor %} 28 {%- endfor %}
16 }; 29 };
17 30
18 // A note on layout: 31 // A note on layout:
19 // "Each non-static data member is allocated as if it were the sole member of 32 // "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) 33 // a struct." - Section 9.5.2 ISO/IEC 14882:2011 (The C++ Spec)
21 union MOJO_ALIGNAS(8) Union_ { 34 union MOJO_ALIGNAS(8) Union_ {
22 {%- for field in union.fields %} 35 {%- for field in union.fields %}
23 {%- if field.kind|is_string_kind %} 36 {%- if field.kind|is_string_kind %}
24 uint64_t f_{{field.name}}; 37 uint64_t f_{{field.name}};
25 {%- elif field.kind.spec == 'b' %} 38 {%- elif field.kind.spec == 'b' %}
26 uint8_t f_{{field.name}} : 1; 39 uint8_t f_{{field.name}} : 1;
27 {%- elif field.kind|is_enum_kind %} 40 {%- elif field.kind|is_enum_kind %}
28 int32_t f_{{field.name}}; 41 int32_t f_{{field.name}};
29 {%- else %} 42 {%- else %}
30 {{field.kind|cpp_pod_type}} f_{{field.name}}; 43 {{field.kind|cpp_pod_type}} f_{{field.name}};
31 {%- endif %} 44 {%- endif %}
32 {%- endfor %} 45 {%- endfor %}
33 uint64_t unknown; 46 uint64_t unknown;
34 }; 47 };
35 48
36 uint32_t reserved; 49 uint32_t flags;
yzshen1 2015/02/17 19:34:35 The name |flags| seems confusing. It is the size o
azani 2015/02/18 00:27:57 See above. I thought we were just reserving the sp
37 {{enum_name}} tag; 50 {{enum_name}} tag;
38 Union_ data; 51 Union_ data;
39 52
40 void EncodePointersAndHandles(std::vector<mojo::Handle>* handles); 53 void EncodePointersAndHandles(std::vector<mojo::Handle>* handles);
41 void DecodePointersAndHandles(std::vector<mojo::Handle>* handles); 54 void DecodePointersAndHandles(std::vector<mojo::Handle>* handles);
42
43 private:
44 {{class_name}}();
45 ~{{class_name}}() = delete;
46 }; 55 };
47 static_assert(sizeof({{class_name}}) == 16, 56 static_assert(sizeof({{class_name}}) == 16,
48 "Bad sizeof({{class_name}})"); 57 "Bad sizeof({{class_name}})");
49 static_assert(sizeof({{class_name}}::Union_) == 8, 58 static_assert(sizeof({{class_name}}::Union_) == 8,
50 "Bad sizeof({{class_name}}::Union_)"); 59 "Bad sizeof({{class_name}}::Union_)");
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698