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 """Utilies and constants specific to Chromium C++ code. | 4 """Utilies and constants specific to Chromium C++ code. |
5 """ | 5 """ |
6 | 6 |
7 from datetime import datetime | 7 from datetime import datetime |
8 from model import PropertyType | 8 from model import PropertyType |
9 | 9 |
10 CHROMIUM_LICENSE = ( | 10 CHROMIUM_LICENSE = ( |
11 """// Copyright (c) %d The Chromium Authors. All rights reserved. | 11 """// Copyright (c) %d The Chromium Authors. All rights reserved. |
12 // Use of this source code is governed by a BSD-style license that can be | 12 // Use of this source code is governed by a BSD-style license that can be |
13 // found in the LICENSE file.""" % datetime.now().year | 13 // found in the LICENSE file.""" % datetime.now().year |
14 ) | 14 ) |
15 GENERATED_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITION IN | 15 GENERATED_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITION IN |
16 // %s | 16 // %s |
17 // DO NOT EDIT. | 17 // DO NOT EDIT. |
18 """ | 18 """ |
19 | 19 |
20 def Classname(s): | 20 def Classname(s): |
21 """Translates a namespace name or function name into something more | 21 """Translates a namespace name or function name into something more |
22 suited to C++. | 22 suited to C++. |
23 | 23 |
24 eg experimental.downloads -> Experimental_Downloads | 24 eg experimental.downloads -> Experimental_Downloads |
25 updateAll -> UpdateAll. | 25 updateAll -> UpdateAll. |
26 """ | 26 """ |
27 return '_'.join([x[0].upper() + x[1:] for x in s.split('.')]) | 27 return '_'.join([x[0].upper() + x[1:] for x in s.split('.')]) |
28 | 28 |
29 def CreateFundamentalValue(prop, var): | |
30 """Returns the C++ code for creating a value of the given property type | |
31 using the given variable. | |
32 | |
33 var: Fundamental or Fundamental* | |
34 """ | |
35 if prop.optional: | |
36 var = '*' + var | |
37 return { | |
38 PropertyType.STRING: 'Value::CreateStringValue(%s)', | |
39 PropertyType.BOOLEAN: 'Value::CreateBooleanValue(%s)', | |
40 PropertyType.INTEGER: 'Value::CreateIntegerValue(%s)', | |
41 PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)', | |
42 }[prop.type_] % var | |
43 | |
44 def GetAsFundamentalValue(prop, src, dst): | 29 def GetAsFundamentalValue(prop, src, dst): |
45 """Returns the C++ code for retrieving a fundamental type from a | 30 """Returns the C++ code for retrieving a fundamental type from a |
46 Value into a variable. | 31 Value into a variable. |
47 | 32 |
48 src: Value* | 33 src: Value* |
49 dst: Property* | 34 dst: Property* |
50 """ | 35 """ |
51 return { | 36 return { |
52 PropertyType.STRING: '%s->GetAsString(%s)', | 37 PropertyType.STRING: '%s->GetAsString(%s)', |
53 PropertyType.BOOLEAN: '%s->GetAsBoolean(%s)', | 38 PropertyType.BOOLEAN: '%s->GetAsBoolean(%s)', |
54 PropertyType.INTEGER: '%s->GetAsInteger(%s)', | 39 PropertyType.INTEGER: '%s->GetAsInteger(%s)', |
55 PropertyType.DOUBLE: '%s->GetAsDouble(%s)', | 40 PropertyType.DOUBLE: '%s->GetAsDouble(%s)', |
56 }[prop.type_] % (src, dst) | 41 }[prop.type_] % (src, dst) |
57 | 42 |
58 def GetFundamentalValue(prop, src, name, dst): | 43 def GetValueType(prop): |
59 """Returns the C++ code for retrieving a fundamental type from a | 44 """Returns the Value::Type corresponding to the model.PropertyType. |
60 DictionaryValue into a variable. | |
61 | |
62 src: DictionaryValue* | |
63 name: key | |
64 dst: Property* | |
65 """ | 45 """ |
66 return { | 46 return { |
67 PropertyType.STRING: '%s->GetString("%s", %s)', | 47 PropertyType.STRING: 'Value::TYPE_STRING', |
68 PropertyType.BOOLEAN: '%s->GetBoolean("%s", %s)', | 48 PropertyType.INTEGER: 'Value::TYPE_INTEGER', |
69 PropertyType.INTEGER: '%s->GetInteger("%s", %s)', | 49 PropertyType.BOOLEAN: 'Value::TYPE_BOOLEAN', |
70 PropertyType.DOUBLE: '%s->GetDouble("%s", %s)', | 50 PropertyType.DOUBLE: 'Value::TYPE_DOUBLE', |
71 }[prop.type_] % (src, name, dst) | 51 PropertyType.REF: 'Value::TYPE_DICTIONARY', |
| 52 PropertyType.OBJECT: 'Value::TYPE_DICTIONARY', |
| 53 PropertyType.ARRAY: 'Value::TYPE_LIST' |
| 54 }[prop.type_] |
| 55 |
72 | 56 |
73 def CreateValueFromSingleProperty(prop, var): | 57 def CreateValueFromSingleProperty(prop, var): |
74 """Creates a Value given a single property. Use for everything except | 58 """Creates a Value given a single property. Use for everything except |
75 PropertyType.ARRAY. | 59 PropertyType.ARRAY. |
76 | 60 |
77 var: raw value | 61 var: variable or variable* |
78 """ | 62 """ |
79 if prop.type_ == PropertyType.REF or prop.type_ == PropertyType.OBJECT: | 63 if prop.type_ in (PropertyType.REF, PropertyType.OBJECT): |
80 if prop.optional: | 64 if prop.optional: |
81 return '%s->ToValue().release()' % var | 65 return '%s->ToValue().release()' % var |
82 else: | 66 else: |
83 return '%s.ToValue().release()' % var | 67 return '%s.ToValue().release()' % var |
84 elif prop.type_.is_fundamental: | 68 elif prop.type_.is_fundamental: |
85 return CreateFundamentalValue(prop, var) | 69 if prop.optional: |
| 70 var = '*' + var |
| 71 return { |
| 72 PropertyType.STRING: 'Value::CreateStringValue(%s)', |
| 73 PropertyType.BOOLEAN: 'Value::CreateBooleanValue(%s)', |
| 74 PropertyType.INTEGER: 'Value::CreateIntegerValue(%s)', |
| 75 PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)', |
| 76 }[prop.type_] % var |
86 else: | 77 else: |
87 raise NotImplementedError('Conversion of single %s to Value not implemented' | 78 raise NotImplementedError('Conversion of single %s to Value not implemented' |
88 % repr(prop.type_)) | 79 % repr(prop.type_)) |
89 | 80 |
90 def GetValueFromList(prop, src, index, dst): | 81 def GetParameterDeclaration(param, type_): |
91 """Returns the C++ code for retrieving a fundamental type from a | 82 """Gets a const parameter declaration of a given model.Property and its C++ |
92 DictionaryValue into a variable. | 83 type. |
93 | |
94 src: ListValue& | |
95 index: int | |
96 dst: Property* | |
97 """ | 84 """ |
98 return { | 85 if param.type_ in (PropertyType.REF, PropertyType.OBJECT, PropertyType.ARRAY, |
99 PropertyType.REF: '%s.GetDictionary(%d, %s)', | 86 PropertyType.STRING): |
100 PropertyType.STRING: '%s.GetString(%d, %s)', | 87 arg = 'const %(type)s& %(name)s' |
101 PropertyType.BOOLEAN: '%s.GetBoolean(%d, %s)', | 88 else: |
102 PropertyType.INTEGER: '%s.GetInteger(%d, %s)', | |
103 PropertyType.DOUBLE: '%s.GetDouble(%d, %s)', | |
104 }[prop.type_] % (src, index, dst) | |
105 | |
106 def GetConstParameterDeclaration(param, type_generator): | |
107 if param.type_.is_fundamental: | |
108 arg = 'const %(type)s %(name)s' | 89 arg = 'const %(type)s %(name)s' |
109 else: | |
110 arg = 'const %(type)s& %(name)s' | |
111 return arg % { | 90 return arg % { |
112 'type': type_generator.GetType(param, wrap_optional=True), | 91 'type': type_, |
113 'name': param.unix_name, | 92 'name': param.unix_name, |
114 } | 93 } |
OLD | NEW |