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
3 # found in the LICENSE file.
6 from model
import PropertyType
11 class CCGenerator(object):
12 def __init__(self
, type_generator
, cpp_namespace
):
13 self
._type
_generator
= type_generator
14 self
._cpp
_namespace
= cpp_namespace
16 def Generate(self
, namespace
):
17 return _Generator(namespace
,
19 self
._cpp
_namespace
).Generate()
22 class _Generator(object):
23 """A .cc generator for a namespace.
25 def __init__(self
, namespace
, cpp_type_generator
, cpp_namespace
):
26 self
._namespace
= namespace
27 self
._type
_helper
= cpp_type_generator
28 self
._cpp
_namespace
= cpp_namespace
29 self
._target
_namespace
= (
30 self
._type
_helper
.GetCppNamespaceName(self
._namespace
))
31 self
._util
_cc
_helper
= (
32 util_cc_helper
.UtilCCHelper(self
._type
_helper
))
33 self
._generate
_error
_messages
= namespace
.compiler_options
.get(
34 'generate_error_messages', False)
37 """Generates a Code object with the .cc for a single namespace.
40 (c
.Append(cpp_util
.CHROMIUM_LICENSE
)
42 .Append(cpp_util
.GENERATED_FILE_MESSAGE
% self
._namespace
.source_file
)
44 .Append(self
._util
_cc
_helper
.GetIncludePath())
45 .Append('#include "base/logging.h"')
46 .Append('#include "base/strings/string_number_conversions.h"')
47 .Append('#include "base/strings/utf_string_conversions.h"')
48 .Append('#include "%s/%s.h"' %
49 (self
._namespace
.source_file_dir
, self
._namespace
.short_filename
))
50 .Append('#include <set>')
51 .Cblock(self
._type
_helper
.GenerateIncludes(include_soft
=True))
53 .Append('using base::UTF8ToUTF16;')
55 .Concat(cpp_util
.OpenNamespace(self
._cpp
_namespace
))
56 .Cblock(self
._type
_helper
.GetNamespaceStart())
58 if self
._namespace
.properties
:
60 .Append('// Properties')
64 for property in self
._namespace
.properties
.values():
65 property_code
= self
._type
_helper
.GeneratePropertyValues(
67 'const %(type)s %(name)s = %(value)s;',
70 c
.Cblock(property_code
)
71 if self
._namespace
.types
:
76 .Cblock(self
._GenerateTypes
(None, self
._namespace
.types
.values()))
78 if self
._namespace
.functions
:
80 .Append('// Functions')
84 for function
in self
._namespace
.functions
.values():
85 c
.Cblock(self
._GenerateFunction
(function
))
86 if self
._namespace
.events
:
92 for event
in self
._namespace
.events
.values():
93 c
.Cblock(self
._GenerateEvent
(event
))
94 (c
.Concat(self
._type
_helper
.GetNamespaceEnd())
95 .Cblock(cpp_util
.CloseNamespace(self
._cpp
_namespace
))
100 def _GenerateType(self
, cpp_namespace
, type_
):
101 """Generates the function definitions for a type.
103 classname
= cpp_util
.Classname(schema_util
.StripNamespace(type_
.name
))
107 # Wrap functions within types in the type's namespace.
108 (c
.Append('namespace %s {' % classname
)
110 for function
in type_
.functions
.values():
111 c
.Cblock(self
._GenerateFunction
(function
))
112 c
.Append('} // namespace %s' % classname
)
113 elif type_
.property_type
== PropertyType
.ARRAY
:
114 c
.Cblock(self
._GenerateType
(cpp_namespace
, type_
.item_type
))
115 elif type_
.property_type
in (PropertyType
.CHOICES
,
116 PropertyType
.OBJECT
):
117 if cpp_namespace
is None:
118 classname_in_namespace
= classname
120 classname_in_namespace
= '%s::%s' % (cpp_namespace
, classname
)
122 if type_
.property_type
== PropertyType
.OBJECT
:
123 c
.Cblock(self
._GeneratePropertyFunctions
(classname_in_namespace
,
124 type_
.properties
.values()))
126 c
.Cblock(self
._GenerateTypes
(classname_in_namespace
, type_
.choices
))
128 (c
.Append('%s::%s()' % (classname_in_namespace
, classname
))
129 .Cblock(self
._GenerateInitializersAndBody
(type_
))
130 .Append('%s::~%s() {}' % (classname_in_namespace
, classname
))
133 if type_
.origin
.from_json
:
134 c
.Cblock(self
._GenerateTypePopulate
(classname_in_namespace
, type_
))
135 if cpp_namespace
is None: # only generate for top-level types
136 c
.Cblock(self
._GenerateTypeFromValue
(classname_in_namespace
, type_
))
137 if type_
.origin
.from_client
:
138 c
.Cblock(self
._GenerateTypeToValue
(classname_in_namespace
, type_
))
139 elif type_
.property_type
== PropertyType
.ENUM
:
140 (c
.Cblock(self
._GenerateEnumToString
(cpp_namespace
, type_
))
141 .Cblock(self
._GenerateEnumFromString
(cpp_namespace
, type_
))
146 def _GenerateInitializersAndBody(self
, type_
):
148 for prop
in type_
.properties
.values():
151 real_t
= self
._type
_helper
.FollowRef(t
)
152 if real_t
.property_type
== PropertyType
.ENUM
:
153 items
.append('%s(%s)' % (
155 self
._type
_helper
.GetEnumNoneValue(t
)))
158 elif t
.property_type
== PropertyType
.INTEGER
:
159 items
.append('%s(0)' % prop
.unix_name
)
160 elif t
.property_type
== PropertyType
.DOUBLE
:
161 items
.append('%s(0.0)' % prop
.unix_name
)
162 elif t
.property_type
== PropertyType
.BOOLEAN
:
163 items
.append('%s(false)' % prop
.unix_name
)
164 elif (t
.property_type
== PropertyType
.ANY
or
165 t
.property_type
== PropertyType
.ARRAY
or
166 t
.property_type
== PropertyType
.BINARY
or # mapped to std::string
167 t
.property_type
== PropertyType
.CHOICES
or
168 t
.property_type
== PropertyType
.OBJECT
or
169 t
.property_type
== PropertyType
.FUNCTION
or
170 t
.property_type
== PropertyType
.REF
or
171 t
.property_type
== PropertyType
.STRING
):
172 # TODO(miket): It would be nice to initialize CHOICES, but we
173 # don't presently have the semantics to indicate which one of a set
174 # should be the default.
180 s
= ': %s' % (', '.join(items
))
184 return Code().Append(s
)
186 def _GenerateTypePopulate(self
, cpp_namespace
, type_
):
187 """Generates the function for populating a type given a pointer to it.
189 E.g for type "Foo", generates Foo::Populate()
191 classname
= cpp_util
.Classname(schema_util
.StripNamespace(type_
.name
))
193 (c
.Append('// static')
194 .Append('bool %(namespace)s::Populate(')
195 .Sblock(' %s) {' % self
._GenerateParams
(
196 ('const base::Value& value', '%(name)s* out'))))
198 if self
._generate
_error
_messages
:
199 c
.Append('DCHECK(error);')
201 if type_
.property_type
== PropertyType
.CHOICES
:
202 for choice
in type_
.choices
:
203 (c
.Sblock('if (%s) {' % self
._GenerateValueIsTypeExpression
('value',
205 .Concat(self
._GeneratePopulateVariableFromValue
(
208 'out->as_%s' % choice
.unix_name
,
211 .Append('return true;')
214 (c
.Concat(self
._GenerateError
(
215 '"expected %s, got " + %s' %
216 (" or ".join(choice
.name
for choice
in type_
.choices
),
217 self
._util
_cc
_helper
.GetValueTypeString('value'))))
218 .Append('return false;'))
219 elif type_
.property_type
== PropertyType
.OBJECT
:
220 (c
.Sblock('if (!value.IsType(base::Value::TYPE_DICTIONARY)) {')
221 .Concat(self
._GenerateError
(
222 '"expected dictionary, got " + ' +
223 self
._util
_cc
_helper
.GetValueTypeString('value')))
224 .Append('return false;')
227 if type_
.properties
or type_
.additional_properties
is not None:
228 c
.Append('const base::DictionaryValue* dict = '
229 'static_cast<const base::DictionaryValue*>(&value);')
230 if self
._generate
_error
_messages
:
231 c
.Append('std::set<std::string> keys;')
232 for prop
in type_
.properties
.itervalues():
233 c
.Concat(self
._InitializePropertyToDefault
(prop
, 'out'))
234 for prop
in type_
.properties
.itervalues():
235 if self
._generate
_error
_messages
:
236 c
.Append('keys.insert("%s");' % (prop
.name
))
237 c
.Concat(self
._GenerateTypePopulateProperty
(prop
, 'dict', 'out'))
238 # Check for extra values.
239 if self
._generate
_error
_messages
:
240 (c
.Sblock('for (base::DictionaryValue::Iterator it(*dict); '
241 '!it.IsAtEnd(); it.Advance()) {')
242 .Sblock('if (!keys.count(it.key())) {')
243 .Concat(self
._GenerateError
('"found unexpected key \'" + '
248 if type_
.additional_properties
is not None:
249 if type_
.additional_properties
.property_type
== PropertyType
.ANY
:
250 c
.Append('out->additional_properties.MergeDictionary(dict);')
252 cpp_type
= self
._type
_helper
.GetCppType(type_
.additional_properties
,
253 is_in_container
=True)
254 (c
.Append('for (base::DictionaryValue::Iterator it(*dict);')
255 .Sblock(' !it.IsAtEnd(); it.Advance()) {')
256 .Append('%s tmp;' % cpp_type
)
257 .Concat(self
._GeneratePopulateVariableFromValue
(
258 type_
.additional_properties
,
262 .Append('out->additional_properties[it.key()] = tmp;')
265 c
.Append('return true;')
267 .Substitute({'namespace': cpp_namespace
, 'name': classname
}))
270 def _GenerateValueIsTypeExpression(self
, var
, type_
):
271 real_type
= self
._type
_helper
.FollowRef(type_
)
272 if real_type
.property_type
is PropertyType
.CHOICES
:
273 return '(%s)' % ' || '.join(self
._GenerateValueIsTypeExpression
(var
,
275 for choice
in real_type
.choices
)
276 return '%s.IsType(%s)' % (var
, cpp_util
.GetValueType(real_type
))
278 def _GenerateTypePopulateProperty(self
, prop
, src
, dst
):
279 """Generate the code to populate a single property in a type.
281 src: base::DictionaryValue*
285 value_var
= prop
.unix_name
+ '_value'
286 c
.Append('const base::Value* %(value_var)s = NULL;')
289 'if (%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {')
290 .Concat(self
._GeneratePopulatePropertyFromValue
(
291 prop
, value_var
, dst
, 'false')))
292 underlying_type
= self
._type
_helper
.FollowRef(prop
.type_
)
293 if underlying_type
.property_type
== PropertyType
.ENUM
:
294 (c
.Append('} else {')
295 .Append('%%(dst)s->%%(name)s = %s;' %
296 self
._type
_helper
.GetEnumNoneValue(prop
.type_
)))
300 'if (!%(src)s->GetWithoutPathExpansion("%(key)s", &%(value_var)s)) {')
301 .Concat(self
._GenerateError
('"\'%%(key)s\' is required"'))
302 .Append('return false;')
304 .Concat(self
._GeneratePopulatePropertyFromValue
(
305 prop
, value_var
, dst
, 'false'))
309 'value_var': value_var
,
313 'name': prop
.unix_name
317 def _GenerateTypeFromValue(self
, cpp_namespace
, type_
):
318 classname
= cpp_util
.Classname(schema_util
.StripNamespace(type_
.name
))
320 (c
.Append('// static')
321 .Append('scoped_ptr<%s> %s::FromValue(%s) {' % (classname
,
322 cpp_namespace
, self
._GenerateParams
(('const base::Value& value',))))
324 if self
._generate
_error
_messages
:
325 c
.Append('DCHECK(error);')
326 (c
.Append(' scoped_ptr<%s> out(new %s());' % (classname
, classname
))
327 .Append(' if (!Populate(%s))' % self
._GenerateArgs
(
328 ('value', 'out.get()')))
329 .Append(' return scoped_ptr<%s>();' % classname
)
330 .Append(' return out.Pass();')
335 def _GenerateTypeToValue(self
, cpp_namespace
, type_
):
336 """Generates a function that serializes the type into a base::Value.
337 E.g. for type "Foo" generates Foo::ToValue()
339 if type_
.property_type
== PropertyType
.OBJECT
:
340 return self
._GenerateObjectTypeToValue
(cpp_namespace
, type_
)
341 elif type_
.property_type
== PropertyType
.CHOICES
:
342 return self
._GenerateChoiceTypeToValue
(cpp_namespace
, type_
)
344 raise ValueError("Unsupported property type %s" % type_
.type_
)
346 def _GenerateObjectTypeToValue(self
, cpp_namespace
, type_
):
347 """Generates a function that serializes an object-representing type
348 into a base::DictionaryValue.
351 (c
.Sblock('scoped_ptr<base::DictionaryValue> %s::ToValue() const {' %
353 .Append('scoped_ptr<base::DictionaryValue> value('
354 'new base::DictionaryValue());')
358 for prop
in type_
.properties
.values():
359 prop_var
= 'this->%s' % prop
.unix_name
361 # Optional enum values are generated with a NONE enum value.
362 underlying_type
= self
._type
_helper
.FollowRef(prop
.type_
)
363 if underlying_type
.property_type
== PropertyType
.ENUM
:
364 c
.Sblock('if (%s != %s) {' %
366 self
._type
_helper
.GetEnumNoneValue(prop
.type_
)))
368 c
.Sblock('if (%s.get()) {' % prop_var
)
370 # ANY is a base::Value which is abstract and cannot be a direct member, so
371 # it will always be a pointer.
372 is_ptr
= prop
.optional
or prop
.type_
.property_type
== PropertyType
.ANY
373 c
.Cblock(self
._CreateValueFromType
(
374 'value->SetWithoutPathExpansion("%s", %%s);' % prop
.name
,
383 if type_
.additional_properties
is not None:
384 if type_
.additional_properties
.property_type
== PropertyType
.ANY
:
385 c
.Append('value->MergeDictionary(&additional_properties);')
387 # Non-copyable types will be wrapped in a linked_ptr for inclusion in
388 # maps, so we need to unwrap them.
390 not self
._type
_helper
.IsCopyable(type_
.additional_properties
))
391 cpp_type
= self
._type
_helper
.GetCppType(type_
.additional_properties
,
392 is_in_container
=True)
393 (c
.Sblock('for (std::map<std::string, %s>::const_iterator it =' %
394 cpp_util
.PadForGenerics(cpp_type
))
395 .Append(' additional_properties.begin();')
396 .Append(' it != additional_properties.end(); ++it) {')
397 .Cblock(self
._CreateValueFromType
(
398 'value->SetWithoutPathExpansion(it->first, %s);',
399 type_
.additional_properties
.name
,
400 type_
.additional_properties
,
401 '%sit->second' % ('*' if needs_unwrap
else '')))
406 .Append('return value.Pass();')
409 def _GenerateChoiceTypeToValue(self
, cpp_namespace
, type_
):
410 """Generates a function that serializes a choice-representing type
414 c
.Sblock('scoped_ptr<base::Value> %s::ToValue() const {' % cpp_namespace
)
415 c
.Append('scoped_ptr<base::Value> result;')
416 for choice
in type_
.choices
:
417 choice_var
= 'as_%s' % choice
.unix_name
418 (c
.Sblock('if (%s) {' % choice_var
)
419 .Append('DCHECK(!result) << "Cannot set multiple choices for %s";' %
421 .Cblock(self
._CreateValueFromType
('result.reset(%s);',
427 (c
.Append('DCHECK(result) << "Must set at least one choice for %s";' %
429 .Append('return result.Pass();')
434 def _GenerateFunction(self
, function
):
435 """Generates the definitions for function structs.
439 # TODO(kalman): use function.unix_name not Classname.
440 function_namespace
= cpp_util
.Classname(function
.name
)
441 # Windows has a #define for SendMessage, so to avoid any issues, we need
442 # to not use the name.
443 if function_namespace
== 'SendMessage':
444 function_namespace
= 'PassMessage'
445 (c
.Append('namespace %s {' % function_namespace
)
449 # Params::Populate function
451 c
.Concat(self
._GeneratePropertyFunctions
('Params', function
.params
))
452 (c
.Append('Params::Params() {}')
453 .Append('Params::~Params() {}')
455 .Cblock(self
._GenerateFunctionParamsCreate
(function
))
458 # Results::Create function
459 if function
.callback
:
460 c
.Concat(self
._GenerateCreateCallbackArguments
(function_namespace
,
464 c
.Append('} // namespace %s' % function_namespace
)
467 def _GenerateEvent(self
, event
):
468 # TODO(kalman): use event.unix_name not Classname.
470 event_namespace
= cpp_util
.Classname(event
.name
)
471 (c
.Append('namespace %s {' % event_namespace
)
473 .Cblock(self
._GenerateEventNameConstant
(None, event
))
474 .Cblock(self
._GenerateCreateCallbackArguments
(event_namespace
,
477 .Append('} // namespace %s' % event_namespace
)
481 def _CreateValueFromType(self
, code
, prop_name
, type_
, var
, is_ptr
=False):
482 """Creates a base::Value given a type. Generated code passes ownership
485 var: variable or variable*
487 E.g for std::string, generate new base::StringValue(var)
490 underlying_type
= self
._type
_helper
.FollowRef(type_
)
491 if underlying_type
.property_type
== PropertyType
.ARRAY
:
492 # Enums are treated specially because C++ templating thinks that they're
493 # ints, but really they're strings. So we create a vector of strings and
494 # populate it with the names of the enum in the array. The |ToString|
495 # function of the enum can be in another namespace when the enum is
496 # referenced. Templates can not be used here because C++ templating does
497 # not support passing a namespace as an argument.
498 item_type
= self
._type
_helper
.FollowRef(underlying_type
.item_type
)
499 if item_type
.property_type
== PropertyType
.ENUM
:
500 vardot
= '(%s)%s' % (var
, '->' if is_ptr
else '.')
503 if type_
.item_type
.property_type
== PropertyType
.REF
:
504 maybe_namespace
= '%s::' % item_type
.namespace
.unix_name
506 enum_list_var
= '%s_list' % prop_name
507 # Scope the std::vector variable declaration inside braces.
509 .Append('std::vector<std::string> %s;' % enum_list_var
)
510 .Append('for (std::vector<%s>::const_iterator it = %sbegin();'
511 % (self
._type
_helper
.GetCppType(item_type
), vardot
))
512 .Sblock(' it != %send(); ++it) {' % vardot
)
513 .Append('%s.push_back(%sToString(*it));' % (enum_list_var
,
517 # Because the std::vector above is always created for both required and
518 # optional enum arrays, |is_ptr| is set to false and uses the
519 # std::vector to create the values.
521 self
._GenerateCreateValueFromType
(type_
, enum_list_var
, False))
525 c
.Append(code
% self
._GenerateCreateValueFromType
(type_
, var
, is_ptr
))
528 def _GenerateCreateValueFromType(self
, type_
, var
, is_ptr
):
529 """Generates the statement to create a base::Value given a type.
531 type_: The type of the values being converted.
532 var: The name of the variable.
533 is_ptr: Whether |type_| is optional.
535 underlying_type
= self
._type
_helper
.FollowRef(type_
)
536 if (underlying_type
.property_type
== PropertyType
.CHOICES
or
537 underlying_type
.property_type
== PropertyType
.OBJECT
):
539 return '(%s)->ToValue().release()' % var
541 return '(%s).ToValue().release()' % var
542 elif (underlying_type
.property_type
== PropertyType
.ANY
or
543 underlying_type
.property_type
== PropertyType
.FUNCTION
):
545 vardot
= '(%s)->' % var
547 vardot
= '(%s).' % var
548 return '%sDeepCopy()' % vardot
549 elif underlying_type
.property_type
== PropertyType
.ENUM
:
551 if type_
.property_type
== PropertyType
.REF
:
552 maybe_namespace
= '%s::' % underlying_type
.namespace
.unix_name
553 return 'new base::StringValue(%sToString(%s))' % (maybe_namespace
, var
)
554 elif underlying_type
.property_type
== PropertyType
.BINARY
:
559 return ('base::BinaryValue::CreateWithCopiedBuffer(%sdata(), %ssize())' %
561 elif underlying_type
.property_type
== PropertyType
.ARRAY
:
562 return '%s.release()' % self
._util
_cc
_helper
.CreateValueFromArray(
565 elif underlying_type
.property_type
.is_fundamental
:
568 if underlying_type
.property_type
== PropertyType
.STRING
:
569 return 'new base::StringValue(%s)' % var
571 return 'new base::FundamentalValue(%s)' % var
573 raise NotImplementedError('Conversion of %s to base::Value not '
574 'implemented' % repr(type_
.type_
))
576 def _GenerateParamsCheck(self
, function
, var
):
577 """Generates a check for the correct number of arguments when creating
582 for param
in function
.params
:
583 if not param
.optional
:
585 if num_required
== len(function
.params
):
586 c
.Sblock('if (%(var)s.GetSize() != %(total)d) {')
587 elif not num_required
:
588 c
.Sblock('if (%(var)s.GetSize() > %(total)d) {')
590 c
.Sblock('if (%(var)s.GetSize() < %(required)d'
591 ' || %(var)s.GetSize() > %(total)d) {')
592 (c
.Concat(self
._GenerateError
(
593 '"expected %%(total)d arguments, got " '
594 '+ base::IntToString(%%(var)s.GetSize())'))
595 .Append('return scoped_ptr<Params>();')
599 'required': num_required
,
600 'total': len(function
.params
),
604 def _GenerateFunctionParamsCreate(self
, function
):
605 """Generate function to create an instance of Params. The generated
606 function takes a base::ListValue of arguments.
608 E.g for function "Bar", generate Bar::Params::Create()
611 (c
.Append('// static')
612 .Sblock('scoped_ptr<Params> Params::Create(%s) {' % self
._GenerateParams
(
613 ['const base::ListValue& args']))
615 if self
._generate
_error
_messages
:
616 c
.Append('DCHECK(error);')
617 (c
.Concat(self
._GenerateParamsCheck
(function
, 'args'))
618 .Append('scoped_ptr<Params> params(new Params());')
621 for param
in function
.params
:
622 c
.Concat(self
._InitializePropertyToDefault
(param
, 'params'))
624 for i
, param
in enumerate(function
.params
):
625 # Any failure will cause this function to return. If any argument is
626 # incorrect or missing, those following it are not processed. Note that
627 # for optional arguments, we allow missing arguments and proceed because
628 # there may be other arguments following it.
629 failure_value
= 'scoped_ptr<Params>()'
631 value_var
= param
.unix_name
+ '_value'
632 (c
.Append('const base::Value* %(value_var)s = NULL;')
633 .Append('if (args.Get(%(i)s, &%(value_var)s) &&')
634 .Sblock(' !%(value_var)s->IsType(base::Value::TYPE_NULL)) {')
635 .Concat(self
._GeneratePopulatePropertyFromValue
(
636 param
, value_var
, 'params', failure_value
))
639 if not param
.optional
:
641 .Concat(self
._GenerateError
('"\'%%(key)s\' is required"'))
642 .Append('return %s;' % failure_value
)
644 c
.Substitute({'value_var': value_var
, 'i': i
, 'key': param
.name
})
646 .Append('return params.Pass();')
653 def _GeneratePopulatePropertyFromValue(self
,
658 """Generates code to populate property |prop| of |dst_class_var| (a
659 pointer) from a Value*. See |_GeneratePopulateVariableFromValue| for
662 return self
._GeneratePopulateVariableFromValue
(prop
.type_
,
664 '%s->%s' % (dst_class_var
,
667 is_ptr
=prop
.optional
)
669 def _GeneratePopulateVariableFromValue(self
,
675 """Generates code to populate a variable |dst_var| of type |type_| from a
676 Value* at |src_var|. The Value* is assumed to be non-NULL. In the generated
677 code, if |dst_var| fails to be populated then Populate will return
682 underlying_type
= self
._type
_helper
.FollowRef(type_
)
684 if underlying_type
.property_type
.is_fundamental
:
686 (c
.Append('%(cpp_type)s temp;')
687 .Sblock('if (!%s) {' % cpp_util
.GetAsFundamentalValue(
688 self
._type
_helper
.FollowRef(type_
), src_var
, '&temp'))
689 .Concat(self
._GenerateError
(
690 '"\'%%(key)s\': expected ' + '%s, got " + %s' % (
692 self
._util
_cc
_helper
.GetValueTypeString(
693 '%%(src_var)s', True)))))
694 c
.Append('%(dst_var)s.reset();')
695 if not self
._generate
_error
_messages
:
696 c
.Append('return %(failure_value)s;')
699 .Append(' %(dst_var)s.reset(new %(cpp_type)s(temp));')
702 (c
.Sblock('if (!%s) {' % cpp_util
.GetAsFundamentalValue(
703 self
._type
_helper
.FollowRef(type_
),
706 .Concat(self
._GenerateError
(
707 '"\'%%(key)s\': expected ' + '%s, got " + %s' % (
709 self
._util
_cc
_helper
.GetValueTypeString(
710 '%%(src_var)s', True))))
711 .Append('return %(failure_value)s;')
714 elif underlying_type
.property_type
== PropertyType
.OBJECT
:
716 (c
.Append('const base::DictionaryValue* dictionary = NULL;')
717 .Sblock('if (!%(src_var)s->GetAsDictionary(&dictionary)) {')
718 .Concat(self
._GenerateError
(
719 '"\'%%(key)s\': expected dictionary, got " + ' +
720 self
._util
_cc
_helper
.GetValueTypeString('%%(src_var)s', True))))
721 # If an optional property fails to populate, the population can still
722 # succeed with a warning. If no error messages are generated, this
723 # warning is not set and we fail out instead.
724 if not self
._generate
_error
_messages
:
725 c
.Append('return %(failure_value)s;')
728 .Append('scoped_ptr<%(cpp_type)s> temp(new %(cpp_type)s());')
729 .Append('if (!%%(cpp_type)s::Populate(%s)) {' % self
._GenerateArgs
(
730 ('*dictionary', 'temp.get()')))
731 .Append(' return %(failure_value)s;')
735 .Append(' %(dst_var)s = temp.Pass();')
739 (c
.Append('const base::DictionaryValue* dictionary = NULL;')
740 .Sblock('if (!%(src_var)s->GetAsDictionary(&dictionary)) {')
741 .Concat(self
._GenerateError
(
742 '"\'%%(key)s\': expected dictionary, got " + ' +
743 self
._util
_cc
_helper
.GetValueTypeString('%%(src_var)s', True)))
744 .Append('return %(failure_value)s;')
746 .Append('if (!%%(cpp_type)s::Populate(%s)) {' % self
._GenerateArgs
(
747 ('*dictionary', '&%(dst_var)s')))
748 .Append(' return %(failure_value)s;')
751 elif underlying_type
.property_type
== PropertyType
.FUNCTION
:
753 c
.Append('%(dst_var)s.reset(new base::DictionaryValue());')
754 elif underlying_type
.property_type
== PropertyType
.ANY
:
755 c
.Append('%(dst_var)s.reset(%(src_var)s->DeepCopy());')
756 elif underlying_type
.property_type
== PropertyType
.ARRAY
:
757 # util_cc_helper deals with optional and required arrays
758 (c
.Append('const base::ListValue* list = NULL;')
759 .Sblock('if (!%(src_var)s->GetAsList(&list)) {')
760 .Concat(self
._GenerateError
(
761 '"\'%%(key)s\': expected list, got " + ' +
762 self
._util
_cc
_helper
.GetValueTypeString('%%(src_var)s', True)))
764 if is_ptr
and self
._generate
_error
_messages
:
765 c
.Append('%(dst_var)s.reset();')
767 c
.Append('return %(failure_value)s;')
770 item_type
= self
._type
_helper
.FollowRef(underlying_type
.item_type
)
771 if item_type
.property_type
== PropertyType
.ENUM
:
772 c
.Concat(self
._GenerateListValueToEnumArrayConversion
(
779 (c
.Sblock('if (!%s) {' % self
._util
_cc
_helper
.PopulateArrayFromList(
783 c
.Concat(self
._GenerateError
(
784 '"unable to populate array \'%%(parent_key)s\'"'))
785 if is_ptr
and self
._generate
_error
_messages
:
786 c
.Append('%(dst_var)s.reset();')
788 c
.Append('return %(failure_value)s;')
791 elif underlying_type
.property_type
== PropertyType
.CHOICES
:
793 (c
.Append('scoped_ptr<%(cpp_type)s> temp(new %(cpp_type)s());')
794 .Append('if (!%%(cpp_type)s::Populate(%s))' % self
._GenerateArgs
(
795 ('*%(src_var)s', 'temp.get()')))
796 .Append(' return %(failure_value)s;')
797 .Append('%(dst_var)s = temp.Pass();')
800 (c
.Append('if (!%%(cpp_type)s::Populate(%s))' % self
._GenerateArgs
(
801 ('*%(src_var)s', '&%(dst_var)s')))
802 .Append(' return %(failure_value)s;'))
803 elif underlying_type
.property_type
== PropertyType
.ENUM
:
804 c
.Concat(self
._GenerateStringToEnumConversion
(underlying_type
,
808 elif underlying_type
.property_type
== PropertyType
.BINARY
:
809 (c
.Append('const base::BinaryValue* binary_value = NULL;')
810 .Sblock('if (!%(src_var)s->IsType(base::Value::TYPE_BINARY)) {')
811 .Concat(self
._GenerateError
(
812 '"\'%%(key)s\': expected binary, got " + ' +
813 self
._util
_cc
_helper
.GetValueTypeString('%%(src_var)s', True)))
815 if not self
._generate
_error
_messages
:
816 c
.Append('return %(failure_value)s;')
819 .Append(' binary_value =')
820 .Append(' static_cast<const base::BinaryValue*>(%(src_var)s);')
823 (c
.Append('%(dst_var)s.reset(')
824 .Append(' new std::string(binary_value->GetBuffer(),')
825 .Append(' binary_value->GetSize()));')
828 (c
.Append('%(dst_var)s.assign(binary_value->GetBuffer(),')
829 .Append(' binary_value->GetSize());')
833 raise NotImplementedError(type_
)
836 return Code().Sblock('{').Concat(c
.Substitute({
837 'cpp_type': self
._type
_helper
.GetCppType(type_
),
840 'failure_value': failure_value
,
842 'parent_key': type_
.parent
.name
,
845 def _GenerateListValueToEnumArrayConversion(self
,
851 """Returns Code that converts a ListValue of string constants from
852 |src_var| into an array of enums of |type_| in |dst_var|. On failure,
853 returns |failure_value|.
859 cpp_type
= self
._type
_helper
.GetCppType(item_type
, is_in_container
=True)
860 c
.Append('%s.reset(new std::vector<%s>);' %
861 (dst_var
, cpp_util
.PadForGenerics(cpp_type
)))
862 (c
.Sblock('for (base::ListValue::const_iterator it = %s->begin(); '
863 'it != %s->end(); ++it) {' % (src_var
, src_var
))
864 .Append('%s tmp;' % self
._type
_helper
.GetCppType(item_type
))
865 .Concat(self
._GenerateStringToEnumConversion
(item_type
,
869 .Append('%s%spush_back(tmp);' % (dst_var
, accessor
))
874 def _GenerateStringToEnumConversion(self
,
879 """Returns Code that converts a string type in |src_var| to an enum with
880 type |type_| in |dst_var|. In the generated code, if |src_var| is not
881 a valid enum name then the function will return |failure_value|.
883 if type_
.property_type
!= PropertyType
.ENUM
:
884 raise TypeError(type_
)
886 enum_as_string
= '%s_as_string' % type_
.unix_name
887 cpp_type_namespace
= ''
888 if type_
.namespace
!= self
._namespace
:
889 cpp_type_namespace
= '%s::' % type_
.namespace
.unix_name
890 cpp_type_name
= self
._type
_helper
.GetCppType(type_
)
891 (c
.Append('std::string %s;' % enum_as_string
)
892 .Sblock('if (!%s->GetAsString(&%s)) {' % (src_var
, enum_as_string
))
893 .Concat(self
._GenerateError
(
894 '"\'%%(key)s\': expected string, got " + ' +
895 self
._util
_cc
_helper
.GetValueTypeString('%%(src_var)s', True)))
896 .Append('return %s;' % failure_value
)
898 .Append('%s = %sParse%s(%s);' % (dst_var
,
900 cpp_util
.Classname(type_
.name
),
902 .Sblock('if (%s == %s%s) {' % (dst_var
,
904 self
._type
_helper
.GetEnumNoneValue(type_
)))
905 .Concat(self
._GenerateError
(
906 '\"\'%%(key)s\': expected \\"' +
909 for enum_value
in self
._type
_helper
.FollowRef(type_
).enum_values
) +
910 '\\", got \\"" + %s + "\\""' % enum_as_string
))
911 .Append('return %s;' % failure_value
)
913 .Substitute({'src_var': src_var
, 'key': type_
.name
})
917 def _GeneratePropertyFunctions(self
, namespace
, params
):
918 """Generates the member functions for a list of parameters.
920 return self
._GenerateTypes
(namespace
, (param
.type_
for param
in params
))
922 def _GenerateTypes(self
, namespace
, types
):
923 """Generates the member functions for a list of types.
927 c
.Cblock(self
._GenerateType
(namespace
, type_
))
930 def _GenerateEnumToString(self
, cpp_namespace
, type_
):
931 """Generates ToString() which gets the string representation of an enum.
934 classname
= cpp_util
.Classname(schema_util
.StripNamespace(type_
.name
))
936 if cpp_namespace
is not None:
937 c
.Append('// static')
938 maybe_namespace
= '' if cpp_namespace
is None else '%s::' % cpp_namespace
940 c
.Sblock('std::string %sToString(%s enum_param) {' %
941 (maybe_namespace
, classname
))
942 c
.Sblock('switch (enum_param) {')
943 for enum_value
in self
._type
_helper
.FollowRef(type_
).enum_values
:
944 name
= enum_value
.name
945 if 'camel_case_enum_to_string' in self
._namespace
.compiler_options
:
946 name
= enum_value
.CamelName()
947 (c
.Append('case %s: ' % self
._type
_helper
.GetEnumValue(type_
, enum_value
))
948 .Append(' return "%s";' % name
))
949 (c
.Append('case %s:' % self
._type
_helper
.GetEnumNoneValue(type_
))
950 .Append(' return "";')
952 .Append('NOTREACHED();')
953 .Append('return "";')
958 def _GenerateEnumFromString(self
, cpp_namespace
, type_
):
959 """Generates FromClassNameString() which gets an enum from its string
963 classname
= cpp_util
.Classname(schema_util
.StripNamespace(type_
.name
))
965 if cpp_namespace
is not None:
966 c
.Append('// static')
967 maybe_namespace
= '' if cpp_namespace
is None else '%s::' % cpp_namespace
969 c
.Sblock('%s%s %sParse%s(const std::string& enum_string) {' %
970 (maybe_namespace
, classname
, maybe_namespace
, classname
))
971 for i
, enum_value
in enumerate(
972 self
._type
_helper
.FollowRef(type_
).enum_values
):
973 # This is broken up into all ifs with no else ifs because we get
974 # "fatal error C1061: compiler limit : blocks nested too deeply"
976 (c
.Append('if (enum_string == "%s")' % enum_value
.name
)
977 .Append(' return %s;' %
978 self
._type
_helper
.GetEnumValue(type_
, enum_value
)))
979 (c
.Append('return %s;' % self
._type
_helper
.GetEnumNoneValue(type_
))
984 def _GenerateCreateCallbackArguments(self
,
988 """Generate all functions to create Value parameters for a callback.
990 E.g for function "Bar", generate Bar::Results::Create
991 E.g for event "Baz", generate Baz::Create
993 function_scope: the function scope path, e.g. Foo::Bar for the function
994 Foo::Bar::Baz(). May be None if there is no function scope.
995 callback: the Function object we are creating callback arguments for.
998 params
= callback
.params
999 c
.Concat(self
._GeneratePropertyFunctions
(function_scope
, params
))
1001 (c
.Sblock('scoped_ptr<base::ListValue> %(function_scope)s'
1002 'Create(%(declaration_list)s) {')
1003 .Append('scoped_ptr<base::ListValue> create_results('
1004 'new base::ListValue());')
1006 declaration_list
= []
1007 for param
in params
:
1008 declaration_list
.append(cpp_util
.GetParameterDeclaration(
1009 param
, self
._type
_helper
.GetCppType(param
.type_
)))
1010 c
.Cblock(self
._CreateValueFromType
('create_results->Append(%s);',
1014 c
.Append('return create_results.Pass();')
1017 'function_scope': ('%s::' % function_scope
) if function_scope
else '',
1018 'declaration_list': ', '.join(declaration_list
),
1019 'param_names': ', '.join(param
.unix_name
for param
in params
)
1023 def _GenerateEventNameConstant(self
, function_scope
, event
):
1024 """Generates a constant string array for the event name.
1027 c
.Append('const char kEventName[] = "%s.%s";' % (
1028 self
._namespace
.name
, event
.name
))
1031 def _InitializePropertyToDefault(self
, prop
, dst
):
1032 """Initialize a model.Property to its default value inside an object.
1034 E.g for optional enum "state", generate dst->state = STATE_NONE;
1039 underlying_type
= self
._type
_helper
.FollowRef(prop
.type_
)
1040 if (underlying_type
.property_type
== PropertyType
.ENUM
and
1042 c
.Append('%s->%s = %s;' % (
1045 self
._type
_helper
.GetEnumNoneValue(prop
.type_
)))
1048 def _GenerateError(self
, body
):
1049 """Generates an error message pertaining to population failure.
1051 E.g 'expected bool, got int'
1054 if not self
._generate
_error
_messages
:
1056 (c
.Append('if (error->length())')
1057 .Append(' error->append(UTF8ToUTF16("; "));')
1058 .Append('error->append(UTF8ToUTF16(%s));' % body
))
1061 def _GenerateParams(self
, params
):
1062 """Builds the parameter list for a function, given an array of parameters.
1064 if self
._generate
_error
_messages
:
1065 params
= list(params
) + ['base::string16* error']
1066 return ', '.join(str(p
) for p
in params
)
1068 def _GenerateArgs(self
, args
):
1069 """Builds the argument list for a function, given an array of arguments.
1071 if self
._generate
_error
_messages
:
1072 args
= list(args
) + ['error']
1073 return ', '.join(str(a
) for a
in args
)