• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1class {{union.name}}DataView {
2 public:
3  using Tag = internal::{{union.name}}_Data::{{union.name}}_Tag;
4
5  {{union.name}}DataView() {}
6
7  {{union.name}}DataView(
8      internal::{{union.name}}_Data* data,
9      mojo::internal::SerializationContext* context)
10{%- if union|requires_context_for_data_view %}
11      : data_(data), context_(context) {}
12{%- else %}
13      : data_(data) {}
14{%- endif %}
15
16  bool is_null() const {
17    // For inlined unions, |data_| is always non-null. In that case we need to
18    // check |data_->is_null()|.
19    return !data_ || data_->is_null();
20  }
21
22  Tag tag() const { return data_->tag; }
23
24{%- for field in union.fields %}
25{%-   set kind = field.kind %}
26{%-   set name = field.name %}
27  bool is_{{name}}() const { return data_->tag == Tag::{{name|upper}}; }
28
29{%-   if kind|is_object_kind %}
30  inline void Get{{name|under_to_camel}}DataView(
31      {{kind|cpp_data_view_type}}* output);
32
33  template <typename UserType>
34  WARN_UNUSED_RESULT bool Read{{name|under_to_camel}}(UserType* output) {
35    DCHECK(is_{{name}}());
36    return mojo::internal::Deserialize<{{kind|unmapped_type_for_serializer}}>(
37        data_->data.f_{{name}}.Get(), output, context_);
38  }
39
40{%-   elif kind|is_enum_kind %}
41  template <typename UserType>
42  WARN_UNUSED_RESULT bool Read{{name|under_to_camel}}(UserType* output) const {
43    DCHECK(is_{{name}}());
44    return mojo::internal::Deserialize<{{kind|unmapped_type_for_serializer}}>(
45        data_->data.f_{{name}}, output);
46  }
47
48  {{kind|cpp_data_view_type}} {{name}}() const {
49    DCHECK(is_{{name}}());
50    return static_cast<{{kind|cpp_data_view_type}}>(
51        data_->data.f_{{name}});
52  }
53
54{%-   elif kind|is_any_handle_kind %}
55  {{kind|cpp_data_view_type}} Take{{name|under_to_camel}}() {
56    DCHECK(is_{{name}}());
57    {{kind|cpp_data_view_type}} result;
58    bool ret =
59        mojo::internal::Deserialize<{{kind|unmapped_type_for_serializer}}>(
60            &data_->data.f_{{name}}, &result, context_);
61    DCHECK(ret);
62    return result;
63  }
64
65{%-   elif kind|is_any_interface_kind %}
66  template <typename UserType>
67  UserType Take{{name|under_to_camel}}() {
68    DCHECK(is_{{name}}());
69    UserType result;
70    bool ret =
71        mojo::internal::Deserialize<{{kind|unmapped_type_for_serializer}}>(
72            &data_->data.f_{{name}}, &result, context_);
73    DCHECK(ret);
74    return result;
75  }
76
77{%-   else %}
78  {{kind|cpp_data_view_type}} {{name}}() const {
79    DCHECK(is_{{name}}());
80    return data_->data.f_{{name}};
81  }
82
83{%-   endif %}
84{%- endfor %}
85
86 private:
87  internal::{{union.name}}_Data* data_ = nullptr;
88{%- if union|requires_context_for_data_view %}
89  mojo::internal::SerializationContext* context_ = nullptr;
90{%- endif %}
91};
92
93