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 any_helper | 7 import any_helper |
| 8 import cpp_util | 8 import cpp_util |
| 9 import model | 9 import model |
| 10 import schema_util | 10 import schema_util |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 35 .Append('#include "%s/%s.h"' % | 35 .Append('#include "%s/%s.h"' % |
| 36 (self._namespace.source_file_dir, self._namespace.unix_name)) | 36 (self._namespace.source_file_dir, self._namespace.unix_name)) |
| 37 ) | 37 ) |
| 38 includes = self._cpp_type_generator.GenerateIncludes() | 38 includes = self._cpp_type_generator.GenerateIncludes() |
| 39 if not includes.IsEmpty(): | 39 if not includes.IsEmpty(): |
| 40 (c.Concat(includes) | 40 (c.Concat(includes) |
| 41 .Append() | 41 .Append() |
| 42 ) | 42 ) |
| 43 | 43 |
| 44 (c.Append() | 44 (c.Append() |
| 45 .Append('using base::Value;') | |
| 46 .Append('using base::DictionaryValue;') | |
| 47 .Append('using base::ListValue;') | |
| 48 .Append('using base::BinaryValue;') | |
|
not at google - send to devlin
2012/06/12 18:34:38
why?
This is a cc file. I would prefer to keep th
benjhayden
2012/06/12 20:15:42
I ran into a very confusing name collision with an
| |
| 49 .Append('using %s;' % any_helper.ANY_CLASS) | |
| 50 .Append() | |
| 51 .Concat(self._cpp_type_generator.GetRootNamespaceStart()) | 45 .Concat(self._cpp_type_generator.GetRootNamespaceStart()) |
| 52 .Concat(self._cpp_type_generator.GetNamespaceStart()) | 46 .Concat(self._cpp_type_generator.GetNamespaceStart()) |
| 53 .Append() | 47 .Append() |
| 54 ) | 48 ) |
| 55 if self._namespace.properties: | 49 if self._namespace.properties: |
| 56 (c.Append('//') | 50 (c.Append('//') |
| 57 .Append('// Properties') | 51 .Append('// Properties') |
| 58 .Append('//') | 52 .Append('//') |
| 59 .Append() | 53 .Append() |
| 60 ) | 54 ) |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 | 160 |
| 167 def _GenerateTypePopulate(self, cpp_namespace, type_): | 161 def _GenerateTypePopulate(self, cpp_namespace, type_): |
| 168 """Generates the function for populating a type given a pointer to it. | 162 """Generates the function for populating a type given a pointer to it. |
| 169 | 163 |
| 170 E.g for type "Foo", generates Foo::Populate() | 164 E.g for type "Foo", generates Foo::Populate() |
| 171 """ | 165 """ |
| 172 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name)) | 166 classname = cpp_util.Classname(schema_util.StripSchemaNamespace(type_.name)) |
| 173 c = Code() | 167 c = Code() |
| 174 (c.Append('// static') | 168 (c.Append('// static') |
| 175 .Sblock('bool %(namespace)s::Populate' | 169 .Sblock('bool %(namespace)s::Populate' |
| 176 '(const Value& value, %(name)s* out) {') | 170 '(const base::Value& value, %(name)s* out) {') |
| 177 .Append('if (!value.IsType(Value::TYPE_DICTIONARY))') | 171 .Append('if (!value.IsType(base::Value::TYPE_DICTIONARY))') |
| 178 .Append(' return false;') | 172 .Append(' return false;') |
| 179 ) | 173 ) |
| 180 if type_.properties: | 174 if type_.properties: |
| 181 (c.Append('const DictionaryValue* dict = ' | 175 (c.Append('const base::DictionaryValue* dict = ' |
| 182 'static_cast<const DictionaryValue*>(&value);') | 176 'static_cast<const base::DictionaryValue*>(&value);') |
| 183 .Append() | 177 .Append() |
| 184 ) | 178 ) |
| 185 for prop in type_.properties.values(): | 179 for prop in type_.properties.values(): |
| 186 c.Concat(self._InitializePropertyToDefault(prop, 'out')) | 180 c.Concat(self._InitializePropertyToDefault(prop, 'out')) |
| 187 for prop in type_.properties.values(): | 181 for prop in type_.properties.values(): |
| 188 if prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: | 182 if prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: |
| 189 c.Append('out->additional_properties.MergeDictionary(dict);') | 183 c.Append('out->additional_properties.MergeDictionary(dict);') |
| 190 # remove all keys that are actual properties | 184 # remove all keys that are actual properties |
| 191 for cur_prop in type_.properties.values(): | 185 for cur_prop in type_.properties.values(): |
| 192 if prop != cur_prop: | 186 if prop != cur_prop: |
| 193 c.Append('out->additional_properties' | 187 c.Append('out->additional_properties' |
| 194 '.RemoveWithoutPathExpansion("%s", NULL);' % cur_prop.name) | 188 '.RemoveWithoutPathExpansion("%s", NULL);' % cur_prop.name) |
| 195 c.Append() | 189 c.Append() |
| 196 else: | 190 else: |
| 197 c.Concat(self._GenerateTypePopulateProperty(prop, 'dict', 'out')) | 191 c.Concat(self._GenerateTypePopulateProperty(prop, 'dict', 'out')) |
| 198 (c.Append('return true;') | 192 (c.Append('return true;') |
| 199 .Eblock('}') | 193 .Eblock('}') |
| 200 ) | 194 ) |
| 201 c.Substitute({'namespace': cpp_namespace, 'name': classname}) | 195 c.Substitute({'namespace': cpp_namespace, 'name': classname}) |
| 202 return c | 196 return c |
| 203 | 197 |
| 204 def _GenerateTypePopulateProperty(self, prop, src, dst): | 198 def _GenerateTypePopulateProperty(self, prop, src, dst): |
| 205 """Generate the code to populate a single property in a type. | 199 """Generate the code to populate a single property in a type. |
| 206 | 200 |
| 207 src: DictionaryValue* | 201 src: base::DictionaryValue* |
| 208 dst: Type* | 202 dst: Type* |
| 209 """ | 203 """ |
| 210 c = Code() | 204 c = Code() |
| 211 value_var = prop.unix_name + '_value' | 205 value_var = prop.unix_name + '_value' |
| 212 c.Append('Value* %(value_var)s = NULL;') | 206 c.Append('base::Value* %(value_var)s = NULL;') |
| 213 if prop.optional: | 207 if prop.optional: |
| 214 (c.Sblock( | 208 (c.Sblock( |
| 215 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {' | 209 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {' |
| 216 ) | 210 ) |
| 217 .Concat(self._GeneratePopulatePropertyFromValue( | 211 .Concat(self._GeneratePopulatePropertyFromValue( |
| 218 prop, value_var, dst, 'false')) | 212 prop, value_var, dst, 'false')) |
| 219 .Eblock('}') | 213 .Eblock('}') |
| 220 ) | 214 ) |
| 221 else: | 215 else: |
| 222 (c.Append( | 216 (c.Append( |
| 223 'if (!%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s))') | 217 'if (!%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s))') |
| 224 .Append(' return false;') | 218 .Append(' return false;') |
| 225 .Concat(self._GeneratePopulatePropertyFromValue( | 219 .Concat(self._GeneratePopulatePropertyFromValue( |
| 226 prop, value_var, dst, 'false')) | 220 prop, value_var, dst, 'false')) |
| 227 ) | 221 ) |
| 228 c.Append() | 222 c.Append() |
| 229 c.Substitute({'value_var': value_var, 'key': prop.name, 'src': src}) | 223 c.Substitute({'value_var': value_var, 'key': prop.name, 'src': src}) |
| 230 return c | 224 return c |
| 231 | 225 |
| 232 def _GenerateTypeToValue(self, cpp_namespace, type_): | 226 def _GenerateTypeToValue(self, cpp_namespace, type_): |
| 233 """Generates a function that serializes the type into a |DictionaryValue|. | 227 """Generates a function that serializes the type into a |
| 228 |base::DictionaryValue|. | |
| 234 | 229 |
| 235 E.g. for type "Foo" generates Foo::ToValue() | 230 E.g. for type "Foo" generates Foo::ToValue() |
| 236 """ | 231 """ |
| 237 c = Code() | 232 c = Code() |
| 238 (c.Sblock('scoped_ptr<DictionaryValue> %s::ToValue() const {' % | 233 (c.Sblock('scoped_ptr<base::DictionaryValue> %s::ToValue() const {' % |
| 239 cpp_namespace) | 234 cpp_namespace) |
| 240 .Append('scoped_ptr<DictionaryValue> value(new DictionaryValue());') | 235 .Append('scoped_ptr<base::DictionaryValue> value(' + |
| 236 'new base::DictionaryValue());') | |
| 241 .Append() | 237 .Append() |
| 242 ) | 238 ) |
| 243 for prop in type_.properties.values(): | 239 for prop in type_.properties.values(): |
| 244 if prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: | 240 if prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: |
| 245 c.Append('value->MergeDictionary(&%s);' % prop.unix_name) | 241 c.Append('value->MergeDictionary(&%s);' % prop.unix_name) |
| 246 else: | 242 else: |
| 247 if prop.optional: | 243 if prop.optional: |
| 248 if prop.type_ == PropertyType.ENUM: | 244 if prop.type_ == PropertyType.ENUM: |
| 249 c.Sblock('if (%s != %s)' % | 245 c.Sblock('if (%s != %s)' % |
| 250 (prop.unix_name, | 246 (prop.unix_name, |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 280 | 276 |
| 281 # Result::Create function | 277 # Result::Create function |
| 282 if function.callback: | 278 if function.callback: |
| 283 c.Concat(self._GenerateFunctionResultCreate(cpp_namespace, function)) | 279 c.Concat(self._GenerateFunctionResultCreate(cpp_namespace, function)) |
| 284 | 280 |
| 285 c.Substitute({'cpp_namespace': cpp_namespace}) | 281 c.Substitute({'cpp_namespace': cpp_namespace}) |
| 286 | 282 |
| 287 return c | 283 return c |
| 288 | 284 |
| 289 def _GenerateCreateEnumValue(self, cpp_namespace, prop): | 285 def _GenerateCreateEnumValue(self, cpp_namespace, prop): |
| 290 """Generates CreateEnumValue() that returns the |StringValue| | 286 """Generates CreateEnumValue() that returns the |base::StringValue| |
| 291 representation of an enum. | 287 representation of an enum. |
| 292 """ | 288 """ |
| 293 c = Code() | 289 c = Code() |
| 294 c.Append('// static') | 290 c.Append('// static') |
| 295 c.Sblock('scoped_ptr<Value> %(cpp_namespace)s::CreateEnumValue(%(arg)s) {') | 291 c.Sblock('scoped_ptr<base::Value> %(cpp_namespace)s::CreateEnumValue(' + |
| 292 '%(arg)s) {') | |
| 296 c.Sblock('switch (%s) {' % prop.unix_name) | 293 c.Sblock('switch (%s) {' % prop.unix_name) |
| 297 if prop.optional: | 294 if prop.optional: |
| 298 (c.Append('case %s: {' % self._cpp_type_generator.GetEnumNoneValue(prop)) | 295 (c.Append('case %s: {' % self._cpp_type_generator.GetEnumNoneValue(prop)) |
| 299 .Append(' return scoped_ptr<Value>();') | 296 .Append(' return scoped_ptr<base::Value>();') |
| 300 .Append('}') | 297 .Append('}') |
| 301 ) | 298 ) |
| 302 for enum_value in prop.enum_values: | 299 for enum_value in prop.enum_values: |
| 303 (c.Append('case %s: {' % | 300 (c.Append('case %s: {' % |
| 304 self._cpp_type_generator.GetEnumValue(prop, enum_value)) | 301 self._cpp_type_generator.GetEnumValue(prop, enum_value)) |
| 305 .Append(' return scoped_ptr<Value>(Value::CreateStringValue("%s"));' % | 302 .Append(' return scoped_ptr<base::Value>(' + |
| 306 enum_value) | 303 'base::Value::CreateStringValue("%s"));' % |
| 304 enum_value) | |
| 307 .Append('}') | 305 .Append('}') |
| 308 ) | 306 ) |
| 309 (c.Append('default: {') | 307 (c.Append('default: {') |
| 310 .Append(' return scoped_ptr<Value>();') | 308 .Append(' return scoped_ptr<base::Value>();') |
| 311 .Append('}') | 309 .Append('}') |
| 312 ) | 310 ) |
| 313 c.Eblock('}') | 311 c.Eblock('}') |
| 314 c.Eblock('}') | 312 c.Eblock('}') |
| 315 c.Substitute({ | 313 c.Substitute({ |
| 316 'cpp_namespace': cpp_namespace, | 314 'cpp_namespace': cpp_namespace, |
| 317 'arg': cpp_util.GetParameterDeclaration( | 315 'arg': cpp_util.GetParameterDeclaration( |
| 318 prop, self._cpp_type_generator.GetType(prop)) | 316 prop, self._cpp_type_generator.GetType(prop)) |
| 319 }) | 317 }) |
| 320 return c | 318 return c |
| 321 | 319 |
| 322 def _CreateValueFromProperty(self, prop, var): | 320 def _CreateValueFromProperty(self, prop, var): |
| 323 """Creates a Value given a property. Generated code passes ownership | 321 """Creates a base::Value given a property. Generated code passes ownership |
| 324 to caller. | 322 to caller. |
| 325 | 323 |
| 326 var: variable or variable* | 324 var: variable or variable* |
| 327 | 325 |
| 328 E.g for std::string, generate Value::CreateStringValue(var) | 326 E.g for std::string, generate base::Value::CreateStringValue(var) |
| 329 """ | 327 """ |
| 330 if prop.type_ == PropertyType.CHOICES: | 328 if prop.type_ == PropertyType.CHOICES: |
| 331 # CHOICES conversion not implemented. If needed, write something to | 329 # CHOICES conversion not implemented. If needed, write something to |
| 332 # generate a function that returns a scoped_ptr<Value> and put it in | 330 # generate a function that returns a scoped_ptr<base::Value> and put it in |
| 333 # _GeneratePropertyFunctions, then use it here. Look at CreateEnumValue() | 331 # _GeneratePropertyFunctions, then use it here. Look at CreateEnumValue() |
| 334 # for reference. | 332 # for reference. |
| 335 raise NotImplementedError( | 333 raise NotImplementedError( |
| 336 'Conversion of CHOICES to Value not implemented') | 334 'Conversion of CHOICES to base::Value not implemented') |
| 337 if self._IsObjectOrObjectRef(prop): | 335 if self._IsObjectOrObjectRef(prop): |
| 338 if prop.optional: | 336 if prop.optional: |
| 339 return '%s->ToValue().release()' % var | 337 return '%s->ToValue().release()' % var |
| 340 else: | 338 else: |
| 341 return '%s.ToValue().release()' % var | 339 return '%s.ToValue().release()' % var |
| 342 elif prop.type_ == PropertyType.ANY: | 340 elif prop.type_ == PropertyType.ANY: |
| 343 return '%s.DeepCopy()' % self._any_helper.GetValue(prop, var) | 341 return '%s.DeepCopy()' % self._any_helper.GetValue(prop, var) |
| 344 elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: | 342 elif prop.type_ == PropertyType.ADDITIONAL_PROPERTIES: |
| 345 return '%s.DeepCopy()' % var | 343 return '%s.DeepCopy()' % var |
| 346 elif prop.type_ == PropertyType.ENUM: | 344 elif prop.type_ == PropertyType.ENUM: |
| 347 return 'CreateEnumValue(%s).release()' % var | 345 return 'CreateEnumValue(%s).release()' % var |
| 348 elif self._IsArrayOrArrayRef(prop): | 346 elif self._IsArrayOrArrayRef(prop): |
| 349 return '%s.release()' % self._util_cc_helper.CreateValueFromArray( | 347 return '%s.release()' % self._util_cc_helper.CreateValueFromArray( |
| 350 self._cpp_type_generator.GetReferencedProperty(prop), var, | 348 self._cpp_type_generator.GetReferencedProperty(prop), var, |
| 351 prop.optional) | 349 prop.optional) |
| 352 elif self._IsFundamentalOrFundamentalRef(prop): | 350 elif self._IsFundamentalOrFundamentalRef(prop): |
| 353 if prop.optional: | 351 if prop.optional: |
| 354 var = '*' + var | 352 var = '*' + var |
| 355 prop = self._cpp_type_generator.GetReferencedProperty(prop); | 353 prop = self._cpp_type_generator.GetReferencedProperty(prop); |
| 356 return { | 354 return { |
| 357 PropertyType.STRING: 'Value::CreateStringValue(%s)', | 355 PropertyType.STRING: 'base::Value::CreateStringValue(%s)', |
| 358 PropertyType.BOOLEAN: 'Value::CreateBooleanValue(%s)', | 356 PropertyType.BOOLEAN: 'base::Value::CreateBooleanValue(%s)', |
| 359 PropertyType.INTEGER: 'Value::CreateIntegerValue(%s)', | 357 PropertyType.INTEGER: 'base::Value::CreateIntegerValue(%s)', |
| 360 PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)', | 358 PropertyType.DOUBLE: 'base::Value::CreateDoubleValue(%s)', |
| 361 }[prop.type_] % var | 359 }[prop.type_] % var |
| 362 else: | 360 else: |
| 363 raise NotImplementedError('Conversion of %s to Value not ' | 361 raise NotImplementedError('Conversion of %s to base::Value not ' |
| 364 'implemented' % repr(prop.type_)) | 362 'implemented' % repr(prop.type_)) |
| 365 | 363 |
| 366 def _GenerateParamsCheck(self, function, var): | 364 def _GenerateParamsCheck(self, function, var): |
| 367 """Generates a check for the correct number of arguments when creating | 365 """Generates a check for the correct number of arguments when creating |
| 368 Params. | 366 Params. |
| 369 """ | 367 """ |
| 370 c = Code() | 368 c = Code() |
| 371 num_required = 0 | 369 num_required = 0 |
| 372 for param in function.params: | 370 for param in function.params: |
| 373 if not param.optional: | 371 if not param.optional: |
| 374 num_required += 1 | 372 num_required += 1 |
| 375 if num_required == len(function.params): | 373 if num_required == len(function.params): |
| 376 c.Append('if (%(var)s.GetSize() != %(total)d)') | 374 c.Append('if (%(var)s.GetSize() != %(total)d)') |
| 377 elif not num_required: | 375 elif not num_required: |
| 378 c.Append('if (%(var)s.GetSize() > %(total)d)') | 376 c.Append('if (%(var)s.GetSize() > %(total)d)') |
| 379 else: | 377 else: |
| 380 c.Append('if (%(var)s.GetSize() < %(required)d' | 378 c.Append('if (%(var)s.GetSize() < %(required)d' |
| 381 ' || %(var)s.GetSize() > %(total)d)') | 379 ' || %(var)s.GetSize() > %(total)d)') |
| 382 c.Append(' return scoped_ptr<Params>();') | 380 c.Append(' return scoped_ptr<Params>();') |
| 383 c.Substitute({ | 381 c.Substitute({ |
| 384 'var': var, | 382 'var': var, |
| 385 'required': num_required, | 383 'required': num_required, |
| 386 'total': len(function.params), | 384 'total': len(function.params), |
| 387 }) | 385 }) |
| 388 return c | 386 return c |
| 389 | 387 |
| 390 def _GenerateFunctionParamsCreate(self, cpp_namespace, function): | 388 def _GenerateFunctionParamsCreate(self, cpp_namespace, function): |
| 391 """Generate function to create an instance of Params. The generated | 389 """Generate function to create an instance of Params. The generated |
| 392 function takes a ListValue of arguments. | 390 function takes a base::ListValue of arguments. |
| 393 | 391 |
| 394 E.g for function "Bar", generate Bar::Params::Create() | 392 E.g for function "Bar", generate Bar::Params::Create() |
| 395 """ | 393 """ |
| 396 c = Code() | 394 c = Code() |
| 397 (c.Append('// static') | 395 (c.Append('// static') |
| 398 .Sblock('scoped_ptr<%(cpp_namespace)s::Params> ' | 396 .Sblock('scoped_ptr<%(cpp_namespace)s::Params> ' |
| 399 '%(cpp_namespace)s::Params::Create(const ListValue& args) {') | 397 '%(cpp_namespace)s::Params::Create(const base::ListValue& args) {') |
| 400 .Concat(self._GenerateParamsCheck(function, 'args')) | 398 .Concat(self._GenerateParamsCheck(function, 'args')) |
| 401 .Append('scoped_ptr<Params> params(new Params());') | 399 .Append('scoped_ptr<Params> params(new Params());') |
| 402 ) | 400 ) |
| 403 c.Substitute({'cpp_namespace': cpp_namespace}) | 401 c.Substitute({'cpp_namespace': cpp_namespace}) |
| 404 | 402 |
| 405 for param in function.params: | 403 for param in function.params: |
| 406 c.Concat(self._InitializePropertyToDefault(param, 'params')) | 404 c.Concat(self._InitializePropertyToDefault(param, 'params')) |
| 407 | 405 |
| 408 for i, param in enumerate(function.params): | 406 for i, param in enumerate(function.params): |
| 409 # Any failure will cause this function to return. If any argument is | 407 # Any failure will cause this function to return. If any argument is |
| 410 # incorrect or missing, those following it are not processed. Note that | 408 # incorrect or missing, those following it are not processed. Note that |
| 411 # for optional arguments, we allow missing arguments and proceed because | 409 # for optional arguments, we allow missing arguments and proceed because |
| 412 # there may be other arguments following it. | 410 # there may be other arguments following it. |
| 413 failure_value = 'scoped_ptr<Params>()' | 411 failure_value = 'scoped_ptr<Params>()' |
| 414 c.Append() | 412 c.Append() |
| 415 value_var = param.unix_name + '_value' | 413 value_var = param.unix_name + '_value' |
| 416 (c.Append('Value* %(value_var)s = NULL;') | 414 (c.Append('base::Value* %(value_var)s = NULL;') |
| 417 .Append('if (args.Get(%(i)s, &%(value_var)s) && ' | 415 .Append('if (args.Get(%(i)s, &%(value_var)s) && ' |
| 418 '!%(value_var)s->IsType(Value::TYPE_NULL))') | 416 '!%(value_var)s->IsType(base::Value::TYPE_NULL))') |
| 419 .Sblock('{') | 417 .Sblock('{') |
| 420 .Concat(self._GeneratePopulatePropertyFromValue( | 418 .Concat(self._GeneratePopulatePropertyFromValue( |
| 421 param, value_var, 'params', failure_value)) | 419 param, value_var, 'params', failure_value)) |
| 422 .Eblock('}') | 420 .Eblock('}') |
| 423 ) | 421 ) |
| 424 if not param.optional: | 422 if not param.optional: |
| 425 (c.Sblock('else {') | 423 (c.Sblock('else {') |
| 426 .Append('return %s;' % failure_value) | 424 .Append('return %s;' % failure_value) |
| 427 .Eblock('}') | 425 .Eblock('}') |
| 428 ) | 426 ) |
| 429 c.Substitute({'value_var': value_var, 'i': i}) | 427 c.Substitute({'value_var': value_var, 'i': i}) |
| 430 (c.Append() | 428 (c.Append() |
| 431 .Append('return params.Pass();') | 429 .Append('return params.Pass();') |
| 432 .Eblock('}') | 430 .Eblock('}') |
| 433 .Append() | 431 .Append() |
| 434 ) | 432 ) |
| 435 | 433 |
| 436 return c | 434 return c |
| 437 | 435 |
| 438 def _GeneratePopulatePropertyFromValue( | 436 def _GeneratePopulatePropertyFromValue( |
| 439 self, prop, value_var, dst, failure_value, check_type=True): | 437 self, prop, value_var, dst, failure_value, check_type=True): |
| 440 """Generates code to populate a model.Property given a Value*. The | 438 """Generates code to populate a model.Property given a base::Value*. The |
| 441 existence of data inside the Value* is assumed so checks for existence | 439 existence of data inside the base::Value* is assumed so checks for existence |
| 442 should be performed before the code this generates. | 440 should be performed before the code this generates. |
| 443 | 441 |
| 444 prop: the property the code is populating. | 442 prop: the property the code is populating. |
| 445 value_var: a Value* that should represent |prop|. | 443 value_var: a base::Value* that should represent |prop|. |
| 446 dst: the object with |prop| as a member. | 444 dst: the object with |prop| as a member. |
| 447 failure_value: the value to return if |prop| cannot be extracted from | 445 failure_value: the value to return if |prop| cannot be extracted from |
| 448 |value_var| | 446 |value_var| |
| 449 check_type: if true, will check if |value_var| is the correct Value::Type | 447 check_type: if true, will check if |value_var| is the correct |
| 448 base::Value::Type | |
| 450 """ | 449 """ |
| 451 c = Code() | 450 c = Code() |
| 452 c.Sblock('{') | 451 c.Sblock('{') |
| 453 | 452 |
| 454 if self._IsFundamentalOrFundamentalRef(prop): | 453 if self._IsFundamentalOrFundamentalRef(prop): |
| 455 if prop.optional: | 454 if prop.optional: |
| 456 (c.Append('%(ctype)s temp;') | 455 (c.Append('%(ctype)s temp;') |
| 457 .Append('if (!%s)' % | 456 .Append('if (!%s)' % |
| 458 cpp_util.GetAsFundamentalValue( | 457 cpp_util.GetAsFundamentalValue( |
| 459 self._cpp_type_generator.GetReferencedProperty(prop), | 458 self._cpp_type_generator.GetReferencedProperty(prop), |
| 460 value_var, | 459 value_var, |
| 461 '&temp')) | 460 '&temp')) |
| 462 .Append(' return %(failure_value)s;') | 461 .Append(' return %(failure_value)s;') |
| 463 .Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));') | 462 .Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));') |
| 464 ) | 463 ) |
| 465 else: | 464 else: |
| 466 (c.Append('if (!%s)' % | 465 (c.Append('if (!%s)' % |
| 467 cpp_util.GetAsFundamentalValue( | 466 cpp_util.GetAsFundamentalValue( |
| 468 self._cpp_type_generator.GetReferencedProperty(prop), | 467 self._cpp_type_generator.GetReferencedProperty(prop), |
| 469 value_var, | 468 value_var, |
| 470 '&%s->%s' % (dst, prop.unix_name))) | 469 '&%s->%s' % (dst, prop.unix_name))) |
| 471 .Append(' return %(failure_value)s;') | 470 .Append(' return %(failure_value)s;') |
| 472 ) | 471 ) |
| 473 elif self._IsObjectOrObjectRef(prop): | 472 elif self._IsObjectOrObjectRef(prop): |
| 474 if prop.optional: | 473 if prop.optional: |
| 475 (c.Append('DictionaryValue* dictionary = NULL;') | 474 (c.Append('base::DictionaryValue* dictionary = NULL;') |
| 476 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') | 475 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') |
| 477 .Append(' return %(failure_value)s;') | 476 .Append(' return %(failure_value)s;') |
| 478 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());') | 477 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());') |
| 479 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))') | 478 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))') |
| 480 .Append(' return %(failure_value)s;') | 479 .Append(' return %(failure_value)s;') |
| 481 .Append('%(dst)s->%(name)s = temp.Pass();') | 480 .Append('%(dst)s->%(name)s = temp.Pass();') |
| 482 ) | 481 ) |
| 483 else: | 482 else: |
| 484 (c.Append('DictionaryValue* dictionary = NULL;') | 483 (c.Append('base::DictionaryValue* dictionary = NULL;') |
| 485 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') | 484 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') |
| 486 .Append(' return %(failure_value)s;') | 485 .Append(' return %(failure_value)s;') |
| 487 .Append( | 486 .Append( |
| 488 'if (!%(ctype)s::Populate(*dictionary, &%(dst)s->%(name)s))') | 487 'if (!%(ctype)s::Populate(*dictionary, &%(dst)s->%(name)s))') |
| 489 .Append(' return %(failure_value)s;') | 488 .Append(' return %(failure_value)s;') |
| 490 ) | 489 ) |
| 491 elif prop.type_ == PropertyType.ANY: | 490 elif prop.type_ == PropertyType.ANY: |
| 492 if prop.optional: | 491 if prop.optional: |
| 493 c.Append('%(dst)s->%(name)s.reset(new Any());') | 492 c.Append('%(dst)s->%(name)s.reset(new ' + any_helper.ANY_CLASS + '());') |
|
not at google - send to devlin
2012/06/12 18:34:38
Is this the only non *Value -> base::*Value change
benjhayden
2012/06/12 20:15:42
See my response to your other comment in this file
| |
| 494 c.Append(self._any_helper.Init(prop, value_var, dst) + ';') | 493 c.Append(self._any_helper.Init(prop, value_var, dst) + ';') |
| 495 elif self._IsArrayOrArrayRef(prop): | 494 elif self._IsArrayOrArrayRef(prop): |
| 496 # util_cc_helper deals with optional and required arrays | 495 # util_cc_helper deals with optional and required arrays |
| 497 (c.Append('ListValue* list = NULL;') | 496 (c.Append('base::ListValue* list = NULL;') |
| 498 .Append('if (!%(value_var)s->GetAsList(&list))') | 497 .Append('if (!%(value_var)s->GetAsList(&list))') |
| 499 .Append(' return %(failure_value)s;') | 498 .Append(' return %(failure_value)s;') |
| 500 .Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( | 499 .Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( |
| 501 self._cpp_type_generator.GetReferencedProperty(prop), 'list', | 500 self._cpp_type_generator.GetReferencedProperty(prop), 'list', |
| 502 dst + '->' + prop.unix_name, prop.optional)) | 501 dst + '->' + prop.unix_name, prop.optional)) |
| 503 .Append(' return %(failure_value)s;') | 502 .Append(' return %(failure_value)s;') |
| 504 ) | 503 ) |
| 505 elif prop.type_ == PropertyType.CHOICES: | 504 elif prop.type_ == PropertyType.CHOICES: |
| 506 type_var = '%(dst)s->%(name)s_type' | 505 type_var = '%(dst)s->%(name)s_type' |
| 507 c.Sblock('switch (%(value_var)s->GetType()) {') | 506 c.Sblock('switch (%(value_var)s->GetType()) {') |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 533 .Append(' %s->%s = %s;' % ( | 532 .Append(' %s->%s = %s;' % ( |
| 534 dst, | 533 dst, |
| 535 prop.unix_name, | 534 prop.unix_name, |
| 536 self._cpp_type_generator.GetEnumValue(prop, enum_value))) | 535 self._cpp_type_generator.GetEnumValue(prop, enum_value))) |
| 537 ) | 536 ) |
| 538 (c.Append('else') | 537 (c.Append('else') |
| 539 .Append(' return %(failure_value)s;') | 538 .Append(' return %(failure_value)s;') |
| 540 ) | 539 ) |
| 541 elif prop.type_ == PropertyType.BINARY: | 540 elif prop.type_ == PropertyType.BINARY: |
| 542 # This is the same if the property is optional or not. We need a pointer | 541 # This is the same if the property is optional or not. We need a pointer |
| 543 # to the BinaryValue to be able to populate it, so a scoped_ptr is used | 542 # to the base::BinaryValue to be able to populate it, so a scoped_ptr is |
| 544 # whether it is optional or required. | 543 # used whether it is optional or required. |
| 545 (c.Append('if (!%(value_var)s->IsType(%(value_type)s))') | 544 (c.Append('if (!%(value_var)s->IsType(%(value_type)s))') |
| 546 .Append(' return %(failure_value)s;') | 545 .Append(' return %(failure_value)s;') |
| 547 .Append('%(dst)s->%(name)s.reset(') | 546 .Append('%(dst)s->%(name)s.reset(') |
| 548 .Append(' static_cast<BinaryValue*>(%(value_var)s)->DeepCopy());') | 547 .Append(' static_cast<base::BinaryValue*>(%(value_var)s)' + |
| 548 '->DeepCopy());') | |
| 549 ) | 549 ) |
| 550 else: | 550 else: |
| 551 raise NotImplementedError(prop.type_) | 551 raise NotImplementedError(prop.type_) |
| 552 c.Eblock('}') | 552 c.Eblock('}') |
| 553 sub = { | 553 sub = { |
| 554 'value_var': value_var, | 554 'value_var': value_var, |
| 555 'name': prop.unix_name, | 555 'name': prop.unix_name, |
| 556 'dst': dst, | 556 'dst': dst, |
| 557 'failure_value': failure_value, | 557 'failure_value': failure_value, |
| 558 } | 558 } |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 584 | 584 |
| 585 def _GenerateFunctionResultCreate(self, cpp_namespace, function): | 585 def _GenerateFunctionResultCreate(self, cpp_namespace, function): |
| 586 """Generate function to create a Result given the return value. | 586 """Generate function to create a Result given the return value. |
| 587 | 587 |
| 588 E.g for function "Bar", generate Bar::Result::Create | 588 E.g for function "Bar", generate Bar::Result::Create |
| 589 """ | 589 """ |
| 590 c = Code() | 590 c = Code() |
| 591 params = function.callback.params | 591 params = function.callback.params |
| 592 | 592 |
| 593 if not params: | 593 if not params: |
| 594 (c.Append('Value* %s::Result::Create() {' % cpp_namespace) | 594 (c.Append('base::Value* %s::Result::Create() {' % cpp_namespace) |
| 595 .Append(' return Value::CreateNullValue();') | 595 .Append(' return base::Value::CreateNullValue();') |
| 596 .Append('}') | 596 .Append('}') |
| 597 ) | 597 ) |
| 598 else: | 598 else: |
| 599 expanded_params = self._cpp_type_generator.GetExpandedChoicesInParams( | 599 expanded_params = self._cpp_type_generator.GetExpandedChoicesInParams( |
| 600 params) | 600 params) |
| 601 c.Concat(self._GeneratePropertyFunctions( | 601 c.Concat(self._GeneratePropertyFunctions( |
| 602 cpp_namespace + '::Result', expanded_params)) | 602 cpp_namespace + '::Result', expanded_params)) |
| 603 | 603 |
| 604 # If there is a single parameter, this is straightforward. However, if | 604 # If there is a single parameter, this is straightforward. However, if |
| 605 # the callback parameter is of 'choices', this generates a Create method | 605 # the callback parameter is of 'choices', this generates a Create method |
| 606 # for each choice. This works because only 1 choice can be returned at a | 606 # for each choice. This works because only 1 choice can be returned at a |
| 607 # time. | 607 # time. |
| 608 for param in expanded_params: | 608 for param in expanded_params: |
| 609 if param.type_ == PropertyType.ANY: | 609 if param.type_ == PropertyType.ANY: |
| 610 # Generation of Value* Create(Value*) is redundant. | 610 # Generation of base::Value* Create(base::Value*) is redundant. |
| 611 continue | 611 continue |
| 612 # We treat this argument as 'required' to avoid wrapping it in a | 612 # We treat this argument as 'required' to avoid wrapping it in a |
| 613 # scoped_ptr if it's optional. | 613 # scoped_ptr if it's optional. |
| 614 param_copy = param.Copy() | 614 param_copy = param.Copy() |
| 615 param_copy.optional = False | 615 param_copy.optional = False |
| 616 c.Sblock('Value* %(cpp_namespace)s::Result::Create(const %(arg)s) {') | 616 c.Sblock('base::Value* %(cpp_namespace)s::Result::Create(' + |
| 617 'const %(arg)s) {') | |
| 617 c.Append('return %s;' % | 618 c.Append('return %s;' % |
| 618 self._CreateValueFromProperty(param_copy, param_copy.unix_name)) | 619 self._CreateValueFromProperty(param_copy, param_copy.unix_name)) |
| 619 c.Eblock('}') | 620 c.Eblock('}') |
| 620 c.Substitute({ | 621 c.Substitute({ |
| 621 'cpp_namespace': cpp_namespace, | 622 'cpp_namespace': cpp_namespace, |
| 622 'arg': cpp_util.GetParameterDeclaration( | 623 'arg': cpp_util.GetParameterDeclaration( |
| 623 param_copy, self._cpp_type_generator.GetType(param_copy)) | 624 param_copy, self._cpp_type_generator.GetType(param_copy)) |
| 624 }) | 625 }) |
| 625 | 626 |
| 626 return c | 627 return c |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 655 """ | 656 """ |
| 656 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == | 657 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == |
| 657 PropertyType.ARRAY) | 658 PropertyType.ARRAY) |
| 658 | 659 |
| 659 def _IsFundamentalOrFundamentalRef(self, prop): | 660 def _IsFundamentalOrFundamentalRef(self, prop): |
| 660 """Determines if this property is a Fundamental type or is a ref to a | 661 """Determines if this property is a Fundamental type or is a ref to a |
| 661 Fundamental type. | 662 Fundamental type. |
| 662 """ | 663 """ |
| 663 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. | 664 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. |
| 664 is_fundamental) | 665 is_fundamental) |
| OLD | NEW |