Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages | Examples

configExample.cpp

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 #include "Aria.h"
00027 
00038 class ConfigExample
00039 {
00040   ArConfig* myConfig;
00041   int myIntParam;
00042   double myDoubleParam;
00043   bool myBoolParam;
00044   char myStringParam[256];
00045   ArRetFunctorC<bool, ConfigExample> myProcessConfigCB;
00046 
00047 public:
00048   ConfigExample():
00049     myIntParam(0),
00050     myDoubleParam(0.5),
00051     myBoolParam(false),
00052     myProcessConfigCB(this, &ConfigExample::processConfigFile)
00053   {
00054     // The global Aria class contains an ArConfig object.  You can create
00055     // other instances of ArConfig, but this is how you can share one ArConfig
00056     // among various program modules.
00057     // If you want to store a config parameter in ArConfig, first you must add 
00058     // it to the ArConfig object.  Parameters are stored in sections, and
00059     // they affect a variable via a pointer provided in an ArConfigArg
00060     // object:
00061     ArConfig* config = Aria::getConfig();
00062     config->setSectionComment("Example Section", "Contains parameters created by the configExample");
00063 
00064     // Add an integer which ranges from -10 to 10:
00065     config->addParam( ArConfigArg("ExampleIntegerParameter", &myIntParam, "Example parameter integer.", -10, 10), "Example Section", ArPriority::NORMAL);
00066     
00067     // Add a floating point number which ranges from 0.0 to 1.0:
00068     config->addParam( ArConfigArg("ExampleDoubleParameter", &myDoubleParam, "Example double precision floating point number.", 0.0, 1.0), "Example Section", ArPriority::NORMAL);
00069 
00070     // Essential parameters can be placed in the "Important" priority level:
00071     config->addParam( ArConfigArg("ExampleBoolParameter", &myBoolParam, "Example boolean parameter."), "Example Section", ArPriority::IMPORTANT);
00072 
00073     // Unimportant parameters can be placed in the "Trivial" priority level:
00074     myStringParam[0] = '\0';  // make string empty
00075     config->addParam( ArConfigArg("ExampleStringParameter", myStringParam, "Example string parameter.", 256), "Example Section", ArPriority::TRIVIAL);
00076 
00077     // You can set a callback to be invoked when the configuration changes, in
00078     // case you need to respond to any changes in the parameter values:
00079     config->addProcessFileCB(&myProcessConfigCB, 0);
00080   }
00081 
00082   
00083   // Method called by config process callback when a new file is loaded.
00084   // It can return false to indicate an error, or true to indicate no error.
00085   bool processConfigFile() 
00086   {
00087     ArLog::log(ArLog::Normal, "configExample: Config changed. New values: int=%d, float=%f, bool=%s, string=\"%s\".", myIntParam, myDoubleParam, myBoolParam?"true":"false", myStringParam);
00088     return true;
00089   }
00090 };
00091   
00092 int main(int argc, char **argv)
00093 {
00094   Aria::init();
00095   ArArgumentParser argParser(&argc, argv);
00096   argParser.loadDefaultArguments();
00097   if (argc < 2 || !Aria::parseArgs() || argParser.checkArgument("-help"))
00098   {
00099     ArLog::log(ArLog::Terse, "configExample usage: configExample <config file>.\nFor example, \"configExample examples/configExample.cfg\".");
00100     Aria::logOptions();
00101     Aria::shutdown();
00102     return 1;
00103   }
00104   
00105   // Object containing config parameters, and responding to changes:
00106   ConfigExample configExample;
00107 
00108   // Load a config file given on the command line into the global 
00109   // ArConfig object kept by Aria.  Normally ArConfig expects config
00110   // files to be in the main ARIA directory (i.e. /usr/local/Aria or
00111   // a directory specified by the $ARIA environment variable).
00112   char error[512];
00113   const char* filename = argParser.getArg(1);
00114   ArConfig* config = Aria::getConfig();
00115   ArLog::log(ArLog::Normal, "configExample: loading configuration file \"%s\"...", filename);
00116   if (! config->parseFile(filename, true, false, error, 512) )
00117   {
00118     ArLog::log(ArLog::Terse, "configExample: Error loading configuration file \"%s\" %s. Try \"examples/configExample.cfg\".", filename, error);
00119     Aria::shutdown();
00120     return -1;
00121   }
00122 
00123   ArLog::log(ArLog::Normal, "configExample: Loaded configuration file \"%s\".", filename);
00124   
00125   // After changing a config value, you should invoke the callbacks:
00126   ArConfigSection* section = config->findSection("Example Section");
00127   if (section)
00128   {
00129     ArConfigArg* arg = section->findParam("ExampleBoolParameter");
00130     if (arg)
00131     {
00132       arg->setBool(!arg->getBool());
00133       if (! config->callProcessFileCallBacks(false, error, 512) )
00134       {
00135         ArLog::log(ArLog::Terse, "configExample: Error processing modified config: %s.", error);
00136       }
00137       else
00138       {
00139         ArLog::log(ArLog::Normal, "configExample: Successfully modified config and invoked callbacks.");
00140       }
00141     }
00142   }
00143 
00144   // You can save the configuration as well:
00145   ArLog::log(ArLog::Normal, "configExample: Saving configuration...");
00146   if(!config->writeFile(filename))
00147   {
00148     ArLog::log(ArLog::Terse, "configExample: Error saving configuration to file \"%s\"!", filename);
00149   }
00150 
00151   // end of program.
00152   ArLog::log(ArLog::Normal, "configExample: end of program.");
00153   Aria::shutdown();
00154   return 0;
00155 }

Generated on Tue Feb 20 10:51:42 2007 for Aria by  doxygen 1.4.0