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

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 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
9 8
10 class HGenerator(object): 9 class HGenerator(object):
11 """A .h generator for a namespace. 10 """A .h generator for a namespace.
12 """ 11 """
13 def __init__(self, namespace, cpp_type_generator): 12 def __init__(self, namespace, cpp_type_generator):
14 self._cpp_type_generator = cpp_type_generator 13 self._cpp_type_generator = cpp_type_generator
15 self._namespace = namespace 14 self._namespace = namespace
16 self._target_namespace = ( 15 self._target_namespace = (
17 self._cpp_type_generator.GetCppNamespaceName(self._namespace)) 16 self._cpp_type_generator.GetCppNamespaceName(self._namespace))
18 17
19 def Generate(self): 18 def Generate(self):
20 """Generates a code.Code object with the .h for a single namespace. 19 """Generates a code.Code object with the .h for a single namespace.
21 """ 20 """
22 c = code.Code() 21 c = code.Code()
23 (c.Append(cpp_util.CHROMIUM_LICENSE) 22 (c.Append(cpp_util.CHROMIUM_LICENSE)
24 .Append() 23 .Append()
25 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file) 24 .Append(cpp_util.GENERATED_FILE_MESSAGE % self._namespace.source_file)
26 .Append() 25 .Append()
27 ) 26 )
28 27
29 ifndef_name = self._GenerateIfndefName() 28 ifndef_name = self._GenerateIfndefName()
30 (c.Append('#ifndef %s' % ifndef_name) 29 (c.Append('#ifndef %s' % ifndef_name)
31 .Append('#define %s' % ifndef_name) 30 .Append('#define %s' % ifndef_name)
32 .Append('#pragma once') 31 .Append('#pragma once')
33 .Append() 32 .Append()
34 .Append('#include <string>') 33 .Append('#include <string>')
35 .Append('#include <vector>') 34 .Append('#include <vector>')
36 .Append() 35 .Append()
37 .Append('#include "base/basictypes.h"') 36 .Append('#include "base/basictypes.h"')
37 .Append('#include "base/memory/linked_ptr.h"')
38 .Append('#include "base/memory/scoped_ptr.h"') 38 .Append('#include "base/memory/scoped_ptr.h"')
39 .Append('#include "base/values.h"') 39 .Append('#include "base/values.h"')
40 .Append() 40 .Append()
41 .Append('using base::Value;') 41 .Append('using base::Value;')
42 .Append('using base::DictionaryValue;') 42 .Append('using base::DictionaryValue;')
43 .Append('using base::ListValue;') 43 .Append('using base::ListValue;')
44 .Append() 44 .Append()
45 ) 45 )
46 46
47 includes = self._cpp_type_generator.GenerateCppIncludes() 47 c.Concat(self._cpp_type_generator.GetRootNamespaceStart())
48 if not includes.IsEmpty(): 48 forward_declarations = (
49 (c.Concat(includes) 49 self._cpp_type_generator.GenerateForwardDeclarations())
50 if not forward_declarations.IsEmpty():
51 (c.Append()
52 .Concat(forward_declarations)
50 .Append() 53 .Append()
51 ) 54 )
52 55
53 (c.Concat(self._cpp_type_generator.GetCppNamespaceStart()) 56 (c.Concat(self._cpp_type_generator.GetNamespaceStart())
54 .Append() 57 .Append()
55 .Append('//') 58 .Append('//')
56 .Append('// Types') 59 .Append('// Types')
57 .Append('//') 60 .Append('//')
58 .Append() 61 .Append()
59 ) 62 )
60 for type_ in self._namespace.types.values(): 63 for type_ in self._namespace.types.values():
61 (c.Concat(self._GenerateType(type_)) 64 (c.Concat(self._GenerateType(type_))
62 .Append() 65 .Append()
63 ) 66 )
64 (c.Append('//') 67 (c.Append('//')
65 .Append('// Functions') 68 .Append('// Functions')
66 .Append('//') 69 .Append('//')
67 .Append() 70 .Append()
68 ) 71 )
69 for function in self._namespace.functions.values(): 72 for function in self._namespace.functions.values():
70 (c.Concat(self._GenerateFunction(function)) 73 (c.Concat(self._GenerateFunction(function))
71 .Append() 74 .Append()
72 ) 75 )
73 (c.Append() 76 (c.Append()
74 .Append() 77 .Concat(self._cpp_type_generator.GetNamespaceEnd())
75 .Concat(self._cpp_type_generator.GetCppNamespaceEnd()) 78 .Concat(self._cpp_type_generator.GetRootNamespaceEnd())
76 .Append() 79 .Append()
77 .Append('#endif // %s' % ifndef_name) 80 .Append('#endif // %s' % ifndef_name)
78 .Append() 81 .Append()
79 ) 82 )
80 return c 83 return c
81 84
82 def _GenerateType(self, type_, serializable=True): 85 def _GenerateType(self, type_, serializable=True):
83 """Generates a struct for a type. 86 """Generates a struct for a type.
84 """ 87 """
85 classname = cpp_util.CppName(type_.name) 88 classname = cpp_util.Classname(type_.name)
86 c = code.Code() 89 c = code.Code()
87 if type_.description: 90 if type_.description:
88 c.Comment(type_.description) 91 c.Comment(type_.description)
89 (c.Sblock('struct %(classname)s {') 92 (c.Sblock('struct %(classname)s {')
90 .Append('~%(classname)s();') 93 .Append('~%(classname)s();')
91 .Append('%(classname)s();') 94 .Append('%(classname)s();')
92 .Append() 95 .Append()
93 ) 96 )
94 97
95 for prop in type_.properties.values(): 98 for prop in type_.properties.values():
96 if prop.description: 99 if prop.description:
97 c.Comment(prop.description) 100 c.Comment(prop.description)
98 if prop.optional: 101 c.Append('%s %s;' % (
99 c.Append('scoped_ptr<%s> %s;' % 102 self._cpp_type_generator.GetType(prop, wrap_optional=True),
100 (self._cpp_type_generator.GetType(prop, pad_for_generics=True), 103 prop.unix_name))
101 prop.name))
102 else:
103 c.Append('%s %s;' %
104 (self._cpp_type_generator.GetType(prop), prop.name))
105 c.Append() 104 c.Append()
106 105
107 (c.Comment('Populates a %(classname)s object from a Value. Returns' 106 (c.Comment('Populates a %(classname)s object from a Value. Returns'
108 ' whether |out| was successfully populated.') 107 ' whether |out| was successfully populated.')
109 .Append('static bool Populate(const Value& value, %(classname)s* out);') 108 .Append('static bool Populate(const Value& value, %(classname)s* out);')
110 .Append() 109 .Append()
111 ) 110 )
112 111
113 if serializable: 112 if serializable:
114 (c.Comment('Returns a new DictionaryValue representing the' 113 (c.Comment('Returns a new DictionaryValue representing the'
115 ' serialized form of this %(classname)s object. Passes' 114 ' serialized form of this %(classname)s object. Passes'
116 'ownership to caller.') 115 'ownership to caller.')
117 .Append('DictionaryValue* ToValue() const;') 116 .Append('DictionaryValue* ToValue() const;')
118 ) 117 )
119 (c.Eblock() 118 (c.Eblock()
120 .Sblock(' private:') 119 .Sblock(' private:')
121 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);') 120 .Append('DISALLOW_COPY_AND_ASSIGN(%(classname)s);')
122 .Eblock('};') 121 .Eblock('};')
123 ) 122 )
124 c.Substitute({'classname': classname}) 123 c.Substitute({'classname': classname})
125 return c 124 return c
126 125
127 def _GenerateFunction(self, function): 126 def _GenerateFunction(self, function):
128 """Generates the structs for a function. 127 """Generates the structs for a function.
129 """ 128 """
130 c = code.Code() 129 c = code.Code()
131 (c.Sblock('namespace %s {' % cpp_util.CppName(function.name)) 130 (c.Sblock('namespace %s {' % cpp_util.Classname(function.name))
132 .Concat(self._GenerateFunctionParams(function)) 131 .Concat(self._GenerateFunctionParams(function))
133 .Append() 132 .Append()
134 .Concat(self._GenerateFunctionResult(function)) 133 .Concat(self._GenerateFunctionResult(function))
135 .Append() 134 .Append()
136 .Eblock('};') 135 .Eblock('};')
137 ) 136 )
138 return c 137 return c
139 138
140 def _GenerateFunctionParams(self, function): 139 def _GenerateFunctionParams(self, function):
141 """Generates the struct for passing parameters into a function. 140 """Generates the struct for passing parameters into a function.
142 """ 141 """
143 c = code.Code() 142 c = code.Code()
144 143
145 if function.params: 144 if function.params:
146 c.Sblock('struct Params {') 145 c.Sblock('struct Params {')
147 for param in function.params: 146 for param in function.params:
148 if param.description: 147 if param.description:
149 c.Comment(param.description) 148 c.Comment(param.description)
150 if param.type_ == PropertyType.OBJECT: 149 if param.type_ == PropertyType.OBJECT:
151 c.Concat(self._GenerateType(param, serializable=False)) 150 c.Concat(self._GenerateType(param, serializable=False))
152 c.Append() 151 c.Append()
153 for param in function.params: 152 for param in function.params:
154 c.Append('%s %s;' % 153 c.Append('%s %s;' %
155 (self._cpp_type_generator.GetType(param), param.name)) 154 (self._cpp_type_generator.GetType(param, wrap_optional=True),
155 param.unix_name))
156 156
157 (c.Append() 157 (c.Append()
158 .Append('~Params();') 158 .Append('~Params();')
159 .Append() 159 .Append()
160 .Append('static scoped_ptr<Params> Create(const ListValue& args);') 160 .Append('static scoped_ptr<Params> Create(const ListValue& args);')
161 .Eblock() 161 .Eblock()
162 .Sblock(' private:') 162 .Sblock(' private:')
163 .Append('Params();') 163 .Append('Params();')
164 .Append() 164 .Append()
165 .Append('DISALLOW_COPY_AND_ASSIGN(Params);') 165 .Append('DISALLOW_COPY_AND_ASSIGN(Params);')
166 .Eblock('};')
166 ) 167 )
167 168
168 c.Eblock('};')
169
170 return c 169 return c
171 170
172 def _GenerateFunctionResult(self, function): 171 def _GenerateFunctionResult(self, function):
173 """Generates the struct for passing a function's result back. 172 """Generates the struct for passing a function's result back.
174 """ 173 """
175 # TODO(calamity): Handle returning an object
176 c = code.Code() 174 c = code.Code()
177 175
178 param = function.callback.param
179 # TODO(calamity): Put this description comment in less stupid place
180 if param.description:
181 c.Comment(param.description)
182 (c.Append('class Result {') 176 (c.Append('class Result {')
183 .Sblock(' public:') 177 .Sblock(' public:')
184 ) 178 )
185 arg = '' 179 params = function.callback.params
186 # TODO(calamity): handle object 180 if not params:
187 if param: 181 c.Append('static Value* Create();')
188 if param.type_ == PropertyType.REF: 182 else:
189 arg = 'const %(type)s& %(name)s' 183 # If there is a single parameter, this is straightforward. However, if
190 else: 184 # the callback parameter is of 'choices', this generates a Create method
185 # for each choice. This works because only 1 choice can be returned at a
186 # time.
187 for param in params:
191 arg = 'const %(type)s %(name)s' 188 arg = 'const %(type)s %(name)s'
192 arg = arg % { 189 if param.type_ == PropertyType.REF:
190 arg = 'const %(type)s& %(name)s'
191 arg %= {
193 'type': self._cpp_type_generator.GetType(param), 192 'type': self._cpp_type_generator.GetType(param),
194 'name': param.name 193 'name': param.unix_name,
195 } 194 }
196 (c.Append('static Value* Create(%s);' % arg) 195 if param.description:
197 .Eblock() 196 c.Comment(param.description)
198 .Sblock(' private:') 197 c.Append('static Value* Create(%s);' % arg)
199 .Append('Result() {};') 198 (c.Eblock()
200 .Append('DISALLOW_COPY_AND_ASSIGN(Result);') 199 .Sblock(' private:')
201 .Eblock('};') 200 .Append('Result() {};')
201 .Append('DISALLOW_COPY_AND_ASSIGN(Result);')
202 .Eblock('};')
202 ) 203 )
203 204
204 return c 205 return c
205 206
206 def _GenerateIfndefName(self): 207 def _GenerateIfndefName(self):
207 """Formats a path and filename as a #define name. 208 """Formats a path and filename as a #define name.
208 209
209 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__. 210 e.g chrome/extensions/gen, file.h becomes CHROME_EXTENSIONS_GEN_FILE_H__.
210 """ 211 """
211 return (('%s_%s_H__' % 212 return (('%s/%s_H__' %
212 (self._namespace.source_file_dir, self._target_namespace)) 213 (self._namespace.source_file_dir, self._target_namespace))
213 .upper().replace(os.sep, '_')) 214 .upper().replace('/', '_'))
214 215
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698