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 model import PropertyType | 5 from model import PropertyType |
| 6 import code | 6 import code |
| 7 import cpp_util | 7 import cpp_util |
| 8 import util_cc_helper | 8 import util_cc_helper |
| 9 | 9 |
| 10 class CCGenerator(object): | 10 class CCGenerator(object): |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 (c.Concat(self._GenerateFunction(function)) | 65 (c.Concat(self._GenerateFunction(function)) |
| 66 .Append() | 66 .Append() |
| 67 ) | 67 ) |
| 68 (c.Concat(self._cpp_type_generator.GetNamespaceEnd()) | 68 (c.Concat(self._cpp_type_generator.GetNamespaceEnd()) |
| 69 .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) | 69 .Concat(self._cpp_type_generator.GetRootNamespaceEnd()) |
| 70 .Append() | 70 .Append() |
| 71 ) | 71 ) |
| 72 # TODO(calamity): Events | 72 # TODO(calamity): Events |
| 73 return c | 73 return c |
| 74 | 74 |
| 75 def _GenerateType(self, cpp_namespace, type_, serializable=True): | 75 def _GenerateType(self, cpp_namespace, type_): |
| 76 """Generates the function definitions for a type. | 76 """Generates the function definitions for a type. |
| 77 """ | 77 """ |
| 78 classname = cpp_util.Classname(type_.name) | 78 classname = cpp_util.Classname(type_.name) |
| 79 c = code.Code() | 79 c = code.Code() |
| 80 | 80 |
| 81 (c.Append('%(namespace)s::%(classname)s() {}') | 81 (c.Concat(self._GeneratePropertyFunctions( |
| 82 cpp_namespace, type_.properties.values())) | |
| 83 .Append('%(namespace)s::%(classname)s() {}') | |
| 82 .Append('%(namespace)s::~%(classname)s() {}') | 84 .Append('%(namespace)s::~%(classname)s() {}') |
| 83 .Append() | 85 .Append() |
| 84 .Concat(self._GenerateTypePopulate(cpp_namespace, type_)) | |
| 85 .Append() | |
| 86 ) | 86 ) |
| 87 if serializable: | 87 if type_.from_client: |
| 88 (c.Concat(self._GenerateTypePopulate(cpp_namespace, type_)) | |
| 89 .Append() | |
| 90 ) | |
| 91 if type_.from_json: | |
|
not at google - send to devlin
2012/02/27 06:04:47
Am I missing something... isn't this inverted? if
calamity
2012/02/28 01:01:45
Done.
| |
| 88 c.Concat(self._GenerateTypeToValue(cpp_namespace, type_)) | 92 c.Concat(self._GenerateTypeToValue(cpp_namespace, type_)) |
| 89 c.Append() | 93 c.Append() |
| 90 c.Substitute({'classname': classname, 'namespace': cpp_namespace}) | 94 c.Substitute({'classname': classname, 'namespace': cpp_namespace}) |
| 91 | 95 |
| 92 return c | 96 return c |
| 93 | 97 |
| 94 def _GenerateTypePopulate(self, cpp_namespace, type_): | 98 def _GenerateTypePopulate(self, cpp_namespace, type_): |
| 95 """Generates the function for populating a type given a pointer to it. | 99 """Generates the function for populating a type given a pointer to it. |
| 96 """ | 100 """ |
| 97 classname = cpp_util.Classname(type_.name) | 101 classname = cpp_util.Classname(type_.name) |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 122 dst: Type* | 126 dst: Type* |
| 123 """ | 127 """ |
| 124 c = code.Code() | 128 c = code.Code() |
| 125 value_var = prop.unix_name + '_value' | 129 value_var = prop.unix_name + '_value' |
| 126 c.Append('Value* %(value_var)s = NULL;') | 130 c.Append('Value* %(value_var)s = NULL;') |
| 127 if prop.optional: | 131 if prop.optional: |
| 128 (c.Sblock( | 132 (c.Sblock( |
| 129 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {' | 133 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {' |
| 130 ) | 134 ) |
| 131 .Concat(self._GeneratePopulatePropertyFromValue( | 135 .Concat(self._GeneratePopulatePropertyFromValue( |
| 132 prop, value_var, 'out', 'false')) | 136 prop, value_var, dst, 'false')) |
| 133 .Eblock('}') | 137 .Eblock('}') |
| 134 ) | 138 ) |
| 135 else: | 139 else: |
| 136 (c.Append( | 140 (c.Append( |
| 137 'if (!%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s))') | 141 'if (!%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s))') |
| 138 .Append(' return false;') | 142 .Append(' return false;') |
| 139 .Concat(self._GeneratePopulatePropertyFromValue( | 143 .Concat(self._GeneratePopulatePropertyFromValue( |
| 140 prop, value_var, 'out', 'false')) | 144 prop, value_var, dst, 'false')) |
| 141 ) | 145 ) |
| 142 c.Append() | 146 c.Append() |
| 143 c.Substitute({'value_var': value_var, 'key': prop.name, 'src': src}) | 147 c.Substitute({'value_var': value_var, 'key': prop.name, 'src': src}) |
| 144 return c | 148 return c |
| 145 | 149 |
| 146 def _GenerateTypeToValue(self, cpp_namespace, type_): | 150 def _GenerateTypeToValue(self, cpp_namespace, type_): |
| 147 """Generates a function that serializes the type into a |DictionaryValue|. | 151 """Generates a function that serializes the type into a |DictionaryValue|. |
| 148 """ | 152 """ |
| 149 c = code.Code() | 153 c = code.Code() |
| 150 (c.Sblock('scoped_ptr<DictionaryValue> %s::ToValue() const {' % | 154 (c.Sblock('scoped_ptr<DictionaryValue> %s::ToValue() const {' % |
| 151 cpp_namespace) | 155 cpp_namespace) |
| 152 .Append('scoped_ptr<DictionaryValue> value(new DictionaryValue());') | 156 .Append('scoped_ptr<DictionaryValue> value(new DictionaryValue());') |
| 153 .Append() | 157 .Append() |
| 154 ) | 158 ) |
| 155 for prop in type_.properties.values(): | 159 for prop in type_.properties.values(): |
| 156 c.Concat(self._CreateValueFromProperty(prop, prop.unix_name, 'value')) | 160 if prop.optional: |
| 161 if prop.type_ == PropertyType.ENUM: | |
| 162 c.Sblock('if (%s != %s)' % | |
| 163 (prop.unix_name, self._cpp_type_generator.GetEnumNoneValue(prop))) | |
| 164 else: | |
| 165 c.Sblock('if (%s.get())' % prop.unix_name) | |
|
Yoyo Zhou
2012/02/28 00:29:46
Does the Sblock without Eblock mess up the indent
calamity
2012/02/28 01:01:45
Done.
| |
| 166 c.Append('value->SetWithoutPathExpansion("%s", %s);' % ( | |
| 167 prop.name, | |
| 168 self._CreateValueFromProperty(prop, prop.unix_name))) | |
| 157 (c.Append() | 169 (c.Append() |
| 158 .Append('return value.Pass();') | 170 .Append('return value.Pass();') |
| 159 .Eblock('}') | 171 .Eblock('}') |
| 160 ) | 172 ) |
| 161 return c | 173 return c |
| 162 | 174 |
| 163 def _CreateValueFromProperty(self, prop, var, dst): | |
| 164 """Generates code to serialize a single property in a type. | |
| 165 | |
| 166 prop: Property to create from | |
| 167 var: variable with value to create from | |
| 168 """ | |
| 169 c = code.Code() | |
| 170 if prop.type_ == PropertyType.ENUM: | |
| 171 c.Sblock('switch (%s) {' % var) | |
| 172 for enum_value in prop.enum_values: | |
| 173 (c.Append('case %s: {' % | |
| 174 self._cpp_type_generator.GetEnumValue(prop, enum_value)) | |
| 175 .Append(' %s->SetWithoutPathExpansion(' | |
| 176 '"%s", Value::CreateStringValue("%s"));' % | |
| 177 (dst, prop.name, enum_value)) | |
| 178 .Append(' break;') | |
| 179 .Append('}') | |
| 180 ) | |
| 181 # C++ requires the entire enum to be handled by a switch. | |
| 182 if prop.optional: | |
| 183 (c.Append('case %s: {' % | |
| 184 self._cpp_type_generator.GetEnumNoneValue(prop)) | |
| 185 .Append(' break;') | |
| 186 .Append('}') | |
| 187 ) | |
| 188 c.Eblock('}') | |
| 189 else: | |
| 190 if prop.optional: | |
| 191 c.Sblock('if (%s.get())' % var) | |
| 192 if prop.type_ == PropertyType.ARRAY: | |
| 193 c.Append('%s;' % self._util_cc_helper.PopulateDictionaryFromArray( | |
| 194 prop, var, prop.name, dst)) | |
| 195 else: | |
| 196 c.Append('%s->SetWithoutPathExpansion("%s", %s);' % | |
| 197 (dst, prop.name, cpp_util.CreateValueFromSingleProperty(prop, var))) | |
| 198 return c | |
| 199 | |
| 200 def _GenerateFunction(self, function): | 175 def _GenerateFunction(self, function): |
| 201 """Generates the definitions for function structs. | 176 """Generates the definitions for function structs. |
| 202 """ | 177 """ |
| 203 classname = cpp_util.Classname(function.name) | 178 classname = cpp_util.Classname(function.name) |
| 204 c = code.Code() | 179 c = code.Code() |
| 205 | 180 |
| 206 # Params::Populate function | 181 # Params::Populate function |
| 207 if function.params: | 182 if function.params: |
| 208 for param in function.params: | 183 c.Concat(self._GeneratePropertyFunctions(classname + '::Params', |
| 209 if param.type_ == PropertyType.OBJECT: | 184 function.params)) |
| 210 param_namespace = '%s::Params::%s' % (classname, | |
| 211 cpp_util.Classname(param.name)) | |
| 212 c.Concat( | |
| 213 self._GenerateType(param_namespace, param, serializable=False)) | |
| 214 c.Append() | |
| 215 (c.Append('%(name)s::Params::Params() {}') | 185 (c.Append('%(name)s::Params::Params() {}') |
| 216 .Append('%(name)s::Params::~Params() {}') | 186 .Append('%(name)s::Params::~Params() {}') |
| 217 .Append() | 187 .Append() |
| 218 .Concat(self._GenerateFunctionParamsCreate(function)) | 188 .Concat(self._GenerateFunctionParamsCreate(function)) |
| 219 .Append() | 189 .Append() |
| 220 ) | 190 ) |
| 221 | 191 |
| 222 # Result::Create function | 192 # Result::Create function |
| 223 c.Concat(self._GenerateFunctionResultCreate(function)) | 193 if function.callback: |
| 194 c.Concat(self._GenerateFunctionResultCreate(function)) | |
| 224 | 195 |
| 225 c.Substitute({'name': classname}) | 196 c.Substitute({'name': classname}) |
| 226 | 197 |
| 227 return c | 198 return c |
| 228 | 199 |
| 200 def _GenerateCreateEnumValue(self, cpp_namespace, prop): | |
| 201 """Generates a function that returns the |StringValue| representation of an | |
| 202 enum. | |
| 203 """ | |
| 204 c = code.Code() | |
| 205 c.Append('// static') | |
| 206 c.Sblock('scoped_ptr<Value> %(cpp_namespace)s::CreateEnumValue(%(arg)s) {') | |
| 207 c.Sblock('switch (%s) {' % prop.unix_name) | |
| 208 if prop.optional: | |
| 209 (c.Append('case %s: {' % self._cpp_type_generator.GetEnumNoneValue(prop)) | |
| 210 .Append(' return scoped_ptr<Value>();') | |
| 211 .Append('}') | |
| 212 ) | |
| 213 for enum_value in prop.enum_values: | |
| 214 (c.Append('case %s: {' % | |
| 215 self._cpp_type_generator.GetEnumValue(prop, enum_value)) | |
| 216 .Append(' return scoped_ptr<Value>(Value::CreateStringValue("%s"));' % | |
| 217 enum_value) | |
| 218 .Append('}') | |
| 219 ) | |
| 220 c.Eblock('}') | |
| 221 c.Eblock('}') | |
| 222 c.Substitute({ | |
| 223 'cpp_namespace': cpp_namespace, | |
| 224 'arg': cpp_util.GetParameterDeclaration( | |
| 225 prop, self._cpp_type_generator.GetType(prop)) | |
| 226 }) | |
| 227 return c | |
| 228 | |
| 229 def _CreateValueFromProperty(self, prop, var): | |
| 230 """Creates a Value given a single property. Generated code passes ownership | |
| 231 to caller. | |
| 232 | |
| 233 var: variable or variable* | |
| 234 """ | |
| 235 if prop.type_ == PropertyType.CHOICES: | |
| 236 # CHOICES conversion not implemented because it's not used. If needed, | |
| 237 # write something to generate a function that returns a scoped_ptr<Value> | |
| 238 # and put it in _GeneratePropertyFunctions. | |
| 239 raise NotImplementedError( | |
| 240 'Conversion of CHOICES to Value not implemented') | |
| 241 if prop.type_ in (PropertyType.REF, PropertyType.OBJECT): | |
| 242 if prop.optional: | |
| 243 return '%s->ToValue().release()' % var | |
| 244 else: | |
| 245 return '%s.ToValue().release()' % var | |
| 246 elif prop.type_ == PropertyType.ENUM: | |
| 247 return 'CreateEnumValue(%s).release()' % var | |
| 248 elif prop.type_ == PropertyType.ARRAY: | |
| 249 return '%s.release()' % self._util_cc_helper.CreateValueFromArray( | |
| 250 prop, var) | |
| 251 elif prop.type_.is_fundamental: | |
| 252 if prop.optional: | |
| 253 var = '*' + var | |
| 254 return { | |
| 255 PropertyType.STRING: 'Value::CreateStringValue(%s)', | |
| 256 PropertyType.BOOLEAN: 'Value::CreateBooleanValue(%s)', | |
| 257 PropertyType.INTEGER: 'Value::CreateIntegerValue(%s)', | |
| 258 PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)', | |
| 259 }[prop.type_] % var | |
| 260 else: | |
| 261 raise NotImplementedError('Conversion of %s to Value not ' | |
| 262 'implemented' % repr(prop.type_)) | |
| 263 | |
| 229 def _GenerateParamsCheck(self, function, var): | 264 def _GenerateParamsCheck(self, function, var): |
| 230 """Generates a check for the correct number of arguments when creating | 265 """Generates a check for the correct number of arguments when creating |
| 231 Params. | 266 Params. |
| 232 """ | 267 """ |
| 233 c = code.Code() | 268 c = code.Code() |
| 234 num_required = 0 | 269 num_required = 0 |
| 235 for param in function.params: | 270 for param in function.params: |
| 236 if not param.optional: | 271 if not param.optional: |
| 237 num_required += 1 | 272 num_required += 1 |
| 238 if num_required == len(function.params): | 273 if num_required == len(function.params): |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 305 prop: the property the code is populating. | 340 prop: the property the code is populating. |
| 306 value_var: a Value* that should represent |prop|. | 341 value_var: a Value* that should represent |prop|. |
| 307 dst: the object with |prop| as a member. | 342 dst: the object with |prop| as a member. |
| 308 failure_value: the value to return if |prop| cannot be extracted from | 343 failure_value: the value to return if |prop| cannot be extracted from |
| 309 |value_var| | 344 |value_var| |
| 310 check_type: if true, will check if |value_var| is the correct Value::Type | 345 check_type: if true, will check if |value_var| is the correct Value::Type |
| 311 """ | 346 """ |
| 312 c = code.Code() | 347 c = code.Code() |
| 313 c.Sblock('{') | 348 c.Sblock('{') |
| 314 | 349 |
| 315 if check_type: | 350 if check_type and prop.type_ != PropertyType.CHOICES: |
| 316 (c.Append('if (!%(value_var)s->IsType(%(value_type)s))') | 351 (c.Append('if (!%(value_var)s->IsType(%(value_type)s))') |
| 317 .Append(' return %(failure_value)s;') | 352 .Append(' return %(failure_value)s;') |
| 318 ) | 353 ) |
| 319 | 354 |
| 320 if prop.type_.is_fundamental: | 355 if prop.type_.is_fundamental: |
| 321 if prop.optional: | 356 if prop.optional: |
| 322 (c.Append('%(ctype)s temp;') | 357 (c.Append('%(ctype)s temp;') |
| 323 .Append('if (%s)' % | 358 .Append('if (%s)' % |
| 324 cpp_util.GetAsFundamentalValue(prop, value_var, '&temp')) | 359 cpp_util.GetAsFundamentalValue(prop, value_var, '&temp')) |
| 325 .Append(' %(dst)s->%(name)s.reset(new %(ctype)s(temp));') | 360 .Append(' %(dst)s->%(name)s.reset(new %(ctype)s(temp));') |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 351 elif prop.type_ == PropertyType.ARRAY: | 386 elif prop.type_ == PropertyType.ARRAY: |
| 352 # util_cc_helper deals with optional and required arrays | 387 # util_cc_helper deals with optional and required arrays |
| 353 (c.Append('ListValue* list = NULL;') | 388 (c.Append('ListValue* list = NULL;') |
| 354 .Append('if (!%(value_var)s->GetAsList(&list))') | 389 .Append('if (!%(value_var)s->GetAsList(&list))') |
| 355 .Append(' return %(failure_value)s;') | 390 .Append(' return %(failure_value)s;') |
| 356 .Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( | 391 .Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( |
| 357 prop, 'list', dst + '->' + prop.unix_name)) | 392 prop, 'list', dst + '->' + prop.unix_name)) |
| 358 .Append(' return %(failure_value)s;') | 393 .Append(' return %(failure_value)s;') |
| 359 ) | 394 ) |
| 360 elif prop.type_ == PropertyType.CHOICES: | 395 elif prop.type_ == PropertyType.CHOICES: |
| 361 return self._GeneratePopulateChoices(prop, value_var, dst, failure_value) | 396 type_var = '%(dst)s->%(name)s_type' |
| 397 c.Sblock('switch (%(value_var)s->GetType()) {') | |
| 398 for choice in self._cpp_type_generator.GetExpandedChoicesInParams([prop]): | |
| 399 (c.Sblock('case %s: {' % cpp_util.GetValueType(choice)) | |
| 400 .Concat(self._GeneratePopulatePropertyFromValue( | |
| 401 choice, value_var, dst, failure_value, check_type=False)) | |
| 402 .Append('%s = %s;' % | |
| 403 (type_var, | |
| 404 self._cpp_type_generator.GetEnumValue( | |
| 405 prop, choice.type_.name))) | |
| 406 .Append('break;') | |
| 407 .Eblock('}') | |
| 408 ) | |
| 409 (c.Append('default:') | |
| 410 .Append(' return %(failure_value)s;') | |
| 411 ) | |
| 412 c.Eblock('}') | |
| 362 elif prop.type_ == PropertyType.ENUM: | 413 elif prop.type_ == PropertyType.ENUM: |
| 363 (c.Append('std::string enum_temp;') | 414 (c.Append('std::string enum_temp;') |
| 364 .Append('if (!%(value_var)s->GetAsString(&enum_temp))') | 415 .Append('if (!%(value_var)s->GetAsString(&enum_temp))') |
| 365 .Append(' return %(failure_value)s;') | 416 .Append(' return %(failure_value)s;') |
| 366 ) | 417 ) |
| 367 for i, enum_value in enumerate(prop.enum_values): | 418 for i, enum_value in enumerate(prop.enum_values): |
| 368 (c.Append( | 419 (c.Append( |
| 369 ('if' if i == 0 else 'else if') + | 420 ('if' if i == 0 else 'else if') + |
| 370 '(enum_temp == "%s")' % enum_value) | 421 '(enum_temp == "%s")' % enum_value) |
| 371 .Append(' %s->%s = %s;' % ( | 422 .Append(' %s->%s = %s;' % ( |
| 372 dst, | 423 dst, |
| 373 prop.unix_name, | 424 prop.unix_name, |
| 374 self._cpp_type_generator.GetEnumValue(prop, enum_value))) | 425 self._cpp_type_generator.GetEnumValue(prop, enum_value))) |
| 375 ) | 426 ) |
| 376 (c.Append('else') | 427 (c.Append('else') |
| 377 .Append(' return %(failure_value)s;') | 428 .Append(' return %(failure_value)s;') |
| 378 ) | 429 ) |
| 379 else: | 430 else: |
| 380 raise NotImplementedError(prop.type_) | 431 raise NotImplementedError(prop.type_) |
| 381 c.Eblock('}') | 432 c.Eblock('}') |
| 382 c.Substitute({ | 433 sub = { |
| 383 'value_var': value_var, | 434 'value_var': value_var, |
| 384 'name': prop.unix_name, | 435 'name': prop.unix_name, |
| 385 'dst': dst, | 436 'dst': dst, |
| 386 'ctype': self._cpp_type_generator.GetType(prop), | |
| 387 'failure_value': failure_value, | 437 'failure_value': failure_value, |
| 388 'value_type': cpp_util.GetValueType(prop), | 438 } |
| 389 }) | 439 if prop.type_ != PropertyType.CHOICES: |
| 440 sub['ctype'] = self._cpp_type_generator.GetType(prop) | |
| 441 sub['value_type'] = cpp_util.GetValueType(prop) | |
| 442 c.Substitute(sub) | |
| 390 return c | 443 return c |
| 391 | 444 |
| 392 def _GeneratePopulateChoices(self, prop, value_var, dst, failure_value): | 445 def _GeneratePropertyFunctions(self, param_namespace, params): |
| 393 """Generates the code to populate a PropertyType.CHOICES parameter or | 446 """Generate the functions for structures generated by a property such as |
| 394 property. The existence of data inside the Value* is assumed so checks for | 447 CreateEnumValue for ENUMs and Populate/ToValue for Params/Result objects. |
| 395 existence should be performed before the code this generates. | |
| 396 | |
| 397 prop: the property the code is populating.. | |
| 398 value_var: a Value* that should represent |prop|. | |
| 399 dst: the object with |prop| as a member. | |
| 400 failure_value: the value to return if |prop| cannot be extracted from | |
| 401 |value_var|. | |
| 402 """ | 448 """ |
| 403 type_var = '%s->%s_type' % (dst, prop.unix_name) | |
| 404 | |
| 405 c = code.Code() | 449 c = code.Code() |
| 406 c.Sblock('switch (%s->GetType()) {' % value_var) | 450 for param in params: |
| 407 for choice in self._cpp_type_generator.GetExpandedChoicesInParams([prop]): | 451 if param.type_ == PropertyType.OBJECT: |
| 408 (c.Sblock('case %s: {' % cpp_util.GetValueType(choice)) | 452 c.Concat(self._GenerateType( |
| 409 .Concat(self._GeneratePopulatePropertyFromValue( | 453 param_namespace + '::' + cpp_util.Classname(param.name), |
| 410 choice, value_var, dst, failure_value, check_type=False)) | 454 param)) |
| 411 .Append('%s = %s;' % | 455 c.Append() |
| 412 (type_var, | 456 elif param.type_ == PropertyType.ENUM: |
| 413 self._cpp_type_generator.GetEnumValue( | 457 c.Concat(self._GenerateCreateEnumValue(param_namespace, param)) |
| 414 prop, choice.type_.name))) | 458 c.Append() |
| 415 .Append('break;') | |
| 416 .Eblock('}') | |
| 417 ) | |
| 418 (c.Append('default:') | |
| 419 .Append(' return %s;' % failure_value) | |
| 420 ) | |
| 421 c.Eblock('}') | |
| 422 return c | 459 return c |
| 423 | 460 |
| 424 def _GenerateFunctionResultCreate(self, function): | 461 def _GenerateFunctionResultCreate(self, function): |
| 425 """Generate function to create a Result given the return value. | 462 """Generate function to create a Result given the return value. |
| 426 """ | 463 """ |
| 427 classname = cpp_util.Classname(function.name) | 464 classname = cpp_util.Classname(function.name) |
| 428 c = code.Code() | 465 c = code.Code() |
| 429 params = function.callback.params | 466 params = function.callback.params |
| 430 | 467 |
| 431 if not params: | 468 if not params: |
| 432 (c.Append('Value* %s::Result::Create() {' % classname) | 469 (c.Append('Value* %s::Result::Create() {' % classname) |
| 433 .Append(' return Value::CreateNullValue();') | 470 .Append(' return Value::CreateNullValue();') |
| 434 .Append('}') | 471 .Append('}') |
| 435 ) | 472 ) |
| 436 else: | 473 else: |
| 474 expanded_params = self._cpp_type_generator.GetExpandedChoicesInParams( | |
| 475 params) | |
| 476 c.Concat(self._GeneratePropertyFunctions( | |
| 477 classname + '::Result', expanded_params)) | |
| 478 | |
| 437 # If there is a single parameter, this is straightforward. However, if | 479 # If there is a single parameter, this is straightforward. However, if |
| 438 # the callback parameter is of 'choices', this generates a Create method | 480 # the callback parameter is of 'choices', this generates a Create method |
| 439 # for each choice. This works because only 1 choice can be returned at a | 481 # for each choice. This works because only 1 choice can be returned at a |
| 440 # time. | 482 # time. |
| 441 for param in self._cpp_type_generator.GetExpandedChoicesInParams(params): | 483 for param in expanded_params: |
| 442 # We treat this argument as 'required' to avoid wrapping it in a | 484 # We treat this argument as 'required' to avoid wrapping it in a |
| 443 # scoped_ptr if it's optional. | 485 # scoped_ptr if it's optional. |
| 444 param_copy = param.Copy() | 486 param_copy = param.Copy() |
| 445 param_copy.optional = False | 487 param_copy.optional = False |
| 446 c.Sblock('Value* %(classname)s::Result::Create(const %(arg)s) {') | 488 c.Sblock('Value* %(classname)s::Result::Create(const %(arg)s) {') |
| 447 if param_copy.type_ == PropertyType.ARRAY: | 489 c.Append('return %s;' % |
| 448 (c.Append('ListValue* value = new ListValue();') | 490 self._CreateValueFromProperty(param_copy, param_copy.unix_name)) |
| 449 .Append('%s;' % self._util_cc_helper.PopulateListFromArray( | 491 c.Eblock('}') |
| 450 param_copy, param_copy.unix_name, 'value')) | |
| 451 .Append('return value;') | |
| 452 ) | |
| 453 else: | |
| 454 c.Append('return %s;' % | |
| 455 cpp_util.CreateValueFromSingleProperty(param_copy, | |
| 456 param_copy.unix_name)) | |
| 457 c.Substitute({'classname': classname, | 492 c.Substitute({'classname': classname, |
| 458 'arg': cpp_util.GetParameterDeclaration( | 493 'arg': cpp_util.GetParameterDeclaration( |
| 459 param_copy, self._cpp_type_generator.GetType(param_copy)) | 494 param_copy, self._cpp_type_generator.GetType(param_copy)) |
| 460 }) | 495 }) |
| 461 c.Eblock('}') | |
| 462 | 496 |
| 463 return c | 497 return c |
| 464 | 498 |
| 465 def _InitializePropertyToDefault(self, prop, dst): | 499 def _InitializePropertyToDefault(self, prop, dst): |
| 466 """Initialize a model.Property to its default value inside an object. | 500 """Initialize a model.Property to its default value inside an object. |
| 467 | 501 |
| 468 dst: Type* | 502 dst: Type* |
| 469 """ | 503 """ |
| 470 c = code.Code() | 504 c = code.Code() |
| 471 if prop.type_ in (PropertyType.ENUM, PropertyType.CHOICES): | 505 if prop.type_ in (PropertyType.ENUM, PropertyType.CHOICES): |
| 472 if prop.optional: | 506 if prop.optional: |
| 473 prop_name = prop.unix_name | 507 prop_name = prop.unix_name |
| 474 if prop.type_ == PropertyType.CHOICES: | 508 if prop.type_ == PropertyType.CHOICES: |
| 475 prop_name = prop.unix_name + '_type' | 509 prop_name = prop.unix_name + '_type' |
| 476 c.Append('%s->%s = %s;' % ( | 510 c.Append('%s->%s = %s;' % ( |
| 477 dst, | 511 dst, |
| 478 prop_name, | 512 prop_name, |
| 479 self._cpp_type_generator.GetEnumNoneValue(prop))) | 513 self._cpp_type_generator.GetEnumNoneValue(prop))) |
| 480 return c | 514 return c |
| 481 | 515 |
| OLD | NEW |