GPT帮我写了一份模板,我感觉好用的很,记录下来~!
同事给我提供了一套接口,在一个QWidget中显示各种结果数据,示例如下
class DataResultShowWidget : public QWidget {
public:
/***************更新数据子函数**********************/
void UpdateInt(const std::string& title, int* value, int size);
void UpdateDouble(const std::string& title, double* value, int size);
void UpdateFloat(const std::string& title, float* value, int size);
void UpdateChar(const std::string& title, char* value, int size);
void UpdateString(const std::string& title, std::string* value, int size);
void UpdateBool(const std::string& title, bool* value, int size);
void UpdateVLImage(const std::string& title, VLImage* value, int size);
void UpdateVLPoint(const std::string& title, VLPoint* value, int size);
void UpdateVLRect(const std::string& title, VLRect* value, int size);
void UpdateVLLine(const std::string& title, VLLine* value, int size);
void UpdateVLLineSegment(const std::string& title, VLLineSegment* value, int size);
void UpdateVLVariableRect(const std::string& title, VLVariableRect* value, int size);
void UpdateVLVarRectCaliper(const std::string& title, VLVarRectCaliper* value, int size);
void UpdateVLLinearTransform(const std::string& title, VLLinearTransform* value, int size);
void UpdateVLCircle(const std::string& title, const VLCircle* value, int size);
void UpdateVLCircularArc(const std::string& title, const VLCircularArc* value, int size);
void UpdateVLCircularRing(const std::string& title, const VLCircularRing* value, int size);
void UpdateVLCircularRingSegment(const std::string& title, const VLCircularRingSegment* value, int size);
void UpdateVLEllipse(const std::string& title, const VLEllipse* value, int size);
void UpdateVLEllipseRing(const std::string& title, const VLEllipseRing* value, int size);
void UpdateVLEllipseRingSegment(const std::string& title, const VLEllipseRingSegment* value, int size);
我大眼一看,这些接口长的挺有共性,同事的接口设计能力可见很强。
我需要实现 在插件appendData的时候,解析接口数据并传球给DataResultShowWidget 进行展示。
为了秀下技术,大体理了理思路,
于是这样做:
- 用hash表把参数类型和方法名对应关系存一下。
- 在appendData接口根据参数类型,将结果路由到不同的处理分支。
代码如下:
// 创建一个包含不同函数的 unordered_map
std::unordered_map<std::string, std::function<void(DataResultShowWidget*, const std::string&, void*, int)>> updateFunctions;
// 模板函数处理不同类型的数据
template <typename T>
void ProcessData(const std::string& protoType, const std::string& protoContent, const std::string& displayParamName
, DataResultShowWidget *dataResultWgt) {
const uint32_t arraySize_bigger = 12;
T* array_out = new T[arraySize_bigger]();
uint32_t ans = ProtoConvert::Instance()->ProtoDeserializeToLocal(protoType, array_out, protoContent.c_str(),
protoContent.length(), true, arraySize_bigger);
if (ans == 0) {
delete[] array_out;
return;
}
// 查找函数指针
auto updateFuncIt = updateFunctions.find(protoType);
if (updateFuncIt != updateFunctions.end()) {
auto updateFunc = updateFuncIt->second;
// 调用函数
updateFunc(dataResultWgt, displayParamName, array_out, ans);
}
delete[] array_out;
}
//创建值类型和方法的映射关系
void ElementDataResultShowWidget::initHash(){
updateFunctions["int"] = [](DataResultShowWidget * wgt, const std::string & title, void* value, int size) {
wgt->UpdateInt(title, reinterpret_cast<int*>(value), size);
};
updateFunctions["double"] = [](DataResultShowWidget * wgt, const std::string & title, void* value, int size) {
wgt->UpdateDouble(title, reinterpret_cast<double*>(value), size);
};
updateFunctions["float"] = [](DataResultShowWidget * wgt, const std::string & title, void* value, int size) {
wgt->UpdateFloat(title, reinterpret_cast<float*>(value), size);
};
updateFunctions["char"] = [](DataResultShowWidget * wgt, const std::string & title, void* value, int size) {
wgt->UpdateChar(title, reinterpret_cast<char*>(value), size);
};
updateFunctions["string"] = [](DataResultShowWidget * wgt, const std::string & title, void* value, int size) {
wgt->UpdateString(title, reinterpret_cast<std::string*>(value), size);
};
updateFunctions["bool"] = [](DataResultShowWidget * wgt, const std::string & title, void* value, int size) {
wgt->UpdateBool(title, reinterpret_cast<bool*>(value), size);
};
updateFunctions["VLPoint"] = [](DataResultShowWidget * wgt, const std::string & title, void* value, int size) {
wgt->UpdateVLPoint(title, reinterpret_cast<VLPoint*>(value), size);
};
updateFunctions["VLRect"] = [](DataResultShowWidget * wgt, const std::string & title, void* value, int size) {
wgt->UpdateVLRect(title, reinterpret_cast<VLRect*>(value), size);
};
updateFunctions["VLLine"] = [](DataResultShowWidget * wgt, const std::string & title, void* value, int size) {
wgt->UpdateVLLine(title, reinterpret_cast<VLLine*>(value), size);
};
updateFunctions["VLLineSegment"] = [](DataResultShowWidget * wgt, const std::string & title, void* value, int size) {
wgt->UpdateVLLineSegment(title, reinterpret_cast<VLLineSegment*>(value), size);
};
}
//业务处理
void ElementDataResultShowWidget::appendData(const std::list<ProtoData> &protoDatas) {
for(auto item : protoDatas) {
std::string protoType = item.protoType;
std::string protoContent = item.protoContent;
std::string displayParamName = item.displayParamName;
if (protoType == "int") {
ProcessData<int>(protoType, protoContent, displayParamName, dataResultWgt_);
} else if (protoType == "double") {
ProcessData<double>(protoType, protoContent, displayParamName, dataResultWgt_);
} else if (protoType == "float") {
ProcessData<float>(protoType, protoContent, displayParamName, dataResultWgt_);
} else if (protoType == "char") {
ProcessData<char>(protoType, protoContent, displayParamName, dataResultWgt_);
} else if (protoType == "std::string") {
ProcessData<std::string>(protoType, protoContent, displayParamName, dataResultWgt_);
} else if (protoType == "bool") {
ProcessData<bool>(protoType, protoContent, displayParamName, dataResultWgt_);
} else if (protoType == "VLPoint") {
ProcessData<VLPoint>(protoType, protoContent, displayParamName, dataResultWgt_);
} else if (protoType == "VLImage") {
ProcessData<VLImage>(protoType, protoContent, displayParamName, dataResultWgt_);
} else if (protoType == "VLRect") {
ProcessData<VLRect>(protoType, protoContent, displayParamName, dataResultWgt_);
} else if (protoType == "VLLine") {
ProcessData<VLLine>(protoType, protoContent, displayParamName, dataResultWgt_);
} else if (protoType == "VLLineSegment") {
ProcessData<VLLineSegment>(protoType, protoContent, displayParamName, dataResultWgt_);
}
}
}