def Variable(self, parent_section, scope, obj):
        """Generates the code for a Variable definition.

    This function will generate the member/global variable declaration, as well
    as the setter/getter functions if specified in the attributes.

    Args:
      parent_section: the main section of the parent scope.
      scope: the parent scope.
      obj: the Variable definition.

    Returns:
      a list of (boolean, Definition) pairs, of all the types that need
      to be declared (boolean is False) or defined (boolean is True) before
      this definition.
    """
        if obj.parent.defn_type == 'Class':
            if 'field_access' in obj.attributes:
                member_section = parent_section.GetSection(
                    obj.attributes['field_access'] + ':')
            else:
                member_section = parent_section.GetSection('private:')
        else:
            member_section = parent_section
        getter_section = self.GetSectionFromAttributes(parent_section, obj)

        bm = obj.type_defn.binding_model
        type_string, need_defn = bm.CppMemberString(scope, obj.type_defn)
        check_types = [(need_defn, obj.type_defn)]
        if 'static' in obj.attributes:
            static = 'static '
        else:
            static = ''
        field_name = naming.Normalize(obj.name, naming.Java)
        self.Documentation(member_section, scope, obj)
        member_section.EmitCode('%s%s %s;' % (static, type_string, field_name))
        if 'getter' in obj.attributes:
            return_type, need_defn = bm.CppReturnValueString(
                scope, obj.type_defn)
            check_types += [(need_defn, obj.type_defn)]
            getter_name = cpp_utils.GetGetterName(obj)
            self.FieldFunctionDocumentation(getter_section, 'Accessor',
                                            type_string, field_name)
            getter_section.EmitCode(
                '%s%s %s() const { return %s; }' %
                (static, return_type, getter_name, field_name))
        if 'setter' in obj.attributes:
            param_type, need_defn = bm.CppParameterString(scope, obj.type_defn)
            check_types += [(need_defn, obj.type_defn)]
            setter_name = cpp_utils.GetSetterName(obj)
            self.FieldFunctionDocumentation(getter_section, 'Mutator',
                                            type_string, field_name)
            getter_section.EmitCode('%svoid %s(%s %s) { %s = %s; }' %
                                    (static, setter_name, param_type, obj.name,
                                     field_name, obj.name))
        return check_types
Exemplo n.º 2
0
  def Variable(self, context, obj):
    """Generates the code for a Variable definition.

    This function will generate the member/global variable declaration, as well
    as the setter/getter functions if specified in the attributes.

    Args:
      parent_section: the main section of the parent scope.
      obj: the Variable definition.
    """
    bm = obj.type.binding_model
    type_string, need_defn = bm.CppMemberString(context.header_scope, obj.type)
    context.CheckType(need_defn, obj.type)
    need_glue = self.NeedsGlue(obj) or (obj.parent.is_type and
                                        self.NeedsGlue(obj.parent));

    getter_attributes = {}
    if 'static' in obj.attributes:
      getter_attributes['static'] = obj.attributes['static']
      static = 'static '
    else:
      static = ''
    for attr in ['public', 'protected', 'private']:
      if attr in obj.attributes:
        getter_attributes[attr] = obj.attributes[attr]

    if not need_glue:
      if obj.parent.defn_type == 'Class':
        if 'field_access' in obj.attributes:
          member_section = context.header_section.GetSection(
              obj.attributes['field_access'] + ':')
        else:
          member_section = context.header_section.GetSection('private:')
      else:
        member_section = context.header_section
      field_name = naming.Normalize(obj.name, naming.LowerTrailing)
      member_section.EmitCode('%s%s %s;' % (static, type_string, field_name))

    if 'getter' in obj.attributes:
      func = obj.MakeGetter(getter_attributes, cpp_utils.GetGetterName(obj))
      if need_glue:
        self.FunctionGlue(context, func)
        impl = None
      else:
        impl = ' { return %s; }' % field_name
      self.FunctionDecl(context, func, impl)
    if 'setter' in obj.attributes:
      func = obj.MakeSetter(getter_attributes, cpp_utils.GetSetterName(obj))
      if need_glue:
        self.FunctionGlue(context, func)
        impl = None
      else:
        impl = ' { %s = %s; }' % (field_name, obj.name)
      self.FunctionDecl(context, func, impl)
Exemplo n.º 3
0
def CppSetStatic(scope, type_defn, field, param_expr):
    """Gets the representation of an expression setting a static field.

  Args:
    scope: a Definition for the scope in which the expression will be written.
    type_defn: a Definition, representing the type of the object containing the
      field being set.
    field: a string, the name of the field to be set.
    param_expr: a strings, being the expression for the value to be set.

  Returns:
    a string, which is the expression for setting the field.
  """
    return '%s::%s(%s)' % (cpp_utils.GetScopedName(
        scope, type_defn), cpp_utils.GetSetterName(field), param_expr)
Exemplo n.º 4
0
def CppSetField(scope, type_defn, object_expr, field, param_expr):
    """Gets the representation of an expression setting a field in an object.

  Args:
    scope: a Definition for the scope in which the expression will be written.
    type_defn: a Definition, representing the type of the object containing the
      field being set.
    object_expr: a string, which is the expression for the object containing
      the field being set.
    field: a string, the name of the field to be set.
    param_expr: a strings, being the expression for the value to be set.

  Returns:
    a string, which is the expression for setting the field.
  """
    (scope, type_defn) = (scope, type_defn)  # silence gpylint.
    return '%s->%s(%s)' % (object_expr, cpp_utils.GetSetterName(field),
                           param_expr)