示例#1
0
    def gen_range_method(self, struct):
        if struct['options'][predef.PredefParseKVMode]:
            return ''

        if predef.PredefRangeMethodKeys not in struct['options']:
            return ''

        keys = genutil.get_struct_keys(struct, predef.PredefRangeMethodKeys, lang.map_cs_type)
        assert len(keys) > 0

        formal_param = []
        params = []
        arg_names = []
        for tpl in keys:
            typename = tpl[0]
            formal_param.append('%s %s' % (typename, tpl[1]))
            arg_names.append(tpl[1])

        content = ''
        content += '    // get a range of items by key\n'
        content += '    public static List<%s> GetRange(%s)\n' % (struct['name'], ', '.join(formal_param))
        content += '    {\n'
        content += '        var range = new List<%s>();\n' % struct['name']
        content += '        foreach (%s item in Data)\n' % struct['name']
        content += '        {\n'
        content += '            if (%s)\n' % self.gen_equal_stmt('item.', struct, 'range-keys')
        content += '            {\n'
        content += '                range.Add(item);\n'
        content += '            }\n'
        content += '        }\n'
        content += '        return range;\n'
        content += '    }\n\n'
        return content
示例#2
0
    def gen_get_method(self, struct):
        if struct['options'][predef.PredefParseKVMode]:
            return ''

        keys = genutil.get_struct_keys(struct, predef.PredefGetMethodKeys,
                                       lang.map_java_type)
        if len(keys) == 0:
            return ''

        formal_param = []
        arg_names = []
        for tpl in keys:
            typename = tpl[0]
            formal_param.append('%s %s' % (typename, tpl[1]))
            arg_names.append(tpl[1])

        content = ''
        content += '    // get an item by key\n'
        content += '    public static %s getItemBy(%s)\n' % (
            struct['name'], ', '.join(formal_param))
        content += '    {\n'
        content += '        for (%s item : data_)\n' % struct['name']
        content += '        {\n'
        content += '            if (%s)\n' % self.gen_equal_stmt(
            'item.', struct, 'get-keys')
        content += '            {\n'
        content += '                return item;\n'
        content += '            }\n'
        content += '        }\n'
        content += '        return null;\n'
        content += '    }\n\n'
        return content
示例#3
0
    def gen_struct_get_method(self, struct):
        content = ''
        if struct['options'][predef.PredefParseKVMode]:
            return content

        keys = genutil.get_struct_keys(struct, predef.PredefGetMethodKeys, lang.map_cpp_type)
        if len(keys) == 0:
            return content

        formal_param = []
        arg_names = []
        for tpl in keys:
            typename = tpl[0]
            if not lang.is_cpp_pod_type(typename):
                typename = 'const %s&' % typename
            formal_param.append('%s %s' % (typename, tpl[1]))
            arg_names.append(tpl[1])

        content += 'const %s* %s::Get(%s)\n' % (struct['name'], struct['name'], ', '.join(formal_param))
        content += '{\n'
        content += '    const vector<%s>* dataptr = GetData();\n' % struct['name']
        content += '    ASSERT(dataptr != nullptr && dataptr->size() > 0);\n'
        content += '    for (size_t i = 0; i < dataptr->size(); i++)\n'
        content += '    {\n'
        content += '        if (%s)\n' % self.gen_equal_stmt('dataptr->at(i).', struct, 'get-keys')
        content += '        {\n'
        content += '            return &dataptr->at(i);\n'
        content += '        }\n'
        content += '    }\n'
        content += '    return nullptr;\n'
        content += '}\n\n'
        return content
示例#4
0
    def gen_struct_method_declare(self, struct):
        content = ''

        if struct['options'][predef.PredefParseKVMode]:
            content += '    static int Load(const char* filepath);\n'
            content += '    static int ParseFromRows(const std::vector<std::vector<StringPiece>>& rows, %s* ptr);\n' % \
                       struct['name']
            content += '    static const %s* Instance();\n' % struct['name']
            return content

        content += '    static int Load(const char* filepath);\n'
        content += '    static int ParseFromRow(const std::vector<StringPiece>& row, %s* ptr);\n' % struct[
            'name']
        content += '    static const std::vector<%s>* GetData(); \n' % struct[
            'name']

        if predef.PredefGetMethodKeys in struct['options']:
            get_keys = genutil.get_struct_keys(struct,
                                               predef.PredefGetMethodKeys,
                                               lang.map_cpp_type)
            if len(get_keys) > 0:
                get_args = []
                for tpl in get_keys:
                    typename = tpl[0]
                    if not lang.is_cpp_pod_type(typename):
                        typename = 'const %s&' % typename
                    get_args.append(typename + ' ' + tpl[1])

                content += '    static const %s* Get(%s);\n' % (
                    struct['name'], ', '.join(get_args))

        if predef.PredefRangeMethodKeys in struct['options']:
            range_keys = genutil.get_struct_keys(struct,
                                                 predef.PredefRangeMethodKeys,
                                                 lang.map_cpp_type)
            range_args = []
            for tpl in range_keys:
                typename = tpl[0]
                if not lang.is_cpp_pod_type(typename):
                    typename = 'const %s&' % typename
                range_args.append(typename + ' ' + tpl[1])
            content += '    static std::vector<const %s*> GetRange(%s);\n' % (
                struct['name'], ', '.join(range_args))

        return content
示例#5
0
 def gen_equal_stmt(self, prefix, struct, key):
     keys = genutil.get_struct_keys(struct, key, lang.map_java_type)
     args = []
     for tpl in keys:
         if lang.is_java_primitive_type(tpl[0]):
             args.append('%s%s == %s' % (prefix, tpl[1], tpl[1]))
         else:
             args.append('%s%s.equals(%s)' % (prefix, tpl[1], tpl[1]))
     return ' && '.join(args)
示例#6
0
    def gen_struct_range_method(self, struct):
        content = ''
        if struct['options'][predef.PredefParseKVMode]:
            return content

        if predef.PredefRangeMethodKeys not in struct['options']:
            return content

        keys = genutil.get_struct_keys(struct, predef.PredefRangeMethodKeys,
                                       lang.map_cpp_type)
        assert len(keys) > 0

        formal_param = []
        params = []
        arg_names = []
        for tpl in keys:
            typename = tpl[0]
            if not lang.is_cpp_pod_type(typename):
                typename = 'const %s&' % typename
            formal_param.append('%s %s' % (typename, tpl[1]))
            arg_names.append(tpl[1])

        content += 'std::vector<const %s*> %s::GetRange(%s)\n' % (
            struct['name'], struct['name'], ', '.join(formal_param))
        content += '{\n'
        content += '    const vector<%s>* dataptr = GetData();\n' % struct[
            'name']
        content += '    std::vector<const %s*> range;\n' % struct['name']
        content += '    ASSERT(dataptr != nullptr && dataptr->size() > 0);\n'
        content += '    for (size_t i = 0; i < dataptr->size(); i++)\n'
        content += '    {\n'
        content += '        if (%s)\n' % self.gen_equal_stmt(
            'dataptr->at(i).', struct, 'range-keys')
        content += '        {\n'
        content += '            range.push_back(&dataptr->at(i));\n'
        content += '        }\n'
        content += '    }\n'
        content += '    return range;\n'
        content += '}\n\n'
        return content
示例#7
0
 def gen_equal_stmt(self, prefix, struct, key):
     keys = genutil.get_struct_keys(struct, key, lang.map_cs_type)
     args = []
     for tpl in keys:
         args.append('%s%s == %s' % (prefix, tpl[1], tpl[1]))
     return ' && '.join(args)