关键代码
QStringList multis = templateStr.split('\n');
bool startConfig = false;
bool startVar = false;
bool startTemplate = false;
for (const auto& line : multis) {
if(startConfig) {
if(line.trimmed().startsWith("camealCase")) {
auto name_val = line.split('=');
if(name_val.empty())
continue;
auto val = name_val.back();
m_isCamelCase = ("true" == val.trimmed());
}
}
if(startVar) {
if(line.trimmed().startsWith("scopeName")) {
auto name_val = line.split('=');
if(name_val.empty())
continue;
m_scopeName = name_val.back().trimmed();
} else if(line.trimmed().startsWith("funcName")) {
auto name_val = line.split('=');
if(name_val.empty())
continue;
m_funcName = name_val.back().trimmed();
} else if(line.trimmed().startsWith("returnType")) {
auto name_val = line.split('=');
if(name_val.empty())
continue;
m_returnType = name_val.back().trimmed();
} else if(line.trimmed().startsWith("defaultReturn")) {
auto name_val = line.split('=');
if(name_val.empty())
continue;
m_defalutValue = name_val.back().trimmed();
} else if(line.trimmed().startsWith("paramType")) {
auto name_val = line.split('=');
if(name_val.empty())
continue;
m_paramType = name_val.back().trimmed();
}
}
if(startTemplate) {
auto replaceText = line;
if(auto index = line.indexOf("${returnType}"); -1 != index) {
replaceText.replace("${returnType}", m_returnType);
}
if(auto index = line.indexOf("${scopeName}"); -1 != index) {
if(m_isCamelCase) {
auto preChar = line.at(index - 1);
auto newScopeName = m_scopeName;
if(QChar(' ') == preChar)
newScopeName.replace(0, 1, newScopeName.at(0).toUpper());
else
newScopeName.replace(0, 1, newScopeName.at(0).toLower());
replaceText.replace("${scopeName}", newScopeName);
} else {
replaceText.replace("${scopeName}", m_scopeName);
}
}
if(auto index = line.indexOf("${funcName}"); -1 != index) {
if(m_isCamelCase) {
auto preChar = line.at(index - 1);
auto newFuncName = m_funcName;
if(QChar(' ') == preChar)
newFuncName.replace(0, 1, newFuncName.at(0).toLower());
else
newFuncName.replace(0, 1, newFuncName.at(0).toUpper());
replaceText.replace("${funcName}", newFuncName);
} else {
replaceText.replace("${funcName}", m_funcName);
}
}
if(auto index = line.indexOf("${paramType}"); -1 != index) {
replaceText.replace("${paramType}", m_paramType);
}
if(auto index = line.indexOf("${defaultReturn}"); -1 != index) {
replaceText.replace("${defaultReturn}", m_defalutValue);
}
m_template += replaceText + '\n';
}
if("[config]" == line.trimmed()) {
startConfig = true;
} else if("[var]" == line.trimmed()) {
startConfig = false;
startVar = true;
} else if("[template]" == line.trimmed()) {
startVar = false;
startTemplate = true;
}
}
模板配置
[config]
camealCase = true
[var]
scopeName = Test
funcName = genetate
returnType = std::string
defaultReturn = ""
[template]
${returnType} ${scopeName}::get${funcName}() const
{
auto node = getNode();
if (!node)
return ${defaultReturn};
${returnType} ${funcName} = node->${funcName}(); // 修改属性名
return ${funcName};
}
生成后
std::string Test::getGenetate() const
{
auto node = getNode();
if (!node)
return "";
std::string genetate = node->genetate(); // 修改属性名
return genetate;
}
C++ 模板代码自动生成_c++自动生成代码-CSDN博客
C++自定义接口类设计器之可对称赋值三-CSDN博客