OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import os | 5 import os |
6 | 6 |
7 from code import Code | 7 from code import Code |
8 from model import PropertyType | 8 from model import PropertyType |
9 import cpp_util | 9 import cpp_util |
10 import schema_util | 10 import schema_util |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 # other cases, generate soft dependencies so that they can include | 46 # other cases, generate soft dependencies so that they can include |
47 # non-optional types from other namespaces. | 47 # non-optional types from other namespaces. |
48 include_soft = self._namespace.name not in ('tabs', 'windows') | 48 include_soft = self._namespace.name not in ('tabs', 'windows') |
49 | 49 |
50 (c.Append('#ifndef %s' % ifndef_name) | 50 (c.Append('#ifndef %s' % ifndef_name) |
51 .Append('#define %s' % ifndef_name) | 51 .Append('#define %s' % ifndef_name) |
52 .Append() | 52 .Append() |
53 .Append('#include <stdint.h>') | 53 .Append('#include <stdint.h>') |
54 .Append() | 54 .Append() |
55 .Append('#include <map>') | 55 .Append('#include <map>') |
| 56 .Append('#include <memory>') |
56 .Append('#include <string>') | 57 .Append('#include <string>') |
57 .Append('#include <vector>') | 58 .Append('#include <vector>') |
58 .Append() | 59 .Append() |
59 .Append('#include "base/logging.h"') | 60 .Append('#include "base/logging.h"') |
60 .Append('#include "base/memory/scoped_ptr.h"') | |
61 .Append('#include "base/values.h"') | 61 .Append('#include "base/values.h"') |
62 .Cblock(self._type_helper.GenerateIncludes(include_soft=include_soft)) | 62 .Cblock(self._type_helper.GenerateIncludes(include_soft=include_soft)) |
63 .Append() | 63 .Append() |
64 ) | 64 ) |
65 | 65 |
66 # Hack: we're not generating soft includes for tabs and windows, so we need | 66 # Hack: we're not generating soft includes for tabs and windows, so we need |
67 # to generate forward declarations for them. | 67 # to generate forward declarations for them. |
68 if not include_soft: | 68 if not include_soft: |
69 c.Cblock(self._type_helper.GenerateForwardDeclarations()) | 69 c.Cblock(self._type_helper.GenerateForwardDeclarations()) |
70 | 70 |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 (c.Append() | 234 (c.Append() |
235 .Comment('Populates a %s object from a base::Value. Returns' | 235 .Comment('Populates a %s object from a base::Value. Returns' |
236 ' whether |out| was successfully populated.' % classname) | 236 ' whether |out| was successfully populated.' % classname) |
237 .Append('static bool Populate(%s);' % self._GenerateParams( | 237 .Append('static bool Populate(%s);' % self._GenerateParams( |
238 ('const base::Value& value', '%s* out' % classname))) | 238 ('const base::Value& value', '%s* out' % classname))) |
239 ) | 239 ) |
240 if is_toplevel: | 240 if is_toplevel: |
241 (c.Append() | 241 (c.Append() |
242 .Comment('Creates a %s object from a base::Value, or NULL on ' | 242 .Comment('Creates a %s object from a base::Value, or NULL on ' |
243 'failure.' % classname) | 243 'failure.' % classname) |
244 .Append('static scoped_ptr<%s> FromValue(%s);' % ( | 244 .Append('static std::unique_ptr<%s> FromValue(%s);' % ( |
245 classname, self._GenerateParams(('const base::Value& value',)))) | 245 classname, self._GenerateParams(('const base::Value& value',)))) |
246 ) | 246 ) |
247 if type_.origin.from_client: | 247 if type_.origin.from_client: |
248 value_type = ('base::Value' | 248 value_type = ('base::Value' |
249 if type_.property_type is PropertyType.CHOICES else | 249 if type_.property_type is PropertyType.CHOICES else |
250 'base::DictionaryValue') | 250 'base::DictionaryValue') |
251 (c.Append() | 251 (c.Append() |
252 .Comment('Returns a new %s representing the serialized form of this ' | 252 .Comment('Returns a new %s representing the serialized form of this ' |
253 '%s object.' % (value_type, classname)) | 253 '%s object.' % (value_type, classname)) |
254 .Append('scoped_ptr<%s> ToValue() const;' % value_type) | 254 .Append('std::unique_ptr<%s> ToValue() const;' % value_type) |
255 ) | 255 ) |
256 if type_.property_type == PropertyType.CHOICES: | 256 if type_.property_type == PropertyType.CHOICES: |
257 # Choices are modelled with optional fields for each choice. Exactly one | 257 # Choices are modelled with optional fields for each choice. Exactly one |
258 # field of the choice is guaranteed to be set by the compiler. | 258 # field of the choice is guaranteed to be set by the compiler. |
259 c.Cblock(self._GenerateTypes(type_.choices)) | 259 c.Cblock(self._GenerateTypes(type_.choices)) |
260 c.Append('// Choices:') | 260 c.Append('// Choices:') |
261 for choice_type in type_.choices: | 261 for choice_type in type_.choices: |
262 c.Append('%s as_%s;' % ( | 262 c.Append('%s as_%s;' % ( |
263 self._type_helper.GetCppType(choice_type, is_ptr=True), | 263 self._type_helper.GetCppType(choice_type, is_ptr=True), |
264 choice_type.unix_name)) | 264 choice_type.unix_name)) |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 return c | 321 return c |
322 | 322 |
323 def _GenerateFunctionParams(self, function): | 323 def _GenerateFunctionParams(self, function): |
324 """Generates the struct for passing parameters from JSON to a function. | 324 """Generates the struct for passing parameters from JSON to a function. |
325 """ | 325 """ |
326 if not function.params: | 326 if not function.params: |
327 return Code() | 327 return Code() |
328 | 328 |
329 c = Code() | 329 c = Code() |
330 (c.Sblock('struct Params {') | 330 (c.Sblock('struct Params {') |
331 .Append('static scoped_ptr<Params> Create(%s);' % self._GenerateParams( | 331 .Append('static std::unique_ptr<Params> Create(%s);' % |
332 ('const base::ListValue& args',))) | 332 self._GenerateParams(('const base::ListValue& args',))) |
333 .Append('~Params();') | 333 .Append('~Params();') |
334 .Append() | 334 .Append() |
335 .Cblock(self._GenerateTypes(p.type_ for p in function.params)) | 335 .Cblock(self._GenerateTypes(p.type_ for p in function.params)) |
336 .Cblock(self._GenerateFields(function.params)) | 336 .Cblock(self._GenerateFields(function.params)) |
337 .Eblock() | 337 .Eblock() |
338 .Append() | 338 .Append() |
339 .Sblock(' private:') | 339 .Sblock(' private:') |
340 .Append('Params();') | 340 .Append('Params();') |
341 .Append() | 341 .Append() |
342 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') | 342 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') |
(...skipping 18 matching lines...) Expand all Loading... |
361 c = Code() | 361 c = Code() |
362 params = function.params | 362 params = function.params |
363 c.Cblock(self._GenerateTypes((p.type_ for p in params), is_toplevel=True)) | 363 c.Cblock(self._GenerateTypes((p.type_ for p in params), is_toplevel=True)) |
364 | 364 |
365 declaration_list = [] | 365 declaration_list = [] |
366 for param in params: | 366 for param in params: |
367 if param.description: | 367 if param.description: |
368 c.Comment(param.description) | 368 c.Comment(param.description) |
369 declaration_list.append(cpp_util.GetParameterDeclaration( | 369 declaration_list.append(cpp_util.GetParameterDeclaration( |
370 param, self._type_helper.GetCppType(param.type_))) | 370 param, self._type_helper.GetCppType(param.type_))) |
371 c.Append('scoped_ptr<base::ListValue> Create(%s);' % | 371 c.Append('std::unique_ptr<base::ListValue> Create(%s);' % |
372 ', '.join(declaration_list)) | 372 ', '.join(declaration_list)) |
373 return c | 373 return c |
374 | 374 |
375 def _GenerateEventNameConstant(self, event): | 375 def _GenerateEventNameConstant(self, event): |
376 """Generates a constant string array for the event name. | 376 """Generates a constant string array for the event name. |
377 """ | 377 """ |
378 c = Code() | 378 c = Code() |
379 c.Append('extern const char kEventName[]; // "%s.%s"' % ( | 379 c.Append('extern const char kEventName[]; // "%s.%s"' % ( |
380 self._namespace.name, event.name)) | 380 self._namespace.name, event.name)) |
381 c.Append() | 381 c.Append() |
(...skipping 14 matching lines...) Expand all Loading... |
396 """Builds the parameter list for a function, given an array of parameters. | 396 """Builds the parameter list for a function, given an array of parameters. |
397 """ | 397 """ |
398 # |error| is populated with warnings and/or errors found during parsing. | 398 # |error| is populated with warnings and/or errors found during parsing. |
399 # |error| being set does not necessarily imply failure and may be | 399 # |error| being set does not necessarily imply failure and may be |
400 # recoverable. | 400 # recoverable. |
401 # For example, optional properties may have failed to parse, but the | 401 # For example, optional properties may have failed to parse, but the |
402 # parser was able to continue. | 402 # parser was able to continue. |
403 if self._generate_error_messages: | 403 if self._generate_error_messages: |
404 params += ('base::string16* error',) | 404 params += ('base::string16* error',) |
405 return ', '.join(str(p) for p in params) | 405 return ', '.join(str(p) for p in params) |
OLD | NEW |