Adds support for the "choices" and "any" types to json_schema_compiler, as well
[chromium-blink-merge.git] / tools / json_schema_compiler / cpp_util.py
blobab8e1596ea8f862909d0f16e6dc12dda214823e3
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 """Utilies and constants specific to Chromium C++ code.
5 """
7 from datetime import datetime
8 from model import PropertyType
10 CHROMIUM_LICENSE = (
11 """// Copyright (c) %d The Chromium Authors. All rights reserved.
12 // Use of this source code is governed by a BSD-style license that can be
13 // found in the LICENSE file.""" % datetime.now().year
15 GENERATED_FILE_MESSAGE = """// GENERATED FROM THE API DEFINITION IN
16 // %s
17 // DO NOT EDIT.
18 """
20 def Classname(s):
21 """Translates a namespace name or function name into something more
22 suited to C++.
24 eg experimental.downloads -> Experimental_Downloads
25 updateAll -> UpdateAll.
26 """
27 return '_'.join([x[0].upper() + x[1:] for x in s.split('.')])
29 def CreateFundamentalValue(prop, var):
30 """Returns the C++ code for creating a value of the given property type
31 using the given variable.
33 var: Fundamental or Fundamental*
34 """
35 if prop.optional:
36 var = '*' + var
37 return {
38 PropertyType.STRING: 'Value::CreateStringValue(%s)',
39 PropertyType.BOOLEAN: 'Value::CreateBooleanValue(%s)',
40 PropertyType.INTEGER: 'Value::CreateIntegerValue(%s)',
41 PropertyType.DOUBLE: 'Value::CreateDoubleValue(%s)',
42 }[prop.type_] % var
44 def GetAsFundamentalValue(prop, src, dst):
45 """Returns the C++ code for retrieving a fundamental type from a
46 Value into a variable.
48 src: Value*
49 dst: Property*
50 """
51 return {
52 PropertyType.STRING: '%s->GetAsString(%s)',
53 PropertyType.BOOLEAN: '%s->GetAsBoolean(%s)',
54 PropertyType.INTEGER: '%s->GetAsInteger(%s)',
55 PropertyType.DOUBLE: '%s->GetAsDouble(%s)',
56 }[prop.type_] % (src, dst)
58 def GetFundamentalValue(prop, src, name, dst):
59 """Returns the C++ code for retrieving a fundamental type from a
60 DictionaryValue into a variable.
62 src: DictionaryValue*
63 name: key
64 dst: Property*
65 """
66 return {
67 PropertyType.STRING: '%s->GetString("%s", %s)',
68 PropertyType.BOOLEAN: '%s->GetBoolean("%s", %s)',
69 PropertyType.INTEGER: '%s->GetInteger("%s", %s)',
70 PropertyType.DOUBLE: '%s->GetDouble("%s", %s)',
71 }[prop.type_] % (src, name, dst)
73 def CreateValueFromSingleProperty(prop, var):
74 """Creates a Value given a single property. Use for everything except
75 PropertyType.ARRAY.
77 var: raw value
78 """
79 if prop.type_ == PropertyType.REF or prop.type_ == PropertyType.OBJECT:
80 if prop.optional:
81 return '%s->ToValue().release()' % var
82 else:
83 return '%s.ToValue().release()' % var
84 elif prop.type_.is_fundamental:
85 return CreateFundamentalValue(prop, var)
86 else:
87 raise NotImplementedError('Conversion of single %s to Value not implemented'
88 % repr(prop.type_))
90 def GetValueFromList(prop, src, index, dst):
91 """Returns the C++ code for retrieving a fundamental type from a
92 DictionaryValue into a variable.
94 src: ListValue&
95 index: int
96 dst: Property*
97 """
98 return {
99 PropertyType.REF: '%s.GetDictionary(%d, %s)',
100 PropertyType.STRING: '%s.GetString(%d, %s)',
101 PropertyType.BOOLEAN: '%s.GetBoolean(%d, %s)',
102 PropertyType.INTEGER: '%s.GetInteger(%d, %s)',
103 PropertyType.DOUBLE: '%s.GetDouble(%d, %s)',
104 }[prop.type_] % (src, index, dst)
106 def GetConstParameterDeclaration(param, type_generator):
107 if param.type_ == PropertyType.REF:
108 arg = 'const %(type)s& %(name)s'
109 else:
110 arg = 'const %(type)s %(name)s'
111 return arg % {
112 'type': type_generator.GetType(param, wrap_optional=True),
113 'name': param.unix_name,