1// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5{%- set header_guard = "%s_SHARED_H_"|format(
6        module.path|upper|replace("/","_")|replace(".","_")|
7            replace("-", "_")) %}
8
9{%- macro mojom_type_traits(kind) %}
10template <>
11struct MojomTypeTraits<{{kind|get_qualified_name_for_kind}}DataView> {
12  using Data = {{kind|get_qualified_name_for_kind(internal=True)}};
13{%-  if kind|is_union_kind %}
14  using DataAsArrayElement = Data;
15  static constexpr MojomTypeCategory category = MojomTypeCategory::kUnion;
16{%-  else %}
17  using DataAsArrayElement = Pointer<Data>;
18  static constexpr MojomTypeCategory category = MojomTypeCategory::kStruct;
19{%-  endif %}
20};
21{%- endmacro %}
22
23{%- macro namespace_begin() %}
24{%-   for namespace in namespaces_as_array %}
25namespace {{namespace}} {
26{%-   endfor %}
27{%- endmacro %}
28
29{%- macro namespace_end() %}
30{%-   for namespace in namespaces_as_array|reverse %}
31}  // namespace {{namespace}}
32{%-   endfor %}
33{%- endmacro %}
34
35#ifndef {{header_guard}}
36#define {{header_guard}}
37
38#include <stdint.h>
39
40#include <functional>
41#include <ostream>
42#include <type_traits>
43#include <utility>
44
45#include "base/compiler_specific.h"
46#include "base/containers/flat_map.h"
47#include "mojo/public/cpp/bindings/array_data_view.h"
48#include "mojo/public/cpp/bindings/enum_traits.h"
49#include "mojo/public/cpp/bindings/interface_data_view.h"
50#include "mojo/public/cpp/bindings/lib/bindings_internal.h"
51#include "mojo/public/cpp/bindings/lib/serialization.h"
52#include "mojo/public/cpp/bindings/map_data_view.h"
53#include "mojo/public/cpp/bindings/string_data_view.h"
54#include "{{module.path}}-shared-internal.h"
55{%- for import in imports %}
56#include "{{import.path}}-shared.h"
57{%- endfor %}
58
59{#- Avoid transitive includes of interface headers if possible. #}
60{% if not disallow_interfaces and uses_interfaces -%}
61#include "mojo/public/cpp/bindings/lib/interface_serialization.h"
62{%- endif %}
63
64{% if not disallow_native_types and uses_native_types %}
65#include "mojo/public/cpp/bindings/native_enum.h"
66#include "mojo/public/cpp/bindings/lib/native_struct_serialization.h"
67{%- endif %}
68
69{%- if export_header %}
70#include "{{export_header}}"
71{%- endif %}
72
73{% if enable_kythe_annotations -%}
74#ifdef KYTHE_IS_RUNNING
75#pragma kythe_inline_metadata "Metadata comment"
76#endif
77{%- endif %}
78
79{{namespace_begin()}}
80
81{#--- Struct Forward Declarations -#}
82{%- for struct in structs %}
83{%-   if struct|is_native_only_kind %}
84using {{struct.name}}DataView = mojo::native::NativeStructDataView;
85{%-   else %}
86class {{struct.name}}DataView;
87{%-   endif %}
88{%  endfor %}
89
90{#--- Union Forward Declarations -#}
91{%- for union in unions %}
92class {{union.name}}DataView;
93{%- endfor %}
94
95{{namespace_end()}}
96
97namespace mojo {
98namespace internal {
99
100{%- for struct in structs %}
101{%-   if not struct|is_native_only_kind %}
102{{mojom_type_traits(struct)}}
103{%-   endif %}
104{%- endfor %}
105
106{%- for union in unions %}
107{{mojom_type_traits(union)}}
108{%- endfor %}
109
110}  // namespace internal
111}  // namespace mojo
112
113{{namespace_begin()}}
114
115{%- set module_prefix = "%s"|format(namespaces_as_array|join(".")) %}
116
117{#--- Enums #}
118{%- from "enum_macros.tmpl" import enum_decl with context %}
119{%- for enum in all_enums %}
120{%-   if enum|is_native_only_kind %}
121using {{enum|get_name_for_kind(flatten_nested_kind=True)}} = mojo::NativeEnum;
122{%-   else %}
123{{enum_decl(enum, export_attribute)}}
124{%-   endif %}
125{%- endfor %}
126
127{#--- Interfaces #}
128{%- if interfaces %}
129// Interface base classes. They are used for type safety check.
130{%- endif %}
131{%- for interface in interfaces %}
132class {{interface.name}}InterfaceBase {};
133
134using {{interface.name}}PtrDataView =
135    mojo::InterfacePtrDataView<{{interface.name}}InterfaceBase>;
136using {{interface.name}}RequestDataView =
137    mojo::InterfaceRequestDataView<{{interface.name}}InterfaceBase>;
138using {{interface.name}}AssociatedPtrInfoDataView =
139    mojo::AssociatedInterfacePtrInfoDataView<{{interface.name}}InterfaceBase>;
140using {{interface.name}}AssociatedRequestDataView =
141    mojo::AssociatedInterfaceRequestDataView<{{interface.name}}InterfaceBase>;
142
143{%- endfor %}
144
145{#--- Structs #}
146{%- for struct in structs %}
147{%-   if not struct|is_native_only_kind %}
148{%      include "struct_data_view_declaration.tmpl" %}
149{%-   endif %}
150{%- endfor %}
151
152{#--- Unions #}
153{%- for union in unions %}
154{%    include "union_data_view_declaration.tmpl" %}
155{%- endfor %}
156
157{{namespace_end()}}
158
159namespace std {
160
161{%- from "enum_macros.tmpl" import enum_hash %}
162{%- for enum in all_enums %}
163{%-   if not enum|is_native_only_kind %}
164{{enum_hash(enum)}}
165{%-   endif %}
166{%- endfor %}
167
168}  // namespace std
169
170namespace mojo {
171
172{#--- Enum Serialization Helpers -#}
173{%- for enum in all_enums %}
174{%-   if not enum|is_native_only_kind %}
175{%      include "enum_serialization_declaration.tmpl" %}
176{%-   endif %}
177{%- endfor %}
178
179{#--- Struct Serialization Helpers -#}
180{%  for struct in structs %}
181{%-   if not struct|is_native_only_kind %}
182{%      include "struct_serialization_declaration.tmpl" %}
183{%-   endif %}
184{%- endfor %}
185
186{#--- Union Serialization Helpers -#}
187{%  if unions %}
188{%-   for union in unions %}
189{%      include "union_serialization_declaration.tmpl" %}
190{%-   endfor %}
191{%- endif %}
192
193}  // namespace mojo
194
195{{namespace_begin()}}
196
197{%- for struct in structs %}
198{%-   if not struct|is_native_only_kind %}
199{%      include "struct_data_view_definition.tmpl" %}
200{%-   endif %}
201{%- endfor %}
202
203{%- for union in unions %}
204{%    include "union_data_view_definition.tmpl" %}
205{%- endfor %}
206
207{{namespace_end()}}
208
209#endif  // {{header_guard}}
210