Fix a type mismatch on Windows caused by r201738.
[chromium-blink-merge.git] / tools / json_schema_compiler / cpp_util.py
blobc7bfc45ed2007baa2d3969a2e987aeb24e0b857c
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.
6 """
8 from code import Code
9 from datetime import datetime
10 from model import Property, PropertyType, Type
11 import os
12 import re
14 CHROMIUM_LICENSE = (
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
20 // %s
21 // DO NOT EDIT.
22 """
23 GENERATED_BUNDLE_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITIONS IN
24 // %s
25 // DO NOT EDIT.
26 """
28 def Classname(s):
29 """Translates a namespace name or function name into something more
30 suited to C++.
32 eg experimental.downloads -> Experimental_Downloads
33 updateAll -> UpdateAll.
34 """
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.
41 src: Value*
42 dst: Property*
43 """
44 return {
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.
53 """
54 return {
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++
68 type.
69 """
70 if param.type_.property_type in (PropertyType.REF,
71 PropertyType.OBJECT,
72 PropertyType.ARRAY,
73 PropertyType.STRING,
74 PropertyType.ANY):
75 arg = 'const %(type)s& %(name)s'
76 else:
77 arg = '%(type)s %(name)s'
78 return arg % {
79 'type': type_,
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__.
87 """
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
93 within generic types.
94 """
95 return ('%s ' % var) if var.endswith('>') else var
97 def OpenNamespace(namespace):
98 """Get opening root namespace declarations.
99 """
100 c = Code()
101 for component in namespace.split('::'):
102 c.Append('namespace %s {' % component)
103 return c
105 def CloseNamespace(namespace):
106 """Get closing root namespace declarations.
108 c = Code()
109 for component in reversed(namespace.split('::')):
110 c.Append('} // namespace %s' % component)
111 return c