Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 class UtilCCHelper(object): | |
| 8 """A util class that generates code that uses | |
| 9 tools/json_schema_compiler/util.cc. | |
| 10 """ | |
| 11 def __init__(self, type_manager): | |
| 12 self._type_manager = type_manager | |
| 13 | |
| 14 def GetArray(self, array_prop, src, name, dst): | |
| 15 """Generates code to get an array from a src.name into dst. | |
| 16 | |
| 17 src: DictionaryValue* | |
| 18 dst: std::vector or scoped_ptr<std::vector> | |
| 19 """ | |
| 20 prop = array_prop.item_type | |
| 21 sub = { | |
| 22 'namespace': API_UTIL_NAMESPACE, | |
| 23 'name': name, | |
| 24 'src': src, | |
| 25 'dst': dst, | |
| 26 } | |
| 27 | |
| 28 sub['type'] = self._type_manager.GetType(prop) | |
| 29 if array_prop.optional: | |
| 30 val = ('%(namespace)s::GetOptionalArrayFromDictionary' | |
| 31 '(*%(src)s, "%(name)s", &%(dst)s)') | |
| 32 else: | |
| 33 val = ('%(namespace)s::GetArrayFromDictionary' | |
| 34 '(*%(src)s, "%(name)s", &%(dst)s)') | |
| 35 | |
| 36 return val % sub | |
| 37 | |
| 38 def GetArrayFromList(self, array_prop, src, dst): | |
| 39 """Generates code to get an array from src into dst. | |
| 40 | |
| 41 src: ListValue* | |
| 42 dst: std::vector or scoped_ptr<std::vector> | |
| 43 """ | |
| 44 prop = array_prop.item_type | |
| 45 sub = { | |
| 46 'namespace': API_UTIL_NAMESPACE, | |
| 47 'src': src, | |
| 48 'dst': dst, | |
| 49 'type': self._type_manager.GetType(prop), | |
| 50 } | |
| 51 | |
| 52 if array_prop.optional: | |
| 53 val = '%(namespace)s::GetOptionalArrayFromList(*%(src)s, &%(dst)s)' | |
| 54 else: | |
| 55 val = '%(namespace)s::GetArrayFromList(*%(src)s, &%(dst)s)' | |
| 56 | |
| 57 return val % sub | |
| 58 | |
| 59 def SetArray(self, array_prop, src, name, dst): | |
| 60 """Sets dst.name to the array at src | |
|
Yoyo Zhou
2012/02/10 01:49:33
Generates code to...
calamity
2012/02/10 03:52:50
Done.
| |
| 61 | |
| 62 src: std::vector or scoped_ptr<std::vector> | |
| 63 dst: scoped_ptr<DictionaryValue> | |
| 64 """ | |
| 65 prop = array_prop.item_type | |
| 66 sub = { | |
| 67 'namespace': API_UTIL_NAMESPACE, | |
| 68 'src': src, | |
| 69 'name': name, | |
| 70 'dst': dst, | |
| 71 'type': self._type_manager.GetType(prop), | |
| 72 } | |
| 73 | |
| 74 if array_prop.optional: | |
| 75 val = ('%(namespace)s::SetOptionalArrayToDictionary' | |
| 76 '(%(src)s, "%(name)s", %(dst)s.get())') | |
| 77 else: | |
| 78 val = '%(namespace)s::SetArrayToDictionary(%(src)s, "%(name)s", %(dst)s.ge t())' | |
| 79 | |
| 80 return val % sub | |
| 81 | |
| 82 def SetArrayToList(self, array_prop, src, dst): | |
| 83 """Sets dst to the array at src | |
| 84 | |
| 85 src: std::vector or scoped_ptr<std::vector> | |
| 86 dst: ListValue* | |
| 87 """ | |
| 88 prop = array_prop.item_type | |
| 89 sub = { | |
| 90 'namespace': API_UTIL_NAMESPACE, | |
| 91 'src': src, | |
| 92 'dst': dst, | |
| 93 'type': self._type_manager.GetType(prop), | |
| 94 } | |
| 95 | |
| 96 if array_prop.optional: | |
| 97 val = '%(namespace)s::SetOptionalArrayToList(%(src)s, %(dst)s)' | |
| 98 else: | |
| 99 val = '%(namespace)s::SetArrayToList(%(src)s, %(dst)s)' | |
| 100 | |
| 101 return val % sub | |
| 102 | |
| 103 def GetIncludePath(self): | |
| 104 return '#include "tools/json_schema_compiler/util.h"' | |
| OLD | NEW |