Chromium Code Reviews| Index: tools/json_schema_compiler/cc_generator.py |
| diff --git a/tools/json_schema_compiler/cc_generator.py b/tools/json_schema_compiler/cc_generator.py |
| index ac1679bee35119d0c57122ef50b5bcf9163b1cc7..8b5e1cbd2c2e524ac350441c260fcdde3c149696 100644 |
| --- a/tools/json_schema_compiler/cc_generator.py |
| +++ b/tools/json_schema_compiler/cc_generator.py |
| @@ -126,8 +126,17 @@ class _Generator(object): |
| (c.Append('%s::%s()' % (classname_in_namespace, classname)) |
| .Cblock(self._GenerateInitializersAndBody(type_)) |
| .Append('%s::~%s() {}' % (classname_in_namespace, classname)) |
| - .Append() |
| ) |
| + if 'use_movable_types' in type_.namespace.compiler_options: |
| + # Note: we use '_other' because some API objects have a member 'other'. |
|
dcheng
2016/03/22 17:42:34
It's probably better to call this 'rhs' rather tha
Devlin
2016/03/22 17:58:40
Here's hoping no apis use "rhs" as a parameter ;)
|
| + (c.Append('%s::%s(%s&& _other)' % |
| + (classname_in_namespace, classname, classname)) |
| + .Cblock(self._GenerateMoveCtor(type_)) |
| + .Append('%s& %s::operator=(%s&& _other)' % |
| + (classname_in_namespace, classname_in_namespace, |
| + classname)) |
| + .Cblock(self._GenerateMoveAssignOperator(type_)) |
| + ) |
| if type_.origin.from_json: |
| c.Cblock(self._GenerateTypePopulate(classname_in_namespace, type_)) |
| if cpp_namespace is None: # only generate for top-level types |
| @@ -178,12 +187,78 @@ class _Generator(object): |
| raise TypeError(t) |
| if items: |
| - s = ': %s' % (', '.join(items)) |
| + s = ': %s' % (',\n'.join(items)) |
| else: |
| s = '' |
| s = s + ' {}' |
| return Code().Append(s) |
| + def _GetMoveProps(self, type_, copy_str, move_str): |
| + """Returns a tuple of (props, dicts) for the type. |
| + |
| + |props| is a list of all the copyable or movable properties generated using |
| + the copy_str and move_str, and |dicts| is a list of all the dictionary |
| + properties by name. |
| + |
| + Properties: |
| + - |type_| the Type to get the properties from |
| + - |copy_str| the string to use when copying a value; should have two |
| + placeholders to take the property name. |
| + - |move_str| the string to use when moving a value; should have two |
| + placeholders to take the property name. |
| + """ |
| + props = [] |
| + dicts = [] |
| + for prop in type_.properties.values(): |
| + t = prop.type_ |
| + |
| + real_t = self._type_helper.FollowRef(t) |
| + if (prop.optional or |
| + t.property_type == PropertyType.ANY or |
| + t.property_type == PropertyType.ARRAY or |
| + t.property_type == PropertyType.BINARY or |
| + t.property_type == PropertyType.CHOICES or |
| + t.property_type == PropertyType.OBJECT or |
| + t.property_type == PropertyType.REF or |
| + t.property_type == PropertyType.STRING): |
| + props.append(move_str % (prop.unix_name, prop.unix_name)) |
| + elif t.property_type == PropertyType.FUNCTION: |
| + dicts.append(prop.unix_name) |
| + elif (real_t.property_type == PropertyType.ENUM or |
| + t.property_type == PropertyType.INTEGER or |
| + t.property_type == PropertyType.DOUBLE or |
| + t.property_type == PropertyType.BOOLEAN): |
| + props.append(copy_str % (prop.unix_name, prop.unix_name)) |
| + else: |
| + raise TypeError(t) |
| + |
| + return (props, dicts) |
| + |
| + def _GenerateMoveCtor(self, type_): |
| + props, dicts = self._GetMoveProps(type_, '%s(_other.%s)', |
| + '%s(std::move(_other.%s))') |
| + s = '' |
| + if props: |
| + s = s + ': %s' % (',\n'.join(props)) |
| + s = s + '{' |
| + for item in dicts: |
| + s = s + ('\n%s.Swap(&_other.%s);' % (item, item)) |
| + s = s + '\n}' |
| + |
| + return Code().Append(s) |
| + |
| + def _GenerateMoveAssignOperator(self, type_): |
| + props, dicts = self._GetMoveProps(type_, '%s = _other.%s;', |
| + '%s = std::move(_other.%s);') |
| + s = '{\n' |
| + if props: |
| + s = s + '\n'.join(props) |
| + for item in dicts: |
| + s = s + ('\n%s.Swap(&_other.%s);' % (item, item)) |
| + s = s + '\nreturn *this;\n}' |
| + |
| + return Code().Append(s) |
| + |
| def _GenerateTypePopulate(self, cpp_namespace, type_): |
| """Generates the function for populating a type given a pointer to it. |