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

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

Issue 1811413002: [Extensions] Update generated code to support move operations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: compile fix Created 4 years, 9 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 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 schema_util 8 import schema_util
9 import util_cc_helper 9 import util_cc_helper
10 from cpp_namespace_environment import CppNamespaceEnvironment 10 from cpp_namespace_environment import CppNamespaceEnvironment
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 119
120 if type_.property_type == PropertyType.OBJECT: 120 if type_.property_type == PropertyType.OBJECT:
121 c.Cblock(self._GeneratePropertyFunctions(classname_in_namespace, 121 c.Cblock(self._GeneratePropertyFunctions(classname_in_namespace,
122 type_.properties.values())) 122 type_.properties.values()))
123 else: 123 else:
124 c.Cblock(self._GenerateTypes(classname_in_namespace, type_.choices)) 124 c.Cblock(self._GenerateTypes(classname_in_namespace, type_.choices))
125 125
126 (c.Append('%s::%s()' % (classname_in_namespace, classname)) 126 (c.Append('%s::%s()' % (classname_in_namespace, classname))
127 .Cblock(self._GenerateInitializersAndBody(type_)) 127 .Cblock(self._GenerateInitializersAndBody(type_))
128 .Append('%s::~%s() {}' % (classname_in_namespace, classname)) 128 .Append('%s::~%s() {}' % (classname_in_namespace, classname))
129 .Append()
130 ) 129 )
130 if 'use_movable_types' in type_.namespace.compiler_options:
131 # Note: we use 'rhs' because some API objects have a member 'other'.
132 (c.Append('%s::%s(%s&& rhs)' %
133 (classname_in_namespace, classname, classname))
134 .Cblock(self._GenerateMoveCtor(type_))
135 .Append('%s& %s::operator=(%s&& rhs)' %
136 (classname_in_namespace, classname_in_namespace,
137 classname))
138 .Cblock(self._GenerateMoveAssignOperator(type_))
139 )
131 if type_.origin.from_json: 140 if type_.origin.from_json:
132 c.Cblock(self._GenerateTypePopulate(classname_in_namespace, type_)) 141 c.Cblock(self._GenerateTypePopulate(classname_in_namespace, type_))
133 if cpp_namespace is None: # only generate for top-level types 142 if cpp_namespace is None: # only generate for top-level types
134 c.Cblock(self._GenerateTypeFromValue(classname_in_namespace, type_)) 143 c.Cblock(self._GenerateTypeFromValue(classname_in_namespace, type_))
135 if type_.origin.from_client: 144 if type_.origin.from_client:
136 c.Cblock(self._GenerateTypeToValue(classname_in_namespace, type_)) 145 c.Cblock(self._GenerateTypeToValue(classname_in_namespace, type_))
137 elif type_.property_type == PropertyType.ENUM: 146 elif type_.property_type == PropertyType.ENUM:
138 (c.Cblock(self._GenerateEnumToString(cpp_namespace, type_)) 147 (c.Cblock(self._GenerateEnumToString(cpp_namespace, type_))
139 .Cblock(self._GenerateEnumFromString(cpp_namespace, type_)) 148 .Cblock(self._GenerateEnumFromString(cpp_namespace, type_))
140 ) 149 )
(...skipping 30 matching lines...) Expand all
171 t.property_type == PropertyType.REF or 180 t.property_type == PropertyType.REF or
172 t.property_type == PropertyType.STRING): 181 t.property_type == PropertyType.STRING):
173 # TODO(miket): It would be nice to initialize CHOICES, but we 182 # TODO(miket): It would be nice to initialize CHOICES, but we
174 # don't presently have the semantics to indicate which one of a set 183 # don't presently have the semantics to indicate which one of a set
175 # should be the default. 184 # should be the default.
176 continue 185 continue
177 else: 186 else:
178 raise TypeError(t) 187 raise TypeError(t)
179 188
180 if items: 189 if items:
181 s = ': %s' % (', '.join(items)) 190 s = ': %s' % (',\n'.join(items))
182 else: 191 else:
183 s = '' 192 s = ''
184 s = s + ' {}' 193 s = s + ' {}'
185 return Code().Append(s) 194 return Code().Append(s)
186 195
196 def _GetMoveProps(self, type_, copy_str, move_str):
197 """Returns a tuple of (props, dicts) for the type.
198
199 |props| is a list of all the copyable or movable properties generated using
200 the copy_str and move_str, and |dicts| is a list of all the dictionary
201 properties by name.
202
203 Properties:
204 - |type_| the Type to get the properties from
205 - |copy_str| the string to use when copying a value; should have two
206 placeholders to take the property name.
207 - |move_str| the string to use when moving a value; should have two
208 placeholders to take the property name.
209 """
210 props = []
211 dicts = []
212 for prop in type_.properties.values():
213 t = prop.type_
214
215 real_t = self._type_helper.FollowRef(t)
216 if (prop.optional or
217 t.property_type == PropertyType.ANY or
218 t.property_type == PropertyType.ARRAY or
219 t.property_type == PropertyType.BINARY or
220 t.property_type == PropertyType.CHOICES or
221 t.property_type == PropertyType.OBJECT or
222 t.property_type == PropertyType.REF or
223 t.property_type == PropertyType.STRING):
224 props.append(move_str % (prop.unix_name, prop.unix_name))
225 elif t.property_type == PropertyType.FUNCTION:
226 dicts.append(prop.unix_name)
227 elif (real_t.property_type == PropertyType.ENUM or
228 t.property_type == PropertyType.INTEGER or
229 t.property_type == PropertyType.DOUBLE or
230 t.property_type == PropertyType.BOOLEAN):
231 props.append(copy_str % (prop.unix_name, prop.unix_name))
232 else:
233 raise TypeError(t)
234
235 return (props, dicts)
236
237 def _GenerateMoveCtor(self, type_):
238 props, dicts = self._GetMoveProps(type_, '%s(rhs.%s)',
239 '%s(std::move(rhs.%s))')
240 s = ''
241 if props:
242 s = s + ': %s' % (',\n'.join(props))
243 s = s + '{'
244 for item in dicts:
245 s = s + ('\n%s.Swap(&rhs.%s);' % (item, item))
246 s = s + '\n}'
247
248 return Code().Append(s)
249
250 def _GenerateMoveAssignOperator(self, type_):
251 props, dicts = self._GetMoveProps(type_, '%s = rhs.%s;',
252 '%s = std::move(rhs.%s);')
253 s = '{\n'
254 if props:
255 s = s + '\n'.join(props)
256 for item in dicts:
257 s = s + ('\n%s.Swap(&rhs.%s);' % (item, item))
258 s = s + '\nreturn *this;\n}'
259
260 return Code().Append(s)
261
187 def _GenerateTypePopulate(self, cpp_namespace, type_): 262 def _GenerateTypePopulate(self, cpp_namespace, type_):
188 """Generates the function for populating a type given a pointer to it. 263 """Generates the function for populating a type given a pointer to it.
189 264
190 E.g for type "Foo", generates Foo::Populate() 265 E.g for type "Foo", generates Foo::Populate()
191 """ 266 """
192 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name)) 267 classname = cpp_util.Classname(schema_util.StripNamespace(type_.name))
193 c = Code() 268 c = Code()
194 (c.Append('// static') 269 (c.Append('// static')
195 .Append('bool %(namespace)s::Populate(') 270 .Append('bool %(namespace)s::Populate(')
196 .Sblock(' %s) {' % self._GenerateParams( 271 .Sblock(' %s) {' % self._GenerateParams(
(...skipping 874 matching lines...) Expand 10 before | Expand all | Expand 10 after
1071 if self._generate_error_messages: 1146 if self._generate_error_messages:
1072 params = list(params) + ['base::string16* error'] 1147 params = list(params) + ['base::string16* error']
1073 return ', '.join(str(p) for p in params) 1148 return ', '.join(str(p) for p in params)
1074 1149
1075 def _GenerateArgs(self, args): 1150 def _GenerateArgs(self, args):
1076 """Builds the argument list for a function, given an array of arguments. 1151 """Builds the argument list for a function, given an array of arguments.
1077 """ 1152 """
1078 if self._generate_error_messages: 1153 if self._generate_error_messages:
1079 args = list(args) + ['error'] 1154 args = list(args) + ['error']
1080 return ', '.join(str(a) for a in args) 1155 return ', '.join(str(a) for a in args)
OLDNEW
« no previous file with comments | « chrome/common/extensions/api/developer_private.idl ('k') | tools/json_schema_compiler/cpp_type_generator.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698