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

标签 c++ type-conversion runtime

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

比如config中可能有type=double作为一行,可以通过程序的设置改为type=long double,并使用无需重新启动应用程序的变量类型。


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

最佳答案

您可能会使用的是 Factory Pattern连同 Strategy Pattern和适当的接口(interface)。沿线的东西:

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

希望你明白我的意思。


如评论中所述,您还可以使用命名空间中的独立函数,而不是上述抽象接口(interface)解决方案:

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++ - 通过配置文件在运行时选择变量类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40850311/

相关文章:

android - Qt 5.1 : QMultimedia 5. 0 in android for Camera

c++ - 为什么不能将 cout 与用户定义的转换一起使用到 std::string?

c++ - WxWidgets:简单的数学公式给出错误的结果?

python - 如何在 Python 中存储计算结果,这样我的程序就不会对同一事物进行两次计算?

c++ - 内核不等待事件

c++ - 获取 HDF5 ArrayType 中的数据类型

c++ - 递减for循环比递增循环更有效吗?

java - 需要帮助将 EBCDIC 转换为十六进制

c++ - KMP算法和LPS表构建的运行时间

.net - 是否可以在同一进程中加载​​两个版本的 .NET 运行时?