tools/json_schema_compiler/h_generator.py
def _GenerateType(self, type_, is_toplevel=False, generate_typedefs=False):
    """Generates a struct for |type_|.

    |is_toplevel|       implies that the type was declared in the "types" field
                        of an API schema. This determines the correct function
                        modifier(s).
    |generate_typedefs| controls whether primitive types should be generated as
                        a typedef. This may not always be desired. If false,
                        primitive types are ignored.
    """
    classname = cpp_util.Classname(schema_util.StripNamespace(type_.name))
    c = Code()

    if type_.functions:
      # Wrap functions within types in the type's namespace.
      (c.Append('namespace %s {' % classname)
        .Append()
      )
      for function in type_.functions.values():
        c.Cblock(self._GenerateFunction(function))
      c.Append('}  // namespace %s' % classname)
    elif type_.property_type == PropertyType.ARRAY:
      if generate_typedefs and type_.description:
        c.Comment(type_.description)
      c.Cblock(self._GenerateType(type_.item_type, is_toplevel=is_toplevel))
      if generate_typedefs:
        (c.Append('typedef std::vector<%s > %s;' % (
                       self._type_helper.GetCppType(type_.item_type),
                       classname))
        )
    elif type_.property_type == PropertyType.STRING:
      if generate_typedefs:
        if type_.description:
          c.Comment(type_.description)
        c.Append('typedef std::string %(classname)s;')
    elif type_.property_type == PropertyType.ENUM:
      if type_.description:
        c.Comment(type_.description)
      c.Cblock(self._GenerateEnumDeclaration(classname, type_))
      # Top level enums are in a namespace scope so the methods shouldn't be
      # static. On the other hand, those declared inline (e.g. in an object) do.
      maybe_static = '' if is_toplevel else 'static '
      (c.Append()
        .Append('%sconst char* ToString(%s as_enum);' %
                (maybe_static, classname))
        .Append('%s%s Parse%s(const std::string& as_string);' %
                (maybe_static, classname, classname))
      )
    elif type_.property_type in (PropertyType.CHOICES,
                                 PropertyType.OBJECT):
      if type_.description:
        c.Comment(type_.description)

      (c.Sblock('struct %(classname)s {')
        .Append('%(classname)s();')
        .Append('~%(classname)s();')
        .Append('%(classname)s(const %(classname)s&) = delete;')
        .Append('%(classname)s& operator=(const %(classname)s&) = delete;')
        .Append('%(classname)s(%(classname)s&& rhs);')
        .Append('%(classname)s& operator=(%(classname)s&& rhs);')
      )

      if type_.origin.from_manifest_keys:
        c.Append()
        c.Comment('Manifest key constants.')
        c.Concat(self._GenerateManifestKeyConstants(type_.properties.values()))

      if type_.origin.from_json:
        (c.Append()
          .Comment('Populates a %s object from a base::Value. Returns'
                   ' whether |out| was successfully populated.' % classname)
          .Append('static bool Populate(%s);' % self._GenerateParams(
              ('const base::Value& value', '%s* out' % classname)))
        )
        if is_toplevel:
          (c.Append()
            .Comment('Creates a %s object from a base::Value, or NULL on '
                     'failure.' % classname)
            .Append('static std::unique_ptr<%s> FromValue(%s);' % (
                classname, self._GenerateParams(('const base::Value& value',))))
          )
      if type_.origin.from_client:
        value_type = ('base::Value'
                      if type_.property_type is PropertyType.CHOICES else
                      'base::DictionaryValue')
        (c.Append()
          .Comment('Returns a new %s representing the serialized form of this '
                   '%s object.' % (value_type, classname))
          .Append('std::unique_ptr<%s> ToValue() const;' % value_type)
        )

      if type_.origin.from_manifest_keys:
        c.Cblock(self._GenerateParseFromDictionary(type_, classname))

      if type_.property_type == PropertyType.CHOICES:
        # Choices are modelled with optional fields for each choice. Exactly one
        # field of the choice is guaranteed to be set by the compiler.
        c.Cblock(self._GenerateTypes(type_.choices))
        c.Append('// Choices:')
        for choice_type in type_.choices:
          c.Append('%s as_%s;' % (
              self._type_helper.GetCppType(choice_type, is_ptr=True),
              choice_type.unix_name))
      else:
        properties = type_.properties.values()
        (c.Append()
          .Cblock(self._GenerateTypes(p.type_ for p in properties))
          .Cblock(self._GenerateFields(properties)))
        if type_.additional_properties is not None:
          # Most additionalProperties actually have type "any", which is better
          # modelled as a DictionaryValue rather than a map of string -> Value.
          if type_.additional_properties.property_type == PropertyType.ANY:
            c.Append('base::DictionaryValue additional_properties;')
          else:
            (c.Cblock(self._GenerateType(type_.additional_properties))
              .Append('std::map<std::string, %s> additional_properties;' %
                      self._type_helper.GetCppType(type_.additional_properties,
                                                   is_in_container=True))
            )
      (c.Eblock('};'))
    return c.Substitute({'classname': classname})
