aboutsummaryrefslogtreecommitdiff
path: root/mojo/public/tools/bindings/generators/cpp_templates/wrapper_class_declaration.tmpl
blob: 7ad9b4e1bc3b2dc5ede948c53c3b0daff86cfd77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
class {{export_attribute}} {{struct.name}} {
 public:
  using DataView = {{struct.name}}DataView;
  using Data_ = internal::{{struct.name}}_Data;

{#--- Enums #}
{%- for enum in struct.enums -%}
  using {{enum.name}} = {{enum|get_name_for_kind(flatten_nested_kind=True)}};
{%- endfor %}

{#--- Constants #}
{%- for constant in struct.constants %}
  static {{constant|format_constant_declaration(nested=True)}};
{%- endfor %}

  template <typename... Args>
  static {{struct.name}}Ptr New(Args&&... args) {
    return {{struct.name}}Ptr(
        base::in_place,
        std::forward<Args>(args)...);
  }

  template <typename U>
  static {{struct.name}}Ptr From(const U& u) {
    return mojo::TypeConverter<{{struct.name}}Ptr, U>::Convert(u);
  }

  template <typename U>
  U To() const {
    return mojo::TypeConverter<U, {{struct.name}}>::Convert(*this);
  }

{% for constructor in struct|struct_constructors %}
  {% if constructor.params|length == 1 %}explicit {% endif %}{{struct.name}}(
{%-   for field in constructor.params %}
{%-     set type = field.kind|cpp_wrapper_param_type %}
{%-     set name = field.name %}
      {{type}} {{name}}
{%-     if not loop.last -%},{%- endif %}
{%-   endfor %});
{% endfor %}
  ~{{struct.name}}();

  // Clone() is a template so it is only instantiated if it is used. Thus, the
  // bindings generator does not need to know whether Clone() or copy
  // constructor/assignment are available for members.
  template <typename StructPtrType = {{struct.name}}Ptr>
  {{struct.name}}Ptr Clone() const;

  // Equals() is a template so it is only instantiated if it is used. Thus, the
  // bindings generator does not need to know whether Equals() or == operator
  // are available for members.
  template <typename T,
            typename std::enable_if<std::is_same<
                T, {{struct.name}}>::value>::type* = nullptr>
  bool Equals(const T& other) const;

{%- if struct|is_hashable %}
  size_t Hash(size_t seed) const;
{%- endif %}

{%- set serialization_result_type = "WTF::Vector<uint8_t>"
        if for_blink else "std::vector<uint8_t>" %}

  template <typename UserType>
  static {{serialization_result_type}} Serialize(UserType* input) {
    return mojo::internal::StructSerializeImpl<
        {{struct.name}}::DataView, {{serialization_result_type}}>(input);
  }

  template <typename UserType>
  static bool Deserialize(const {{serialization_result_type}}& input,
                          UserType* output) {
    return mojo::internal::StructDeserializeImpl<
        {{struct.name}}::DataView, {{serialization_result_type}}>(
            input, output, Validate);
  }

{#--- Struct members #}
{%  for field in struct.fields %}
{%-   set type = field.kind|cpp_wrapper_type %}
{%-   set name = field.name %}
  {{type}} {{name}};
{%- endfor %}

 private:
  static bool Validate(const void* data,
                       mojo::internal::ValidationContext* validation_context);

{%- if struct|contains_move_only_members %}
  DISALLOW_COPY_AND_ASSIGN({{struct.name}});
{%- endif %}
};