Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / tools / json_schema_compiler / util_cc_helper.py
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.
4
5 from model import PropertyType
6
7
8 _API_UTIL_NAMESPACE = 'json_schema_compiler::util'
9
10
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
17
18   def PopulateArrayFromDictionary(self, array_prop, src, name, dst):
19     """Generates code to get an array from a src.name into dst.
20
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,
30     }
31
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)')
39
40     return val % sub
41
42   def PopulateArrayFromList(self, src, dst, optional):
43     """Generates code to get an array from src into dst.
44
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
56     }
57
58   def CreateValueFromArray(self, cpp_namespace, type_, src, optional):
59     """Generates code to create a scoped_pt<Value> from the array at src.
60
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)
83
84   def GetIncludePath(self):
85     return '#include "tools/json_schema_compiler/util.h"'
86
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)