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 Property
, PropertyType
, Type
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
)])
37 def GetAsFundamentalValue(type_
, src
, dst
):
38 """Returns the C++ code for retrieving a fundamental type from a
39 Value into a variable.
45 PropertyType
.STRING
: '%s->GetAsString(%s)',
46 PropertyType
.BOOLEAN
: '%s->GetAsBoolean(%s)',
47 PropertyType
.INTEGER
: '%s->GetAsInteger(%s)',
48 PropertyType
.DOUBLE
: '%s->GetAsDouble(%s)',
49 }[type_
.property_type
] % (src
, dst
)
51 def GetValueType(type_
):
52 """Returns the Value::Type corresponding to the model.PropertyType.
55 PropertyType
.STRING
: 'Value::TYPE_STRING',
56 PropertyType
.INTEGER
: 'Value::TYPE_INTEGER',
57 PropertyType
.BOOLEAN
: 'Value::TYPE_BOOLEAN',
58 PropertyType
.DOUBLE
: 'Value::TYPE_DOUBLE',
59 PropertyType
.ENUM
: 'Value::TYPE_STRING',
60 PropertyType
.OBJECT
: 'Value::TYPE_DICTIONARY',
61 PropertyType
.FUNCTION
: 'Value::TYPE_DICTIONARY',
62 PropertyType
.ARRAY
: 'Value::TYPE_LIST',
63 PropertyType
.BINARY
: 'Value::TYPE_BINARY',
64 }[type_
.property_type
]
66 def GetParameterDeclaration(param
, type_
):
67 """Gets a parameter declaration of a given model.Property and its C++
70 if param
.type_
.property_type
in (PropertyType
.REF
,
75 arg
= 'const %(type)s& %(name)s'
77 arg
= '%(type)s %(name)s'
80 'name': param
.unix_name
,
83 def GenerateIfndefName(path
, filename
):
84 """Formats a path and filename as a #define name.
86 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__.
88 return (('%s_%s_H__' % (path
, filename
))
89 .upper().replace(os
.sep
, '_').replace('/', '_'))
91 def PadForGenerics(var
):
92 """Appends a space to |var| if it ends with a >, so that it can be compiled
95 return ('%s ' % var
) if var
.endswith('>') else var
97 def OpenNamespace(namespace
):
98 """Get opening root namespace declarations.
101 for component
in namespace
.split('::'):
102 c
.Append('namespace %s {' % component
)
105 def CloseNamespace(namespace
):
106 """Get closing root namespace declarations.
109 for component
in reversed(namespace
.split('::')):
110 c
.Append('} // namespace %s' % component
)