Generates a struct for |type_|.

    |is_toplevel|       implies that the type was declared in the "types" field
                        of an API schema. This determines the correct function
                        modifier(s).
    |generate_typedefs| controls whether primitive types should be generated as
                        a typedef. This may not always be desired. If false,
                        primitive types are ignored.
    
Generates a struct for |type_|.
|is_toplevel|       implies that the type was declared in the "types" field
of an API schema. This determines the correct function
modifier(s).
|generate_typedefs| controls whether primitive types should be generated as
a typedef. This may not always be desired. If false,
primitive types are ignored.
Generates a struct for |type_| . |is_toplevel| implies that the type was declared in the " types " field of an API schema . This determines the correct function modifier ( s ) . |generate_typedefs| controls whether primitive types should be generated as a typedef . This may not always be desired . If false primitive types are ignored .
def _GenerateType(self, type_, is_toplevel=False, generate_typedefs=False):
    classname = cpp_util.Classname(schema_util.StripNamespace(type_.name))
    c = Code()
    if type_.functions:
      (c.Append('namespace %s {' % classname)
        .Append()
      )
      for function in type_.functions.values():
        c.Cblock(self._GenerateFunction(function))
      c.Append('}  // namespace %s' % classname)
    elif type_.property_type == PropertyType.ARRAY:
      if generate_typedefs and type_.description:
        c.Comment(type_.description)
      c.Cblock(self._GenerateType(type_.item_type, is_toplevel=is_toplevel))
      if generate_typedefs:
        (c.Append('typedef std::vector<%s > %s;' % (
                       self._type_helper.GetCppType(type_.item_type),
                       classname))
        )
    elif type_.property_type == PropertyType.STRING:
      if generate_typedefs:
        if type_.description:
          c.Comment(type_.description)
        c.Append('typedef std::string %(classname)s;')
    elif type_.property_type == PropertyType.ENUM:
      if type_.description:
        c.Comment(type_.description)
      c.Cblock(self._GenerateEnumDeclaration(classname, type_))
      maybe_static = '' if is_toplevel else 'static '
      (c.Append()
        .Append('%sconst char* ToString(%s as_enum);' %
                (maybe_static, classname))
        .Append('%s%s Parse%s(const std::string& as_string);' %
                (maybe_static, classname, classname))
      )
    elif type_.property_type in (PropertyType.CHOICES,
                                 PropertyType.OBJECT):
      if type_.description:
        c.Comment(type_.description)
      (c.Sblock('struct %(classname)s {')
        .Append('%(classname)s();')
        .Append('~%(classname)s();')
        .Append('%(classname)s(const %(classname)s&) = delete;')
        .Append('%(classname)s& operator=(const %(classname)s&) = delete;')
        .Append('%(classname)s(%(classname)s&& rhs);')
        .Append('%(classname)s& operator=(%(classname)s&& rhs);')
      )
      if type_.origin.from_manifest_keys:
        c.Append()
        c.Comment('Manifest key constants.')
        c.Concat(self._GenerateManifestKeyConstants(type_.properties.values()))
      if type_.origin.from_json:
        (c.Append()
          .Comment('Populates a %s object from a base::Value. Returns'
                   ' whether |out| was successfully populated.' % classname)
          .Append('static bool Populate(%s);' % self._GenerateParams(
              ('const base::Value& value', '%s* out' % classname)))
        )
        if is_toplevel:
          (c.Append()
            .Comment('Creates a %s object from a base::Value, or NULL on '
                     'failure.' % classname)
            .Append('static std::unique_ptr<%s> FromValue(%s);' % (
                classname, self._GenerateParams(('const base::Value& value',))))
          )
      if type_.origin.from_client:
        value_type = ('base::Value'
                      if type_.property_type is PropertyType.CHOICES else
                      'base::DictionaryValue')
        (c.Append()
          .Comment('Returns a new %s representing the serialized form of this '
                   '%s object.' % (value_type, classname))
          .Append('std::unique_ptr<%s> ToValue() const;' % value_type)
        )
      if type_.origin.from_manifest_keys:
        c.Cblock(self._GenerateParseFromDictionary(type_, classname))
      if type_.property_type == PropertyType.CHOICES:
        c.Cblock(self._GenerateTypes(type_.choices))
        c.Append('// Choices:')
        for choice_type in type_.choices:
          c.Append('%s as_%s;' % (
              self._type_helper.GetCppType(choice_type, is_ptr=True),
              choice_type.unix_name))
      else:
        properties = type_.properties.values()
        (c.Append()
          .Cblock(self._GenerateTypes(p.type_ for p in properties))
          .Cblock(self._GenerateFields(properties)))
        if type_.additional_properties is not None:
          if type_.additional_properties.property_type == PropertyType.ANY:
            c.Append('base::DictionaryValue additional_properties;')
          else:
            (c.Cblock(self._GenerateType(type_.additional_properties))
              .Append('std::map<std::string, %s> additional_properties;' %
                      self._type_helper.GetCppType(type_.additional_properties,
                                                   is_in_container=True))
            )
      (c.Eblock('};'))
    return c.Substitute({'classname': classname})
