通过配置文件在运行时选择变量类型
Choose variable type at runtime through config file
我已经看到了在运行时动态选择变量类型的答案(此和此链接),但是,即使某些人可能会有所了解我的头(相当新的C ),是否有一种方法可以从配置文件中读取变量类型并在运行时使用它?
例如,配置可以将type=double
作为行,可以通过程序的设置更改为type=long double
,并在不重新启动应用程序的情况下使用该变量类型。
我需要计算某些多项式的根。有些人,即使是小订单,都需要大量的精度,如果他们达到高订单,则需要更大。尽管如此,我仍然可以使用足够小的数字来消除double
或long double
,但是随着我的更高,我需要mpfrc (这是我目前正在唱歌的内容)。我想避免放慢速度(即使对于较小的订单而言并不明显)BU仅在需要时才能使用高精度。
您可能会使用的是工厂模式以及策略模式和适当的接口。沿线的东西:
struct IPolyRootSolver {
virtual PolyRoot calcRoot(const Polynom& poly) const = 0;
virtual ~IPolyRootSolver () {}
};
class DoublePolyRootSolver : public IPolyRootSolver {
public:
PolyRoot calcRoot(const Polynom& poly) {
// Implementation based on double precision
}
};
class LongDoublePolyRootSolver : public IPolyRootSolver {
public:
PolyRoot calcRoot(const Polynom& poly) {
// Implementation based on long double precision
}
};
class MPFRCPolyRootSolver : public IPolyRootSolver {
public:
PolyRoot calcRoot(const Polynom& poly) {
// Implementation based on MPFRC++ precision
}
};
class RootSolverFactory {
public:
RootSolverFactory(const std::string configFile) {
// Read config file and install a mechanism to watch for changes
}
std::unique_ptr<IPolyRootSolver> getConfiguredPolyRootSolver() {
if(config file contains type = double) {
return make_unique<IPolyRootSolver>(new DoublePolyRootSolver());
}
else if(config file contains type = long double) {
return make_unique<IPolyRootSolver>(new LongDoublePolyRootSolver());
}
else if(config file contains type = MPFRC) {
return make_unique<IPolyRootSolver>(new MPFRCPolyRootSolver ());
}
else {
// Handle the default case
}
};
我希望你能得到我的意思。
如注释中所述
namespace PolyRootDoublePrecision {
PolyRoot calcRoot(const Polynom&);
}
namespace PolyRootLongDoublePrecision {
PolyRoot calcRoot(const Polynom&);
}
namespace PolyRootMPFRCPrecision {
PolyRoot calcRoot(const Polynom&);
}
class RootSolverFactory {
public:
RootSolverFactory(const std::string configFile) {
// Read config file and install a mechanism to watch for changes
}
std::function<PolyRoot (const Polynom&)> getConfiguredPolyRootSolver() {
if(config file contains type = double) {
return std::function<PolyRoot (const Polynom&)>
(PolyRootDoublePrecision::calcRoot);
}
else if(config file contains type = long double) {
return std::function<PolyRoot (const Polynom&)>
(PolyRootLongDoublePrecision::calcRoot);
}
else if(config file contains type = MPFRC) {
return std::function<PolyRoot (const Polynom&)>
(PolyRootMPFRCPrecision::calcRoot);
}
else {
// Handle the default case
}
};
除了使用配置文件外,您还可以考虑选择从其他条件用作配置文件的策略。
例如。对于您的情况,多项式表达的复杂性(即子项数)似乎在选择最佳使用策略中起着至关重要的作用。因此,您可以编写一些代码,例如
std::function<PolyRoot (const Polynom&)> getPolyRootSolver(const Polynom& polynom) {
if(polynom.complexity() < 7) {
return std::function<PolyRoot (const Polynom&)>
(PolyRootDoublePrecision::calcRoot);
}
else if(polynom.complexity() >= 7 &&
polynom.complexity() < 50) {
return std::function<PolyRoot (const Polynom&)>
(PolyRootLongDoublePrecision::calcRoot);
}
else if(polynom.complexity() >= 50) {
return std::function<PolyRoot (const Polynom&)>
(PolyRootMPFRCPrecision::calcRoot);
}
};
相关文章:
- 在 c++ 中使用字符串变量选择静态类结构,而不带对象
- 使用指针选择长无符号变量中的数字
- 用于在运行时选择变量的 C++ 预处理器指令
- 运行时检查失败 #2 - 变量"数字选择"周围的堆栈已损坏
- 动态更改/选择类成员变量的类型
- Microsoft Visual Studio Intellisense未选择选定的变量
- 在编译时选择一个变量
- 有什么方法可以让用户选择要输入哪个变量
- 生成文件 :从变量中按扩展名选择文件
- 通过配置文件在运行时选择变量类型
- 成员变量的初始化必须延迟。此类的最佳设计选择是什么?
- 如何仅从输入文件中"20071406"的数字中选择"2007"的数字并将其存储在变量中?
- 在 C++ 中,我无法选择用于用户输入的特定变量
- 访问成员变量或访问成员函数是最佳选择
- C++具有无限可选参数和不同输入变量选择的函数
- 根据 c-tor 的特定条件选择要初始化的变量
- 使用if语句选择类型来声明变量C++
- 如何根据参数选择变量
- C++运行时选择变量类型
- "Refreshing"一个字符串变量,以便这个多项选择 while 循环不会无限重复