• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1class {{export_attribute}} {{struct.name}} {
2 public:
3  using DataView = {{struct.name}}DataView;
4  using Data_ = internal::{{struct.name}}_Data;
5
6{#--- Enums #}
7{%- for enum in struct.enums -%}
8  using {{enum.name}} = {{enum|get_name_for_kind(flatten_nested_kind=True)}};
9{%- endfor %}
10
11{#--- Constants #}
12{%- for constant in struct.constants %}
13  static {{constant|format_constant_declaration(nested=True)}};
14{%- endfor %}
15
16  template <typename... Args>
17  static {{struct.name}}Ptr New(Args&&... args) {
18    return {{struct.name}}Ptr(
19        base::in_place, std::forward<Args>(args)...);
20  }
21
22  template <typename U>
23  static {{struct.name}}Ptr From(const U& u) {
24    return mojo::TypeConverter<{{struct.name}}Ptr, U>::Convert(u);
25  }
26
27  template <typename U>
28  U To() const {
29    return mojo::TypeConverter<U, {{struct.name}}>::Convert(*this);
30  }
31
32{% for constructor in struct|struct_constructors %}
33  {% if constructor.params|length == 1 %}explicit {% endif %}{{struct.name}}(
34{%-   for field in constructor.params %}
35{%-     set type = field.kind|cpp_wrapper_param_type_new %}
36{%-     set name = field.name %}
37      {{type}} {{name}}
38{%-     if not loop.last -%},{%- endif %}
39{%-   endfor %});
40{% endfor %}
41  ~{{struct.name}}();
42
43  // Clone() is a template so it is only instantiated if it is used. Thus, the
44  // bindings generator does not need to know whether Clone() or copy
45  // constructor/assignment are available for members.
46  template <typename StructPtrType = {{struct.name}}Ptr>
47  {{struct.name}}Ptr Clone() const;
48
49  // Equals() is a template so it is only instantiated if it is used. Thus, the
50  // bindings generator does not need to know whether Equals() or == operator
51  // are available for members.
52  template <typename T,
53            typename std::enable_if<std::is_same<
54                T, {{struct.name}}>::value>::type* = nullptr>
55  bool Equals(const T& other) const;
56
57{%- if struct|is_hashable %}
58  size_t Hash(size_t seed) const;
59{%- endif %}
60
61{%- set serialization_result_type = "WTF::Vector<uint8_t>"
62        if for_blink else "std::vector<uint8_t>" %}
63
64  template <typename UserType>
65  static {{serialization_result_type}} Serialize(UserType* input) {
66    return mojo::internal::SerializeImpl<
67        {{struct.name}}::DataView, {{serialization_result_type}}>(input);
68  }
69
70  template <typename UserType>
71  static mojo::Message SerializeAsMessage(UserType* input) {
72    return mojo::internal::SerializeAsMessageImpl<
73        {{struct.name}}::DataView>(input);
74  }
75
76  // The returned Message is serialized only if the message is moved
77  // cross-process or cross-language. Otherwise if the message is Deserialized
78  // as the same UserType |input| will just be moved to |output| in
79  // DeserializeFromMessage.
80  template <typename UserType>
81  static mojo::Message WrapAsMessage(UserType input) {
82    return mojo::Message(std::make_unique<
83        internal::{{struct.name}}_UnserializedMessageContext<
84            UserType, {{struct.name}}::DataView>>(0, 0, std::move(input)));
85  }
86
87  template <typename UserType>
88  static bool Deserialize(const void* data,
89                          size_t data_num_bytes,
90                          UserType* output) {
91    return mojo::internal::DeserializeImpl<{{struct.name}}::DataView>(
92        data, data_num_bytes, std::vector<mojo::ScopedHandle>(), output, Validate);
93  }
94
95  template <typename UserType>
96  static bool Deserialize(const {{serialization_result_type}}& input,
97                          UserType* output) {
98    return {{struct.name}}::Deserialize(
99        input.size() == 0 ? nullptr : &input.front(), input.size(), output);
100  }
101
102  template <typename UserType>
103  static bool DeserializeFromMessage(mojo::Message input,
104                                     UserType* output) {
105    auto context = input.TakeUnserializedContext<
106        internal::{{struct.name}}_UnserializedMessageContext<
107            UserType, {{struct.name}}::DataView>>();
108    if (context) {
109      *output = std::move(context->TakeData());
110      return true;
111    }
112    input.SerializeIfNecessary();
113    return mojo::internal::DeserializeImpl<{{struct.name}}::DataView>(
114        input.payload(), input.payload_num_bytes(),
115        std::move(*input.mutable_handles()), output, Validate);
116  }
117
118{#--- Struct members #}
119{%  for field in struct.fields %}
120{%-   set type = field.kind|cpp_wrapper_type %}
121{%-   set name = field.name %}
122  {{type}} {{name}};
123{%- endfor %}
124
125 private:
126  static bool Validate(const void* data,
127                       mojo::internal::ValidationContext* validation_context);
128
129{%- if struct|contains_move_only_members %}
130  DISALLOW_COPY_AND_ASSIGN({{struct.name}});
131{%- endif %}
132};
133