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 """Utilies and constants specific to Chromium C++ code.
7 from datetime
import datetime
8 from model
import PropertyType
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
13 // found in the LICENSE file.""" % datetime
.now().year
15 GENERATED_FILE_MESSAGE
= """// GENERATED FROM THE API DEFINITION IN
21 """Translates a namespace name or function name into something more
24 eg experimental.downloads -> Experimental_Downloads
25 updateAll -> UpdateAll.
27 return '_'.join([x
[0].upper() + x
[1:] for x
in s
.split('.')])
29 def CreateFundamentalValue(prop
, var
):
30 """Returns the C++ code for creating a value of the given property type
31 using the given variable.
33 var: Fundamental or Fundamental*
38 PropertyType
.STRING
: 'Value::CreateStringValue(%s)',
39 PropertyType
.BOOLEAN
: 'Value::CreateBooleanValue(%s)',
40 PropertyType
.INTEGER
: 'Value::CreateIntegerValue(%s)',
41 PropertyType
.DOUBLE
: 'Value::CreateDoubleValue(%s)',
44 def GetAsFundamentalValue(prop
, src
, dst
):
45 """Returns the C++ code for retrieving a fundamental type from a
46 Value into a variable.
52 PropertyType
.STRING
: '%s->GetAsString(%s)',
53 PropertyType
.BOOLEAN
: '%s->GetAsBoolean(%s)',
54 PropertyType
.INTEGER
: '%s->GetAsInteger(%s)',
55 PropertyType
.DOUBLE
: '%s->GetAsDouble(%s)',
56 }[prop
.type_
] % (src
, dst
)
58 def GetFundamentalValue(prop
, src
, name
, dst
):
59 """Returns the C++ code for retrieving a fundamental type from a
60 DictionaryValue into a variable.
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
)
73 def CreateValueFromSingleProperty(prop
, var
):
74 """Creates a Value given a single property. Use for everything except
79 if prop
.type_
== PropertyType
.REF
or prop
.type_
== PropertyType
.OBJECT
:
81 return '%s->ToValue().release()' % var
83 return '%s.ToValue().release()' % var
84 elif prop
.type_
.is_fundamental
:
85 return CreateFundamentalValue(prop
, var
)
87 raise NotImplementedError('Conversion of single %s to Value not implemented'
90 def GetValueFromList(prop
, src
, index
, dst
):
91 """Returns the C++ code for retrieving a fundamental type from a
92 DictionaryValue into a variable.
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
)
106 def GetConstParameterDeclaration(param
, type_generator
):
107 if param
.type_
== PropertyType
.REF
:
108 arg
= 'const %(type)s& %(name)s'
110 arg
= 'const %(type)s %(name)s'
112 'type': type_generator
.GetType(param
, wrap_optional
=True),
113 'name': param
.unix_name
,