通过配置文件在运行时选择变量类型

Choose variable type at runtime through config file

本文关键字:选择 变量 类型 运行时 配置文件      更新时间:2023-10-16

我已经看到了在运行时动态选择变量类型的答案(此和此链接),但是,即使某些人可能会有所了解我的头(相当新的C ),是否有一种方法可以从配置文件中读取变量类型并在运行时使用它?

例如,配置可以将type=double作为行,可以通过程序的设置更改为type=long double,并在不重新启动应用程序的情况下使用该变量类型。


我需要计算某些多项式的根。有些人,即使是小订单,都需要大量的精度,如果他们达到高订单,则需要更大。尽管如此,我仍然可以使用足够小的数字来消除doublelong 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);
         }
};