I have seen answers that deal with choosing a variable type, dynamically, at runtime(this, and this, and links from there on), but, even if some may go a bit over my head (fairly new to C++), is there a way to read the variable type from a config file and use it during runtime?
For example, the config may have
type=double
type=long double
double
long double
What you may be could use is a Factory Pattern along with the Strategy Pattern and an appropriate interface. Something along the lines:
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
}
};
I hope you get what I mean.