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_json: |
| 88 (c.Concat(self._GenerateTypePopulate(cpp_namespace, type_)) |
| 89 .Append() |
| 90 ) |
| 91 if type_.from_client: |
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) |
| 166 c.Append('value->SetWithoutPathExpansion("%s", %s);' % ( |
| 167 prop.name, |
| 168 self._CreateValueFromProperty(prop, prop.unix_name))) |
| 169 if prop.optional: |
| 170 c.Eblock(); |
157 (c.Append() | 171 (c.Append() |
158 .Append('return value.Pass();') | 172 .Append('return value.Pass();') |
159 .Eblock('}') | 173 .Eblock('}') |
160 ) | 174 ) |
161 return c | 175 return c |
162 | 176 |
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): | 177 def _GenerateFunction(self, function): |
201 """Generates the definitions for function structs. | 178 """Generates the definitions for function structs. |
202 """ | 179 """ |
203 classname = cpp_util.Classname(function.name) | 180 classname = cpp_util.Classname(function.name) |
204 c = code.Code() | 181 c = code.Code() |
205 | 182 |
206 # Params::Populate function | 183 # Params::Populate function |
207 if function.params: | 184 if function.params: |
208 for param in function.params: | 185 c.Concat(self._GeneratePropertyFunctions(classname + '::Params', |
209 if param.type_ == PropertyType.OBJECT: | 186 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() {}') | 187 (c.Append('%(name)s::Params::Params() {}') |
216 .Append('%(name)s::Params::~Params() {}') | 188 .Append('%(name)s::Params::~Params() {}') |
217 .Append() | 189 .Append() |
218 .Concat(self._GenerateFunctionParamsCreate(function)) | 190 .Concat(self._GenerateFunctionParamsCreate(function)) |
219 .Append() | 191 .Append() |
220 ) | 192 ) |
221 | 193 |
222 # Result::Create function | 194 # Result::Create function |
223 c.Concat(self._GenerateFunctionResultCreate(function)) | 195 if function.callback: |
| 196 c.Concat(self._GenerateFunctionResultCreate(function)) |
224 | 197 |
225 c.Substitute({'name': classname}) | 198 c.Substitute({'name': classname}) |
226 | 199 |
227 return c | 200 return c |
228 | 201 |
| 202 def _GenerateCreateEnumValue(self, cpp_namespace, prop): |
| 203 """Generates a function that returns the |StringValue| representation of an |
| 204 enum. |
| 205 """ |
| 206 c = code.Code() |
| 207 c.Append('// static') |
| 208 c.Sblock('scoped_ptr<Value> %(cpp_namespace)s::CreateEnumValue(%(arg)s) {') |
| 209 c.Sblock('switch (%s) {' % prop.unix_name) |
| 210 if prop.optional: |
| 211 (c.Append('case %s: {' % self._cpp_type_generator.GetEnumNoneValue(prop)) |
| 212 .Append(' return scoped_ptr<Value>();') |
| 213 .Append('}') |
| 214 ) |
| 215 for enum_value in prop.enum_values: |
| 216 (c.Append('case %s: {' % |
| 217 self._cpp_type_generator.GetEnumValue(prop, enum_value)) |
| 218 .Append(' return scoped_ptr<Value>(Value::CreateStringValue("%s"));' % |
| 219 enum_value) |
| 220 .Append('}') |
| 221 ) |
| 222 (c.Append('default: {') |
| 223 .Append(' return scoped_ptr<Value>();') |
| 224 .Append('}') |
| 225 ) |
| 226 c.Eblock('}') |
| 227 c.Eblock('}') |
| 228 c.Substitute({ |
| 229 'cpp_namespace': cpp_namespace, |
| 230 'arg': cpp_util.GetParameterDeclaration( |
| 231 prop, self._cpp_type_generator.GetType(prop)) |
| 232 }) |
| 233 return c |
| 234 |
| 235 def _CreateValueFromProperty(self, prop, var): |
| 236 """Creates a Value given a single property. Generated code passes ownership |
| 237 to caller. |
| 238 |
| 239 var: variable or variable* |
| 240 """ |
| 241 if prop.type_ == PropertyType.CHOICES: |
| 242 # CHOICES conversion not implemented because it's not used. If needed, |
| 243 # write something to generate a function that returns a scoped_ptr<Value> |
| 244 # and put it in _GeneratePropertyFunctions. |
| 245 raise NotImplementedError( |
| 246 'Conversion of CHOICES to Value not implemented') |
| 247 if prop.type_ in (PropertyType.REF, PropertyType.OBJECT): |
| 248 if prop.optional: |
| 249 return '%s->ToValue().release()' % var |
| 250 else: |
| 251 return '%s.ToValue().release()' % var |
| 252 elif prop.type_ == PropertyType.ENUM: |
| 253 return 'CreateEnumValue(%s).release()' % var |
| 254 elif prop.type_ == PropertyType.ARRAY: |
| 255 return '%s.release()' % self._util_cc_helper.CreateValueFromArray( |
| 256 prop, var) |
| 257 elif prop.type_.is_fundamental: |
| 258 if prop.optional: |
| 259 var = '*' + var |
| 260 return { |
| 261 PropertyType.STRING: 'Value::CreateStringValue(%s)', |
| 262 PropertyType.BOOLEAN: 'Value::CreateBooleanValue(%s)', |
| 263 PropertyType.INTEGER: 'Value::CreateIntegerValue(%s)', |
| 264 PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)', |
| 265 }[prop.type_] % var |
| 266 else: |
| 267 raise NotImplementedError('Conversion of %s to Value not ' |
| 268 'implemented' % repr(prop.type_)) |
| 269 |
229 def _GenerateParamsCheck(self, function, var): | 270 def _GenerateParamsCheck(self, function, var): |
230 """Generates a check for the correct number of arguments when creating | 271 """Generates a check for the correct number of arguments when creating |
231 Params. | 272 Params. |
232 """ | 273 """ |
233 c = code.Code() | 274 c = code.Code() |
234 num_required = 0 | 275 num_required = 0 |
235 for param in function.params: | 276 for param in function.params: |
236 if not param.optional: | 277 if not param.optional: |
237 num_required += 1 | 278 num_required += 1 |
238 if num_required == len(function.params): | 279 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. | 346 prop: the property the code is populating. |
306 value_var: a Value* that should represent |prop|. | 347 value_var: a Value* that should represent |prop|. |
307 dst: the object with |prop| as a member. | 348 dst: the object with |prop| as a member. |
308 failure_value: the value to return if |prop| cannot be extracted from | 349 failure_value: the value to return if |prop| cannot be extracted from |
309 |value_var| | 350 |value_var| |
310 check_type: if true, will check if |value_var| is the correct Value::Type | 351 check_type: if true, will check if |value_var| is the correct Value::Type |
311 """ | 352 """ |
312 c = code.Code() | 353 c = code.Code() |
313 c.Sblock('{') | 354 c.Sblock('{') |
314 | 355 |
315 if check_type: | 356 if check_type and prop.type_ != PropertyType.CHOICES: |
316 (c.Append('if (!%(value_var)s->IsType(%(value_type)s))') | 357 (c.Append('if (!%(value_var)s->IsType(%(value_type)s))') |
317 .Append(' return %(failure_value)s;') | 358 .Append(' return %(failure_value)s;') |
318 ) | 359 ) |
319 | 360 |
320 if prop.type_.is_fundamental: | 361 if prop.type_.is_fundamental: |
321 if prop.optional: | 362 if prop.optional: |
322 (c.Append('%(ctype)s temp;') | 363 (c.Append('%(ctype)s temp;') |
323 .Append('if (%s)' % | 364 .Append('if (%s)' % |
324 cpp_util.GetAsFundamentalValue(prop, value_var, '&temp')) | 365 cpp_util.GetAsFundamentalValue(prop, value_var, '&temp')) |
325 .Append(' %(dst)s->%(name)s.reset(new %(ctype)s(temp));') | 366 .Append(' %(dst)s->%(name)s.reset(new %(ctype)s(temp));') |
(...skipping 25 matching lines...) Expand all Loading... |
351 elif prop.type_ == PropertyType.ARRAY: | 392 elif prop.type_ == PropertyType.ARRAY: |
352 # util_cc_helper deals with optional and required arrays | 393 # util_cc_helper deals with optional and required arrays |
353 (c.Append('ListValue* list = NULL;') | 394 (c.Append('ListValue* list = NULL;') |
354 .Append('if (!%(value_var)s->GetAsList(&list))') | 395 .Append('if (!%(value_var)s->GetAsList(&list))') |
355 .Append(' return %(failure_value)s;') | 396 .Append(' return %(failure_value)s;') |
356 .Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( | 397 .Append('if (!%s)' % self._util_cc_helper.PopulateArrayFromList( |
357 prop, 'list', dst + '->' + prop.unix_name)) | 398 prop, 'list', dst + '->' + prop.unix_name)) |
358 .Append(' return %(failure_value)s;') | 399 .Append(' return %(failure_value)s;') |
359 ) | 400 ) |
360 elif prop.type_ == PropertyType.CHOICES: | 401 elif prop.type_ == PropertyType.CHOICES: |
361 return self._GeneratePopulateChoices(prop, value_var, dst, failure_value) | 402 type_var = '%(dst)s->%(name)s_type' |
| 403 c.Sblock('switch (%(value_var)s->GetType()) {') |
| 404 for choice in self._cpp_type_generator.GetExpandedChoicesInParams([prop]): |
| 405 (c.Sblock('case %s: {' % cpp_util.GetValueType(choice)) |
| 406 .Concat(self._GeneratePopulatePropertyFromValue( |
| 407 choice, value_var, dst, failure_value, check_type=False)) |
| 408 .Append('%s = %s;' % |
| 409 (type_var, |
| 410 self._cpp_type_generator.GetEnumValue( |
| 411 prop, choice.type_.name))) |
| 412 .Append('break;') |
| 413 .Eblock('}') |
| 414 ) |
| 415 (c.Append('default:') |
| 416 .Append(' return %(failure_value)s;') |
| 417 ) |
| 418 c.Eblock('}') |
362 elif prop.type_ == PropertyType.ENUM: | 419 elif prop.type_ == PropertyType.ENUM: |
363 (c.Append('std::string enum_temp;') | 420 (c.Append('std::string enum_temp;') |
364 .Append('if (!%(value_var)s->GetAsString(&enum_temp))') | 421 .Append('if (!%(value_var)s->GetAsString(&enum_temp))') |
365 .Append(' return %(failure_value)s;') | 422 .Append(' return %(failure_value)s;') |
366 ) | 423 ) |
367 for i, enum_value in enumerate(prop.enum_values): | 424 for i, enum_value in enumerate(prop.enum_values): |
368 (c.Append( | 425 (c.Append( |
369 ('if' if i == 0 else 'else if') + | 426 ('if' if i == 0 else 'else if') + |
370 '(enum_temp == "%s")' % enum_value) | 427 '(enum_temp == "%s")' % enum_value) |
371 .Append(' %s->%s = %s;' % ( | 428 .Append(' %s->%s = %s;' % ( |
372 dst, | 429 dst, |
373 prop.unix_name, | 430 prop.unix_name, |
374 self._cpp_type_generator.GetEnumValue(prop, enum_value))) | 431 self._cpp_type_generator.GetEnumValue(prop, enum_value))) |
375 ) | 432 ) |
376 (c.Append('else') | 433 (c.Append('else') |
377 .Append(' return %(failure_value)s;') | 434 .Append(' return %(failure_value)s;') |
378 ) | 435 ) |
379 else: | 436 else: |
380 raise NotImplementedError(prop.type_) | 437 raise NotImplementedError(prop.type_) |
381 c.Eblock('}') | 438 c.Eblock('}') |
382 c.Substitute({ | 439 sub = { |
383 'value_var': value_var, | 440 'value_var': value_var, |
384 'name': prop.unix_name, | 441 'name': prop.unix_name, |
385 'dst': dst, | 442 'dst': dst, |
386 'ctype': self._cpp_type_generator.GetType(prop), | |
387 'failure_value': failure_value, | 443 'failure_value': failure_value, |
388 'value_type': cpp_util.GetValueType(prop), | 444 } |
389 }) | 445 if prop.type_ != PropertyType.CHOICES: |
| 446 sub['ctype'] = self._cpp_type_generator.GetType(prop) |
| 447 sub['value_type'] = cpp_util.GetValueType(prop) |
| 448 c.Substitute(sub) |
390 return c | 449 return c |
391 | 450 |
392 def _GeneratePopulateChoices(self, prop, value_var, dst, failure_value): | 451 def _GeneratePropertyFunctions(self, param_namespace, params): |
393 """Generates the code to populate a PropertyType.CHOICES parameter or | 452 """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 | 453 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 """ | 454 """ |
403 type_var = '%s->%s_type' % (dst, prop.unix_name) | |
404 | |
405 c = code.Code() | 455 c = code.Code() |
406 c.Sblock('switch (%s->GetType()) {' % value_var) | 456 for param in params: |
407 for choice in self._cpp_type_generator.GetExpandedChoicesInParams([prop]): | 457 if param.type_ == PropertyType.OBJECT: |
408 (c.Sblock('case %s: {' % cpp_util.GetValueType(choice)) | 458 c.Concat(self._GenerateType( |
409 .Concat(self._GeneratePopulatePropertyFromValue( | 459 param_namespace + '::' + cpp_util.Classname(param.name), |
410 choice, value_var, dst, failure_value, check_type=False)) | 460 param)) |
411 .Append('%s = %s;' % | 461 c.Append() |
412 (type_var, | 462 elif param.type_ == PropertyType.ENUM: |
413 self._cpp_type_generator.GetEnumValue( | 463 c.Concat(self._GenerateCreateEnumValue(param_namespace, param)) |
414 prop, choice.type_.name))) | 464 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 | 465 return c |
423 | 466 |
424 def _GenerateFunctionResultCreate(self, function): | 467 def _GenerateFunctionResultCreate(self, function): |
425 """Generate function to create a Result given the return value. | 468 """Generate function to create a Result given the return value. |
426 """ | 469 """ |
427 classname = cpp_util.Classname(function.name) | 470 classname = cpp_util.Classname(function.name) |
428 c = code.Code() | 471 c = code.Code() |
429 params = function.callback.params | 472 params = function.callback.params |
430 | 473 |
431 if not params: | 474 if not params: |
432 (c.Append('Value* %s::Result::Create() {' % classname) | 475 (c.Append('Value* %s::Result::Create() {' % classname) |
433 .Append(' return Value::CreateNullValue();') | 476 .Append(' return Value::CreateNullValue();') |
434 .Append('}') | 477 .Append('}') |
435 ) | 478 ) |
436 else: | 479 else: |
| 480 expanded_params = self._cpp_type_generator.GetExpandedChoicesInParams( |
| 481 params) |
| 482 c.Concat(self._GeneratePropertyFunctions( |
| 483 classname + '::Result', expanded_params)) |
| 484 |
437 # If there is a single parameter, this is straightforward. However, if | 485 # If there is a single parameter, this is straightforward. However, if |
438 # the callback parameter is of 'choices', this generates a Create method | 486 # 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 | 487 # for each choice. This works because only 1 choice can be returned at a |
440 # time. | 488 # time. |
441 for param in self._cpp_type_generator.GetExpandedChoicesInParams(params): | 489 for param in expanded_params: |
442 # We treat this argument as 'required' to avoid wrapping it in a | 490 # We treat this argument as 'required' to avoid wrapping it in a |
443 # scoped_ptr if it's optional. | 491 # scoped_ptr if it's optional. |
444 param_copy = param.Copy() | 492 param_copy = param.Copy() |
445 param_copy.optional = False | 493 param_copy.optional = False |
446 c.Sblock('Value* %(classname)s::Result::Create(const %(arg)s) {') | 494 c.Sblock('Value* %(classname)s::Result::Create(const %(arg)s) {') |
447 if param_copy.type_ == PropertyType.ARRAY: | 495 c.Append('return %s;' % |
448 (c.Append('ListValue* value = new ListValue();') | 496 self._CreateValueFromProperty(param_copy, param_copy.unix_name)) |
449 .Append('%s;' % self._util_cc_helper.PopulateListFromArray( | 497 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, | 498 c.Substitute({'classname': classname, |
458 'arg': cpp_util.GetParameterDeclaration( | 499 'arg': cpp_util.GetParameterDeclaration( |
459 param_copy, self._cpp_type_generator.GetType(param_copy)) | 500 param_copy, self._cpp_type_generator.GetType(param_copy)) |
460 }) | 501 }) |
461 c.Eblock('}') | |
462 | 502 |
463 return c | 503 return c |
464 | 504 |
465 def _InitializePropertyToDefault(self, prop, dst): | 505 def _InitializePropertyToDefault(self, prop, dst): |
466 """Initialize a model.Property to its default value inside an object. | 506 """Initialize a model.Property to its default value inside an object. |
467 | 507 |
468 dst: Type* | 508 dst: Type* |
469 """ | 509 """ |
470 c = code.Code() | 510 c = code.Code() |
471 if prop.type_ in (PropertyType.ENUM, PropertyType.CHOICES): | 511 if prop.type_ in (PropertyType.ENUM, PropertyType.CHOICES): |
472 if prop.optional: | 512 if prop.optional: |
473 prop_name = prop.unix_name | 513 prop_name = prop.unix_name |
474 if prop.type_ == PropertyType.CHOICES: | 514 if prop.type_ == PropertyType.CHOICES: |
475 prop_name = prop.unix_name + '_type' | 515 prop_name = prop.unix_name + '_type' |
476 c.Append('%s->%s = %s;' % ( | 516 c.Append('%s->%s = %s;' % ( |
477 dst, | 517 dst, |
478 prop_name, | 518 prop_name, |
479 self._cpp_type_generator.GetEnumNoneValue(prop))) | 519 self._cpp_type_generator.GetEnumNoneValue(prop))) |
480 return c | 520 return c |
481 | 521 |
OLD | NEW |