Move prefs::kLastPolicyStatisticsUpdate to the policy component.
[chromium-blink-merge.git] / tools / json_schema_compiler / cpp_util.py
blob2d4934393443e25ebcfb29986c9b2d49a265e18a
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 PropertyType
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)])
38 def GetAsFundamentalValue(type_, src, dst):
39 """Returns the C++ code for retrieving a fundamental type from a
40 Value into a variable.
42 src: Value*
43 dst: Property*
44 """
45 return {
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.
55 """
56 return {
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++
72 type.
73 """
74 if param.type_.property_type in (PropertyType.ANY,
75 PropertyType.ARRAY,
76 PropertyType.CHOICES,
77 PropertyType.OBJECT,
78 PropertyType.REF,
79 PropertyType.STRING):
80 arg = 'const %(type)s& %(name)s'
81 else:
82 arg = '%(type)s %(name)s'
83 return arg % {
84 'type': type_,
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__.
93 """
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.
108 c = Code()
109 for component in namespace.split('::'):
110 c.Append('namespace %s {' % component)
111 return c
114 def CloseNamespace(namespace):
115 """Get closing root namespace declarations.
117 c = Code()
118 for component in reversed(namespace.split('::')):
119 c.Append('} // namespace %s' % component)
120 return c