Ejemplo n.º 1
0
  def _GenerateCreateCallbackArguments(self, function_scope, callback):
    """Generate all functions to create Value parameters for a callback.

    E.g for function "Bar", generate Bar::Results::Create
    E.g for event "Baz", generate Baz::Create

    function_scope: the function scope path, e.g. Foo::Bar for the function
                    Foo::Bar::Baz(). May be None if there is no function scope.
    callback: the Function object we are creating callback arguments for.
    """
    c = Code()
    params = callback.params
    c.Concat(self._GeneratePropertyFunctions(function_scope, params))

    (c.Sblock('scoped_ptr<base::ListValue> %(function_scope)s'
                  'Create(%(declaration_list)s) {')
      .Append('scoped_ptr<base::ListValue> create_results('
              'new base::ListValue());')
    )
    declaration_list = []
    for param in params:
      declaration_list.append(cpp_util.GetParameterDeclaration(
          param, self._type_helper.GetCppType(param.type_)))
      c.Append('create_results->Append(%s);' %
          self._CreateValueFromType(param.type_, param.unix_name))
    c.Append('return create_results.Pass();')
    c.Eblock('}')
    c.Substitute({
        'function_scope': ('%s::' % function_scope) if function_scope else '',
        'declaration_list': ', '.join(declaration_list),
        'param_names': ', '.join(param.unix_name for param in params)
    })
    return c
Ejemplo n.º 2
0
 def _GenerateCreateEnumValue(self, cpp_namespace, prop):
     """Generates CreateEnumValue() that returns the |StringValue|
 representation of an enum.
 """
     c = Code()
     c.Append('// static')
     c.Sblock(
         'scoped_ptr<Value> %(cpp_namespace)s::CreateEnumValue(%(arg)s) {')
     c.Sblock('switch (%s) {' % prop.unix_name)
     if prop.optional:
         (c.Append('case %s: {' %
                   self._cpp_type_generator.GetEnumNoneValue(prop)).Append(
                       '  return scoped_ptr<Value>();').Append('}'))
     for enum_value in prop.enum_values:
         (c.Append(
             'case %s: {' %
             self._cpp_type_generator.GetEnumValue(prop, enum_value)
         ).Append(
             '  return scoped_ptr<Value>(Value::CreateStringValue("%s"));' %
             enum_value).Append('}'))
     (c.Append('default: {').Append('  return scoped_ptr<Value>();').Append(
         '}'))
     c.Eblock('}')
     c.Eblock('}')
     c.Substitute({
         'cpp_namespace':
         cpp_namespace,
         'arg':
         cpp_util.GetParameterDeclaration(
             prop, self._cpp_type_generator.GetType(prop))
     })
     return c
Ejemplo n.º 3
0
  def _GenerateFunctionResult(self, function):
    """Generates functions for passing a function's result back.
    """
    c = Code()

    c.Sblock('namespace Result {')
    params = function.callback.params
    if not params:
      c.Append('Value* Create();')
    else:
      c.Concat(self._GeneratePropertyStructures(params))

      # If there is a single parameter, this is straightforward. However, if
      # the callback parameter is of 'choices', this generates a Create method
      # for each choice. This works because only 1 choice can be returned at a
      # time.
      for param in self._cpp_type_generator.GetExpandedChoicesInParams(params):
        if param.description:
          c.Comment(param.description)
        if param.type_ == PropertyType.ANY:
          c.Comment("Value* Result::Create(Value*) not generated "
                    "because it's redundant.")
          continue
        c.Append('Value* Create(const %s);' % cpp_util.GetParameterDeclaration(
            param, self._cpp_type_generator.GetType(param)))
    c.Eblock('};')

    return c
Ejemplo n.º 4
0
  def _GenerateCreateCallbackArguments(self, function):
    """Generates functions for passing parameters to a callback.
    """
    c = Code()
    params = function.params
    c.Cblock(self._GenerateTypes((p.type_ for p in params), is_toplevel=True))

    declaration_list = []
    for param in params:
      if param.description:
        c.Comment(param.description)
      declaration_list.append(cpp_util.GetParameterDeclaration(
          param, self._type_helper.GetCppType(param.type_)))
    c.Append('scoped_ptr<base::ListValue> Create(%s);' %
             ', '.join(declaration_list))
    return c
