Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1629)

Side by Side Diff: tools/json_schema_compiler/h_generator.py

Issue 9309044: Supporting more APIs with json_schema_compiler (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: rework, updated base Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 from model import PropertyType 5 from model import PropertyType
6 import code 6 import code
7 import cpp_util 7 import cpp_util
8 import os 8 import os
9 9
10 class HGenerator(object): 10 class HGenerator(object):
(...skipping 17 matching lines...) Expand all
28 28
29 ifndef_name = self._GenerateIfndefName() 29 ifndef_name = self._GenerateIfndefName()
30 (c.Append('#ifndef %s' % ifndef_name) 30 (c.Append('#ifndef %s' % ifndef_name)
31 .Append('#define %s' % ifndef_name) 31 .Append('#define %s' % ifndef_name)
32 .Append('#pragma once') 32 .Append('#pragma once')
33 .Append() 33 .Append()
34 .Append('#include <string>') 34 .Append('#include <string>')
35 .Append('#include <vector>') 35 .Append('#include <vector>')
36 .Append() 36 .Append()
37 .Append('#include "base/basictypes.h"') 37 .Append('#include "base/basictypes.h"')
38 .Append('#include "base/memory/linked_ptr.h"')
38 .Append('#include "base/memory/scoped_ptr.h"') 39 .Append('#include "base/memory/scoped_ptr.h"')
39 .Append('#include "base/values.h"') 40 .Append('#include "base/values.h"')
40 .Append() 41 .Append()
41 .Append('using base::Value;')
42 .Append('using base::DictionaryValue;')
43 .Append('using base::ListValue;')
44 .Append()
45 ) 42 )
46 43
47 includes = self._cpp_type_generator.GenerateCppIncludes() 44 c.Concat(self._cpp_type_generator.GetRootNamespaceStart())
48 if not includes.IsEmpty(): 45 forward_declarations = (
49 (c.Concat(includes) 46 self._cpp_type_generator.GenerateForwardDeclarations())
47 if not forward_declarations.IsEmpty():
48 (c.Append()
49 .Concat(forward_declarations)
50 .Append() 50 .Append()
51 ) 51 )
52 52
53 (c.Concat(self._cpp_type_generator.GetCppNamespaceStart()) 53 (c.Concat(self._cpp_type_generator.GetNamespaceStart())
54 .Append() 54 .Append()
55 .Append('//') 55 .Append('//')
56 .Append('// Types') 56 .Append('// Types')
57 .Append('//') 57 .Append('//')
58 .Append() 58 .Append()
59 ) 59 )
60 for type_ in self._namespace.types.values(): 60 for type_ in self._namespace.types.values():
61 (c.Concat(self._GenerateType(type_)) 61 (c.Concat(self._GenerateType(type_))
62 .Append() 62 .Append()
63 ) 63 )
64 (c.Append('//') 64 (c.Append('//')
65 .Append('// Functions') 65 .Append('// Functions')
66 .Append('//') 66 .Append('//')
67 .Append() 67 .Append()
68 ) 68 )
69 for function in self._namespace.functions.values(): 69 for function in self._namespace.functions.values():
70 (c.Concat(self._GenerateFunction(function)) 70 (c.Concat(self._GenerateFunction(function))
71 .Append() 71 .Append()
72 ) 72 )
73 (c.Append() 73 (c.Append()
74 .Append() 74 .Concat(self._cpp_type_generator.GetNamespaceEnd())
75 .Concat(self._cpp_type_generator.GetCppNamespaceEnd()) 75 .Concat(self._cpp_type_generator.GetRootNamespaceEnd())
76 .Append() 76 .Append()
77 .Append('#endif // %s' % ifndef_name) 77 .Append('#endif // %s' % ifndef_name)
78 .Append() 78 .Append()
79 ) 79 )
80 return c 80 return c
81 81
82 def _GenerateFields(self, props):
83 """Generates the field declarations when declaring a type.
84 """
85 c = code.Code()
86 for prop in self._cpp_type_generator.GetExpandedChoicesInParams(props):
87 if prop.description:
88 c.Comment(prop.description)
89 c.Append('%s %s;' % (
90 self._cpp_type_generator.GetType(prop, wrap_optional=True),
91 prop.unix_name))
92 c.Append()
93 # Generate the enums needed for any fields with "choices"
94 for prop in [x for x in props if x.type_ == PropertyType.CHOICES]:
Yoyo Zhou 2012/02/10 01:49:33 Why not just for prop in props: if prop.type__ =
calamity 2012/02/10 03:52:50 Hah, that's what is was before. I thought it'd be
Yoyo Zhou 2012/02/10 19:43:21 It is more python-y if it becomes less code, but i
95 enum_name = self._cpp_type_generator.GetChoicesEnumType(prop)
96 c.Sblock('enum %s {' % enum_name)
97 c.Append(self._cpp_type_generator.GetChoiceEnumNoneValue(prop) + ',')
98 for choice in prop.choices.values():
99 c.Append(self._cpp_type_generator.GetChoiceEnumValue(prop, choice.type_)
100 + ',')
101 (c.Eblock('};')
102 .Append()
103 .Append('%s %s_type;' % (enum_name, prop.unix_name))
104 )
105 return c
106
82 def _GenerateType(self, type_, serializable=True): 107 def _GenerateType(self, type_, serializable=True):
83 """Generates a struct for a type. 108 """Generates a struct for a type.
84 """ 109 """
85 classname = cpp_util.CppName(type_.name) 110 classname = cpp_util.Classname(type_.name)
86 c = code.Code() 111 c = code.Code()
87 if type_.description: 112 if type_.description:
88 c.Comment(type_.description) 113 c.Comment(type_.description)
89 (c.Sblock('struct %(classname)s {') 114 (c.Sblock('struct %(classname)s {')
90 .Append('~%(classname)s();') 115 .Append('~%(classname)s();')
91 .Append('%(classname)s();') 116 .Append('%(classname)s();')
92 .Append() 117 .Append()
93 ) 118 .Concat(self._GenerateFields(type_.properties.values()))
94 119 .Comment('Populates a %(classname)s object from a Value. Returns'
95 for prop in type_.properties.values(): 120 ' whether |out| was successfully populated.')
96 if prop.description: 121 .Append('static bool Populate(const Value& value, %(classname)s* out);')
97 c.Comment(prop.description) 122 .Append()
98 if prop.optional:
99 c.Append('scoped_ptr<%s> %s;' %
100 (self._cpp_type_generator.GetType(prop, pad_for_generics=True),
101 prop.name))
102 else:
103 c.Append('%s %s;' %
104 (self._cpp_type_generator.GetType(prop), prop.name))
105 c.Append()
106
107 (c.Comment('Populates a %(classname)s object from a Value. Returns'
108 ' whether |out| was successfully populated.')
109 .Append('static bool Populate(const Value& value, %(classname)s* out);')
110 .Append()
111 ) 123 )
112 124
113 if serializable: 125 if serializable:
114 (c.Comment('Returns a new DictionaryValue representing the' 126 (c.Comment('Returns a new DictionaryValue representing the'
115 ' serialized form of this %(classname)s object. Passes' 127 ' serialized form of this %(classname)s object. Passes '
116 'ownership to caller.') 128 'ownership to caller.')
117 .Append('DictionaryValue* ToValue() const;') 129 .Append('scoped_ptr<DictionaryValue> ToValue() const;')
118 ) 130 )
119 (c.Eblock() 131 (c.Eblock()
120 .Sblock(' private:') 132 .Sblock(' private:')
121 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);') 133 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);')
122 .Eblock('};') 134 .Eblock('};')
123 ) 135 )
124 c.Substitute({'classname': classname}) 136 c.Substitute({'classname': classname})
125 return c 137 return c
126 138
127 def _GenerateFunction(self, function): 139 def _GenerateFunction(self, function):
128 """Generates the structs for a function. 140 """Generates the structs for a function.
129 """ 141 """
130 c = code.Code() 142 c = code.Code()
131 (c.Sblock('namespace %s {' % cpp_util.CppName(function.name)) 143 (c.Sblock('namespace %s {' % cpp_util.Classname(function.name))
132 .Concat(self._GenerateFunctionParams(function)) 144 .Concat(self._GenerateFunctionParams(function))
133 .Append() 145 .Append()
134 .Concat(self._GenerateFunctionResult(function)) 146 .Concat(self._GenerateFunctionResult(function))
135 .Append() 147 .Append()
136 .Eblock('};') 148 .Eblock('};')
137 ) 149 )
138 return c 150 return c
139 151
140 def _GenerateFunctionParams(self, function): 152 def _GenerateFunctionParams(self, function):
141 """Generates the struct for passing parameters into a function. 153 """Generates the struct for passing parameters into a function.
142 """ 154 """
143 c = code.Code() 155 c = code.Code()
144 156
145 if function.params: 157 if function.params:
146 c.Sblock('struct Params {') 158 c.Sblock('struct Params {')
147 for param in function.params: 159 for param in function.params:
148 if param.description:
149 c.Comment(param.description)
150 if param.type_ == PropertyType.OBJECT: 160 if param.type_ == PropertyType.OBJECT:
151 c.Concat(self._GenerateType(param, serializable=False)) 161 c.Concat(self._GenerateType(param, serializable=False))
152 c.Append() 162 c.Append()
153 for param in function.params: 163 (c.Concat(self._GenerateFields(function.params))
154 c.Append('%s %s;' %
155 (self._cpp_type_generator.GetType(param), param.name))
156
157 (c.Append()
158 .Append('~Params();') 164 .Append('~Params();')
159 .Append() 165 .Append()
160 .Append('static scoped_ptr<Params> Create(const ListValue& args);') 166 .Append('static scoped_ptr<Params> Create(const ListValue& args);')
161 .Eblock() 167 .Eblock()
162 .Sblock(' private:') 168 .Sblock(' private:')
163 .Append('Params();') 169 .Append('Params();')
164 .Append() 170 .Append()
165 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') 171 .Append('DISALLOW_COPY_AND_ASSIGN(Params);')
172 .Eblock('};')
166 ) 173 )
167 174
168 c.Eblock('};')
169
170 return c 175 return c
171 176
172 def _GenerateFunctionResult(self, function): 177 def _GenerateFunctionResult(self, function):
173 """Generates the struct for passing a function's result back. 178 """Generates functions for passing a function's result back.
174 """ 179 """
175 # TODO(calamity): Handle returning an object
176 c = code.Code() 180 c = code.Code()
177 181
178 param = function.callback.param 182 c.Sblock('namespace Result {')
179 # TODO(calamity): Put this description comment in less stupid place 183 params = function.callback.params
180 if param.description: 184 if not params:
181 c.Comment(param.description) 185 c.Append('Value* Create();')
182 (c.Append('class Result {') 186 else:
183 .Sblock(' public:') 187 # If there is a single parameter, this is straightforward. However, if
184 ) 188 # the callback parameter is of 'choices', this generates a Create method
185 arg = '' 189 # for each choice. This works because only 1 choice can be returned at a
186 # TODO(calamity): handle object 190 # time.
187 if param: 191 for param in self._cpp_type_generator.GetExpandedChoicesInParams(params):
188 if param.type_ == PropertyType.REF:
189 arg = 'const %(type)s& %(name)s'
190 else:
191 arg = 'const %(type)s %(name)s' 192 arg = 'const %(type)s %(name)s'
192 arg = arg % { 193 if param.type_ == PropertyType.REF:
193 'type': self._cpp_type_generator.GetType(param), 194 arg = 'const %(type)s& %(name)s'
194 'name': param.name 195 arg %= {
195 } 196 'type': self._cpp_type_generator.GetType(param, wrap_optional=True),
196 (c.Append('static Value* Create(%s);' % arg) 197 'name': param.unix_name,
197 .Eblock() 198 }
198 .Sblock(' private:') 199 if param.description:
199 .Append('Result() {};') 200 c.Comment(param.description)
200 .Append('DISALLOW_COPY_AND_ASSIGN(Result);') 201 c.Append('Value* Create(%s);' % arg)
201 .Eblock('};') 202 c.Eblock('};')
202 )
203 203
204 return c 204 return c
205 205
206 def _GenerateIfndefName(self): 206 def _GenerateIfndefName(self):
207 """Formats a path and filename as a #define name. 207 """Formats a path and filename as a #define name.
208 208
209 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__. 209 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__.
210 """ 210 """
211 return (('%s_%s_H__' % 211 return (('%s_%s_H__' %
212 (self._namespace.source_file_dir, self._target_namespace)) 212 (self._namespace.source_file_dir, self._target_namespace))
213 .upper().replace(os.sep, '_')) 213 .upper().replace(os.sep, '_'))
214 214
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698