Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: templates/TypeBuilder_cpp.template

Issue 2468923002: [inspector_protocol] support fall through and moveable Maybe (Closed)
Patch Set: added missing std::move Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/Maybe_h.template ('k') | templates/TypeBuilder_h.template » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // This file is generated 1 // This file is generated
2 2
3 // Copyright (c) 2016 The Chromium Authors. All rights reserved. 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 4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file. 5 // found in the LICENSE file.
6 6
7 #include "{{config.protocol.package}}/{{domain.domain}}.h" 7 #include "{{config.protocol.package}}/{{domain.domain}}.h"
8 8
9 #include "{{config.protocol.package}}/Protocol.h" 9 #include "{{config.protocol.package}}/Protocol.h"
10 10
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 {% endfor %} 143 {% endfor %}
144 {% endfor %} 144 {% endfor %}
145 145
146 // ------------- Frontend notifications. 146 // ------------- Frontend notifications.
147 {% for event in domain.events %} 147 {% for event in domain.events %}
148 {% if "handlers" in event and not ("renderer" in event["handlers"]) %}{% con tinue %}{% endif %} 148 {% if "handlers" in event and not ("renderer" in event["handlers"]) %}{% con tinue %}{% endif %}
149 149
150 void Frontend::{{event.name}}( 150 void Frontend::{{event.name}}(
151 {%- for parameter in event.parameters %} 151 {%- for parameter in event.parameters %}
152 {% if "optional" in parameter -%} 152 {% if "optional" in parameter -%}
153 {%- if new_style(domain) -%}
154 Maybe<{{resolve_type(parameter).raw_type}}>
155 {%- else -%}
153 const Maybe<{{resolve_type(parameter).raw_type}}>& 156 const Maybe<{{resolve_type(parameter).raw_type}}>&
157 {%- endif -%}
154 {%- else -%} 158 {%- else -%}
155 {{resolve_type(parameter).pass_type}} 159 {{resolve_type(parameter).pass_type}}
156 {%- endif %} {{parameter.name}}{%- if not loop.last -%}, {% endif -%} 160 {%- endif %} {{parameter.name}}{%- if not loop.last -%}, {% endif -%}
157 {% endfor -%}) 161 {% endfor -%})
158 { 162 {
159 std::unique_ptr<protocol::DictionaryValue> jsonMessage = DictionaryValue::cr eate(); 163 std::unique_ptr<protocol::DictionaryValue> jsonMessage = DictionaryValue::cr eate();
160 jsonMessage->setString("method", "{{domain.domain}}.{{event.name}}"); 164 jsonMessage->setString("method", "{{domain.domain}}.{{event.name}}");
161 std::unique_ptr<protocol::DictionaryValue> paramsObject = DictionaryValue::c reate(); 165 std::unique_ptr<protocol::DictionaryValue> paramsObject = DictionaryValue::c reate();
162 {% for parameter in event.parameters %} 166 {% for parameter in event.parameters %}
163 {% if "optional" in parameter %} 167 {% if "optional" in parameter %}
(...skipping 21 matching lines...) Expand all
185 DispatcherImpl(FrontendChannel* frontendChannel, Backend* backend) 189 DispatcherImpl(FrontendChannel* frontendChannel, Backend* backend)
186 : DispatcherBase(frontendChannel) 190 : DispatcherBase(frontendChannel)
187 , m_backend(backend) { 191 , m_backend(backend) {
188 {% for command in domain.commands %} 192 {% for command in domain.commands %}
189 {% if "redirect" in command %}{% continue %}{% endif %} 193 {% if "redirect" in command %}{% continue %}{% endif %}
190 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %} 194 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %}
191 m_dispatchMap["{{domain.domain}}.{{command.name}}"] = &DispatcherImpl::{ {command.name}}; 195 m_dispatchMap["{{domain.domain}}.{{command.name}}"] = &DispatcherImpl::{ {command.name}};
192 {% endfor %} 196 {% endfor %}
193 } 197 }
194 ~DispatcherImpl() override { } 198 ~DispatcherImpl() override { }
195 void dispatch(int callId, const String& method, std::unique_ptr<protocol::Di ctionaryValue> messageObject) override; 199 DispatchResponse::Status dispatch(int callId, const String& method, std::uni que_ptr<protocol::DictionaryValue> messageObject) override;
196 200
197 protected: 201 protected:
198 using CallHandler = void (DispatcherImpl::*)(int callId, std::unique_ptr<Dic tionaryValue> messageObject, ErrorSupport* errors); 202 using CallHandler = DispatchResponse::Status (DispatcherImpl::*)(int callId, std::unique_ptr<DictionaryValue> messageObject, ErrorSupport* errors);
199 using DispatchMap = protocol::HashMap<String, CallHandler>; 203 using DispatchMap = protocol::HashMap<String, CallHandler>;
200 DispatchMap m_dispatchMap; 204 DispatchMap m_dispatchMap;
201 205
202 {% for command in domain.commands %} 206 {% for command in domain.commands %}
203 {% if "redirect" in command %}{% continue %}{% endif %} 207 {% if "redirect" in command %}{% continue %}{% endif %}
204 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %} 208 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %}
205 void {{command.name}}(int callId, std::unique_ptr<DictionaryValue> requestMe ssageObject, ErrorSupport*); 209 DispatchResponse::Status {{command.name}}(int callId, std::unique_ptr<Dictio naryValue> requestMessageObject, ErrorSupport*);
206 {% endfor %} 210 {% endfor %}
207 211
208 Backend* m_backend; 212 Backend* m_backend;
209 }; 213 };
210 214
211 void DispatcherImpl::dispatch(int callId, const String& method, std::unique_ptr< protocol::DictionaryValue> messageObject) 215 DispatchResponse::Status DispatcherImpl::dispatch(int callId, const String& meth od, std::unique_ptr<protocol::DictionaryValue> messageObject)
212 { 216 {
213 protocol::HashMap<String, CallHandler>::iterator it = m_dispatchMap.find(met hod); 217 protocol::HashMap<String, CallHandler>::iterator it = m_dispatchMap.find(met hod);
214 if (it == m_dispatchMap.end()) { 218 if (it == m_dispatchMap.end()) {
215 reportProtocolError(callId, MethodNotFound, "'" + method + "' wasn't fou nd", nullptr); 219 reportProtocolError(callId, DispatchResponse::kMethodNotFound, "'" + met hod + "' wasn't found", nullptr);
216 return; 220 return DispatchResponse::kError;
217 } 221 }
218 222
219 protocol::ErrorSupport errors; 223 protocol::ErrorSupport errors;
220 (this->*(it->second))(callId, std::move(messageObject), &errors); 224 return (this->*(it->second))(callId, std::move(messageObject), &errors);
221 } 225 }
222 226
223 {% for command in domain.commands %} 227 {% for command in domain.commands %}
224 {% if "redirect" in command %}{% continue %}{% endif %} 228 {% if "redirect" in command %}{% continue %}{% endif %}
225 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %} 229 {% if "handlers" in command and not ("renderer" in command["handlers"]) %}{% continue %}{% endif %}
226 {% if "async" in command %} 230 {% if "async" in command %}
227 231
228 class {{command.name | to_title_case}}CallbackImpl : public Backend::{{command.n ame | to_title_case}}Callback, public DispatcherBase::Callback { 232 class {{command.name | to_title_case}}CallbackImpl : public Backend::{{command.n ame | to_title_case}}Callback, public DispatcherBase::Callback {
229 public: 233 public:
230 {{command.name | to_title_case}}CallbackImpl(std::unique_ptr<DispatcherBase: :WeakPtr> backendImpl, int callId) 234 {{command.name | to_title_case}}CallbackImpl(std::unique_ptr<DispatcherBase: :WeakPtr> backendImpl, int callId)
231 : DispatcherBase::Callback(std::move(backendImpl), callId) { } 235 : DispatcherBase::Callback(std::move(backendImpl), callId) { }
232 236
233 void sendSuccess( 237 void sendSuccess(
234 {%- for parameter in command.returns -%} 238 {%- for parameter in command.returns -%}
235 {%- if "optional" in parameter -%} 239 {%- if "optional" in parameter -%}
240 {%- if new_style(domain) -%}
241 Maybe<{{resolve_type(parameter).raw_type}}> {{parameter.name}}
242 {%- else -%}
236 const Maybe<{{resolve_type(parameter).raw_type}}>& {{parameter.name}} 243 const Maybe<{{resolve_type(parameter).raw_type}}>& {{parameter.name}}
237 {%- else -%} 244 {%- endif -%}
245 {%- else -%}
238 {{resolve_type(parameter).pass_type}} {{parameter.name}} 246 {{resolve_type(parameter).pass_type}} {{parameter.name}}
239 {%- endif -%} 247 {%- endif -%}
240 {%- if not loop.last -%}, {% endif -%} 248 {%- if not loop.last -%}, {% endif -%}
241 {%- endfor -%}) override 249 {%- endfor -%}) override
242 { 250 {
243 std::unique_ptr<protocol::DictionaryValue> resultObject = DictionaryValu e::create(); 251 std::unique_ptr<protocol::DictionaryValue> resultObject = DictionaryValu e::create();
244 {% for parameter in command.returns %} 252 {% for parameter in command.returns %}
245 {% if "optional" in parameter %} 253 {% if "optional" in parameter %}
246 if ({{parameter.name}}.isJust()) 254 if ({{parameter.name}}.isJust())
247 resultObject->setValue("{{parameter.name}}", ValueConversions<{{reso lve_type(parameter).raw_type}}>::serialize({{parameter.name}}.fromJust())); 255 resultObject->setValue("{{parameter.name}}", ValueConversions<{{reso lve_type(parameter).raw_type}}>::serialize({{parameter.name}}.fromJust()));
248 {% else %} 256 {% else %}
249 resultObject->setValue("{{parameter.name}}", ValueConversions<{{resolve_ type(parameter).raw_type}}>::serialize({{resolve_type(parameter).to_raw_type % p arameter.name}})); 257 resultObject->setValue("{{parameter.name}}", ValueConversions<{{resolve_ type(parameter).raw_type}}>::serialize({{resolve_type(parameter).to_raw_type % p arameter.name}}));
250 {% endif %} 258 {% endif %}
251 {% endfor %} 259 {% endfor %}
252 sendIfActive(std::move(resultObject), ErrorString()); 260 sendIfActive(std::move(resultObject), DispatchResponse::OK());
253 } 261 }
254 262
263 {% if new_style(domain) %}
264 void sendFailure(const DispatchResponse& response) override
265 {
266 DCHECK(response.status() == DispatchResponse::kError);
267 sendIfActive(nullptr, response);
268 }
269 {% else %}
255 void sendFailure(const ErrorString& error) override 270 void sendFailure(const ErrorString& error) override
256 { 271 {
257 DCHECK(error.length()); 272 DCHECK(error.length());
258 sendIfActive(nullptr, error); 273 sendIfActive(nullptr, DispatchResponse::Error(error));
259 } 274 }
260 275 {% endif %}
261 }; 276 };
262 {% endif %} 277 {% endif %}
263 278
264 void DispatcherImpl::{{command.name}}(int callId, std::unique_ptr<DictionaryValu e> requestMessageObject, ErrorSupport* errors) 279 DispatchResponse::Status DispatcherImpl::{{command.name}}(int callId, std::uniqu e_ptr<DictionaryValue> requestMessageObject, ErrorSupport* errors)
265 { 280 {
266 {% if "parameters" in command %} 281 {% if "parameters" in command %}
267 // Prepare input parameters. 282 // Prepare input parameters.
268 protocol::DictionaryValue* object = DictionaryValue::cast(requestMessageObje ct->get("params")); 283 protocol::DictionaryValue* object = DictionaryValue::cast(requestMessageObje ct->get("params"));
269 errors->push(); 284 errors->push();
270 {% for property in command.parameters %} 285 {% for property in command.parameters %}
271 protocol::Value* {{property.name}}Value = object ? object->get("{{property.n ame}}") : nullptr; 286 protocol::Value* {{property.name}}Value = object ? object->get("{{property.n ame}}") : nullptr;
272 {% if property.optional %} 287 {% if property.optional %}
273 Maybe<{{resolve_type(property).raw_type}}> in_{{property.name}}; 288 Maybe<{{resolve_type(property).raw_type}}> in_{{property.name}};
274 if ({{property.name}}Value) { 289 if ({{property.name}}Value) {
275 errors->setName("{{property.name}}"); 290 errors->setName("{{property.name}}");
276 in_{{property.name}} = ValueConversions<{{resolve_type(property).raw_typ e}}>::parse({{property.name}}Value, errors); 291 in_{{property.name}} = ValueConversions<{{resolve_type(property).raw_typ e}}>::parse({{property.name}}Value, errors);
277 } 292 }
278 {% else %} 293 {% else %}
279 errors->setName("{{property.name}}"); 294 errors->setName("{{property.name}}");
280 {{resolve_type(property).type}} in_{{property.name}} = ValueConversions<{{re solve_type(property).raw_type}}>::parse({{property.name}}Value, errors); 295 {{resolve_type(property).type}} in_{{property.name}} = ValueConversions<{{re solve_type(property).raw_type}}>::parse({{property.name}}Value, errors);
281 {% endif %} 296 {% endif %}
282 {% endfor %} 297 {% endfor %}
283 errors->pop(); 298 errors->pop();
284 if (errors->hasErrors()) { 299 if (errors->hasErrors()) {
285 reportProtocolError(callId, InvalidParams, kInvalidRequest, errors); 300 reportProtocolError(callId, DispatchResponse::kInvalidParams, kInvalidPa ramsString, errors);
286 return; 301 return DispatchResponse::kError;
287 } 302 }
288 {% endif %} 303 {% endif %}
289 {% if "async" in command %} 304 {% if "returns" in command and not ("async" in command) %}
290 std::unique_ptr<{{command.name | to_title_case}}CallbackImpl> callback(new { {command.name | to_title_case}}CallbackImpl(weakPtr(), callId));
291 {% elif "returns" in command %}
292 // Declare output parameters. 305 // Declare output parameters.
293 std::unique_ptr<protocol::DictionaryValue> result = DictionaryValue::create( );
294 {% for property in command.returns %} 306 {% for property in command.returns %}
295 {% if "optional" in property %} 307 {% if "optional" in property %}
296 Maybe<{{resolve_type(property).raw_type}}> out_{{property.name}}; 308 Maybe<{{resolve_type(property).raw_type}}> out_{{property.name}};
297 {% else %} 309 {% else %}
298 {{resolve_type(property).type}} out_{{property.name}}; 310 {{resolve_type(property).type}} out_{{property.name}};
299 {% endif %} 311 {% endif %}
300 {% endfor %} 312 {% endfor %}
301 {% endif %} 313 {% endif %}
302 314
315 {% if not("async" in command) %}
303 std::unique_ptr<DispatcherBase::WeakPtr> weak = weakPtr(); 316 std::unique_ptr<DispatcherBase::WeakPtr> weak = weakPtr();
304 {% if not("async" in command) %} 317 {% if not new_style(domain) %}
305 ErrorString error; 318 ErrorString error;
306 m_backend->{{command.name}}(&error 319 m_backend->{{command.name}}(&error
320 {%- else %}
321 DispatchResponse response = m_backend->{{command.name}}(
322 {%- endif -%}
307 {%- for property in command.parameters -%} 323 {%- for property in command.parameters -%}
324 {%- if not loop.first or not new_style(domain) -%}, {% endif -%}
308 {%- if "optional" in property -%} 325 {%- if "optional" in property -%}
309 , in_{{property.name}} 326 {%- if new_style(domain) -%}
327 std::move(in_{{property.name}})
328 {%- else -%}
329 in_{{property.name}}
330 {%- endif -%}
310 {%- else -%} 331 {%- else -%}
311 , {{resolve_type(property).to_pass_type % ("in_" + property.name)}} 332 {{resolve_type(property).to_pass_type % ("in_" + property.name)}}
312 {%- endif -%} 333 {%- endif -%}
313 {%- endfor %} 334 {%- endfor %}
314 {%- if "returns" in command %} 335 {%- if "returns" in command %}
315 {%- for property in command.returns -%} 336 {%- for property in command.returns -%}
316 , &out_{{property.name}} 337 {%- if not loop.first or command.parameters or not new_style(domain) - %}, {% endif -%}
338 &out_{{property.name}}
317 {%- endfor %} 339 {%- endfor %}
318 {% endif %}); 340 {% endif %});
319 {% if "returns" in command and not("async" in command) %} 341 {% if not new_style(domain) %}
320 if (!error.length()) { 342 DispatchResponse response = error.length() ? DispatchResponse::Error(error) : DispatchResponse::OK();
343 {% endif %}
344 {% if "returns" in command %}
345 if (response.status() == DispatchResponse::kFallThrough)
346 return response.status();
347 std::unique_ptr<protocol::DictionaryValue> result = DictionaryValue::create( );
348 if (response.status() == DispatchResponse::kSuccess) {
321 {% for parameter in command.returns %} 349 {% for parameter in command.returns %}
322 {% if "optional" in parameter %} 350 {% if "optional" in parameter %}
323 if (out_{{parameter.name}}.isJust()) 351 if (out_{{parameter.name}}.isJust())
324 result->setValue("{{parameter.name}}", ValueConversions<{{resolve_ty pe(parameter).raw_type}}>::serialize(out_{{parameter.name}}.fromJust())); 352 result->setValue("{{parameter.name}}", ValueConversions<{{resolve_ty pe(parameter).raw_type}}>::serialize(out_{{parameter.name}}.fromJust()));
325 {% else %} 353 {% else %}
326 result->setValue("{{parameter.name}}", ValueConversions<{{resolve_type(p arameter).raw_type}}>::serialize({{resolve_type(parameter).to_raw_type % ("out_" + parameter.name)}})); 354 result->setValue("{{parameter.name}}", ValueConversions<{{resolve_type(p arameter).raw_type}}>::serialize({{resolve_type(parameter).to_raw_type % ("out_" + parameter.name)}}));
327 {% endif %} 355 {% endif %}
328 {% endfor %} 356 {% endfor %}
329 } 357 }
330 if (weak->get()) 358 if (weak->get())
331 weak->get()->sendResponse(callId, error, std::move(result)); 359 weak->get()->sendResponse(callId, response, std::move(result));
332 {% else %} 360 {% else %}
333 if (weak->get()) 361 if (weak->get())
334 weak->get()->sendResponse(callId, error); 362 weak->get()->sendResponse(callId, response);
335 {% endif %} 363 {% endif %}
336 {%- else %} 364 return response.status();
365 {% else %}
366 std::unique_ptr<{{command.name | to_title_case}}CallbackImpl> callback(new { {command.name | to_title_case}}CallbackImpl(weakPtr(), callId));
337 m_backend->{{command.name}}( 367 m_backend->{{command.name}}(
338 {%- for property in command.parameters -%} 368 {%- for property in command.parameters -%}
369 {%- if not loop.first -%}, {% endif -%}
339 {%- if "optional" in property -%} 370 {%- if "optional" in property -%}
340 in_{{property.name}}, 371 {%- if new_style(domain) -%}
372 std::move(in_{{property.name}})
373 {%- else -%}
374 in_{{property.name}}
375 {%- endif -%}
341 {%- else -%} 376 {%- else -%}
342 {{resolve_type(property).to_pass_type % ("in_" + property.name)}}, 377 {{resolve_type(property).to_pass_type % ("in_" + property.name)}}
343 {%- endif -%} 378 {%- endif -%}
344 {%- endfor -%} 379 {%- endfor -%}
380 {%- if command.parameters -%}, {% endif -%}
345 std::move(callback)); 381 std::move(callback));
382 return DispatchResponse::kAsync;
346 {% endif %} 383 {% endif %}
347 } 384 }
348 {% endfor %} 385 {% endfor %}
349 386
350 // static 387 // static
351 void Dispatcher::wire(UberDispatcher* dispatcher, Backend* backend) 388 void Dispatcher::wire(UberDispatcher* dispatcher, Backend* backend)
352 { 389 {
353 dispatcher->registerBackend("{{domain.domain}}", wrapUnique(new DispatcherIm pl(dispatcher->channel(), backend))); 390 dispatcher->registerBackend("{{domain.domain}}", wrapUnique(new DispatcherIm pl(dispatcher->channel(), backend)));
354 } 391 }
355 392
356 } // {{domain.domain}} 393 } // {{domain.domain}}
357 {% for namespace in config.protocol.namespace %} 394 {% for namespace in config.protocol.namespace %}
358 } // namespace {{namespace}} 395 } // namespace {{namespace}}
359 {% endfor %} 396 {% endfor %}
OLDNEW
« no previous file with comments | « lib/Maybe_h.template ('k') | templates/TypeBuilder_h.template » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698