Ejemplo n.º 5
0
    def _GenerateFunctionResultCreate(self, cpp_namespace, function):
        """Generate function to create a Result given the return value.

    E.g for function "Bar", generate Bar::Result::Create
    """
        c = Code()
        params = function.callback.params

        if not params:
            (c.Append('Value* %s::Result::Create() {' % cpp_namespace).Append(
                '  return Value::CreateNullValue();').Append('}'))
        else:
            expanded_params = self._cpp_type_generator.GetExpandedChoicesInParams(
                params)
            c.Concat(
                self._GeneratePropertyFunctions(cpp_namespace + '::Result',
                                                expanded_params))

            # If there is a single parameter, this is straightforward. However, if
            # the callback parameter is of 'choices', this generates a Create method
            # for each choice. This works because only 1 choice can be returned at a
            # time.
            for param in expanded_params:
                if param.type_ == PropertyType.ANY:
                    # Generation of Value* Create(Value*) is redundant.
                    continue
                # We treat this argument as 'required' to avoid wrapping it in a
                # scoped_ptr if it's optional.
                param_copy = param.Copy()
                param_copy.optional = False
                c.Sblock(
                    'Value* %(cpp_namespace)s::Result::Create(const %(arg)s) {'
                )
                c.Append('return %s;' % self._CreateValueFromProperty(
                    param_copy, param_copy.unix_name))
                c.Eblock('}')
                c.Substitute({
                    'cpp_namespace':
                    cpp_namespace,
                    'arg':
                    cpp_util.GetParameterDeclaration(
                        param_copy,
                        self._cpp_type_generator.GetType(param_copy))
                })

        return c
Ejemplo n.º 6
0
    def _GenerateAsyncResponseArguments(self, params):
        """Generates a function to create the arguments to pass as results to a
    function callback, promise or event details.
    """
        c = Code()
        c.Cblock(
            self._GenerateTypes((p.type_ for p in params), is_toplevel=True))

        declaration_list = []
        for param in params:
            if param.description:
                c.Comment(param.description)
            declaration_list.append(
                cpp_util.GetParameterDeclaration(
                    param, self._type_helper.GetCppType(param.type_)))
        c.Append('std::unique_ptr<base::ListValue> Create(%s);' %
                 ', '.join(declaration_list))
        return c
Ejemplo n.º 7
0
  def _GenerateCreateCallbackArguments(self, function, generate_to_json=False):
    """Generates functions for passing paramaters to a callback.
    """
    c = Code()
    params = function.params
    c.Concat(self._GeneratePropertyStructures(params))

    param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params)
    for param_list in param_lists:
      declaration_list = []
      for param in param_list:
        if param.description:
          c.Comment(param.description)
        declaration_list.append('const %s' % cpp_util.GetParameterDeclaration(
            param, self._cpp_type_generator.GetCompiledType(param)))
      c.Append('scoped_ptr<base::ListValue> Create(%s);' %
               ', '.join(declaration_list))
      if generate_to_json:
        c.Append('std::string ToJson(%s);' % ', '.join(declaration_list))
    return c
Ejemplo n.º 8
0
  def _GenerateCreateCallbackArguments(self, function_scope, callback):
    """Generate all functions to create Value parameters for a callback.

    E.g for function "Bar", generate Bar::Results::Create
    E.g for event "Baz", generate Baz::Create

    function_scope: the function scope path, e.g. Foo::Bar for the function
    Foo::Bar::Baz().
    callback: the Function object we are creating callback arguments for.
    """
    c = Code()
    params = callback.params
    expanded_params = self._cpp_type_generator.ExpandParams(params)
    c.Concat(self._GeneratePropertyFunctions(function_scope, expanded_params))

    param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(params)
    for param_list in param_lists:
      (c.Sblock('scoped_ptr<base::ListValue> %(function_scope)s::'
                'Create(%(declaration_list)s) {')
        .Append('scoped_ptr<base::ListValue> create_results('
                'new base::ListValue());')
      )
      declaration_list = []
      for param in param_list:
        # We treat this argument as 'required' to avoid wrapping it in a
        # scoped_ptr if it's optional.
        param_copy = param.Copy()
        param_copy.optional = False
        c.Append('create_results->Append(%s);' %
            self._CreateValueFromProperty(param_copy, param_copy.unix_name))
        declaration_list.append("const %s" % cpp_util.GetParameterDeclaration(
            param_copy, self._cpp_type_generator.GetType(param_copy)))

      c.Append('return create_results.Pass();')
      c.Eblock('}')
      c.Substitute({
          'function_scope': function_scope,
          'declaration_list': ', '.join(declaration_list)
      })

    return c
