| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 from model import PropertyType | |
| 6 | |
| 7 | |
| 8 _API_UTIL_NAMESPACE = 'json_schema_compiler::util' | 5 _API_UTIL_NAMESPACE = 'json_schema_compiler::util' |
| 9 | 6 |
| 10 | 7 |
| 11 class UtilCCHelper(object): | 8 class UtilCCHelper(object): |
| 12 """A util class that generates code that uses | 9 """A util class that generates code that uses |
| 13 tools/json_schema_compiler/util.cc. | 10 tools/json_schema_compiler/util.cc. |
| 14 """ | 11 """ |
| 15 def __init__(self, type_manager): | 12 def __init__(self, type_manager): |
| 16 self._type_manager = type_manager | 13 self._type_manager = type_manager |
| 17 | 14 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 48 if optional: | 45 if optional: |
| 49 val = '%(namespace)s::PopulateOptionalArrayFromList(*%(src)s, &%(dst)s)' | 46 val = '%(namespace)s::PopulateOptionalArrayFromList(*%(src)s, &%(dst)s)' |
| 50 else: | 47 else: |
| 51 val = '%(namespace)s::PopulateArrayFromList(*%(src)s, &%(dst)s)' | 48 val = '%(namespace)s::PopulateArrayFromList(*%(src)s, &%(dst)s)' |
| 52 return val % { | 49 return val % { |
| 53 'namespace': _API_UTIL_NAMESPACE, | 50 'namespace': _API_UTIL_NAMESPACE, |
| 54 'src': src, | 51 'src': src, |
| 55 'dst': dst | 52 'dst': dst |
| 56 } | 53 } |
| 57 | 54 |
| 58 def CreateValueFromArray(self, cpp_namespace, type_, src, optional): | 55 def CreateValueFromArray(self, src, optional): |
| 59 """Generates code to create a scoped_pt<Value> from the array at src. | 56 """Generates code to create a scoped_pt<Value> from the array at src. |
| 60 | 57 |
| 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>. | 58 |src| The variable to convert, either a vector or scoped_ptr<vector>. |
| 67 |optional| Whether |type_| was optional. Optional types are pointers so | 59 |optional| Whether |type_| was optional. Optional types are pointers so |
| 68 must be treated differently. | 60 must be treated differently. |
| 69 """ | 61 """ |
| 70 if type_.item_type.property_type == PropertyType.ENUM: | 62 if optional: |
| 71 # Enums are treated specially because C++ templating thinks that they're | 63 name = 'CreateValueFromOptionalArray' |
| 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: | 64 else: |
| 78 if optional: | 65 name = 'CreateValueFromArray' |
| 79 name = 'CreateValueFromOptionalArray' | |
| 80 else: | |
| 81 name = 'CreateValueFromArray' | |
| 82 return '%s::%s(%s)' % (_API_UTIL_NAMESPACE, name, src) | 66 return '%s::%s(%s)' % (_API_UTIL_NAMESPACE, name, src) |
| 83 | 67 |
| 84 def GetIncludePath(self): | 68 def GetIncludePath(self): |
| 85 return '#include "tools/json_schema_compiler/util.h"' | 69 return '#include "tools/json_schema_compiler/util.h"' |
| 86 | 70 |
| 87 def GetValueTypeString(self, value, is_ptr=False): | 71 def GetValueTypeString(self, value, is_ptr=False): |
| 88 call = '.GetType()' | 72 call = '.GetType()' |
| 89 if is_ptr: | 73 if is_ptr: |
| 90 call = '->GetType()' | 74 call = '->GetType()' |
| 91 return 'json_schema_compiler::util::ValueTypeToString(%s%s)' % (value, call) | 75 return 'json_schema_compiler::util::ValueTypeToString(%s%s)' % (value, call) |
| OLD | NEW |