Getter and Setter for a View
Getter and Setter for a View
我有一个getter和一个视图的设置器。目前,我正在传递一根字符串并检查它。并相应地设置数据。由于我有大约50个混合小部件,所以它变得笨拙。
您可以推荐比我正在使用的当前的解决方案更好的解决方案。
setter的代码
void wxfbIntegrationFrame::SetWidgetValue(wxString widget, wxString data)
{
if (widget.compare("hours") == 0) { hours->SetValue(data); }
else if (widget.compare("flowrateon") == 0) { flowrateon->SetValue(data); }
else if (widget.compare("pressureon") == 0) { pressureon->SetValue(data); }
else if (widget.compare("flowrateoff") == 0) { flowrateoff->SetValue(data); }
else if (widget.compare("pressureoff") == 0) { pressureoff->SetValue(data); }
else if (widget.compare("surveynumber") == 0) { surveynumber->SetValue(data); }
else if (widget.compare("warninglabel") == 0) { warninglabel->SetLabelText(data); }
}
getter的代码
int wxfbIntegrationFrame::GetWidgetValue(wxString widget)
{
long val;
if (widget.compare("surveyvalue") == 0)
{
return surveynumber->GetValue();
}
else if (widget.compare("xoffset") == 0)
{
wxString data = apply_offset_x->GetValue();
data.ToLong(&val);
return val;
}
else if (widget.compare("yoffset") == 0)
{
wxString data = apply_offset_y->GetValue();
data.ToLong(&val);
return val;
}
else if (widget.compare("countdowntimer") == 0)
{
return timercount->GetValue();
}
else if (widget.compare("sensornumber") == 0)
{
wxString data = sensornumber->GetValue();
data.ToLong(&val);
return val;
}
else if (widget.compare("temperatureselection") == 0)
{
return TempSelection->GetValue();
}
}
可以通过使用unordered_map
您只是在构造函数中初始化它:
#include <unordered_map>
#include <string>
#include <exception>
#include <functional>
class wxfbIntegrationFrame
{
public:
wxfbIntegrationFrame()
{
_setters["hours"] = [this](const wxString &data) { this->hours->SetValue(data); };
_setters["flowrateon"] = [this](const wxString &data) { this->flowrateon->SetValue(data); };
...
_getters["hours"] = [this](int &data){ data = this->hours->GetValue();};
}
void SetWidgetValue(const wxString &widget, const wxString &data)
{
try
{
_setters.at(widget)(data);
}
catch (const std::out_of_range &err)
{
whatever you want to do
}
}
int GetWidgetValue(const wxString &widget)
{
int data;
try
{
_getters.at(widget)(data);
return data;
}
catch (const std::out_of_range &err)
{
whatever you want to do
}
}
private:
std::unordered_map<std::string, std::function<void (const wxString &)> > _setters;
std::unordered_map<std::string, std::function<void (int &)> > _getters;
}
相关文章:
- "error: no matching function for call to"构造函数错误
- 表示"accepting anything for this template argument" C++概念的通配符
- 如何在C++中从两个单独的for循环中添加两个数组
- 在Linux for Windows上编译C++代码时出错
- 调用专用模板时出错"no matching function for call to [...]"
- 为什么我的for循环不能正确获取argv
- 为什么我不能在 FOR LOOP 中使用 i/10,C++?
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 在基于范围的for循环中使用结构化绑定声明
- 通过for循环使用用户输入填充列表
- 使用for循环检查数组中的重复项
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 正在使用for循环创建QScatterSerie
- Python中的for循环与C++有何不同
- std::memory_order for std::atomic:<T>:wait
- 在更改for循环的第三部分后,未使用for循环结果
- 在 for 循环中查找问题时遇到困难
- 嵌套for循环C++的问题(初学者)
- Getter and Setter for a View