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 API_UTIL_NAMESPACE
= 'json_schema_compiler::util'
7 class UtilCCHelper(object):
8 """A util class that generates code that uses
9 tools/json_schema_compiler/util.cc.
11 def __init__(self
, type_manager
):
12 self
._type
_manager
= type_manager
14 def PopulateArrayFromDictionary(self
, array_prop
, src
, name
, dst
):
15 """Generates code to get an array from a src.name into dst.
18 dst: std::vector or scoped_ptr<std::vector>
20 prop
= array_prop
.item_type
22 'namespace': API_UTIL_NAMESPACE
,
28 sub
['type'] = self
._type
_manager
.GetCppType(prop
),
29 if array_prop
.optional
:
30 val
= ('%(namespace)s::PopulateOptionalArrayFromDictionary'
31 '(*%(src)s, "%(name)s", &%(dst)s)')
33 val
= ('%(namespace)s::PopulateArrayFromDictionary'
34 '(*%(src)s, "%(name)s", &%(dst)s)')
38 def PopulateArrayFromList(self
, array_prop
, src
, dst
, optional
):
39 """Generates code to get an array from src into dst.
42 dst: std::vector or scoped_ptr<std::vector>
44 prop
= array_prop
.item_type
46 'namespace': API_UTIL_NAMESPACE
,
49 'type': self
._type
_manager
.GetCppType(prop
),
53 val
= '%(namespace)s::PopulateOptionalArrayFromList(*%(src)s, &%(dst)s)'
55 val
= '%(namespace)s::PopulateArrayFromList(*%(src)s, &%(dst)s)'
59 def CreateValueFromArray(self
, array_prop
, src
, optional
):
60 """Generates code to create a scoped_pt<Value> from the array at src.
62 src: std::vector or scoped_ptr<std::vector>
64 prop
= array_prop
.item_type
66 'namespace': API_UTIL_NAMESPACE
,
68 'type': self
._type
_manager
.GetCppType(prop
),
72 val
= '%(namespace)s::CreateValueFromOptionalArray(%(src)s)'
74 val
= '%(namespace)s::CreateValueFromArray(%(src)s)'
78 def GetIncludePath(self
):
79 return '#include "tools/json_schema_compiler/util.h"'