solarpowerlog trunk
Handling the Configuration

Overview

Solarpowerlog uses a configuration file to get the settings of the individual components. It even decided out of the config file which components are needed ans wich objects needs to work together.

For easier handling of the configuration, solarpowerlog provides methods to gather the settings.

First, every object gets a so-called configuaration path. This is a string, describing where the configuration of the object is located. (As currently libconfig is used, this is the "path" of libconfig. Every object should safe this path.

Second, a helper class, CConfigHelper exists. This class has some methods allowing to extract the configuration very conveniently:

  • Check for existence of a key (including error reporting)
  • Check for the type of a key (including error reporting)
  • Retrieve the value
  • Handle "optional" values by specifying the optional value to be used if the configuration cannot be read.

Usage and Examples

Without many words, here are the some examples.

Example 1: Check for key type and existence.

     CConfigHelper hlp(configurationpath);
     // Required Key: Will set fail=true if non-existent or wrong type
     fail |= !hlp.CheckConfig("datasource", Setting::TypeString);
     // Optional key: Will set fail=truel only if clearscreen is not boolean.
     fail |= !hlp.CheckConfig("clearscreen", Setting::TypeBoolean,
          true);

Example 2: Retrieve key

     CConfigHelper cfghlp(configurationpath);
     float interval;
     // This sets the value to 5.0 if not in the configuration:
     cfghlp.GetConfig("queryinterval", interval, 5.0f);
     // This would just set the value. if not configured, the value is left
     // alone.
     cfghlp.GetConfig("queryinterval", interval);
Note:
C++ has strong types. So make sure, that everything matches. The above example would not compile, if you write "5.0" instead of "5.0f", as gcc would make a "double" and interval would be "float". This is also true for integers: int != unsigned int!