| OLD | NEW |
| (Empty) |
| 1 // This file is generated | |
| 2 | |
| 3 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | |
| 4 // Use of this source code is governed by a BSD-style license that can be | |
| 5 // found in the LICENSE file. | |
| 6 | |
| 7 #ifndef {{"_".join(config.protocol.namespace)}}_{{domain.domain}}_h | |
| 8 #define {{"_".join(config.protocol.namespace)}}_{{domain.domain}}_h | |
| 9 | |
| 10 {% if config.protocol.export_header %} | |
| 11 #include {{format_include(config.protocol.export_header)}} | |
| 12 {% endif %} | |
| 13 #include "{{config.protocol.package}}/Protocol.h" | |
| 14 // For each imported domain we generate a ValueConversions struct instead of a f
ull domain definition | |
| 15 // and include Domain::API version from there. | |
| 16 {% for name in domain.dependencies %} | |
| 17 #include "{{config.protocol.package}}/{{name}}.h" | |
| 18 {% endfor %} | |
| 19 {% if domain["has_exports"] %} | |
| 20 #include "{{config.exported.package}}/{{domain.domain}}.h" | |
| 21 {% endif %} | |
| 22 | |
| 23 {% for namespace in config.protocol.namespace %} | |
| 24 namespace {{namespace}} { | |
| 25 {% endfor %} | |
| 26 namespace {{domain.domain}} { | |
| 27 | |
| 28 // ------------- Forward and enum declarations. | |
| 29 {% for type in domain.types %} | |
| 30 {% if type.type == "object" %} | |
| 31 {% if "properties" in type %} | |
| 32 // {{type.description}} | |
| 33 class {{type.id}}; | |
| 34 {% else %} | |
| 35 // {{type.description}} | |
| 36 using {{type.id}} = Object; | |
| 37 {% endif %} | |
| 38 {% elif type.type != "array" %} | |
| 39 // {{type.description}} | |
| 40 using {{type.id}} = {{resolve_type(type).type}}; | |
| 41 {% endif %} | |
| 42 {% endfor %} | |
| 43 {% for type in domain.types %} | |
| 44 {% if "enum" in type %} | |
| 45 | |
| 46 namespace {{type.id}}Enum { | |
| 47 {% for literal in type.enum %} | |
| 48 {{config.protocol.export_macro}} extern const char* {{ literal | dash_to_camelca
se}}; | |
| 49 {% endfor %} | |
| 50 } // namespace {{type.id}}Enum | |
| 51 {% endif %} | |
| 52 {% endfor %} | |
| 53 {% for command in join_arrays(domain, ["commands", "events"]) %} | |
| 54 {% for param in join_arrays(command, ["parameters", "returns"]) %} | |
| 55 {% if "enum" in param %} | |
| 56 | |
| 57 namespace {{command.name | to_title_case}} { | |
| 58 namespace {{param.name | to_title_case}}Enum { | |
| 59 {% for literal in param.enum %} | |
| 60 {{config.protocol.export_macro}} extern const char* {{literal | dash_to_camelcas
e}}; | |
| 61 {% endfor %} | |
| 62 } // {{param.name | to_title_case}}Enum | |
| 63 } // {{command.name | to_title_case }} | |
| 64 {% endif %} | |
| 65 {% endfor %} | |
| 66 {% endfor %} | |
| 67 | |
| 68 // ------------- Type and builder declarations. | |
| 69 {% for type in domain.types %} | |
| 70 {% if not (type.type == "object") or not ("properties" in type) %}{% continu
e %}{% endif %} | |
| 71 {% set type_def = type_definition(domain.domain + "." + type.id)%} | |
| 72 | |
| 73 // {{type.description}} | |
| 74 class {{config.protocol.export_macro}} {{type.id}} {% if type.exported %}: publi
c API::{{type.id}} {% endif %}{ | |
| 75 PROTOCOL_DISALLOW_COPY({{type.id}}); | |
| 76 public: | |
| 77 static std::unique_ptr<{{type.id}}> parse(protocol::Value* value, ErrorSuppo
rt* errors); | |
| 78 | |
| 79 ~{{type.id}}() { } | |
| 80 {% for property in type.properties %} | |
| 81 {% if "enum" in property %} | |
| 82 | |
| 83 struct {{config.protocol.export_macro}} {{property.name | to_title_case}}Enu
m { | |
| 84 {% for literal in property.enum %} | |
| 85 static const char* {{literal | dash_to_camelcase}}; | |
| 86 {% endfor %} | |
| 87 }; // {{property.name | to_title_case}}Enum | |
| 88 {% endif %} | |
| 89 | |
| 90 {% if property.optional %} | |
| 91 bool has{{property.name | to_title_case}}() { return m_{{property.name}}.isJ
ust(); } | |
| 92 {{resolve_type(property).raw_return_type}} get{{property.name | to_title_cas
e}}({{resolve_type(property).raw_pass_type}} defaultValue) { return m_{{property
.name}}.isJust() ? m_{{property.name}}.fromJust() : defaultValue; } | |
| 93 {% else %} | |
| 94 {{resolve_type(property).raw_return_type}} get{{property.name | to_title_cas
e}}() { return {{resolve_type(property).to_raw_type % ("m_" + property.name)}};
} | |
| 95 {% endif %} | |
| 96 void set{{property.name | to_title_case}}({{resolve_type(property).pass_type
}} value) { m_{{property.name}} = {{resolve_type(property).to_rvalue % "value"}}
; } | |
| 97 {% endfor %} | |
| 98 | |
| 99 std::unique_ptr<protocol::DictionaryValue> serialize() const; | |
| 100 std::unique_ptr<{{type.id}}> clone() const; | |
| 101 {% if type.exported %} | |
| 102 {{config.exported.string_out}} toJSONString() const override; | |
| 103 {% endif %} | |
| 104 | |
| 105 template<int STATE> | |
| 106 class {{type.id}}Builder { | |
| 107 public: | |
| 108 enum { | |
| 109 NoFieldsSet = 0, | |
| 110 {% set count = 0 %} | |
| 111 {% for property in type.properties %} | |
| 112 {% if not(property.optional) %} | |
| 113 {% set count = count + 1 %} | |
| 114 {{property.name | to_title_case}}Set = 1 << {{count}}, | |
| 115 {% endif %} | |
| 116 {% endfor %} | |
| 117 AllFieldsSet = ( | |
| 118 {%- for property in type.properties %} | |
| 119 {% if not(property.optional) %}{{property.name | to_title_case}}Set | {%en
dif %} | |
| 120 {% endfor %}0)}; | |
| 121 | |
| 122 {% for property in type.properties %} | |
| 123 | |
| 124 {% if property.optional %} | |
| 125 {{type.id}}Builder<STATE>& set{{property.name | to_title_case}}({{resolv
e_type(property).pass_type}} value) | |
| 126 { | |
| 127 m_result->set{{property.name | to_title_case}}({{resolve_type(proper
ty).to_rvalue % "value"}}); | |
| 128 return *this; | |
| 129 } | |
| 130 {% else %} | |
| 131 {{type.id}}Builder<STATE | {{property.name | to_title_case}}Set>& set{{p
roperty.name | to_title_case}}({{resolve_type(property).pass_type}} value) | |
| 132 { | |
| 133 static_assert(!(STATE & {{property.name | to_title_case}}Set), "prop
erty {{property.name}} should not be set yet"); | |
| 134 m_result->set{{property.name | to_title_case}}({{resolve_type(proper
ty).to_rvalue % "value"}}); | |
| 135 return castState<{{property.name | to_title_case}}Set>(); | |
| 136 } | |
| 137 {% endif %} | |
| 138 {% endfor %} | |
| 139 | |
| 140 std::unique_ptr<{{type.id}}> build() | |
| 141 { | |
| 142 static_assert(STATE == AllFieldsSet, "state should be AllFieldsSet")
; | |
| 143 return std::move(m_result); | |
| 144 } | |
| 145 | |
| 146 private: | |
| 147 friend class {{type.id}}; | |
| 148 {{type.id}}Builder() : m_result(new {{type.id}}()) { } | |
| 149 | |
| 150 template<int STEP> {{type.id}}Builder<STATE | STEP>& castState() | |
| 151 { | |
| 152 return *reinterpret_cast<{{type.id}}Builder<STATE | STEP>*>(this); | |
| 153 } | |
| 154 | |
| 155 {{type_def.type}} m_result; | |
| 156 }; | |
| 157 | |
| 158 static {{type.id}}Builder<0> create() | |
| 159 { | |
| 160 return {{type.id}}Builder<0>(); | |
| 161 } | |
| 162 | |
| 163 private: | |
| 164 {{type.id}}() | |
| 165 { | |
| 166 {% for property in type.properties %} | |
| 167 {% if not(property.optional) and "default_value" in resolve_type(property)
%} | |
| 168 m_{{property.name}} = {{resolve_type(property).default_value}}; | |
| 169 {%endif %} | |
| 170 {% endfor %} | |
| 171 } | |
| 172 | |
| 173 {% for property in type.properties %} | |
| 174 {% if property.optional %} | |
| 175 Maybe<{{resolve_type(property).raw_type}}> m_{{property.name}}; | |
| 176 {% else %} | |
| 177 {{resolve_type(property).type}} m_{{property.name}}; | |
| 178 {% endif %} | |
| 179 {% endfor %} | |
| 180 }; | |
| 181 | |
| 182 {% endfor %} | |
| 183 | |
| 184 // ------------- Backend interface. | |
| 185 | |
| 186 class {{config.protocol.export_macro}} Backend { | |
| 187 public: | |
| 188 virtual ~Backend() { } | |
| 189 | |
| 190 {% for command in domain.commands %} | |
| 191 {% if "redirect" in command %}{% continue %}{% endif %} | |
| 192 {% if ("handlers" in command) and not ("renderer" in command["handlers"]) %}
{% continue %}{% endif %} | |
| 193 {% if "async" in command %} | |
| 194 class {{config.protocol.export_macro}} {{command.name | to_title_case}}Callb
ack : public BackendCallback { | |
| 195 public: | |
| 196 virtual void sendSuccess( | |
| 197 {%- for parameter in command.returns -%} | |
| 198 {%- if "optional" in parameter -%} | |
| 199 const Maybe<{{resolve_type(parameter).raw_type}}>& {{parameter.name}
} | |
| 200 {%- else -%} | |
| 201 {{resolve_type(parameter).pass_type}} {{parameter.name}} | |
| 202 {%- endif -%} | |
| 203 {%- if not loop.last -%}, {% endif -%} | |
| 204 {%- endfor -%} | |
| 205 ) = 0; | |
| 206 }; | |
| 207 {% endif %} | |
| 208 virtual void {{command.name}}( | |
| 209 {%- if not("async" in command) -%} | |
| 210 ErrorString* | |
| 211 {%- endif -%} | |
| 212 {%- for parameter in command.parameters -%} | |
| 213 {%- if (not loop.first) or not("async" in command) -%}, {% endif -%} | |
| 214 {%- if "optional" in parameter -%} | |
| 215 const Maybe<{{resolve_type(parameter).raw_type}}>& in_{{parameter.name}} | |
| 216 {%- else -%} | |
| 217 {{resolve_type(parameter).pass_type}} in_{{parameter.name}} | |
| 218 {%- endif -%} | |
| 219 {%- endfor -%} | |
| 220 {%- if "async" in command -%} | |
| 221 {%- if command.parameters -%}, {% endif -%} | |
| 222 std::unique_ptr<{{command.name | to_title_case}}Callback> callback | |
| 223 {%- else -%} | |
| 224 {%- for parameter in command.returns -%} | |
| 225 {%- if "optional" in parameter -%} | |
| 226 , Maybe<{{resolve_type(parameter).raw_type}}>* out_{{parameter.name}} | |
| 227 {%- else -%} | |
| 228 , {{resolve_type(parameter).type}}* out_{{parameter.name}} | |
| 229 {%- endif -%} | |
| 230 {%- endfor -%} | |
| 231 {%- endif -%} | |
| 232 ) = 0; | |
| 233 {% endfor %} | |
| 234 | |
| 235 {% if not has_disable(domain.commands) %} | |
| 236 virtual void disable(ErrorString*) { } | |
| 237 {% endif %} | |
| 238 }; | |
| 239 | |
| 240 // ------------- Frontend interface. | |
| 241 | |
| 242 class {{config.protocol.export_macro}} Frontend { | |
| 243 public: | |
| 244 Frontend(FrontendChannel* frontendChannel) : m_frontendChannel(frontendChann
el) { } | |
| 245 {% for event in domain.events %} | |
| 246 {% if "handlers" in event and not ("renderer" in event["handlers"]) %}{% con
tinue %}{% endif %} | |
| 247 void {{event.name}}( | |
| 248 {%- for parameter in event.parameters -%} | |
| 249 {%- if "optional" in parameter -%} | |
| 250 const Maybe<{{resolve_type(parameter).raw_type}}>& {{parameter.name}} =
Maybe<{{resolve_type(parameter).raw_type}}>() | |
| 251 {%- else -%} | |
| 252 {{resolve_type(parameter).pass_type}} {{parameter.name}} | |
| 253 {%- endif -%}{%- if not loop.last -%}, {% endif -%} | |
| 254 {%- endfor -%} | |
| 255 ); | |
| 256 {% endfor %} | |
| 257 | |
| 258 void flush(); | |
| 259 private: | |
| 260 FrontendChannel* m_frontendChannel; | |
| 261 }; | |
| 262 | |
| 263 // ------------- Dispatcher. | |
| 264 | |
| 265 class {{config.protocol.export_macro}} Dispatcher { | |
| 266 public: | |
| 267 static void wire(UberDispatcher*, Backend*); | |
| 268 | |
| 269 private: | |
| 270 Dispatcher() { } | |
| 271 }; | |
| 272 | |
| 273 // ------------- Metainfo. | |
| 274 | |
| 275 class {{config.protocol.export_macro}} Metainfo { | |
| 276 public: | |
| 277 using BackendClass = Backend; | |
| 278 using FrontendClass = Frontend; | |
| 279 using DispatcherClass = Dispatcher; | |
| 280 static const char domainName[]; | |
| 281 static const char commandPrefix[]; | |
| 282 static const char version[]; | |
| 283 }; | |
| 284 | |
| 285 } // namespace {{domain.domain}} | |
| 286 {% for namespace in config.protocol.namespace %} | |
| 287 } // namespace {{namespace}} | |
| 288 {% endfor %} | |
| 289 | |
| 290 #endif // !defined({{"_".join(config.protocol.namespace)}}_{{domain.domain}}_h) | |
| OLD | NEW |