cpp_util.py revision 58537e28ecd584eab876aee8be7156509866d23a
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.
4
5"""Utilies and constants specific to Chromium C++ code.
6"""
7
8from code import Code
9from datetime import datetime
10from model import PropertyType
11import os
12import re
13
14CHROMIUM_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
18)
19GENERATED_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITION IN
20//   %s
21// DO NOT EDIT.
22"""
23GENERATED_BUNDLE_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITIONS IN
24//   %s
25// DO NOT EDIT.
26"""
27
28def Classname(s):
29  """Translates a namespace name or function name into something more
30  suited to C++.
31
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)])
36
37
38def GetAsFundamentalValue(type_, src, dst):
39  """Returns the C++ code for retrieving a fundamental type from a
40  Value into a variable.
41
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)
51
52
53def 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]
68
69
70def 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,
86  }
87
88
89def GenerateIfndefName(path, filename):
90  """Formats a path and filename as a #define name.
91
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('/', '_'))
96
97
98def PadForGenerics(var):
99  """Appends a space to |var| if it ends with a >, so that it can be compiled
100  within generic types.
101  """
102  return ('%s ' % var) if var.endswith('>') else var
103
104
105def OpenNamespace(namespace):
106  """Get opening root namespace declarations.
107  """
108  c = Code()
109  for component in namespace.split('::'):
110    c.Append('namespace %s {' % component)
111  return c
112
113
114def CloseNamespace(namespace):
115  """Get closing root namespace declarations.
116  """
117  c = Code()
118  for component in reversed(namespace.split('::')):
119    c.Append('}  // namespace %s' % component)
120  return c
121