MistyD
MistyD

Reputation: 17223

Migrating a header file to a config file

I currently have a header file with more than 20 defines in it. These defines set the properties of various objects. Now I must move these defines to a config file so that I do not have to rebuild the entire project when one of these values changes. I am currently thinking of having a static class whose static properties are set from reading from a config text file. Is there a better way to go? I know that I will have to write a whole text parser that will check for key values in the text files and then populate values. Are there any other ways? Suggestions for retrieving map value pairs?

Upvotes: 1

Views: 866

Answers (2)

Brent Bradburn
Brent Bradburn

Reputation: 54919

If you are just trying to speed up builds by minimizing re-compiles, you don't need to write your own parser. You can just write the configuration in C++.

Put the parameter declarations in an include file.

/* config.hpp */
class config
   {
public:
   static const int PARAM1;
   static const std::string PARAM2;
   static const double PARAM3;
   };

Edit and recompile the following C++ file when the configuration needs to change.

/* config.cpp */
const int config::PARAM1 = 1234;
const std::string config::PARAM2 = "hello";
const double config::PARAM3 = sin(M_PI/4);

Upvotes: 2

Scott Jones
Scott Jones

Reputation: 2908

If you're on Windows, an ini file and GetPrivateProfileString is quick and dirty.

Upvotes: 2

Related Questions