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 from code import Code | 5 from code import Code |
6 from model import PropertyType, Type | 6 from model import PropertyType, Type |
7 import cpp_util | 7 import cpp_util |
8 import schema_util | 8 import schema_util |
9 | 9 |
10 class HGenerator(object): | 10 class HGenerator(object): |
| 11 def __init__(self, type_generator, cpp_namespace): |
| 12 self._type_generator = type_generator |
| 13 self._cpp_namespace = cpp_namespace |
| 14 |
| 15 def Generate(self, namespace): |
| 16 return _Generator(namespace, |
| 17 self._type_generator, |
| 18 self._cpp_namespace).Generate() |
| 19 |
| 20 class _Generator(object): |
11 """A .h generator for a namespace. | 21 """A .h generator for a namespace. |
12 """ | 22 """ |
13 def __init__(self, namespace, cpp_type_generator): | 23 def __init__(self, namespace, cpp_type_generator, cpp_namespace): |
| 24 self._namespace = namespace |
14 self._type_helper = cpp_type_generator | 25 self._type_helper = cpp_type_generator |
15 self._namespace = namespace | 26 self._cpp_namespace = cpp_namespace |
16 self._target_namespace = ( | 27 self._target_namespace = ( |
17 self._type_helper.GetCppNamespaceName(self._namespace)) | 28 self._type_helper.GetCppNamespaceName(self._namespace)) |
18 | 29 |
19 def Generate(self): | 30 def Generate(self): |
20 """Generates a Code object with the .h for a single namespace. | 31 """Generates a Code object with the .h for a single namespace. |
21 """ | 32 """ |
22 c = Code() | 33 c = Code() |
23 (c.Append(cpp_util.CHROMIUM_LICENSE) | 34 (c.Append(cpp_util.CHROMIUM_LICENSE) |
24 .Append() | 35 .Append() |
25 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) | 36 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) |
(...skipping 11 matching lines...) Expand all Loading... |
37 .Append() | 48 .Append() |
38 .Append('#include "base/basictypes.h"') | 49 .Append('#include "base/basictypes.h"') |
39 .Append('#include "base/logging.h"') | 50 .Append('#include "base/logging.h"') |
40 .Append('#include "base/memory/linked_ptr.h"') | 51 .Append('#include "base/memory/linked_ptr.h"') |
41 .Append('#include "base/memory/scoped_ptr.h"') | 52 .Append('#include "base/memory/scoped_ptr.h"') |
42 .Append('#include "base/values.h"') | 53 .Append('#include "base/values.h"') |
43 .Cblock(self._type_helper.GenerateIncludes()) | 54 .Cblock(self._type_helper.GenerateIncludes()) |
44 .Append() | 55 .Append() |
45 ) | 56 ) |
46 | 57 |
47 c.Concat(self._type_helper.GetRootNamespaceStart()) | 58 c.Concat(cpp_util.OpenNamespace(self._cpp_namespace)) |
48 # TODO(calamity): These forward declarations should be #includes to allow | 59 # TODO(calamity): These forward declarations should be #includes to allow |
49 # $ref types from other files to be used as required params. This requires | 60 # $ref types from other files to be used as required params. This requires |
50 # some detangling of windows and tabs which will currently lead to circular | 61 # some detangling of windows and tabs which will currently lead to circular |
51 # #includes. | 62 # #includes. |
52 forward_declarations = ( | 63 forward_declarations = ( |
53 self._type_helper.GenerateForwardDeclarations()) | 64 self._type_helper.GenerateForwardDeclarations()) |
54 if not forward_declarations.IsEmpty(): | 65 if not forward_declarations.IsEmpty(): |
55 (c.Append() | 66 (c.Append() |
56 .Cblock(forward_declarations) | 67 .Cblock(forward_declarations) |
57 ) | 68 ) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 c.Cblock(self._GenerateFunction(function)) | 100 c.Cblock(self._GenerateFunction(function)) |
90 if self._namespace.events: | 101 if self._namespace.events: |
91 (c.Append('//') | 102 (c.Append('//') |
92 .Append('// Events') | 103 .Append('// Events') |
93 .Append('//') | 104 .Append('//') |
94 .Append() | 105 .Append() |
95 ) | 106 ) |
96 for event in self._namespace.events.values(): | 107 for event in self._namespace.events.values(): |
97 c.Cblock(self._GenerateEvent(event)) | 108 c.Cblock(self._GenerateEvent(event)) |
98 (c.Concat(self._type_helper.GetNamespaceEnd()) | 109 (c.Concat(self._type_helper.GetNamespaceEnd()) |
99 .Cblock(self._type_helper.GetRootNamespaceEnd()) | 110 .Concat(cpp_util.CloseNamespace(self._cpp_namespace)) |
100 .Append('#endif // %s' % ifndef_name) | 111 .Append('#endif // %s' % ifndef_name) |
101 .Append() | 112 .Append() |
102 ) | 113 ) |
103 return c | 114 return c |
104 | 115 |
105 def _FieldDependencyOrder(self): | 116 def _FieldDependencyOrder(self): |
106 """Generates the list of types in the current namespace in an order in which | 117 """Generates the list of types in the current namespace in an order in which |
107 depended-upon types appear before types which depend on them. | 118 depended-upon types appear before types which depend on them. |
108 """ | 119 """ |
109 dependency_order = [] | 120 dependency_order = [] |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 if type_.additional_properties.property_type == PropertyType.ANY: | 246 if type_.additional_properties.property_type == PropertyType.ANY: |
236 c.Append('base::DictionaryValue additional_properties;') | 247 c.Append('base::DictionaryValue additional_properties;') |
237 else: | 248 else: |
238 (c.Cblock(self._GenerateType(type_.additional_properties)) | 249 (c.Cblock(self._GenerateType(type_.additional_properties)) |
239 .Append('std::map<std::string, %s> additional_properties;' % | 250 .Append('std::map<std::string, %s> additional_properties;' % |
240 cpp_util.PadForGenerics( | 251 cpp_util.PadForGenerics( |
241 self._type_helper.GetCppType(type_.additional_properties, | 252 self._type_helper.GetCppType(type_.additional_properties, |
242 is_in_container=True))) | 253 is_in_container=True))) |
243 ) | 254 ) |
244 (c.Eblock() | 255 (c.Eblock() |
| 256 .Append() |
245 .Sblock(' private:') | 257 .Sblock(' private:') |
246 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);') | 258 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);') |
247 .Eblock('};') | 259 .Eblock('};') |
248 ) | 260 ) |
249 elif type_.property_type == PropertyType.CHOICES: | 261 elif type_.property_type == PropertyType.CHOICES: |
250 if type_.description: | 262 if type_.description: |
251 c.Comment(type_.description) | 263 c.Comment(type_.description) |
252 # Choices are modelled with optional fields for each choice. Exactly one | 264 # Choices are modelled with optional fields for each choice. Exactly one |
253 # field of the choice is guaranteed to be set by the compiler. | 265 # field of the choice is guaranteed to be set by the compiler. |
254 (c.Sblock('struct %(classname)s {') | 266 (c.Sblock('struct %(classname)s {') |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 return Code() | 325 return Code() |
314 | 326 |
315 c = Code() | 327 c = Code() |
316 (c.Sblock('struct Params {') | 328 (c.Sblock('struct Params {') |
317 .Append('static scoped_ptr<Params> Create(const base::ListValue& args);') | 329 .Append('static scoped_ptr<Params> Create(const base::ListValue& args);') |
318 .Append('~Params();') | 330 .Append('~Params();') |
319 .Append() | 331 .Append() |
320 .Cblock(self._GenerateTypes(p.type_ for p in function.params)) | 332 .Cblock(self._GenerateTypes(p.type_ for p in function.params)) |
321 .Cblock(self._GenerateFields(function.params)) | 333 .Cblock(self._GenerateFields(function.params)) |
322 .Eblock() | 334 .Eblock() |
| 335 .Append() |
323 .Sblock(' private:') | 336 .Sblock(' private:') |
324 .Append('Params();') | 337 .Append('Params();') |
325 .Append() | 338 .Append() |
326 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') | 339 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') |
327 .Eblock('};') | 340 .Eblock('};') |
328 ) | 341 ) |
329 return c | 342 return c |
330 | 343 |
331 def _GenerateTypes(self, types, is_toplevel=False, generate_typedefs=False): | 344 def _GenerateTypes(self, types, is_toplevel=False, generate_typedefs=False): |
332 """Generate the structures required by a property such as OBJECT classes | 345 """Generate the structures required by a property such as OBJECT classes |
(...skipping 26 matching lines...) Expand all Loading... |
359 def _GenerateFunctionResults(self, callback): | 372 def _GenerateFunctionResults(self, callback): |
360 """Generates namespace for passing a function's result back. | 373 """Generates namespace for passing a function's result back. |
361 """ | 374 """ |
362 c = Code() | 375 c = Code() |
363 (c.Append('namespace Results {') | 376 (c.Append('namespace Results {') |
364 .Append() | 377 .Append() |
365 .Concat(self._GenerateCreateCallbackArguments(callback)) | 378 .Concat(self._GenerateCreateCallbackArguments(callback)) |
366 .Append('} // namespace Results') | 379 .Append('} // namespace Results') |
367 ) | 380 ) |
368 return c | 381 return c |
OLD | NEW |