您的位置:首页 > 健康 > 美食 > C++自定义接口类设计器之可对称赋值三

C++自定义接口类设计器之可对称赋值三

2024/12/23 16:01:26 来源:https://blog.csdn.net/qq_30220519/article/details/140881624  浏览:    关键词:C++自定义接口类设计器之可对称赋值三
关键代码
   QStringList newLines;for (const auto& line : lines) {auto equalIndex = line.indexOf("=");if(-1 != equalIndex) {// a = b; 赋值auto var = line.mid(0, equalIndex).trimmed();auto value = line.mid(equalIndex + 1).trimmed();if(value.endsWith(");")) { // a = b.get(); 赋值value.replace("get", "set");value.insert(value.size() - 2, var);} else if(value.endsWith(";")) {value.chop(1);value += " = " + var;}newLines << value;} else {auto paramStartIndex = line.indexOf('(');auto paramEndIndex = line.lastIndexOf(')');if(-1 == paramStartIndex || -1 == paramEndIndex)continue;// 创建正则表达式  a->set(b.c.d);QString pattern = R"(\w+(\.|->)set\w+\((\w+\.*)+\);)";QRegularExpression regex(pattern);QRegularExpressionMatch matchor = regex.match(line);if (matchor.hasMatch()) {// 获取匹配的内容auto text = matchor.capturedTexts();text << matchor.captured(0);    // linetext << matchor.captured(1);    // objtext << matchor.captured(2);    // functext << matchor.captured(3);    // paramauto var_name = line.mid(0, paramStartIndex);var_name.replace("set", "get");var_name += "()";auto param_name = line.mid(paramStartIndex + 1, paramEndIndex - paramStartIndex - 1);param_name += " = " + var_name + ";";newLines << param_name;}// b->get() (\\w+)(->|\\.)\\w+(\\(|$)// 创建正则表达式 a->set(b.get());pattern = R"(\w+(->|\.)set\w+\(((\w+)(->|\.))*get\w+\(\)\);)";regex.setPattern(pattern);// 查找匹配项matchor = regex.match(line);if (matchor.hasMatch()) {// 获取匹配的内容QString matchedText = matchor.captured(0);QString objectName = matchor.captured(1);QString functionName = matchor.captured(2);QString parameterFunctionCall = matchor.captured(3);auto var_name = line.mid(0, paramStartIndex);var_name.replace("set", "get");var_name += "()";auto param_name = line.mid(paramStartIndex + 1, paramEndIndex - paramStartIndex - 1);param_name.replace("get", "set");param_name.insert(param_name.size() - 1, var_name);param_name += ';';newLines << param_name;}}}
对称前

对称后

C++自定义接口类设计器之函数解析二-CSDN博客


创作不易,小小的支持一下吧!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com