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.
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.
22 dst: std::vector or scoped_ptr<std::vector>
24 prop
= array_prop
.item_type
26 'namespace': _API_UTIL_NAMESPACE
,
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)')
37 val
= ('%(namespace)s::PopulateArrayFromDictionary'
38 '(*%(src)s, "%(name)s", &%(dst)s)')
42 def PopulateArrayFromList(self
, src
, dst
, optional
):
43 """Generates code to get an array from src into dst.
46 dst: std::vector or scoped_ptr<std::vector>
49 val
= '%(namespace)s::PopulateOptionalArrayFromList(*%(src)s, &%(dst)s)'
51 val
= '%(namespace)s::PopulateArrayFromList(*%(src)s, &%(dst)s)'
53 'namespace': _API_UTIL_NAMESPACE
,
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
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.
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.
74 name
= 'CreateValueFromOptionalEnumArray<%s>' % cpp_namespace
76 name
= 'CreateValueFromEnumArray<%s>' % cpp_namespace
79 name
= 'CreateValueFromOptionalArray'
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):
91 return 'json_schema_compiler::util::ValueTypeToString(%s%s)' % (value
, call
)