Ejemplo n.º 9
0
 def _GenerateCreateEnumValue(self, cpp_namespace, prop):
     """Generates CreateEnumValue() that returns the base::StringValue
 representation of an enum.
 """
     c = Code()
     (c.Append('// static').Sblock(
         'scoped_ptr<base::Value> %(cpp_namespace)s::CreateEnumValue('
         '%(arg)s) {').Append(
             'std::string enum_temp = ToString(%s);' %
             prop.unix_name).Append('if (enum_temp.empty())').Append(
                 '  return scoped_ptr<base::Value>();').Append(
                     'return scoped_ptr<base::Value>('
                     'base::Value::CreateStringValue(enum_temp));').Eblock(
                         '}').Substitute({
                             'cpp_namespace':
                             cpp_namespace,
                             'arg':
                             cpp_util.GetParameterDeclaration(
                                 prop,
                                 self._cpp_type_generator.GetType(prop))
                         }))
     return c
Ejemplo n.º 10
0
    def _GenerateCreateCallbackArguments(self,
                                         function_scope,
                                         callback,
                                         generate_to_json=False):
        """Generate all functions to create Value parameters for a callback.

    E.g for function "Bar", generate Bar::Results::Create
    E.g for event "Baz", generate Baz::Create

    function_scope: the function scope path, e.g. Foo::Bar for the function
    Foo::Bar::Baz().
    callback: the Function object we are creating callback arguments for.
    generate_to_json: Generate a ToJson method.
    """
        c = Code()
        params = callback.params
        expanded_params = self._cpp_type_generator.ExpandParams(params)
        c.Concat(
            self._GeneratePropertyFunctions(function_scope, expanded_params))

        param_lists = self._cpp_type_generator.GetAllPossibleParameterLists(
            params)
        for param_list in param_lists:
            (c.Sblock('scoped_ptr<base::ListValue> %(function_scope)s::'
                      'Create(%(declaration_list)s) {').Append(
                          'scoped_ptr<base::ListValue> create_results('
                          'new base::ListValue());'))
            declaration_list = []
            for param in param_list:
                # We treat this argument as 'required' to avoid wrapping it in a
                # scoped_ptr if it's optional.
                param_copy = param.Copy()
                param_copy.optional = False
                declaration_list.append(
                    "const %s" % cpp_util.GetParameterDeclaration(
                        param_copy,
                        self._cpp_type_generator.GetCompiledType(param_copy)))
                param_name = param_copy.unix_name
                if param_copy.type_ != param_copy.compiled_type:
                    param_name = 'temp_' + param_name
                    (c.Append('%s %s;' % (self._cpp_type_generator.GetType(
                        param_copy), param_name)).Append(
                            cpp_util.GenerateCompiledTypeToTypeConversion(
                                param_copy, param_copy.unix_name, param_name) +
                            ';'))
                c.Append('create_results->Append(%s);' %
                         self._CreateValueFromProperty(param_copy, param_name))

            c.Append('return create_results.Pass();')
            c.Eblock('}')
            if generate_to_json:
                c.Append()
                (c.Sblock('std::string %(function_scope)s::'
                          'ToJson(%(declaration_list)s) {').Append(
                              'scoped_ptr<base::ListValue> create_results = '
                              '%(function_scope)s::Create(%(param_list)s);').
                 Append('std::string json;').Append(
                     'base::JSONWriter::Write(create_results.get(), &json);').
                 Append('return json;'))
                c.Eblock('}')

            c.Substitute({
                'function_scope':
                function_scope,
                'declaration_list':
                ', '.join(declaration_list),
                'param_list':
                ', '.join(param.unix_name for param in param_list)
            })

        return c