Ejemplo n.º 1
0
    def gen_java_class(self, struct):
        content = ''

        fields = struct['fields']
        if struct['options'][predef.PredefParseKVMode]:
            fields = structutil.get_struct_kv_fields(struct)

        content += '// %s, %s\n' % (struct['comment'], struct['file'])
        content += 'public class %s\n{\n' % struct['name']

        inner_class_done = False
        inner_typename = ''
        inner_var_name = ''
        inner_type_class = ''
        inner_field_names, inner_fields = structutil.get_inner_class_mapped_fields(
            struct)
        if len(inner_fields) > 0:
            content += self.gen_java_inner_class(struct)
            inner_type_class = struct["options"][predef.PredefInnerTypeClass]
            inner_var_name = struct["options"][predef.PredefInnerTypeName]

        vec_done = False
        vec_names, vec_name = structutil.get_vec_field_range(struct)

        max_name_len = strutil.max_field_length(fields, 'name', None)
        max_type_len = strutil.max_field_length(fields, 'original_type_name',
                                                lang.map_java_type)
        if len(inner_typename) > max_type_len:
            max_type_len = len(inner_typename)

        for field in fields:
            if not field['enable']:
                continue
            text = ''
            field_name = field['name']
            if field_name in inner_field_names:
                if not inner_class_done:
                    typename = "ArrayList<>();"
                    text += '    public List<%s> %s = new %s \n' % (
                        inner_type_class, inner_var_name, typename)
                    inner_class_done = True
            else:
                typename = lang.map_java_type(field['original_type_name'])
                assert typename != "", field['original_type_name']
                typename = strutil.pad_spaces(typename, max_type_len + 1)
                if field['name'] not in vec_names:
                    name = lang.name_with_default_java_value(field, typename)
                    name = strutil.pad_spaces(name, max_name_len + 8)
                    text += '    public %s %s // %s\n' % (typename, name,
                                                          field['comment'])
                elif not vec_done:
                    name = '%s = new %s[%d];' % (vec_name, typename.strip(),
                                                 len(vec_names))
                    name = strutil.pad_spaces(name, max_name_len + 8)
                    text += '    public %s[] %s // %s\n' % (
                        typename.strip(), name, field['comment'])
                    vec_done = True
            content += text

        return content
Ejemplo n.º 2
0
    def gen_cpp_struct_define(self, struct):
        content = '// %s\n' % struct['comment']
        content += 'struct %s \n{\n' % struct['name']

        inner_class_done = False
        inner_typename = ''
        inner_var_name = ''
        inner_field_names, mapped_inner_fields = structutil.get_inner_class_mapped_fields(
            struct)
        if len(mapped_inner_fields) > 0:
            content += self.gen_inner_struct_define(struct)
            inner_type_class = struct["options"][predef.PredefInnerTypeClass]
            inner_var_name = struct["options"][predef.PredefInnerTypeName]
            inner_typename = 'std::vector<%s>' % inner_type_class

        vec_done = False
        vec_names, vec_name = structutil.get_vec_field_range(struct)

        fields = struct['fields']
        max_name_len = strutil.max_field_length(fields, 'name', None)
        max_type_len = strutil.max_field_length(fields, 'original_type_name',
                                                lang.map_cpp_type)
        if len(inner_typename) > max_type_len:
            max_type_len = len(inner_typename)

        for field in fields:
            if not field['enable']:
                continue
            text = ''
            field_name = field['name']
            if field_name in inner_field_names:
                if not inner_class_done:
                    typename = strutil.pad_spaces(inner_typename,
                                                  max_type_len + 1)
                    name = strutil.pad_spaces(inner_var_name, max_name_len + 8)
                    text += '    %s %s; //\n' % (typename, name)
                    inner_class_done = True

            else:
                typename = lang.map_cpp_type(field['original_type_name'])
                assert typename != "", field['original_type_name']
                typename = strutil.pad_spaces(typename, max_type_len + 1)
                if field_name not in vec_names:
                    name = lang.name_with_default_cpp_value(field, typename)
                    name = strutil.pad_spaces(name, max_name_len + 8)
                    text += '    %s %s // %s\n' % (typename, name,
                                                   field['comment'])
                elif not vec_done:
                    name = '%s[%d];' % (vec_name, len(vec_names))
                    name = strutil.pad_spaces(name, max_name_len + 8)
                    text += '    %s %s // %s\n' % (typename, name,
                                                   field['comment'])
                    vec_done = True
            content += text
        return content
Ejemplo n.º 3
0
 def gen_cs_inner_class(struct):
     content = ''
     class_name = struct["options"][predef.PredefInnerTypeClass]
     inner_fields = structutil.get_inner_class_struct_fields(struct)
     content += 'public class %s \n' % class_name
     content += '{\n'
     max_name_len = strutil.max_field_length(inner_fields, 'name', None)
     max_type_len = strutil.max_field_length(inner_fields,
                                             'original_type_name',
                                             lang.map_cs_type)
     for field in inner_fields:
         typename = lang.map_cs_type(field['original_type_name'])
         assert typename != "", field['original_type_name']
         typename = strutil.pad_spaces(typename, max_type_len + 1)
         name = lang.name_with_default_cs_value(field, typename)
         name = strutil.pad_spaces(name, max_name_len + 8)
         content += '    public %s %s // %s\n' % (typename.strip(), name,
                                                  field['comment'])
     content += '};\n\n'
     return content