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
29 """Translates a namespace name or function name into something more
32 eg experimental.downloads -> Experimental_Downloads
33 updateAll -> UpdateAll.
35 return '_'.join([x
[0].upper() + x
[1:] for x
in re
.split('\W', s
)])
38 def GetAsFundamentalValue(type_
, src
, dst
):
39 """Returns the C++ code for retrieving a fundamental type from a
40 Value into a variable.
46 PropertyType
.BOOLEAN
: '%s->GetAsBoolean(%s)',
47 PropertyType
.DOUBLE
: '%s->GetAsDouble(%s)',
48 PropertyType
.INTEGER
: '%s->GetAsInteger(%s)',
49 PropertyType
.STRING
: '%s->GetAsString(%s)',
50 }[type_
.property_type
] % (src
, dst
)
53 def GetValueType(type_
):
54 """Returns the Value::Type corresponding to the model.Type.
57 PropertyType
.ARRAY
: 'base::Value::TYPE_LIST',
58 PropertyType
.BINARY
: 'base::Value::TYPE_BINARY',
59 PropertyType
.BOOLEAN
: 'base::Value::TYPE_BOOLEAN',
60 # PropertyType.CHOICES can be any combination of types.
61 PropertyType
.DOUBLE
: 'base::Value::TYPE_DOUBLE',
62 PropertyType
.ENUM
: 'base::Value::TYPE_STRING',
63 PropertyType
.FUNCTION
: 'base::Value::TYPE_DICTIONARY',
64 PropertyType
.INTEGER
: 'base::Value::TYPE_INTEGER',
65 PropertyType
.OBJECT
: 'base::Value::TYPE_DICTIONARY',
66 PropertyType
.STRING
: 'base::Value::TYPE_STRING',
67 }[type_
.property_type
]
70 def GetParameterDeclaration(param
, type_
):
71 """Gets a parameter declaration of a given model.Property and its C++
74 if param
.type_
.property_type
in (PropertyType
.ANY
,
80 arg
= 'const %(type)s& %(name)s'
82 arg
= '%(type)s %(name)s'
85 'name': param
.unix_name
,
89 def GenerateIfndefName(path
, filename
):
90 """Formats a path and filename as a #define name.
92 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__.
94 return (('%s_%s_H__' % (path
, filename
))
95 .upper().replace(os
.sep
, '_').replace('/', '_'))
98 def PadForGenerics(var
):
99 """Appends a space to |var| if it ends with a >, so that it can be compiled
100 within generic types.
102 return ('%s ' % var
) if var
.endswith('>') else var
105 def OpenNamespace(namespace
):
106 """Get opening root namespace declarations.
109 for component
in namespace
.split('::'):
110 c
.Append('namespace %s {' % component
)
114 def CloseNamespace(namespace
):
115 """Get closing root namespace declarations.
118 for component
in reversed(namespace
.split('::')):
119 c
.Append('} // namespace %s' % component
)