Revert of Use ExtensionRegistryObserver instead of deprecated extension notification...
[chromium-blink-merge.git] / tools / json_schema_compiler / util_cc_helper.py
blob864028364eece9787ba441fa9a846c845fe95e7b
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.
5 from model import PropertyType
8 _API_UTIL_NAMESPACE = 'json_schema_compiler::util'
11 class UtilCCHelper(object):
12 """A util class that generates code that uses
13 tools/json_schema_compiler/util.cc.
14 """
15 def __init__(self, type_manager):
16 self._type_manager = type_manager
18 def PopulateArrayFromDictionary(self, array_prop, src, name, dst):
19 """Generates code to get an array from a src.name into dst.
21 src: DictionaryValue*
22 dst: std::vector or scoped_ptr<std::vector>
23 """
24 prop = array_prop.item_type
25 sub = {
26 'namespace': _API_UTIL_NAMESPACE,
27 'name': name,
28 'src': src,
29 'dst': dst,
32 sub['type'] = self._type_manager.GetCppType(prop),
33 if array_prop.optional:
34 val = ('%(namespace)s::PopulateOptionalArrayFromDictionary'
35 '(*%(src)s, "%(name)s", &%(dst)s)')
36 else:
37 val = ('%(namespace)s::PopulateArrayFromDictionary'
38 '(*%(src)s, "%(name)s", &%(dst)s)')
40 return val % sub
42 def PopulateArrayFromList(self, src, dst, optional):
43 """Generates code to get an array from src into dst.
45 src: ListValue*
46 dst: std::vector or scoped_ptr<std::vector>
47 """
48 if optional:
49 val = '%(namespace)s::PopulateOptionalArrayFromList(*%(src)s, &%(dst)s)'
50 else:
51 val = '%(namespace)s::PopulateArrayFromList(*%(src)s, &%(dst)s)'
52 return val % {
53 'namespace': _API_UTIL_NAMESPACE,
54 'src': src,
55 'dst': dst
58 def CreateValueFromArray(self, cpp_namespace, type_, src, optional):
59 """Generates code to create a scoped_pt<Value> from the array at src.
61 |cpp_namespace| The namespace which contains |type_|. This is needed for
62 enum conversions, where the ToString method is on the containing
63 namespace.
64 |type_| The type of the values being converted. This is needed for enum
65 conversions, to know whether to use the Enum form of conversion.
66 |src| The variable to convert, either a vector or scoped_ptr<vector>.
67 |optional| Whether |type_| was optional. Optional types are pointers so
68 must be treated differently.
69 """
70 if type_.item_type.property_type == PropertyType.ENUM:
71 # Enums are treated specially because C++ templating thinks that they're
72 # ints, but really they're strings.
73 if optional:
74 name = 'CreateValueFromOptionalEnumArray<%s>' % cpp_namespace
75 else:
76 name = 'CreateValueFromEnumArray<%s>' % cpp_namespace
77 else:
78 if optional:
79 name = 'CreateValueFromOptionalArray'
80 else:
81 name = 'CreateValueFromArray'
82 return '%s::%s(%s)' % (_API_UTIL_NAMESPACE, name, src)
84 def GetIncludePath(self):
85 return '#include "tools/json_schema_compiler/util.h"'
87 def GetValueTypeString(self, value, is_ptr=False):
88 call = '.GetType()'
89 if is_ptr:
90 call = '->GetType()'
91 return 'json_schema_compiler::util::ValueTypeToString(%s%s)' % (value, call)