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 | 6 from model import PropertyType |
7 import cpp_util | 7 import cpp_util |
8 import schema_util | 8 import schema_util |
9 import util_cc_helper | 9 import util_cc_helper |
10 | 10 |
11 class CCGenerator(object): | 11 class CCGenerator(object): |
12 def __init__(self, type_generator, cpp_namespace): | 12 def __init__(self, type_generator, cpp_namespace_pattern): |
13 self._type_generator = type_generator | 13 self._type_generator = type_generator |
14 self._cpp_namespace = cpp_namespace | 14 self._cpp_namespace_pattern = cpp_namespace_pattern |
15 | 15 |
16 def Generate(self, namespace): | 16 def Generate(self, namespace): |
17 return _Generator(namespace, | 17 return _Generator(namespace, |
18 self._type_generator, | 18 self._type_generator, |
19 self._cpp_namespace).Generate() | 19 self._cpp_namespace_pattern).Generate() |
20 | 20 |
21 | 21 |
22 class _Generator(object): | 22 class _Generator(object): |
23 """A .cc generator for a namespace. | 23 """A .cc generator for a namespace. |
24 """ | 24 """ |
25 def __init__(self, namespace, cpp_type_generator, cpp_namespace): | 25 def __init__(self, namespace, cpp_type_generator, cpp_namespace_pattern): |
26 self._namespace = namespace | 26 self._namespace = namespace |
27 self._type_helper = cpp_type_generator | 27 self._type_helper = cpp_type_generator |
28 self._cpp_namespace = cpp_namespace | 28 self._cpp_namespace_pattern = cpp_namespace_pattern |
29 self._target_namespace = ( | |
30 self._type_helper.GetCppNamespaceName(self._namespace)) | |
31 self._util_cc_helper = ( | 29 self._util_cc_helper = ( |
32 util_cc_helper.UtilCCHelper(self._type_helper)) | 30 util_cc_helper.UtilCCHelper(self._type_helper)) |
33 self._generate_error_messages = namespace.compiler_options.get( | 31 self._generate_error_messages = namespace.compiler_options.get( |
34 'generate_error_messages', False) | 32 'generate_error_messages', False) |
35 | 33 |
36 def Generate(self): | 34 def Generate(self): |
37 """Generates a Code object with the .cc for a single namespace. | 35 """Generates a Code object with the .cc for a single namespace. |
38 """ | 36 """ |
| 37 cpp_namespace = cpp_util.GetCppNamespace(self._cpp_namespace_pattern, |
| 38 self._namespace.unix_name) |
| 39 |
39 c = Code() | 40 c = Code() |
40 (c.Append(cpp_util.CHROMIUM_LICENSE) | 41 (c.Append(cpp_util.CHROMIUM_LICENSE) |
41 .Append() | 42 .Append() |
42 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) | 43 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) |
43 .Append() | 44 .Append() |
44 .Append(self._util_cc_helper.GetIncludePath()) | 45 .Append(self._util_cc_helper.GetIncludePath()) |
45 .Append('#include "base/logging.h"') | 46 .Append('#include "base/logging.h"') |
46 .Append('#include "base/strings/string_number_conversions.h"') | 47 .Append('#include "base/strings/string_number_conversions.h"') |
47 .Append('#include "base/strings/utf_string_conversions.h"') | 48 .Append('#include "base/strings/utf_string_conversions.h"') |
48 .Append('#include "%s/%s.h"' % | 49 .Append('#include "%s/%s.h"' % |
49 (self._namespace.source_file_dir, self._namespace.short_filename)) | 50 (self._namespace.source_file_dir, self._namespace.short_filename)) |
50 .Append('#include <set>') | 51 .Append('#include <set>') |
51 .Cblock(self._type_helper.GenerateIncludes(include_soft=True)) | 52 .Cblock(self._type_helper.GenerateIncludes(include_soft=True)) |
52 .Append() | 53 .Append() |
53 .Append('using base::UTF8ToUTF16;') | 54 .Append('using base::UTF8ToUTF16;') |
54 .Append() | 55 .Append() |
55 .Concat(cpp_util.OpenNamespace(self._cpp_namespace)) | 56 .Concat(cpp_util.OpenNamespace(cpp_namespace)) |
56 .Cblock(self._type_helper.GetNamespaceStart()) | |
57 ) | 57 ) |
58 if self._namespace.properties: | 58 if self._namespace.properties: |
59 (c.Append('//') | 59 (c.Append('//') |
60 .Append('// Properties') | 60 .Append('// Properties') |
61 .Append('//') | 61 .Append('//') |
62 .Append() | 62 .Append() |
63 ) | 63 ) |
64 for property in self._namespace.properties.values(): | 64 for property in self._namespace.properties.values(): |
65 property_code = self._type_helper.GeneratePropertyValues( | 65 property_code = self._type_helper.GeneratePropertyValues( |
66 property, | 66 property, |
(...skipping 17 matching lines...) Expand all Loading... |
84 for function in self._namespace.functions.values(): | 84 for function in self._namespace.functions.values(): |
85 c.Cblock(self._GenerateFunction(function)) | 85 c.Cblock(self._GenerateFunction(function)) |
86 if self._namespace.events: | 86 if self._namespace.events: |
87 (c.Append('//') | 87 (c.Append('//') |
88 .Append('// Events') | 88 .Append('// Events') |
89 .Append('//') | 89 .Append('//') |
90 .Append() | 90 .Append() |
91 ) | 91 ) |
92 for event in self._namespace.events.values(): | 92 for event in self._namespace.events.values(): |
93 c.Cblock(self._GenerateEvent(event)) | 93 c.Cblock(self._GenerateEvent(event)) |
94 (c.Concat(self._type_helper.GetNamespaceEnd()) | 94 c.Cblock(cpp_util.CloseNamespace(cpp_namespace)) |
95 .Cblock(cpp_util.CloseNamespace(self._cpp_namespace)) | |
96 ) | |
97 c.Append() | 95 c.Append() |
98 return c | 96 return c |
99 | 97 |
100 def _GenerateType(self, cpp_namespace, type_): | 98 def _GenerateType(self, cpp_namespace, type_): |
101 """Generates the function definitions for a type. | 99 """Generates the function definitions for a type. |
102 """ | 100 """ |
103 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) | 101 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) |
104 c = Code() | 102 c = Code() |
105 | 103 |
106 if type_.functions: | 104 if type_.functions: |
(...skipping 957 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1064 if self._generate_error_messages: | 1062 if self._generate_error_messages: |
1065 params = list(params) + ['base::string16* error'] | 1063 params = list(params) + ['base::string16* error'] |
1066 return ', '.join(str(p) for p in params) | 1064 return ', '.join(str(p) for p in params) |
1067 | 1065 |
1068 def _GenerateArgs(self, args): | 1066 def _GenerateArgs(self, args): |
1069 """Builds the argument list for a function, given an array of arguments. | 1067 """Builds the argument list for a function, given an array of arguments. |
1070 """ | 1068 """ |
1071 if self._generate_error_messages: | 1069 if self._generate_error_messages: |
1072 args = list(args) + ['error'] | 1070 args = list(args) + ['error'] |
1073 return ', '.join(str(a) for a in args) | 1071 return ', '.join(str(a) for a in args) |
OLD | NEW |