func gen_clear_func(cfg *generator.Cfg_t) string { var text string = `%clear_func% {%clear_stmt% } ` var clear_stmt string if !cfg.Only_has_1_row() { clear_stmt += "\n m_" + get_vec_name(cfg) + ".clear();" if len(cfg.Keys) > 1 { clear_stmt += "\n m_" + get_n_key_map_name(cfg) + ".clear();" } for _, v := range cfg.Fields { if !v.Is_1_key() || v.Is_n_key() { continue } clear_stmt += "\n m_" + get_1_key_map_name(cfg, &v) + ".clear();" } } else { clear_stmt = "\n m_%cfg% = %cfgtype%();" clear_stmt = strings.Replace(clear_stmt, "%cfgtype%", get_cfg_type_name(cfg), -1) clear_stmt = strings.Replace(clear_stmt, "%cfg%", cfg.En_name, -1) } text = strings.Replace(text, "%clear_func%", get_clear_func_declare(cfg), -1) text = strings.Replace(text, "%clear_stmt%", clear_stmt, -1) return text }
func gen_find_func(cfg *generator.Cfg_t) string { if cfg.Only_has_1_row() { return "" } var text string text += get_comment(cfg) text += gen_n_key_find_func(cfg) + "\n" + gen_1_key_find_func(cfg) return text }
func gen_load_func(cfg *generator.Cfg_t) string { var text string = ` // %comment% %load_func_name% { tick_t load_tick; rapidxml::file<> fdoc(this->get_path("%cfg%.xml").c_str()); rapidxml::xml_document<> doc; if(!fdoc.data()){ std::cout << "err: load " << this->get_path("%cfg%.xml") << " failed, please check the file path" << std::endl; return false; } doc.parse<0>(fdoc.data()); rapidxml::xml_node<>* root = doc.first_node(); if(!root){ std::cout << "err: parse <%cfg%.xml> failed, please check xml file format" << std::endl; return false; } clear_%cfg%(); int n_row = 0; for(rapidxml::xml_node<> *node = root->first_node(); node; node = node->next_sibling()){ ++n_row; } %vec_reserve_stmt% %cfgtype% cfg; for(rapidxml::xml_node<> *node = root->first_node(); node; node = node->next_sibling()){ ` var vec_reserve_stmt string if !cfg.Only_has_1_row() { vec_reserve_stmt = "\n m_%vec%.reserve(n_row);\n" } text = strings.Replace(text, "%vec_reserve_stmt%", vec_reserve_stmt, -1) text = strings.Replace(text, "%cfg%", cfg.En_name, -1) text = strings.Replace(text, "%load_func_name%", get_load_func_declare(cfg), -1) text = strings.Replace(text, "%comment%", cfg.Cn_name, -1) text = strings.Replace(text, "%vec%", get_vec_name(cfg), -1) text = strings.Replace(text, "%cfgtype%", get_cfg_type_name(cfg), -1) var load_attr_stmt string var n_field int = len(cfg.Fields) for n, v := range cfg.Fields { load_attr_stmt = " " + gen_load_attr_statement(&v) + "\n" if n*2+1 > n_field { load_attr_stmt = strings.Replace(load_attr_stmt, "node->first_attribute", "node->last_attribute", -1) } text += load_attr_stmt } if !cfg.Only_has_1_row() { var vec_push_stmt string = ` m_%vec%.push_back(cfg); %cfgtype%* curcfg = &m_%vec%.back();` vec_push_stmt = strings.Replace(vec_push_stmt, "%vec%", get_vec_name(cfg), -1) vec_push_stmt = strings.Replace(vec_push_stmt, "%cfg%", cfg.En_name, -1) vec_push_stmt = strings.Replace(vec_push_stmt, "%cfgtype%", get_cfg_type_name(cfg), -1) var map_insert_stmt string = "\n " map_insert_stmt += gen_n_key_map_insert_stmt(cfg) map_insert_stmt += gen_1_key_map_insert_stmt(cfg) text += vec_push_stmt text += map_insert_stmt } else { var eval_stmt string = ` m_%cfg% = cfg; break; ` eval_stmt = strings.Replace(eval_stmt, "%cfg%", cfg.En_name, -1) text += eval_stmt } var end_stmt string = ` } printf("load <%-20s> success, cost time = <%-6f>s\n", "%cfg%.xml", load_tick.end_tick()); return true; } ` end_stmt = strings.Replace(end_stmt, "%cfg%", cfg.En_name, -1) text += end_stmt return text }