Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a global parameters object

Here's a common, simple task: Read configuration settings from a configuration file, save the settings (e.g. as a hash) in an object, access this object from various objects that need to access the configuration parameters.

I found this implementation for the ConfigFile class implementation and it works. My question is: what is the best way to make an instance of this class available from my other classes and be thread safe, avoid static initialization order fiasco, etc.

My current approach is to construct it in main() using

    // Read face detection related parameter values from the configuration file.
string configFileName = "detection_parameters.txt";
try {
    parameters = ConfigFile( configFileName );
}
catch(ConfigFile::file_not_found) {
    cerr << "configuration file not found: " << configFileName << endl;
    exit(-1);
}

and then make parameters a global variable. But I also read that singletons should be used instead of global variables. How can the singleton be instantiated with the file name?

This must be such a common task that I think there must be a generally accepted good way of doing it? I would appreciate if someone can point me to it.

Thanks, C

like image 244
recipriversexclusion Avatar asked Jul 08 '09 17:07

recipriversexclusion


People also ask

How do you add global parameters in Revit?

In the Manage Tab > Settings Panel, select the Global Parameters tool, and assign the value for the mounting height as needed per the projects specific Code Requirements.

What are global parameters in Revit?

Global parameters are specific to a single project file, but are not assigned to categories like project parameters. Global parameters can be simple values, values derived from equations, or values taken from the model using other global parameters. Manage tab Settings panel (Global Parameters)

What is global parameters in Informatica?

Parameters can be defined in global or local section. The Parameter defined under global can be used in any of the session defined in that parameter file. Like database connection, database user name, database password etc. The parameters defined under session are application for only that particular session only.


2 Answers

If you're going to roll-your-own, I would recommend using the Singleton design pattern for your configuration class. Have the class itself store a static pointer of its own type, and the constructor be private so one would be forced to use the static getter to get the one instance of the class.

so a mock-up (that may not compile, an is missing the fun Config functionality, but should illustrate the point)

class Config
{
public:
   static Config * getConfig();
   static void setConfigFileName(string filename);
private:
   Config();
   static string m_filename;
   static Config * m_configInstance;
};

In case I'm not being clear, the getConfig() would look at m_configInstance. If it isn't a valid one, then it would create one (has access to the private constructor) and store it in m_configInstance so every subsequent call would access the same one.

So your main() would use setConfigFileName(), then any class would just have to call Config::getConfig() and then call the operations on it. A lot cleaner than a standard global variable.

Blast - in the time I spent writing this, other people have suggested the singleton design pattern too. Ah well - hope the additional explanation helps.

like image 185
Marc Avatar answered Oct 31 '22 07:10

Marc


Have you looked at Boost's Program Options library?

like image 43
Michael Kristofik Avatar answered Oct 31 '22 07:10

Michael Kristofik