configExample.cpp

Example program demonstrating the use of ArConfig

This program shows how to use ArConfig to store configuration parameters and load/save them from a file.

The ArNetworking library includes server classes that will let you use a remote client such as MobileEyes to view and change the configuration. See ArNetworking documentation and examples.

00001 #include "Aria.h"
00002 
00013 class ConfigExample
00014 {
00015   ArConfig* myConfig;
00016   int myIntParam;
00017   double myDoubleParam;
00018   bool myBoolParam;
00019   char myStringParam[256];
00020   ArRetFunctorC<bool, ConfigExample> myProcessConfigCB;
00021 
00022 public:
00023   ConfigExample():
00024     myIntParam(0),
00025     myDoubleParam(0.5),
00026     myBoolParam(false),
00027     myProcessConfigCB(this, &ConfigExample::processConfigFile)
00028   {
00029     // The global Aria class contains an ArConfig object.  You can create
00030     // other instances of ArConfig, but this is how you can share one ArConfig
00031     // among various program modules.
00032     // If you want to store a config parameter in ArConfig, first you must add 
00033     // it to the ArConfig object.  Parameters are stored in sections, and
00034     // they affect a variable via a pointer provided in an ArConfigArg
00035     // object:
00036     ArConfig* config = Aria::getConfig();
00037     config->setSectionComment("Example Section", "Contains parameters created by the configExample");
00038 
00039     // Add an integer which ranges from -10 to 10:
00040     config->addParam( ArConfigArg("ExampleIntegerParameter", &myIntParam, "Example parameter integer.", -10, 10), "Example Section", ArPriority::NORMAL);
00041     
00042     // Add a floating point number which ranges from 0.0 to 1.0:
00043     config->addParam( ArConfigArg("ExampleDoubleParameter", &myDoubleParam, "Example double precision floating point number.", 0.0, 1.0), "Example Section", ArPriority::NORMAL);
00044 
00045     // Essential parameters can be placed in the "Important" priority level:
00046     config->addParam( ArConfigArg("ExampleBoolParameter", &myBoolParam, "Example boolean parameter."), "Example Section", ArPriority::IMPORTANT);
00047 
00048     // Unimportant parameters can be placed in the "Trivial" priority level:
00049     myStringParam[0] = '\0';  // make string empty
00050     config->addParam( ArConfigArg("ExampleStringParameter", myStringParam, "Example string parameter.", 256), "Example Section", ArPriority::TRIVIAL);
00051 
00052     // You can set a callback to be invoked when the configuration changes, in
00053     // case you need to respond to any changes in the parameter values:
00054     config->addProcessFileCB(&myProcessConfigCB, 0);
00055   }
00056 
00057   
00058   // Method called by config process callback when a new file is loaded.
00059   // It can return false to indicate an error, or true to indicate no error.
00060   bool processConfigFile() 
00061   {
00062     ArLog::log(ArLog::Normal, "configExample: Config changed. New values: int=%d, float=%f, bool=%s, string=\"%s\".", myIntParam, myDoubleParam, myBoolParam?"true":"false", myStringParam);
00063     return true;
00064   }
00065 };
00066   
00067 int main(int argc, char **argv)
00068 {
00069   Aria::init();
00070   ArArgumentParser argParser(&argc, argv);
00071   argParser.loadDefaultArguments();
00072   if (argc < 2 || !Aria::parseArgs() || argParser.checkArgument("-help"))
00073   {
00074     ArLog::log(ArLog::Terse, "configExample usage: configExample <config file>.\nFor example, \"configExample examples/configExample.cfg\".");
00075     Aria::logOptions();
00076     Aria::shutdown();
00077     return 1;
00078   }
00079   
00080   // Object containing config parameters, and responding to changes:
00081   ConfigExample configExample;
00082 
00083   // Load a config file given on the command line into the global 
00084   // ArConfig object kept by Aria.  Normally ArConfig expects config
00085   // files to be in the main ARIA directory (i.e. /usr/local/Aria or
00086   // a directory specified by the $ARIA environment variable).
00087   char error[512];
00088   const char* filename = argParser.getArg(1);
00089   ArConfig* config = Aria::getConfig();
00090   ArLog::log(ArLog::Normal, "configExample: loading configuration file \"%s\"...", filename);
00091   if (! config->parseFile(filename, true, false, error, 512) )
00092   {
00093     ArLog::log(ArLog::Terse, "configExample: Error loading configuration file \"%s\" %s. Try \"examples/configExample.cfg\".", filename, error);
00094     Aria::shutdown();
00095     return -1;
00096   }
00097 
00098   ArLog::log(ArLog::Normal, "configExample: Loaded configuration file \"%s\".", filename);
00099   
00100   // After changing a config value, you should invoke the callbacks:
00101   ArConfigSection* section = config->findSection("Example Section");
00102   if (section)
00103   {
00104     ArConfigArg* arg = section->findParam("ExampleBoolParameter");
00105     if (arg)
00106     {
00107       arg->setBool(!arg->getBool());
00108       if (! config->callProcessFileCallBacks(false, error, 512) )
00109       {
00110         ArLog::log(ArLog::Terse, "configExample: Error processing modified config: %s.", error);
00111       }
00112       else
00113       {
00114         ArLog::log(ArLog::Normal, "configExample: Successfully modified config and invoked callbacks.");
00115       }
00116     }
00117   }
00118 
00119   // You can save the configuration as well:
00120   ArLog::log(ArLog::Normal, "configExample: Saving configuration...");
00121   if(!config->writeFile(filename))
00122   {
00123     ArLog::log(ArLog::Terse, "configExample: Error saving configuration to file \"%s\"!", filename);
00124   }
00125 
00126   // end of program.
00127   ArLog::log(ArLog::Normal, "configExample: end of program.");
00128   Aria::shutdown();
00129   return 0;
00130 }

Generated on Fri Jul 31 12:36:37 2009 for Aria by  doxygen 1.4.7