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 | 20 def Classname(s): |
21 def CppName(s): | |
22 """Translates a namespace name or function name into something more | 21 """Translates a namespace name or function name into something more |
23 suited to C++. | 22 suited to C++. |
24 | 23 |
25 eg experimental.downloads -> Experimental_Downloads | 24 eg experimental.downloads -> Experimental_Downloads |
26 updateAll -> UpdateAll. | 25 updateAll -> UpdateAll. |
27 """ | 26 """ |
28 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('.')]) |
29 | 28 |
30 def CreateFundamentalValue(prop, var): | 29 def CreateFundamentalValue(prop, var): |
31 """Returns the C++ code for creating a value of the given property type | 30 """Returns the C++ code for creating a value of the given property type |
32 using the given variable. | 31 using the given variable. |
| 32 |
| 33 var: Fundamental or Fundamental* |
33 """ | 34 """ |
| 35 if prop.optional: |
| 36 var = '*' + var |
34 return { | 37 return { |
35 PropertyType.STRING: 'Value::CreateStringValue(%s)', | 38 PropertyType.STRING: 'Value::CreateStringValue(%s)', |
36 PropertyType.BOOLEAN: 'Value::CreateBooleanValue(%s)', | 39 PropertyType.BOOLEAN: 'Value::CreateBooleanValue(%s)', |
37 PropertyType.INTEGER: 'Value::CreateIntegerValue(%s)', | 40 PropertyType.INTEGER: 'Value::CreateIntegerValue(%s)', |
38 PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)', | 41 PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)', |
39 }[prop.type_] % var | 42 }[prop.type_] % var |
40 | 43 |
| 44 def GetAsFundamentalValue(prop, src, dst): |
| 45 """Returns the C++ code for retrieving a fundamental type from a |
| 46 Value into a variable. |
41 | 47 |
42 def GetFundamentalValue(prop, var): | 48 src: Value* |
43 """Returns the C++ code for retrieving a fundamental type from a Value | 49 dst: Property* |
44 into a variable. | |
45 """ | 50 """ |
46 return { | 51 return { |
47 PropertyType.STRING: 'GetAsString(%s)', | 52 PropertyType.STRING: '%s->GetAsString(%s)', |
48 PropertyType.BOOLEAN: 'GetAsBoolean(%s)', | 53 PropertyType.BOOLEAN: '%s->GetAsBoolean(%s)', |
49 PropertyType.INTEGER: 'GetAsInteger(%s)', | 54 PropertyType.INTEGER: '%s->GetAsInteger(%s)', |
50 PropertyType.DOUBLE: 'GetAsDouble(%s)', | 55 PropertyType.DOUBLE: '%s->GetAsDouble(%s)', |
51 }[prop.type_] % var | 56 }[prop.type_] % (src, dst) |
| 57 |
| 58 def GetFundamentalValue(prop, src, name, dst): |
| 59 """Returns the C++ code for retrieving a fundamental type from a |
| 60 DictionaryValue into a variable. |
| 61 |
| 62 src: DictionaryValue* |
| 63 name: key |
| 64 dst: Property* |
| 65 """ |
| 66 return { |
| 67 PropertyType.STRING: '%s->GetString("%s", %s)', |
| 68 PropertyType.BOOLEAN: '%s->GetBoolean("%s", %s)', |
| 69 PropertyType.INTEGER: '%s->GetInteger("%s", %s)', |
| 70 PropertyType.DOUBLE: '%s->GetDouble("%s", %s)', |
| 71 }[prop.type_] % (src, name, dst) |
| 72 |
| 73 def CreateValueFromSingleProperty(prop, var): |
| 74 """Creates a Value given a single property. Use for everything except |
| 75 PropertyType.ARRAY. |
| 76 |
| 77 var: raw value |
| 78 """ |
| 79 if prop.type_ == PropertyType.REF or prop.type_ == PropertyType.OBJECT: |
| 80 if prop.optional: |
| 81 return '%s->ToValue()' % var |
| 82 else: |
| 83 return '%s.ToValue()' % var |
| 84 elif prop.type_.is_fundamental: |
| 85 return CreateFundamentalValue(prop, var) |
| 86 else: |
| 87 raise NotImplementedError('Conversion of single %s to Value not implemented' |
| 88 % repr(prop.type_)) |
| 89 |
| 90 def GetValueFromList(prop, src, index, dst): |
| 91 """Returns the C++ code for retrieving a fundamental type from a |
| 92 DictionaryValue into a variable. |
| 93 |
| 94 src: ListValue& |
| 95 index: int |
| 96 dst: Property* |
| 97 """ |
| 98 return { |
| 99 PropertyType.REF: '%s.GetDictionary(%d, %s)', |
| 100 PropertyType.STRING: '%s.GetString(%d, %s)', |
| 101 PropertyType.BOOLEAN: '%s.GetBoolean(%d, %s)', |
| 102 PropertyType.INTEGER: '%s.GetInteger(%d, %s)', |
| 103 PropertyType.DOUBLE: '%s.GetDouble(%d, %s)', |
| 104 }[prop.type_] % (src, index, dst) |
OLD | NEW |