Chromium Code Reviews| 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 model | 8 import model |
| 9 import os | 9 import os |
| 10 import schema_util | 10 import schema_util |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 if self._namespace.functions: | 85 if self._namespace.functions: |
| 86 (c.Append('//') | 86 (c.Append('//') |
| 87 .Append('// Functions') | 87 .Append('// Functions') |
| 88 .Append('//') | 88 .Append('//') |
| 89 .Append() | 89 .Append() |
| 90 ) | 90 ) |
| 91 for function in self._namespace.functions.values(): | 91 for function in self._namespace.functions.values(): |
| 92 (c.Concat(self._GenerateFunction(function)) | 92 (c.Concat(self._GenerateFunction(function)) |
| 93 .Append() | 93 .Append() |
| 94 ) | 94 ) |
| 95 if self._namespace.events: | |
| 96 (c.Append('//') | |
| 97 .Append('// Events') | |
| 98 .Append('//') | |
| 99 .Append() | |
| 100 ) | |
| 101 for event in self._namespace.events.values(): | |
| 102 (c.Concat(self._GenerateEvent(event)) | |
| 103 .Append() | |
| 104 ) | |
| 95 (c.Concat(self._cpp_type_generator.GetNamespaceEnd()) | 105 (c.Concat(self._cpp_type_generator.GetNamespaceEnd()) |
| 96 .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | 106 .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) |
| 97 .Append() | 107 .Append() |
| 98 .Append('#endif // %s' % ifndef_name) | 108 .Append('#endif // %s' % ifndef_name) |
| 99 .Append() | 109 .Append() |
| 100 ) | 110 ) |
| 101 return c | 111 return c |
| 102 | 112 |
| 103 def _FieldDependencyOrder(self): | 113 def _FieldDependencyOrder(self): |
| 104 """Generates the list of types in the current namespace in an order in which | 114 """Generates the list of types in the current namespace in an order in which |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 def _GenerateFields(self, props): | 148 def _GenerateFields(self, props): |
| 139 """Generates the field declarations when declaring a type. | 149 """Generates the field declarations when declaring a type. |
| 140 """ | 150 """ |
| 141 c = Code() | 151 c = Code() |
| 142 # Generate the enums needed for any fields with "choices" | 152 # Generate the enums needed for any fields with "choices" |
| 143 for prop in props: | 153 for prop in props: |
| 144 if prop.type_ == PropertyType.CHOICES: | 154 if prop.type_ == PropertyType.CHOICES: |
| 145 enum_name = self._cpp_type_generator.GetChoicesEnumType(prop) | 155 enum_name = self._cpp_type_generator.GetChoicesEnumType(prop) |
| 146 c.Append('%s %s_type;' % (enum_name, prop.unix_name)) | 156 c.Append('%s %s_type;' % (enum_name, prop.unix_name)) |
| 147 c.Append() | 157 c.Append() |
| 148 for prop in self._cpp_type_generator.GetExpandedChoicesInParams(props): | 158 |
| 159 for prop in self._cpp_type_generator.GetAllPossibleParameters(props): | |
| 149 if prop.description: | 160 if prop.description: |
| 150 c.Comment(prop.description) | 161 c.Comment(prop.description) |
| 151 c.Append('%s %s;' % ( | 162 c.Append('%s %s;' % ( |
| 152 self._cpp_type_generator.GetType(prop, wrap_optional=True), | 163 self._cpp_type_generator.GetType(prop, wrap_optional=True), |
| 153 prop.unix_name)) | 164 prop.unix_name)) |
| 154 c.Append() | 165 c.Append() |
| 155 return c | 166 return c |
| 156 | 167 |
| 157 def _GenerateType(self, type_): | 168 def _GenerateType(self, type_): |
| 158 """Generates a struct for a type. | 169 """Generates a struct for a type. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 205 ) | 216 ) |
| 206 | 217 |
| 207 (c.Eblock() | 218 (c.Eblock() |
| 208 .Sblock(' private:') | 219 .Sblock(' private:') |
| 209 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);') | 220 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);') |
| 210 .Eblock('};') | 221 .Eblock('};') |
| 211 ) | 222 ) |
| 212 c.Substitute({'classname': classname}) | 223 c.Substitute({'classname': classname}) |
| 213 return c | 224 return c |
| 214 | 225 |
| 226 def _GenerateEvent(self, event): | |
| 227 """Generates the namespaces for an event. | |
| 228 """ | |
| 229 c = Code() | |
| 230 c.Sblock('namespace %s {' % cpp_util.Classname(event.name)) | |
| 231 c.Concat(self._GenerateCreateCallbackArguments(event)) | |
| 232 c.Eblock('};') | |
|
not at google - send to devlin
2012/07/11 07:22:06
nit: style elsewhere in the file is to chain toget
Matt Tytel
2012/07/12 03:07:56
Done.
| |
| 233 return c | |
| 234 | |
| 215 def _GenerateFunction(self, function): | 235 def _GenerateFunction(self, function): |
| 216 """Generates the structs for a function. | 236 """Generates the namespaces and structs for a function. |
| 217 """ | 237 """ |
| 218 c = Code() | 238 c = Code() |
| 219 (c.Sblock('namespace %s {' % cpp_util.Classname(function.name)) | 239 (c.Sblock('namespace %s {' % cpp_util.Classname(function.name)) |
| 220 .Concat(self._GenerateFunctionParams(function)) | 240 .Concat(self._GenerateFunctionParams(function)) |
| 221 .Append() | 241 .Append() |
| 222 ) | 242 ) |
| 223 if function.callback: | 243 if function.callback: |
| 224 (c.Concat(self._GenerateFunctionResult(function)) | 244 (c.Concat(self._GenerateFunctionResult(function.callback)) |
| 225 .Append() | 245 .Append() |
| 226 ) | 246 ) |
| 227 c.Eblock('};') | 247 c.Eblock('};') |
| 228 | 248 |
| 229 return c | 249 return c |
| 230 | 250 |
| 231 def _GenerateFunctionParams(self, function): | 251 def _GenerateFunctionParams(self, function): |
| 232 """Generates the struct for passing parameters into a function. | 252 """Generates the struct for passing parameters from JSON to a function. |
| 233 """ | 253 """ |
| 234 c = Code() | 254 c = Code() |
| 235 | 255 |
| 236 if function.params: | 256 if function.params: |
| 237 (c.Sblock('struct Params {') | 257 (c.Sblock('struct Params {') |
| 238 .Concat(self._GeneratePropertyStructures(function.params)) | 258 .Concat(self._GeneratePropertyStructures(function.params)) |
| 239 .Concat(self._GenerateFields(function.params)) | 259 .Concat(self._GenerateFields(function.params)) |
| 240 .Append('~Params();') | 260 .Append('~Params();') |
| 241 .Append() | 261 .Append() |
| 242 .Append('static scoped_ptr<Params> Create(const ListValue& args);') | 262 .Append('static scoped_ptr<Params> Create(const ListValue& args);') |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 267 self._cpp_type_generator.GetChoicesEnumType(prop), | 287 self._cpp_type_generator.GetChoicesEnumType(prop), |
| 268 prop, | 288 prop, |
| 269 [choice.type_.name for choice in prop.choices.values()])) | 289 [choice.type_.name for choice in prop.choices.values()])) |
| 270 c.Concat(self._GeneratePropertyStructures(prop.choices.values())) | 290 c.Concat(self._GeneratePropertyStructures(prop.choices.values())) |
| 271 elif prop.type_ == PropertyType.ENUM: | 291 elif prop.type_ == PropertyType.ENUM: |
| 272 enum_name = self._cpp_type_generator.GetType(prop) | 292 enum_name = self._cpp_type_generator.GetType(prop) |
| 273 c.Concat(self._GenerateEnumDeclaration( | 293 c.Concat(self._GenerateEnumDeclaration( |
| 274 enum_name, | 294 enum_name, |
| 275 prop, | 295 prop, |
| 276 prop.enum_values)) | 296 prop.enum_values)) |
| 277 c.Append('static scoped_ptr<Value> CreateEnumValue(%s %s);' % | 297 create_enum_value = ('scoped_ptr<Value> CreateEnumValue(%s %s);' % |
| 278 (enum_name, prop.unix_name)) | 298 (enum_name, prop.unix_name)) |
| 299 # If the property is from the UI then we're in a struct so this function | |
| 300 # should be static. If it's from the client, then we're just in a | |
| 301 # namespace so we can't have the static keyword. | |
| 302 if prop.from_json: | |
| 303 create_enum_value = 'static ' + create_enum_value | |
| 304 c.Append(create_enum_value) | |
| 279 return c | 305 return c |
| 280 | 306 |
| 281 def _GenerateFunctionResult(self, function): | 307 def _GenerateCreateCallbackArguments(self, function): |
| 282 """Generates functions for passing a function's result back. | 308 """Generates functions for passing paramaters to a callback. |
| 283 """ | 309 """ |
| 284 c = Code() | 310 c = Code() |
| 311 params = function.params | |
| 312 c.Concat(self._GeneratePropertyStructures(params)) | |
| 285 | 313 |
| 286 c.Sblock('namespace Result {') | 314 param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params) |
| 287 params = function.callback.params | 315 for param_list in param_lists: |
| 288 if not params: | 316 declaration_list = [] |
| 289 c.Append('Value* Create();') | 317 for param in param_list: |
| 290 else: | |
| 291 c.Concat(self._GeneratePropertyStructures(params)) | |
| 292 | |
| 293 # If there is a single parameter, this is straightforward. However, if | |
| 294 # the callback parameter is of 'choices', this generates a Create method | |
| 295 # for each choice. This works because only 1 choice can be returned at a | |
| 296 # time. | |
| 297 for param in self._cpp_type_generator.GetExpandedChoicesInParams(params): | |
| 298 if param.description: | 318 if param.description: |
| 299 c.Comment(param.description) | 319 c.Comment(param.description) |
| 300 if param.type_ == PropertyType.ANY: | 320 declaration_list.append('const %s' % cpp_util.GetParameterDeclaration( |
| 301 c.Comment("Value* Result::Create(Value*) not generated " | |
| 302 "because it's redundant.") | |
| 303 continue | |
| 304 c.Append('Value* Create(const %s);' % cpp_util.GetParameterDeclaration( | |
| 305 param, self._cpp_type_generator.GetType(param))) | 321 param, self._cpp_type_generator.GetType(param))) |
| 322 | |
| 323 c.Append('ListValue* Create(%s);' % | |
| 324 ', '.join(declaration_list)) | |
|
not at google - send to devlin
2012/07/11 07:22:06
scoped_ptr, can fit on 1 line?
Matt Tytel
2012/07/12 03:07:56
Done.
| |
| 325 return c | |
| 326 | |
| 327 def _GenerateFunctionResult(self, callback): | |
| 328 """Generates namespace for passing a function's result back. | |
| 329 """ | |
| 330 c = Code() | |
| 331 c.Sblock('namespace Result {') | |
| 332 c.Concat(self._GenerateCreateCallbackArguments(callback)) | |
| 306 c.Eblock('};') | 333 c.Eblock('};') |
|
not at google - send to devlin
2012/07/11 07:22:06
ditto with
return (Code()
...)
Matt Tytel
2012/07/12 03:07:56
Done.
| |
| 307 | 334 |
| 308 return c | 335 return c |
| OLD | NEW |