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

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

Issue 9617010: Move chrome.downloads out of experimental to dev (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 6 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 | Annotate | Revision Log
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 code import Code 5 from code import Code
6 from model import PropertyType 6 from model import PropertyType
7 import cpp_util 7 import cpp_util
8 import model
9 import os
10 import schema_util 8 import schema_util
11 9
12 class HGenerator(object): 10 class HGenerator(object):
13 """A .h generator for a namespace. 11 """A .h generator for a namespace.
14 """ 12 """
15 def __init__(self, namespace, cpp_type_generator): 13 def __init__(self, namespace, cpp_type_generator):
16 self._cpp_type_generator = cpp_type_generator 14 self._cpp_type_generator = cpp_type_generator
17 self._namespace = namespace 15 self._namespace = namespace
18 self._target_namespace = ( 16 self._target_namespace = (
19 self._cpp_type_generator.GetCppNamespaceName(self._namespace)) 17 self._cpp_type_generator.GetCppNamespaceName(self._namespace))
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 """Generates the list of types in the current namespace in an order in which 102 """Generates the list of types in the current namespace in an order in which
105 depended-upon types appear before types which depend on them. 103 depended-upon types appear before types which depend on them.
106 """ 104 """
107 dependency_order = [] 105 dependency_order = []
108 106
109 def ExpandType(path, type_): 107 def ExpandType(path, type_):
110 if type_ in path: 108 if type_ in path:
111 raise ValueError("Illegal circular dependency via cycle " + 109 raise ValueError("Illegal circular dependency via cycle " +
112 ", ".join(map(lambda x: x.name, path + [type_]))) 110 ", ".join(map(lambda x: x.name, path + [type_])))
113 for prop in type_.properties.values(): 111 for prop in type_.properties.values():
114 if not prop.optional and prop.type_ == PropertyType.REF: 112 if (prop.type_ == PropertyType.REF and
113 schema_util.GetNamespace(prop.ref_type) == self._namespace.name):
115 ExpandType(path + [type_], self._namespace.types[prop.ref_type]) 114 ExpandType(path + [type_], self._namespace.types[prop.ref_type])
116 if not type_ in dependency_order: 115 if not type_ in dependency_order:
117 dependency_order.append(type_) 116 dependency_order.append(type_)
118 117
119 for type_ in self._namespace.types.values(): 118 for type_ in self._namespace.types.values():
120 ExpandType([], type_) 119 ExpandType([], type_)
121 return dependency_order 120 return dependency_order
122 121
123 def _GenerateEnumDeclaration(self, enum_name, prop, values): 122 def _GenerateEnumDeclaration(self, enum_name, prop, values):
124 """Generate the declaration of a C++ enum for the given property and 123 """Generate the declaration of a C++ enum for the given property and
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 if type_.description: 170 if type_.description:
172 c.Comment(type_.description) 171 c.Comment(type_.description)
173 c.Append('typedef std::vector<%(item_type)s> %(classname)s;') 172 c.Append('typedef std::vector<%(item_type)s> %(classname)s;')
174 c.Substitute({'classname': classname, 'item_type': 173 c.Substitute({'classname': classname, 'item_type':
175 self._cpp_type_generator.GetType(type_.item_type, 174 self._cpp_type_generator.GetType(type_.item_type,
176 wrap_optional=True)}) 175 wrap_optional=True)})
177 elif type_.type_ == PropertyType.STRING: 176 elif type_.type_ == PropertyType.STRING:
178 if type_.description: 177 if type_.description:
179 c.Comment(type_.description) 178 c.Comment(type_.description)
180 c.Append('typedef std::string %(classname)s;') 179 c.Append('typedef std::string %(classname)s;')
181 c.Substitute({'classname': classname})
182 else: 180 else:
183 if type_.description: 181 if type_.description:
184 c.Comment(type_.description) 182 c.Comment(type_.description)
185 (c.Sblock('struct %(classname)s {') 183 (c.Sblock('struct %(classname)s {')
186 .Append('~%(classname)s();') 184 .Append('~%(classname)s();')
187 .Append('%(classname)s();') 185 .Append('%(classname)s();')
188 .Append() 186 .Append()
189 .Concat(self._GeneratePropertyStructures(type_.properties.values())) 187 .Concat(self._GeneratePropertyStructures(type_.properties.values()))
190 .Concat(self._GenerateFields(type_.properties.values())) 188 .Concat(self._GenerateFields(type_.properties.values()))
191 ) 189 )
192 if type_.from_json: 190 if type_.from_json:
193 (c.Comment('Populates a %s object from a Value. Returns' 191 (c.Comment('Populates a %s object from a base::Value. Returns'
194 ' whether |out| was successfully populated.' % classname) 192 ' whether |out| was successfully populated.' % classname)
195 .Append( 193 .Append('static bool Populate(' +
196 'static bool Populate(const Value& value, %(classname)s* out);') 194 'const base::Value& value, %(classname)s* out);')
197 .Append() 195 .Append()
198 ) 196 )
199 197
200 if type_.from_client: 198 if type_.from_client:
201 (c.Comment('Returns a new DictionaryValue representing the' 199 (c.Comment('Returns a new base::DictionaryValue representing the'
202 ' serialized form of this %s object. Passes ' 200 ' serialized form of this %s object. Passes '
203 'ownership to caller.' % classname) 201 'ownership to caller.' % classname)
204 .Append('scoped_ptr<DictionaryValue> ToValue() const;') 202 .Append('scoped_ptr<base::DictionaryValue> ToValue() const;')
not at google - send to devlin 2012/06/12 18:34:38 thanks :)
benjhayden 2012/06/12 20:15:42 Done.
205 ) 203 )
206 204
207 (c.Eblock() 205 (c.Eblock()
208 .Sblock(' private:') 206 .Sblock(' private:')
209 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);') 207 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);')
210 .Eblock('};') 208 .Eblock('};')
211 ) 209 )
212 c.Substitute({'classname': classname}) 210 c.Substitute({'classname': classname})
213 return c 211 return c
214 212
(...skipping 17 matching lines...) Expand all
232 """Generates the struct for passing parameters into a function. 230 """Generates the struct for passing parameters into a function.
233 """ 231 """
234 c = Code() 232 c = Code()
235 233
236 if function.params: 234 if function.params:
237 (c.Sblock('struct Params {') 235 (c.Sblock('struct Params {')
238 .Concat(self._GeneratePropertyStructures(function.params)) 236 .Concat(self._GeneratePropertyStructures(function.params))
239 .Concat(self._GenerateFields(function.params)) 237 .Concat(self._GenerateFields(function.params))
240 .Append('~Params();') 238 .Append('~Params();')
241 .Append() 239 .Append()
242 .Append('static scoped_ptr<Params> Create(const ListValue& args);') 240 .Append('static scoped_ptr<Params> Create(' +
241 'const base::ListValue& args);')
243 .Eblock() 242 .Eblock()
244 .Sblock(' private:') 243 .Sblock(' private:')
245 .Append('Params();') 244 .Append('Params();')
246 .Append() 245 .Append()
247 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') 246 .Append('DISALLOW_COPY_AND_ASSIGN(Params);')
248 .Eblock('};') 247 .Eblock('};')
249 ) 248 )
250 249
251 return c 250 return c
252 251
(...skipping 11 matching lines...) Expand all
264 self._cpp_type_generator.GetChoicesEnumType(prop), 263 self._cpp_type_generator.GetChoicesEnumType(prop),
265 prop, 264 prop,
266 [choice.type_.name for choice in prop.choices.values()])) 265 [choice.type_.name for choice in prop.choices.values()]))
267 c.Concat(self._GeneratePropertyStructures(prop.choices.values())) 266 c.Concat(self._GeneratePropertyStructures(prop.choices.values()))
268 elif prop.type_ == PropertyType.ENUM: 267 elif prop.type_ == PropertyType.ENUM:
269 enum_name = self._cpp_type_generator.GetType(prop) 268 enum_name = self._cpp_type_generator.GetType(prop)
270 c.Concat(self._GenerateEnumDeclaration( 269 c.Concat(self._GenerateEnumDeclaration(
271 enum_name, 270 enum_name,
272 prop, 271 prop,
273 prop.enum_values)) 272 prop.enum_values))
274 c.Append('static scoped_ptr<Value> CreateEnumValue(%s %s);' % 273 c.Append('static scoped_ptr<base::Value> CreateEnumValue(%s %s);' %
275 (enum_name, prop.unix_name)) 274 (enum_name, prop.unix_name))
276 return c 275 return c
277 276
278 def _GenerateFunctionResult(self, function): 277 def _GenerateFunctionResult(self, function):
279 """Generates functions for passing a function's result back. 278 """Generates functions for passing a function's result back.
280 """ 279 """
281 c = Code() 280 c = Code()
282 281
283 c.Sblock('namespace Result {') 282 c.Sblock('namespace Result {')
284 params = function.callback.params 283 params = function.callback.params
285 if not params: 284 if not params:
286 c.Append('Value* Create();') 285 c.Append('base::Value* Create();')
287 else: 286 else:
288 c.Concat(self._GeneratePropertyStructures(params)) 287 c.Concat(self._GeneratePropertyStructures(params))
289 288
290 # If there is a single parameter, this is straightforward. However, if 289 # If there is a single parameter, this is straightforward. However, if
291 # the callback parameter is of 'choices', this generates a Create method 290 # the callback parameter is of 'choices', this generates a Create method
292 # for each choice. This works because only 1 choice can be returned at a 291 # for each choice. This works because only 1 choice can be returned at a
293 # time. 292 # time.
294 for param in self._cpp_type_generator.GetExpandedChoicesInParams(params): 293 for param in self._cpp_type_generator.GetExpandedChoicesInParams(params):
295 if param.description: 294 if param.description:
296 c.Comment(param.description) 295 c.Comment(param.description)
297 if param.type_ == PropertyType.ANY: 296 if param.type_ == PropertyType.ANY:
298 c.Comment("Value* Result::Create(Value*) not generated " 297 c.Comment("base::Value* Result::Create(base::Value*) not generated "
299 "because it's redundant.") 298 "because it's redundant.")
300 continue 299 continue
301 c.Append('Value* Create(const %s);' % cpp_util.GetParameterDeclaration( 300 c.Append('base::Value* Create(const %s);' %
302 param, self._cpp_type_generator.GetType(param))) 301 cpp_util.GetParameterDeclaration(
302 param, self._cpp_type_generator.GetType(param)))
303 c.Eblock('};') 303 c.Eblock('};')
304 304
305 return c 305 return c
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698