summaryrefslogtreecommitdiff
path: root/mojo/public/tools/bindings/generators/cpp_templates/module-shared.h.tmpl
blob: 148dc7aeb512489de3686b7ca07f3e9b2c94ac99 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

{%- set header_guard = "%s_SHARED_H_"|format(
        module.path|upper|replace("/","_")|replace(".","_")|
            replace("-", "_")) %}

{%- macro mojom_type_traits(kind) %}
template <>
struct MojomTypeTraits<{{kind|get_qualified_name_for_kind}}DataView> {
  using Data = {{kind|get_qualified_name_for_kind(internal=True)}};
{%-  if kind|is_union_kind %}
  using DataAsArrayElement = Data;
  static constexpr MojomTypeCategory category = MojomTypeCategory::UNION;
{%-  else %}
  using DataAsArrayElement = Pointer<Data>;
  static constexpr MojomTypeCategory category = MojomTypeCategory::STRUCT;
{%-  endif %}
};
{%- endmacro %}

{%- macro namespace_begin() %}
{%-   for namespace in namespaces_as_array %}
namespace {{namespace}} {
{%-   endfor %}
{%- endmacro %}

{%- macro namespace_end() %}
{%-   for namespace in namespaces_as_array|reverse %}
}  // namespace {{namespace}}
{%-   endfor %}
{%- endmacro %}

#ifndef {{header_guard}}
#define {{header_guard}}

#include <stdint.h>

#include <functional>
#include <ostream>
#include <type_traits>
#include <utility>

#include "base/compiler_specific.h"
#include "mojo/public/cpp/bindings/array_data_view.h"
#include "mojo/public/cpp/bindings/enum_traits.h"
#include "mojo/public/cpp/bindings/interface_data_view.h"
#include "mojo/public/cpp/bindings/lib/bindings_internal.h"
#include "mojo/public/cpp/bindings/lib/serialization.h"
#include "mojo/public/cpp/bindings/map_data_view.h"
#include "mojo/public/cpp/bindings/native_enum.h"
#include "mojo/public/cpp/bindings/native_struct_data_view.h"
#include "mojo/public/cpp/bindings/string_data_view.h"
#include "{{module.path}}-shared-internal.h"
{%- for import in imports %}
#include "{{import.path}}-shared.h"
{%- endfor %}

{%- if export_header %}
#include "{{export_header}}"
{%- endif %}

{{namespace_begin()}}

{#--- Struct Forward Declarations -#}
{%- for struct in structs %}
{%-   if struct|is_native_only_kind %}
using {{struct.name}}DataView = mojo::NativeStructDataView;
{%-   else %}
class {{struct.name}}DataView;
{%-   endif %}
{%  endfor %}

{#--- Union Forward Declarations -#}
{%- for union in unions %}
class {{union.name}}DataView;
{%- endfor %}

{{namespace_end()}}

namespace mojo {
namespace internal {

{%- for struct in structs %}
{%-   if not struct|is_native_only_kind %}
{{mojom_type_traits(struct)}}
{%-   endif %}
{%- endfor %}

{%- for union in unions %}
{{mojom_type_traits(union)}}
{%- endfor %}

}  // namespace internal
}  // namespace mojo

{{namespace_begin()}}

{#--- Enums #}
{%- from "enum_macros.tmpl" import enum_decl%}
{%- for enum in all_enums %}
{%-   if enum|is_native_only_kind %}
using {{enum|get_name_for_kind(flatten_nested_kind=True)}} = mojo::NativeEnum;
{%-   else %}
{{enum_decl(enum)}}
{%-   endif %}
{%- endfor %}

{#--- Interfaces #}
{%- if interfaces %}
// Interface base classes. They are used for type safety check.
{%- endif %}
{%- for interface in interfaces %}
class {{interface.name}}InterfaceBase {};

using {{interface.name}}PtrDataView =
    mojo::InterfacePtrDataView<{{interface.name}}InterfaceBase>;
using {{interface.name}}RequestDataView =
    mojo::InterfaceRequestDataView<{{interface.name}}InterfaceBase>;
using {{interface.name}}AssociatedPtrInfoDataView =
    mojo::AssociatedInterfacePtrInfoDataView<{{interface.name}}InterfaceBase>;
using {{interface.name}}AssociatedRequestDataView =
    mojo::AssociatedInterfaceRequestDataView<{{interface.name}}InterfaceBase>;

{%- endfor %}

{#--- Structs #}
{%- for struct in structs %}
{%-   if not struct|is_native_only_kind %}
{%      include "struct_data_view_declaration.tmpl" %}
{%-   endif %}
{%- endfor %}

{#--- Interface parameter definitions #}
{%- for interface in interfaces %}
{%-   for method in interface.methods %}
{%-     set struct = method.param_struct %}
{%      include "struct_data_view_declaration.tmpl" %}
{%-     if method.response_parameters != None %}
{%-       set struct = method.response_param_struct %}
{%        include "struct_data_view_declaration.tmpl" %}
{%-     endif %}
{%-   endfor %}
{%- endfor %}

{#--- Unions #}
{%- for union in unions %}
{%    include "union_data_view_declaration.tmpl" %}
{%- endfor %}

{{namespace_end()}}

namespace std {

{%- from "enum_macros.tmpl" import enum_hash %}
{%- for enum in all_enums %}
{%-   if not enum|is_native_only_kind %}
{{enum_hash(enum)}}
{%-   endif %}
{%- endfor %}

}  // namespace std

namespace mojo {

{#--- Enum Serialization Helpers -#}
{%- for enum in all_enums %}
{%-   if not enum|is_native_only_kind %}
{%      include "enum_serialization_declaration.tmpl" %}
{%-   endif %}
{%- endfor %}

{#--- Struct Serialization Helpers -#}
{%  for struct in structs %}
{%-   if not struct|is_native_only_kind %}
{%      include "struct_serialization_declaration.tmpl" %}
{%-   endif %}
{%- endfor %}

{#--- Union Serialization Helpers -#}
{%  if unions %}
{%-   for union in unions %}
{%      include "union_serialization_declaration.tmpl" %}
{%-   endfor %}
{%- endif %}

}  // namespace mojo

{{namespace_begin()}}

{%- for struct in structs %}
{%-   if not struct|is_native_only_kind %}
{%      include "struct_data_view_definition.tmpl" %}
{%-   endif %}
{%- endfor %}

{%- for interface in interfaces %}
{%-   for method in interface.methods %}
{%-     set struct = method.param_struct %}
{%      include "struct_data_view_definition.tmpl" %}
{%-     if method.response_parameters != None %}
{%-       set struct = method.response_param_struct %}
{%        include "struct_data_view_definition.tmpl" %}
{%-     endif %}
{%-   endfor %}
{%- endfor %}

{%- for union in unions %}
{%    include "union_data_view_definition.tmpl" %}
{%- endfor %}

{{namespace_end()}}

#endif  // {{header_guard}}