| 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;') | |
| 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 prop.type_ == PropertyType.BINARY: | 346 elif prop.type_ == PropertyType.BINARY: |
| 349 return '%s->DeepCopy()' % var | 347 return '%s->DeepCopy()' % var |
| 350 elif self._IsArrayOrArrayRef(prop): | 348 elif self._IsArrayOrArrayRef(prop): |
| 351 return '%s.release()' % self._util_cc_helper.CreateValueFromArray( | 349 return '%s.release()' % self._util_cc_helper.CreateValueFromArray( |
| 352 self._cpp_type_generator.GetReferencedProperty(prop), var, | 350 self._cpp_type_generator.GetReferencedProperty(prop), var, |
| 353 prop.optional) | 351 prop.optional) |
| 354 elif self._IsFundamentalOrFundamentalRef(prop): | 352 elif self._IsFundamentalOrFundamentalRef(prop): |
| 355 if prop.optional: | 353 if prop.optional: |
| 356 var = '*' + var | 354 var = '*' + var |
| 357 prop = self._cpp_type_generator.GetReferencedProperty(prop); | 355 prop = self._cpp_type_generator.GetReferencedProperty(prop); |
| 358 return { | 356 return { |
| 359 PropertyType.STRING: 'Value::CreateStringValue(%s)', | 357 PropertyType.STRING: 'base::Value::CreateStringValue(%s)', |
| 360 PropertyType.BOOLEAN: 'Value::CreateBooleanValue(%s)', | 358 PropertyType.BOOLEAN: 'base::Value::CreateBooleanValue(%s)', |
| 361 PropertyType.INTEGER: 'Value::CreateIntegerValue(%s)', | 359 PropertyType.INTEGER: 'base::Value::CreateIntegerValue(%s)', |
| 362 PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)', | 360 PropertyType.DOUBLE: 'base::Value::CreateDoubleValue(%s)', |
| 363 }[prop.type_] % var | 361 }[prop.type_] % var |
| 364 else: | 362 else: |
| 365 raise NotImplementedError('Conversion of %s to Value not ' | 363 raise NotImplementedError('Conversion of %s to base::Value not ' |
| 366 'implemented' % repr(prop.type_)) | 364 'implemented' % repr(prop.type_)) |
| 367 | 365 |
| 368 def _GenerateParamsCheck(self, function, var): | 366 def _GenerateParamsCheck(self, function, var): |
| 369 """Generates a check for the correct number of arguments when creating | 367 """Generates a check for the correct number of arguments when creating |
| 370 Params. | 368 Params. |
| 371 """ | 369 """ |
| 372 c = Code() | 370 c = Code() |
| 373 num_required = 0 | 371 num_required = 0 |
| 374 for param in function.params: | 372 for param in function.params: |
| 375 if not param.optional: | 373 if not param.optional: |
| 376 num_required += 1 | 374 num_required += 1 |
| 377 if num_required == len(function.params): | 375 if num_required == len(function.params): |
| 378 c.Append('if (%(var)s.GetSize() != %(total)d)') | 376 c.Append('if (%(var)s.GetSize() != %(total)d)') |
| 379 elif not num_required: | 377 elif not num_required: |
| 380 c.Append('if (%(var)s.GetSize() > %(total)d)') | 378 c.Append('if (%(var)s.GetSize() > %(total)d)') |
| 381 else: | 379 else: |
| 382 c.Append('if (%(var)s.GetSize() < %(required)d' | 380 c.Append('if (%(var)s.GetSize() < %(required)d' |
| 383 ' || %(var)s.GetSize() > %(total)d)') | 381 ' || %(var)s.GetSize() > %(total)d)') |
| 384 c.Append(' return scoped_ptr<Params>();') | 382 c.Append(' return scoped_ptr<Params>();') |
| 385 c.Substitute({ | 383 c.Substitute({ |
| 386 'var': var, | 384 'var': var, |
| 387 'required': num_required, | 385 'required': num_required, |
| 388 'total': len(function.params), | 386 'total': len(function.params), |
| 389 }) | 387 }) |
| 390 return c | 388 return c |
| 391 | 389 |
| 392 def _GenerateFunctionParamsCreate(self, cpp_namespace, function): | 390 def _GenerateFunctionParamsCreate(self, cpp_namespace, function): |
| 393 """Generate function to create an instance of Params. The generated | 391 """Generate function to create an instance of Params. The generated |
| 394 function takes a ListValue of arguments. | 392 function takes a base::ListValue of arguments. |
| 395 | 393 |
| 396 E.g for function "Bar", generate Bar::Params::Create() | 394 E.g for function "Bar", generate Bar::Params::Create() |
| 397 """ | 395 """ |
| 398 c = Code() | 396 c = Code() |
| 399 (c.Append('// static') | 397 (c.Append('// static') |
| 400 .Sblock('scoped_ptr<%(cpp_namespace)s::Params> ' | 398 .Sblock('scoped_ptr<%(cpp_namespace)s::Params> ' |
| 401 '%(cpp_namespace)s::Params::Create(const ListValue& args) {') | 399 '%(cpp_namespace)s::Params::Create(const base::ListValue& args) {') |
| 402 .Concat(self._GenerateParamsCheck(function, 'args')) | 400 .Concat(self._GenerateParamsCheck(function, 'args')) |
| 403 .Append('scoped_ptr<Params> params(new Params());') | 401 .Append('scoped_ptr<Params> params(new Params());') |
| 404 ) | 402 ) |
| 405 c.Substitute({'cpp_namespace': cpp_namespace}) | 403 c.Substitute({'cpp_namespace': cpp_namespace}) |
| 406 | 404 |
| 407 for param in function.params: | 405 for param in function.params: |
| 408 c.Concat(self._InitializePropertyToDefault(param, 'params')) | 406 c.Concat(self._InitializePropertyToDefault(param, 'params')) |
| 409 | 407 |
| 410 for i, param in enumerate(function.params): | 408 for i, param in enumerate(function.params): |
| 411 # Any failure will cause this function to return. If any argument is | 409 # Any failure will cause this function to return. If any argument is |
| 412 # incorrect or missing, those following it are not processed. Note that | 410 # incorrect or missing, those following it are not processed. Note that |
| 413 # for optional arguments, we allow missing arguments and proceed because | 411 # for optional arguments, we allow missing arguments and proceed because |
| 414 # there may be other arguments following it. | 412 # there may be other arguments following it. |
| 415 failure_value = 'scoped_ptr<Params>()' | 413 failure_value = 'scoped_ptr<Params>()' |
| 416 c.Append() | 414 c.Append() |
| 417 value_var = param.unix_name + '_value' | 415 value_var = param.unix_name + '_value' |
| 418 (c.Append('Value* %(value_var)s = NULL;') | 416 (c.Append('base::Value* %(value_var)s = NULL;') |
| 419 .Append('if (args.Get(%(i)s, &%(value_var)s) && ' | 417 .Append('if (args.Get(%(i)s, &%(value_var)s) &&\n' |
| 420 '!%(value_var)s->IsType(Value::TYPE_NULL))') | 418 ' !%(value_var)s->IsType(base::Value::TYPE_NULL))') |
| 421 .Sblock('{') | 419 .Sblock('{') |
| 422 .Concat(self._GeneratePopulatePropertyFromValue( | 420 .Concat(self._GeneratePopulatePropertyFromValue( |
| 423 param, value_var, 'params', failure_value)) | 421 param, value_var, 'params', failure_value)) |
| 424 .Eblock('}') | 422 .Eblock('}') |
| 425 ) | 423 ) |
| 426 if not param.optional: | 424 if not param.optional: |
| 427 (c.Sblock('else {') | 425 (c.Sblock('else {') |
| 428 .Append('return %s;' % failure_value) | 426 .Append('return %s;' % failure_value) |
| 429 .Eblock('}') | 427 .Eblock('}') |
| 430 ) | 428 ) |
| 431 c.Substitute({'value_var': value_var, 'i': i}) | 429 c.Substitute({'value_var': value_var, 'i': i}) |
| 432 (c.Append() | 430 (c.Append() |
| 433 .Append('return params.Pass();') | 431 .Append('return params.Pass();') |
| 434 .Eblock('}') | 432 .Eblock('}') |
| 435 .Append() | 433 .Append() |
| 436 ) | 434 ) |
| 437 | 435 |
| 438 return c | 436 return c |
| 439 | 437 |
| 440 def _GeneratePopulatePropertyFromValue( | 438 def _GeneratePopulatePropertyFromValue( |
| 441 self, prop, value_var, dst, failure_value, check_type=True): | 439 self, prop, value_var, dst, failure_value, check_type=True): |
| 442 """Generates code to populate a model.Property given a Value*. The | 440 """Generates code to populate a model.Property given a base::Value*. The |
| 443 existence of data inside the Value* is assumed so checks for existence | 441 existence of data inside the base::Value* is assumed so checks for existence |
| 444 should be performed before the code this generates. | 442 should be performed before the code this generates. |
| 445 | 443 |
| 446 prop: the property the code is populating. | 444 prop: the property the code is populating. |
| 447 value_var: a Value* that should represent |prop|. | 445 value_var: a base::Value* that should represent |prop|. |
| 448 dst: the object with |prop| as a member. | 446 dst: the object with |prop| as a member. |
| 449 failure_value: the value to return if |prop| cannot be extracted from | 447 failure_value: the value to return if |prop| cannot be extracted from |
| 450 |value_var| | 448 |value_var| |
| 451 check_type: if true, will check if |value_var| is the correct Value::Type | 449 check_type: if true, will check if |value_var| is the correct |
| 450 base::Value::Type |
| 452 """ | 451 """ |
| 453 c = Code() | 452 c = Code() |
| 454 c.Sblock('{') | 453 c.Sblock('{') |
| 455 | 454 |
| 456 if self._IsFundamentalOrFundamentalRef(prop): | 455 if self._IsFundamentalOrFundamentalRef(prop): |
| 457 if prop.optional: | 456 if prop.optional: |
| 458 (c.Append('%(ctype)s temp;') | 457 (c.Append('%(ctype)s temp;') |
| 459 .Append('if (!%s)' % | 458 .Append('if (!%s)' % |
| 460 cpp_util.GetAsFundamentalValue( | 459 cpp_util.GetAsFundamentalValue( |
| 461 self._cpp_type_generator.GetReferencedProperty(prop), | 460 self._cpp_type_generator.GetReferencedProperty(prop), |
| 462 value_var, | 461 value_var, |
| 463 '&temp')) | 462 '&temp')) |
| 464 .Append(' return %(failure_value)s;') | 463 .Append(' return %(failure_value)s;') |
| 465 .Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));') | 464 .Append('%(dst)s->%(name)s.reset(new %(ctype)s(temp));') |
| 466 ) | 465 ) |
| 467 else: | 466 else: |
| 468 (c.Append('if (!%s)' % | 467 (c.Append('if (!%s)' % |
| 469 cpp_util.GetAsFundamentalValue( | 468 cpp_util.GetAsFundamentalValue( |
| 470 self._cpp_type_generator.GetReferencedProperty(prop), | 469 self._cpp_type_generator.GetReferencedProperty(prop), |
| 471 value_var, | 470 value_var, |
| 472 '&%s->%s' % (dst, prop.unix_name))) | 471 '&%s->%s' % (dst, prop.unix_name))) |
| 473 .Append(' return %(failure_value)s;') | 472 .Append(' return %(failure_value)s;') |
| 474 ) | 473 ) |
| 475 elif self._IsObjectOrObjectRef(prop): | 474 elif self._IsObjectOrObjectRef(prop): |
| 476 if prop.optional: | 475 if prop.optional: |
| 477 (c.Append('DictionaryValue* dictionary = NULL;') | 476 (c.Append('base::DictionaryValue* dictionary = NULL;') |
| 478 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') | 477 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') |
| 479 .Append(' return %(failure_value)s;') | 478 .Append(' return %(failure_value)s;') |
| 480 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());') | 479 .Append('scoped_ptr<%(ctype)s> temp(new %(ctype)s());') |
| 481 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))') | 480 .Append('if (!%(ctype)s::Populate(*dictionary, temp.get()))') |
| 482 .Append(' return %(failure_value)s;') | 481 .Append(' return %(failure_value)s;') |
| 483 .Append('%(dst)s->%(name)s = temp.Pass();') | 482 .Append('%(dst)s->%(name)s = temp.Pass();') |
| 484 ) | 483 ) |
| 485 else: | 484 else: |
| 486 (c.Append('DictionaryValue* dictionary = NULL;') | 485 (c.Append('base::DictionaryValue* dictionary = NULL;') |
| 487 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') | 486 .Append('if (!%(value_var)s->GetAsDictionary(&dictionary))') |
| 488 .Append(' return %(failure_value)s;') | 487 .Append(' return %(failure_value)s;') |
| 489 .Append( | 488 .Append( |
| 490 'if (!%(ctype)s::Populate(*dictionary, &%(dst)s->%(name)s))') | 489 'if (!%(ctype)s::Populate(*dictionary, &%(dst)s->%(name)s))') |
| 491 .Append(' return %(failure_value)s;') | 490 .Append(' return %(failure_value)s;') |
| 492 ) | 491 ) |
| 493 elif prop.type_ == PropertyType.ANY: | 492 elif prop.type_ == PropertyType.ANY: |
| 494 if prop.optional: | 493 if prop.optional: |
| 495 c.Append('%(dst)s->%(name)s.reset(new Any());') | 494 c.Append('%(dst)s->%(name)s.reset(new ' + any_helper.ANY_CLASS + '());') |
| 496 c.Append(self._any_helper.Init(prop, value_var, dst) + ';') | 495 c.Append(self._any_helper.Init(prop, value_var, dst) + ';') |
| 497 elif self._IsArrayOrArrayRef(prop): | 496 elif self._IsArrayOrArrayRef(prop): |
| 498 # util_cc_helper deals with optional and required arrays | 497 # util_cc_helper deals with optional and required arrays |
| 499 (c.Append('ListValue* list = NULL;') | 498 (c.Append('base::ListValue* list = NULL;') |
| 500 .Append('if (!%(value_var)s->GetAsList(&list))') | 499 .Append('if (!%(value_var)s->GetAsList(&list))') |
| 501 .Append(' return %(failure_value)s;')) | 500 .Append(' return %(failure_value)s;')) |
| 502 if prop.item_type.type_ == PropertyType.ENUM: | 501 if prop.item_type.type_ == PropertyType.ENUM: |
| 503 self._GenerateListValueToEnumArrayConversion(c, prop) | 502 self._GenerateListValueToEnumArrayConversion(c, prop) |
| 504 else: | 503 else: |
| 505 (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( | 504 (c.Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( |
| 506 self._cpp_type_generator.GetReferencedProperty(prop), 'list', | 505 self._cpp_type_generator.GetReferencedProperty(prop), 'list', |
| 507 dst + '->' + prop.unix_name, prop.optional)) | 506 dst + '->' + prop.unix_name, prop.optional)) |
| 508 .Append(' return %(failure_value)s;') | 507 .Append(' return %(failure_value)s;') |
| 509 ) | 508 ) |
| (...skipping 16 matching lines...) Expand all Loading... |
| 526 .Append(' return %(failure_value)s;') | 525 .Append(' return %(failure_value)s;') |
| 527 ) | 526 ) |
| 528 c.Eblock('}') | 527 c.Eblock('}') |
| 529 elif prop.type_ == PropertyType.ENUM: | 528 elif prop.type_ == PropertyType.ENUM: |
| 530 c.Sblock('{') | 529 c.Sblock('{') |
| 531 self._GenerateStringToEnumConversion(c, prop, value_var, 'enum_temp') | 530 self._GenerateStringToEnumConversion(c, prop, value_var, 'enum_temp') |
| 532 c.Append('%(dst)s->%(name)s = enum_temp;') | 531 c.Append('%(dst)s->%(name)s = enum_temp;') |
| 533 c.Eblock('}') | 532 c.Eblock('}') |
| 534 elif prop.type_ == PropertyType.BINARY: | 533 elif prop.type_ == PropertyType.BINARY: |
| 535 # This is the same if the property is optional or not. We need a pointer | 534 # This is the same if the property is optional or not. We need a pointer |
| 536 # to the BinaryValue to be able to populate it, so a scoped_ptr is used | 535 # to the base::BinaryValue to be able to populate it, so a scoped_ptr is |
| 537 # whether it is optional or required. | 536 # used whether it is optional or required. |
| 538 (c.Append('if (!%(value_var)s->IsType(%(value_type)s))') | 537 (c.Append('if (!%(value_var)s->IsType(%(value_type)s))') |
| 539 .Append(' return %(failure_value)s;') | 538 .Append(' return %(failure_value)s;') |
| 540 .Append('%(dst)s->%(name)s.reset(') | 539 .Append('%(dst)s->%(name)s.reset(') |
| 541 .Append(' static_cast<BinaryValue*>(%(value_var)s)->DeepCopy());') | 540 .Append(' static_cast<base::BinaryValue*>(%(value_var)s)' |
| 541 '->DeepCopy());') |
| 542 ) | 542 ) |
| 543 else: | 543 else: |
| 544 raise NotImplementedError(prop.type_) | 544 raise NotImplementedError(prop.type_) |
| 545 c.Eblock('}') | 545 c.Eblock('}') |
| 546 sub = { | 546 sub = { |
| 547 'value_var': value_var, | 547 'value_var': value_var, |
| 548 'name': prop.unix_name, | 548 'name': prop.unix_name, |
| 549 'dst': dst, | 549 'dst': dst, |
| 550 'failure_value': failure_value, | 550 'failure_value': failure_value, |
| 551 } | 551 } |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 625 | 625 |
| 626 def _GenerateFunctionResultCreate(self, cpp_namespace, function): | 626 def _GenerateFunctionResultCreate(self, cpp_namespace, function): |
| 627 """Generate function to create a Result given the return value. | 627 """Generate function to create a Result given the return value. |
| 628 | 628 |
| 629 E.g for function "Bar", generate Bar::Result::Create | 629 E.g for function "Bar", generate Bar::Result::Create |
| 630 """ | 630 """ |
| 631 c = Code() | 631 c = Code() |
| 632 params = function.callback.params | 632 params = function.callback.params |
| 633 | 633 |
| 634 if not params: | 634 if not params: |
| 635 (c.Append('Value* %s::Result::Create() {' % cpp_namespace) | 635 (c.Append('base::Value* %s::Result::Create() {' % cpp_namespace) |
| 636 .Append(' return Value::CreateNullValue();') | 636 .Append(' return base::Value::CreateNullValue();') |
| 637 .Append('}') | 637 .Append('}') |
| 638 ) | 638 ) |
| 639 else: | 639 else: |
| 640 expanded_params = self._cpp_type_generator.GetExpandedChoicesInParams( | 640 expanded_params = self._cpp_type_generator.GetExpandedChoicesInParams( |
| 641 params) | 641 params) |
| 642 c.Concat(self._GeneratePropertyFunctions( | 642 c.Concat(self._GeneratePropertyFunctions( |
| 643 cpp_namespace + '::Result', expanded_params)) | 643 cpp_namespace + '::Result', expanded_params)) |
| 644 | 644 |
| 645 # If there is a single parameter, this is straightforward. However, if | 645 # If there is a single parameter, this is straightforward. However, if |
| 646 # the callback parameter is of 'choices', this generates a Create method | 646 # the callback parameter is of 'choices', this generates a Create method |
| 647 # for each choice. This works because only 1 choice can be returned at a | 647 # for each choice. This works because only 1 choice can be returned at a |
| 648 # time. | 648 # time. |
| 649 for param in expanded_params: | 649 for param in expanded_params: |
| 650 if param.type_ == PropertyType.ANY: | 650 if param.type_ == PropertyType.ANY: |
| 651 # Generation of Value* Create(Value*) is redundant. | 651 # Generation of base::Value* Create(base::Value*) is redundant. |
| 652 continue | 652 continue |
| 653 # We treat this argument as 'required' to avoid wrapping it in a | 653 # We treat this argument as 'required' to avoid wrapping it in a |
| 654 # scoped_ptr if it's optional. | 654 # scoped_ptr if it's optional. |
| 655 param_copy = param.Copy() | 655 param_copy = param.Copy() |
| 656 param_copy.optional = False | 656 param_copy.optional = False |
| 657 c.Sblock('Value* %(cpp_namespace)s::Result::Create(const %(arg)s) {') | 657 c.Sblock('base::Value* %(cpp_namespace)s::Result::Create(' |
| 658 'const %(arg)s) {') |
| 658 c.Append('return %s;' % | 659 c.Append('return %s;' % |
| 659 self._CreateValueFromProperty(param_copy, param_copy.unix_name)) | 660 self._CreateValueFromProperty(param_copy, param_copy.unix_name)) |
| 660 c.Eblock('}') | 661 c.Eblock('}') |
| 661 c.Substitute({ | 662 c.Substitute({ |
| 662 'cpp_namespace': cpp_namespace, | 663 'cpp_namespace': cpp_namespace, |
| 663 'arg': cpp_util.GetParameterDeclaration( | 664 'arg': cpp_util.GetParameterDeclaration( |
| 664 param_copy, self._cpp_type_generator.GetType(param_copy)) | 665 param_copy, self._cpp_type_generator.GetType(param_copy)) |
| 665 }) | 666 }) |
| 666 | 667 |
| 667 return c | 668 return c |
| (...skipping 28 matching lines...) Expand all Loading... |
| 696 """ | 697 """ |
| 697 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == | 698 return (self._cpp_type_generator.GetReferencedProperty(prop).type_ == |
| 698 PropertyType.ARRAY) | 699 PropertyType.ARRAY) |
| 699 | 700 |
| 700 def _IsFundamentalOrFundamentalRef(self, prop): | 701 def _IsFundamentalOrFundamentalRef(self, prop): |
| 701 """Determines if this property is a Fundamental type or is a ref to a | 702 """Determines if this property is a Fundamental type or is a ref to a |
| 702 Fundamental type. | 703 Fundamental type. |
| 703 """ | 704 """ |
| 704 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. | 705 return (self._cpp_type_generator.GetReferencedProperty(prop).type_. |
| 705 is_fundamental) | 706 is_fundamental) |
| OLD | NEW |