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.
5 """Utilies and constants specific to Chromium C++ code.
9 from datetime
import datetime
10 from model
import PropertyType
15 """// Copyright (c) %d The Chromium Authors. All rights reserved.
16 // Use of this source code is governed by a BSD-style license that can be
17 // found in the LICENSE file.""" % datetime
.now().year
19 GENERATED_FILE_MESSAGE
= """// GENERATED FROM THE API DEFINITION IN
23 GENERATED_BUNDLE_FILE_MESSAGE
= """// GENERATED FROM THE API DEFINITIONS IN
27 GENERATED_FEATURE_MESSAGE
= """// GENERATED FROM THE FEATURE DEFINITIONS IN
33 """Translates a namespace name or function name into something more
36 eg experimental.downloads -> Experimental_Downloads
37 updateAll -> UpdateAll.
41 return '_'.join([x
[0].upper() + x
[1:] for x
in re
.split('\W', s
)])
44 def GetAsFundamentalValue(type_
, src
, dst
):
45 """Returns the C++ code for retrieving a fundamental type from a
46 Value into a variable.
52 PropertyType
.BOOLEAN
: '%s->GetAsBoolean(%s)',
53 PropertyType
.DOUBLE
: '%s->GetAsDouble(%s)',
54 PropertyType
.INTEGER
: '%s->GetAsInteger(%s)',
55 PropertyType
.STRING
: '%s->GetAsString(%s)',
56 }[type_
.property_type
] % (src
, dst
)
59 def GetValueType(type_
):
60 """Returns the Value::Type corresponding to the model.Type.
63 PropertyType
.ARRAY
: 'base::Value::TYPE_LIST',
64 PropertyType
.BINARY
: 'base::Value::TYPE_BINARY',
65 PropertyType
.BOOLEAN
: 'base::Value::TYPE_BOOLEAN',
66 # PropertyType.CHOICES can be any combination of types.
67 PropertyType
.DOUBLE
: 'base::Value::TYPE_DOUBLE',
68 PropertyType
.ENUM
: 'base::Value::TYPE_STRING',
69 PropertyType
.FUNCTION
: 'base::Value::TYPE_DICTIONARY',
70 PropertyType
.INTEGER
: 'base::Value::TYPE_INTEGER',
71 PropertyType
.OBJECT
: 'base::Value::TYPE_DICTIONARY',
72 PropertyType
.STRING
: 'base::Value::TYPE_STRING',
73 }[type_
.property_type
]
76 def GetParameterDeclaration(param
, type_
):
77 """Gets a parameter declaration of a given model.Property and its C++
80 if param
.type_
.property_type
in (PropertyType
.ANY
,
87 arg
= 'const %(type)s& %(name)s'
89 arg
= '%(type)s %(name)s'
92 'name': param
.unix_name
,
96 def GenerateIfndefName(file_path
):
97 """Formats |file_path| as a #define name. Presumably |file_path| is a header
98 file, or there's little point in generating a #define for it.
100 e.g chrome/extensions/gen/file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__.
102 return (('%s__' % file_path
).upper()
108 def PadForGenerics(var
):
109 """Appends a space to |var| if it ends with a >, so that it can be compiled
110 within generic types.
112 return ('%s ' % var
) if var
.endswith('>') else var
116 def OpenNamespace(cpp_namespace
):
117 """Get opening root namespace declarations.
120 for component
in cpp_namespace
.split('::'):
121 c
.Append('namespace %s {' % component
)
125 def CloseNamespace(cpp_namespace
):
126 """Get closing root namespace declarations.
129 for component
in reversed(cpp_namespace
.split('::')):
130 c
.Append('} // namespace %s' % component
)
134 def ConstantName(feature_name
):
135 """Returns a kName for a feature's name.
137 return ('k' + ''.join(word
[0].upper() + word
[1:]
138 for word
in feature_name
.replace('.', ' ').split()))
141 def CamelCase(unix_name
):
142 return ''.join(word
.capitalize() for word
in unix_name
.split('_'))
145 def ClassName(filepath
):
146 return CamelCase(os
.path
.split(filepath
)[1])
149 def GetCppNamespace(pattern
, namespace
):
150 '''Returns the C++ namespace given |pattern| which includes a %(namespace)s
151 substitution, and the |namespace| to substitute. It is expected that |pattern|
152 has been passed as a flag to compiler.py from GYP/GN.
154 # For some reason Windows builds escape the % characters, so unescape them.
155 # This means that %% can never appear legitimately within a pattern, but
156 # that's ok. It should never happen.
157 cpp_namespace
= pattern
.replace('%%', '%') % { 'namespace': namespace
}
158 assert '%' not in cpp_namespace
, \
159 ('Did not manage to fully substitute namespace "%s" into pattern "%s"'
160 % (namespace
, pattern
))