def _GenerateType ( self , type_ , is_toplevel = False , generate_typedefs = False ) : classname = cpp_util . Classname ( schema_util . StripNamespace ( type_ . name ) ) c = Code ( ) if type_ . functions : ( c . Append ( 'namespace %s {' % classname ) . Append ( ) ) for function in type_ . functions . values ( ) : c . Cblock ( self . _GenerateFunction ( function ) ) c . Append ( '}  // namespace %s' % classname ) elif type_ . property_type == PropertyType . ARRAY : if generate_typedefs and type_ . description : c . Comment ( type_ . description ) c . Cblock ( self . _GenerateType ( type_ . item_type , is_toplevel = is_toplevel ) ) if generate_typedefs : ( c . Append ( 'typedef std::vector<%s > %s;' % ( self . _type_helper . GetCppType ( type_ . item_type ) , classname ) ) ) elif type_ . property_type == PropertyType . STRING : if generate_typedefs : if type_ . description : c . Comment ( type_ . description ) c . Append ( 'typedef std::string %(classname)s;' ) elif type_ . property_type == PropertyType . ENUM : if type_ . description : c . Comment ( type_ . description ) c . Cblock ( self . _GenerateEnumDeclaration ( classname , type_ ) ) maybe_static = '' if is_toplevel else 'static ' ( c . Append ( ) . Append ( '%sconst char* ToString(%s as_enum);' % ( maybe_static , classname ) ) . Append ( '%s%s Parse%s(const std::string& as_string);' % ( maybe_static , classname , classname ) ) ) elif type_ . property_type in ( PropertyType . CHOICES , PropertyType . OBJECT ) : if type_ . description : c . Comment ( type_ . description ) ( c . Sblock ( 'struct %(classname)s {' ) . Append ( '%(classname)s();' ) . Append ( '~%(classname)s();' ) . Append ( '%(classname)s(const %(classname)s&) = delete;' ) . Append ( '%(classname)s& operator=(const %(classname)s&) = delete;' ) . Append ( '%(classname)s(%(classname)s&& rhs);' ) . Append ( '%(classname)s& operator=(%(classname)s&& rhs);' ) ) if type_ . origin . from_manifest_keys : c . Append ( ) c . Comment ( 'Manifest key constants.' ) c . Concat ( self . _GenerateManifestKeyConstants ( type_ . properties . values ( ) ) ) if type_ . origin . from_json : ( c . Append ( ) . Comment ( 'Populates a %s object from a base::Value. Returns' ' whether |out| was successfully populated.' % classname ) . Append ( 'static bool Populate(%s);' % self . _GenerateParams ( ( 'const base::Value& value' , '%s* out' % classname ) ) ) ) if is_toplevel : ( c . Append ( ) . Comment ( 'Creates a %s object from a base::Value, or NULL on ' 'failure.' % classname ) . Append ( 'static std::unique_ptr<%s> FromValue(%s);' % ( classname , self . _GenerateParams ( ( 'const base::Value& value' , ) ) ) ) ) if type_ . origin . from_client : value_type = ( 'base::Value' if type_ . property_type is PropertyType . CHOICES else 'base::DictionaryValue' ) ( c . Append ( ) . Comment ( 'Returns a new %s representing the serialized form of this ' '%s object.' % ( value_type , classname ) ) . Append ( 'std::unique_ptr<%s> ToValue() const;' % value_type ) ) if type_ . origin . from_manifest_keys : c . Cblock ( self . _GenerateParseFromDictionary ( type_ , classname ) ) if type_ . property_type == PropertyType . CHOICES : c . Cblock ( self . _GenerateTypes ( type_ . choices ) ) c . Append ( '// Choices:' ) for choice_type in type_ . choices : c . Append ( '%s as_%s;' % ( self . _type_helper . GetCppType ( choice_type , is_ptr = True ) , choice_type . unix_name ) ) else : properties = type_ . properties . values ( ) ( c . Append ( ) . Cblock ( self . _GenerateTypes ( p . type_ for p in properties ) ) . Cblock ( self . _GenerateFields ( properties ) ) ) if type_ . additional_properties is not None : if type_ . additional_properties . property_type == PropertyType . ANY : c . Append ( 'base::DictionaryValue additional_properties;' ) else : ( c . Cblock ( self . _GenerateType ( type_ . additional_properties ) ) . Append ( 'std::map<std::string, %s> additional_properties;' % self . _type_helper . GetCppType ( type_ . additional_properties , is_in_container = True ) ) ) ( c . Eblock ( '};' ) ) return c . Substitute ( { 'classname' : classname } )
Generates a struct for |type_|.
Generates a struct for |type_| .
"""Generates a struct for |type_|.

    |is_toplevel|       implies that the type was declared in the "types" field
                        of an API schema. This determines the correct function
                        modifier(s).
    |generate_typedefs| controls whether primitive types should be generated as
                        a typedef. This may not always be desired. If false,
                        primitive types are ignored.
    """ # Wrap functions within types in the type's namespace. # Top level enums are in a namespace scope so the methods shouldn't be # static. On the other hand, those declared inline (e.g. in an object) do. # Choices are modelled with optional fields for each choice. Exactly one # field of the choice is guaranteed to be set by the compiler. # Most additionalProperties actually have type "any", which is better # modelled as a DictionaryValue rather than a map of string -> Value.
self
type_
is_toplevel
generate_typedefs