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 """Utilies and constants specific to Chromium C++ code. | 5 """Utilies and constants specific to Chromium C++ code. |
5 """ | 6 """ |
6 | 7 |
7 from datetime import datetime | 8 from datetime import datetime |
8 from model import PropertyType | 9 from model import Property, PropertyType, Type |
9 import os | 10 import os |
10 | 11 |
11 CHROMIUM_LICENSE = ( | 12 CHROMIUM_LICENSE = ( |
12 """// Copyright (c) %d The Chromium Authors. All rights reserved. | 13 """// Copyright (c) %d The Chromium Authors. All rights reserved. |
13 // Use of this source code is governed by a BSD-style license that can be | 14 // Use of this source code is governed by a BSD-style license that can be |
14 // found in the LICENSE file.""" % datetime.now().year | 15 // found in the LICENSE file.""" % datetime.now().year |
15 ) | 16 ) |
16 GENERATED_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITION IN | 17 GENERATED_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITION IN |
17 // %s | 18 // %s |
18 // DO NOT EDIT. | 19 // DO NOT EDIT. |
19 """ | 20 """ |
20 GENERATED_BUNDLE_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITIONS IN | 21 GENERATED_BUNDLE_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITIONS IN |
21 // %s | 22 // %s |
22 // DO NOT EDIT. | 23 // DO NOT EDIT. |
23 """ | 24 """ |
24 | 25 |
25 def Classname(s): | 26 def Classname(s): |
26 """Translates a namespace name or function name into something more | 27 """Translates a namespace name or function name into something more |
27 suited to C++. | 28 suited to C++. |
28 | 29 |
29 eg experimental.downloads -> Experimental_Downloads | 30 eg experimental.downloads -> Experimental_Downloads |
30 updateAll -> UpdateAll. | 31 updateAll -> UpdateAll. |
31 """ | 32 """ |
32 return '_'.join([x[0].upper() + x[1:] for x in s.split('.')]) | 33 return '_'.join([x[0].upper() + x[1:] for x in s.split('.')]) |
33 | 34 |
34 def GetAsFundamentalValue(prop, src, dst): | 35 def GetAsFundamentalValue(type_, src, dst): |
35 """Returns the C++ code for retrieving a fundamental type from a | 36 """Returns the C++ code for retrieving a fundamental type from a |
36 Value into a variable. | 37 Value into a variable. |
37 | 38 |
38 src: Value* | 39 src: Value* |
39 dst: Property* | 40 dst: Property* |
40 """ | 41 """ |
41 return { | 42 return { |
42 PropertyType.STRING: '%s->GetAsString(%s)', | 43 PropertyType.STRING: '%s->GetAsString(%s)', |
43 PropertyType.BOOLEAN: '%s->GetAsBoolean(%s)', | 44 PropertyType.BOOLEAN: '%s->GetAsBoolean(%s)', |
44 PropertyType.INTEGER: '%s->GetAsInteger(%s)', | 45 PropertyType.INTEGER: '%s->GetAsInteger(%s)', |
45 PropertyType.DOUBLE: '%s->GetAsDouble(%s)', | 46 PropertyType.DOUBLE: '%s->GetAsDouble(%s)', |
46 }[prop.type_] % (src, dst) | 47 }[type_.property_type] % (src, dst) |
47 | 48 |
48 def GetValueType(type_): | 49 def GetValueType(type_): |
49 """Returns the Value::Type corresponding to the model.PropertyType. | 50 """Returns the Value::Type corresponding to the model.PropertyType. |
50 """ | 51 """ |
51 return { | 52 return { |
52 PropertyType.STRING: 'Value::TYPE_STRING', | 53 PropertyType.STRING: 'Value::TYPE_STRING', |
53 PropertyType.INTEGER: 'Value::TYPE_INTEGER', | 54 PropertyType.INTEGER: 'Value::TYPE_INTEGER', |
54 PropertyType.BOOLEAN: 'Value::TYPE_BOOLEAN', | 55 PropertyType.BOOLEAN: 'Value::TYPE_BOOLEAN', |
55 PropertyType.DOUBLE: 'Value::TYPE_DOUBLE', | 56 PropertyType.DOUBLE: 'Value::TYPE_DOUBLE', |
56 PropertyType.ENUM: 'Value::TYPE_STRING', | 57 PropertyType.ENUM: 'Value::TYPE_STRING', |
57 PropertyType.OBJECT: 'Value::TYPE_DICTIONARY', | 58 PropertyType.OBJECT: 'Value::TYPE_DICTIONARY', |
58 PropertyType.FUNCTION: 'Value::TYPE_DICTIONARY', | 59 PropertyType.FUNCTION: 'Value::TYPE_DICTIONARY', |
59 PropertyType.ARRAY: 'Value::TYPE_LIST', | 60 PropertyType.ARRAY: 'Value::TYPE_LIST', |
60 PropertyType.BINARY: 'Value::TYPE_BINARY', | 61 PropertyType.BINARY: 'Value::TYPE_BINARY', |
61 }[type_] | 62 }[type_.property_type] |
62 | 63 |
63 def GetParameterDeclaration(param, type_): | 64 def GetParameterDeclaration(param, type_): |
64 """Gets a parameter declaration of a given model.Property and its C++ | 65 """Gets a parameter declaration of a given model.Property and its C++ |
65 type. | 66 type. |
66 """ | 67 """ |
67 if param.type_ in (PropertyType.REF, PropertyType.OBJECT, PropertyType.ARRAY, | 68 if param.type_.property_type in (PropertyType.REF, |
68 PropertyType.STRING, PropertyType.ANY): | 69 PropertyType.OBJECT, |
69 arg = '%(type)s& %(name)s' | 70 PropertyType.ARRAY, |
| 71 PropertyType.STRING, |
| 72 PropertyType.ANY): |
| 73 arg = 'const %(type)s& %(name)s' |
70 else: | 74 else: |
71 arg = '%(type)s %(name)s' | 75 arg = '%(type)s %(name)s' |
72 return arg % { | 76 return arg % { |
73 'type': type_, | 77 'type': type_, |
74 'name': param.unix_name, | 78 'name': param.unix_name, |
75 } | 79 } |
76 | 80 |
77 def GenerateIfndefName(path, filename): | 81 def GenerateIfndefName(path, filename): |
78 """Formats a path and filename as a #define name. | 82 """Formats a path and filename as a #define name. |
79 | 83 |
80 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__. | 84 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__. |
81 """ | 85 """ |
82 return (('%s_%s_H__' % (path, filename)) | 86 return (('%s_%s_H__' % (path, filename)) |
83 .upper().replace(os.sep, '_').replace('/', '_')) | 87 .upper().replace(os.sep, '_').replace('/', '_')) |
84 | 88 |
85 def GenerateTypeToCompiledTypeConversion(prop, from_, to): | 89 def PadForGenerics(var): |
86 try: | 90 """Appends a space to |var| if it ends with a >, so that it can be compiled |
87 return _GenerateTypeConversionHelper(prop.type_, prop.compiled_type, from_, | 91 within generic types. |
88 to) | |
89 except KeyError: | |
90 raise NotImplementedError('Conversion from %s to %s in %s not supported' % | |
91 (prop.type_, prop.compiled_type, prop.name)) | |
92 | |
93 def GenerateCompiledTypeToTypeConversion(prop, from_, to): | |
94 try: | |
95 return _GenerateTypeConversionHelper(prop.compiled_type, prop.type_, from_, | |
96 to) | |
97 except KeyError: | |
98 raise NotImplementedError('Conversion from %s to %s in %s not supported' % | |
99 (prop.compiled_type, prop.type_, prop.name)) | |
100 | |
101 def _GenerateTypeConversionHelper(from_type, to_type, from_, to): | |
102 """Converts from PropertyType from_type to PropertyType to_type. | |
103 | |
104 from_type: The PropertyType to be converted from. | |
105 to_type: The PropertyType to be converted to. | |
106 from_: The variable name of the type to be converted from. | |
107 to: The variable name of the type to be converted to. | |
108 """ | 92 """ |
109 # TODO(mwrosen): Add support for more from/to combinations as necessary. | 93 return ('%s ' % var) if var.endswith('>') else var |
110 return { | |
111 PropertyType.STRING: { | |
112 PropertyType.INTEGER: 'base::StringToInt(%(from)s, &%(to)s)', | |
113 PropertyType.INT64: 'base::StringToInt64(%(from)s, &%(to)s)', | |
114 }, | |
115 PropertyType.INTEGER: { | |
116 PropertyType.STRING: '%(to)s = base::IntToString(%(from)s)', | |
117 }, | |
118 PropertyType.INT64: { | |
119 PropertyType.STRING: '%(to)s = base::Int64ToString(%(from)s)', | |
120 } | |
121 }[from_type][to_type] % {'from': from_, 'to': to} | |
OLD | NEW |