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_API_UTIL_NAMESPACE = 'json_schema_compiler::util' 6 7 8class UtilCCHelper(object): 9 """A util class that generates code that uses 10 tools/json_schema_compiler/util.cc. 11 """ 12 def __init__(self, type_manager): 13 self._type_manager = type_manager 14 15 def PopulateArrayFromDictionary(self, array_prop, src, name, dst): 16 """Generates code to get an array from a src.name into dst. 17 18 src: DictionaryValue* 19 dst: std::vector or scoped_ptr<std::vector> 20 """ 21 prop = array_prop.item_type 22 sub = { 23 'namespace': _API_UTIL_NAMESPACE, 24 'name': name, 25 'src': src, 26 'dst': dst, 27 } 28 29 sub['type'] = self._type_manager.GetCppType(prop), 30 if array_prop.optional: 31 val = ('%(namespace)s::PopulateOptionalArrayFromDictionary' 32 '(*%(src)s, "%(name)s", &%(dst)s)') 33 else: 34 val = ('%(namespace)s::PopulateArrayFromDictionary' 35 '(*%(src)s, "%(name)s", &%(dst)s)') 36 37 return val % sub 38 39 def PopulateArrayFromList(self, src, dst, optional): 40 """Generates code to get an array from src into dst. 41 42 src: ListValue* 43 dst: std::vector or scoped_ptr<std::vector> 44 """ 45 if optional: 46 val = '%(namespace)s::PopulateOptionalArrayFromList(*%(src)s, &%(dst)s)' 47 else: 48 val = '%(namespace)s::PopulateArrayFromList(*%(src)s, &%(dst)s)' 49 return val % { 50 'namespace': _API_UTIL_NAMESPACE, 51 'src': src, 52 'dst': dst 53 } 54 55 def CreateValueFromArray(self, src, optional): 56 """Generates code to create a scoped_pt<Value> from the array at src. 57 58 |src| The variable to convert, either a vector or scoped_ptr<vector>. 59 |optional| Whether |type_| was optional. Optional types are pointers so 60 must be treated differently. 61 """ 62 if optional: 63 name = 'CreateValueFromOptionalArray' 64 else: 65 name = 'CreateValueFromArray' 66 return '%s::%s(%s)' % (_API_UTIL_NAMESPACE, name, src) 67 68 def GetIncludePath(self): 69 return '#include "tools/json_schema_compiler/util.h"' 70 71 def GetValueTypeString(self, value, is_ptr=False): 72 call = '.GetType()' 73 if is_ptr: 74 call = '->GetType()' 75 return 'json_schema_compiler::util::ValueTypeToString(%s%s)' % (value, call) 76