Reputation: 2087
I have a c++ project which has multiple cpp files and functions.
Each file needs its own parameters (i.e., MaxHorizonVelocity, MaxVerticalVelocity etc.).
I want that these parameters won't be hard coded but read from a single configuration-parameters file at an initialization step.
For now I think to define a parameters class which will read the parameters at the begining and will be "static" (in some sense which I'm not sure about ...).
Is that considered a good practice? Is there another way?
Upvotes: 1
Views: 393
Reputation: 2701
I usually handle this problem in below way. The code itself is self-explanatory.
All configurable items must be derived from IConfigurable interface.
class IConfigurable {
public:
virtual void configure(XMLNode&) = 0;
};
Each configurable item assumes that configure function will be called and given XMLNode will be the root node in configuration xml file. After that each configurable item does specific parsing according to itself.
class CommandClick : public IConfigurable {
public:
void configure(XMLNode& xCommandNode) {
XMLNode xClickCoordinate = xCommandNode.getChildNode("Coordinate");
unsigned int x = atoi(xClickCoordinate.getAttribute("x"));
unsigned int y = atoi(xClickCoordinate.getAttribute("y"));
mClickCoordinate.setX(x);
mClickCoordinate.setY(y);
}
private:
Coordinate mClickCoordinate;
};
Upvotes: 2