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

Choose variable type at runtime through config file

我看到了有关在运行时动态选择变量类型的答案(this, and this,以及从那里开始的链接),但是,即使有些答案可能让我有点头疼(相当新到 C++),有没有办法从配置文件中读取变量类型并在运行时使用它?

比如config可能有type=double一行,可以通过程序的设置改为type=long double,不用重启应用程序就可以使用那个变量类型


我需要计算一些多项式的根。有些,即使是小订单,也需要很大的精度,如果是高订单,就需要更大的精度。不过,对于足够小的数字,我可以取消 doublelong double,但随着我变得更高,我需要 MPFRC++(这就是我目前正在唱的)。我想避免减速(即使它对于较小的订单来说并不那么明显)但只能在需要时才能够使用高精度。

您可能会使用的是 Factory Pattern along with the Strategy Pattern 和适当的界面。沿线的东西:

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
         }
};

除了使用配置文件外,您还可以考虑select使用其他条件作为配置文件的策略。

例如对于您的情况,多项式表达式的复杂性(即子项的数量)似乎对 select 使用最佳策略起着至关重要的作用。所以你可以写一些代码,比如

    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);
         }
};