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

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

Issue 9114036: Code generation for extensions api (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: more rework Created 8 years, 11 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
(Empty)
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 from model import PropertyType
6 import code
7 import cpp_type_manager
8 import cpp_util
9
10 class HGenerator(object):
11 """A .h generator for a namespace.
12 """
13 def __init__(self, namespace, model):
14 self.__cpp_type_manager = cpp_type_manager.CppTypeManager(namespace, model)
15 self.__namespace = namespace
16
17 def generate(self):
18 """Generates a code.Code object with the .h for a single namespace.
19 """
20 include_path = self.__namespace.parent_dir
21 filename = self.__namespace.filename
22 c = code.Code()
23 (c.append(cpp_util.CHROMIUM_LICENSE).append()
24 .append(cpp_util.GENERATED_FILE_MESSAGE % self.__namespace.parent_path)
25 .append()
26 )
27
28 ifndef_name = generate_ifndef_name(include_path, filename)
29 (c.append('#ifndef %s' % ifndef_name)
30 .append('#define %s' % ifndef_name)
31 .append('#pragma once')
32 .append()
33 .append('#include <string>')
34 .append('#include <vector>')
35 .append()
36 .append('#include "base/basictypes.h"')
37 .append('#include "base/memory/scoped_ptr.h"')
38 .append('#include "base/values.h"')
39 .append()
40 .append('using base::Value;')
41 .append('using base::DictionaryValue;')
42 .append('using base::ListValue;')
43 .append()
44 )
45
46 includes = self.__cpp_type_manager.generate_cpp_includes()
47 if includes.render():
not at google - send to devlin 2012/01/17 05:42:32 seems weird to render() this here then not use the
calamity 2012/01/18 05:43:08 Done.
48 (c.concat(includes)
49 .append()
50 )
51
52 (c.append('namespace %s {' % self.__namespace.root_namespace)
53 .append('namespace %s {' % filename)
54 .append()
55 .append('//')
56 .append('// Types')
57 .append('//')
58 .append()
59 )
60 for tipe in self.__namespace.types.values():
61 (c.concat(self.generate_type(tipe))
62 .append()
63 )
64 (c.append('//')
65 .append('// Functions')
66 .append('//')
67 .append()
68 )
69 for function in self.__namespace.functions.values():
70 (c.concat(self.generate_function(function))
71 .append()
72 )
73 (c.append()
74 .append()
75 .append('} // namespace %s' % self.__namespace.root_namespace)
76 .append('} // namespace %s' % filename)
77 .append()
78 .append('#endif // %s' % ifndef_name)
79 )
80 return c
81
82 def generate_type(self, tipe, serializable=True):
83 """Generates a struct for a type.
84 """
85 classname = cpp_util.cpp_name(tipe.name)
86 c = code.Code()
87 if tipe.description:
88 c.comment(tipe.description)
89 (c.sblock('struct %(classname)s {')
90 .append('~%(classname)s();')
91 .append('%(classname)s();')
92 .append()
93 )
not at google - send to devlin 2012/01/17 05:42:32 blank line here perhaps?
calamity 2012/01/18 05:43:08 Done.
94 for prop in tipe.properties.values():
95 if prop.description:
96 c.comment(prop.description)
97 if prop.optional:
98 c.append('scoped_ptr<%s> %s;' %
99 (self.__cpp_type_manager.get_type(prop, pad_for_generics=True),
100 prop.name))
101 else:
102 c.append('%s %s;' % (self.__cpp_type_manager.get_type(prop), prop.name))
103 c.append()
104
105 (c.comment('Populates a %(classname)s object from a Value. Returns'
106 ' whether |out| was successfully populated.')
107 .append('static bool Populate(const Value& value, %(classname)s* out);')
108 .append()
109 )
110 if serializable:
111 (c.comment('Returns a new DictionaryValue representing the'
112 ' serialized form of this %(classname)s object.')
113 .append('DictionaryValue* ToValue() const;')
114 )
115 (c.eblock()
not at google - send to devlin 2012/01/17 05:42:32 blank line here perhaps?
calamity 2012/01/18 05:43:08 Done.
116 .sblock(' private:')
117 .append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);')
118 .eblock('};')
119 )
120 c.substitute({'classname': classname})
121 return c
122
123 def generate_function(self, function):
124 """Generates the structs for a function.
125 """
126 c = code.Code()
127 (c.sblock('struct %s {' % cpp_util.cpp_name(function.name))
128 .concat(self.generate_function_params(function))
129 .append()
130 .concat(self.generate_function_result(function))
131 .append()
132 .eblock('};')
133 )
134 return c
135
136 def generate_function_params(self, function):
137 """Generates the struct for passing parameters into a function.
138 """
139 c = code.Code()
140
141 c.sblock('struct Params {')
142 for param in function.params:
143 if param.description:
144 c.comment(param.description)
145 if param.type == PropertyType.OBJECT:
146 c.concat(self.generate_type(param, serializable=False))
147 c.append()
148 for param in function.params:
149 c.append('%s %s;' % (self.__cpp_type_manager.get_type(param), param.name))
150
151 if function.params:
152 (c.append()
153 .append('Params();')
154 .append('~Params();')
155 .append()
156 .append('static bool Populate(const ListValue& args, Params* out);')
157 .eblock()
158 .sblock(' private:')
159 .append('DISALLOW_COPY_AND_ASSIGN(Params);')
160 )
161
162 c.eblock('};')
163
164 return c
165
166 def generate_function_result(self, function):
167 """Generates the struct for passing a function's result back.
168 """
169 # TODO(calamity): Handle returning an object
170 c = code.Code()
171
172 param = function.callback.param
173 # TODO(calamity): Put this description comment in less stupid place
174 if param.description:
175 c.comment(param.description)
176 (c.append('class Result {')
177 .sblock(' public:')
178 )
179 arg = ''
180 # TODO(calamity): handle object
181 if param:
182 if param.type == PropertyType.REF:
183 arg = 'const %(type)s& %(name)s'
184 else:
185 arg = 'const %(type)s %(name)s'
186 arg = arg % {'type': self.__cpp_type_manager.get_type(param),
187 'name': param.name}
188 (c.append('static Value* Create(%s);' % arg)
189 .eblock()
190 .sblock(' private:')
191 .append('Result() {};')
192 .append('DISALLOW_COPY_AND_ASSIGN(Result);')
193 .eblock('};')
194 )
195
196 return c
197
198 def generate_ifndef_name(path, filename):
199 """Formats a path and filename as a #define name.
200
201 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__.
202 """
203 return ('%s/%s_H__' % (path, filename)).upper().replace('/', '_')
204
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698