solarpowerlog trunk
/home/tobi/workspace/solarpowerlog/src/configuration/CConfigHelper.h
Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  solarpowerlog
00003  Copyright (C) 2009  Tobias Frost
00004 
00005  This file is part of solarpowerlog.
00006 
00007  Solarpowerlog is free software; However, it is dual-licenced
00008  as described in the file "COPYING".
00009 
00010  For this file (CConfigHelper.h), the license terms are:
00011 
00012  You can redistribute it and/or  modify it under the terms of the GNU Lesser
00013  General Public License (LGPL) as published by the Free Software Foundation;
00014  either version 3 of the License, or (at your option) any later version.
00015 
00016  This program is distributed in the hope that it will be useful, but
00017  WITHOUT ANY WARRANTY; without even the implied warranty of
00018  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  Lesser General Public License for more details.
00020 
00021  You should have received a copy of the GNU Library General Public
00022  License along with this proramm; if not, see
00023  <http://www.gnu.org/licenses/>.
00024  ----------------------------------------------------------------------------
00025  */
00026 
00089 #ifndef CCONFIGHELPER_H_
00090 #define CCONFIGHELPER_H_
00091 
00092 #include <string>
00093 #include <libconfig.h++>
00094 
00095 #include "configuration/Registry.h"
00096 
00097 using namespace std;
00098 
00110 class CConfigHelper
00111 {
00112 public:
00113      /***/
00114      CConfigHelper( const string& configurationpath );
00115      virtual ~CConfigHelper();
00116 
00117 // FIXME Document me
00133      bool CheckConfig( const string &setting, libconfig::Setting::Type type,
00134           bool optional = false, bool printerr = true );
00135 
00136      template<class T>
00137 
00141      bool GetConfig( string const &setting, T &store, T defvalue )
00142      {
00143           libconfig::Setting & set
00144                = Registry::Instance().GetSettingsForObject(cfgpath);
00145 
00146           if (!set.lookupValue(setting, store)) {
00147                store = defvalue;
00148                return false;
00149           }
00150           return true;
00151      }
00152 
00153      template<class T>
00154      bool GetConfig( char const *setting, T &store, const T defvalue )
00155      {
00156           string s = setting;
00157           return GetConfig(s, store, defvalue);
00158      }
00159 
00160      template<class T>
00161      bool GetConfig( const string &setting, T &store )
00162      {
00163           libconfig::Setting & set
00164                = Registry::Instance().GetSettingsForObject(cfgpath);
00165 
00166           if (!set.lookupValue(setting, store)) {
00167                return false;
00168           }
00169           return true;
00170      }
00171 
00172      template<class T>
00173      bool GetConfig( char const *setting, T &store )
00174      {
00175           string s = setting;
00176           return GetConfig(s, store);
00177      }
00178 
00193      template<class T>
00194      bool GetConfigArray( const string& setting, int index, T &store )
00195      {
00196           libconfig::Setting & set
00197                = Registry::Instance().GetSettingsForObject(cfgpath);
00198 
00199           try {
00200                store = set[setting][index];
00201                return true;
00202           } catch (libconfig::SettingNotFoundException e) {
00203                return false;
00204           } catch (libconfig::SettingTypeException e) {
00205                // TODO: Assert here?
00206                return false;
00207           }
00208      }
00209 
00210 
00227      bool GetConfigArray( const string& setting, int index, string &store )
00228      {
00229           libconfig::Setting & set
00230                = Registry::Instance().GetSettingsForObject(cfgpath);
00231 
00232           try {
00233                store = (const char *) set[setting][index];
00234                return true;
00235           } catch (libconfig::SettingNotFoundException e) {
00236                return false;
00237           } catch (libconfig::SettingTypeException e) {
00238                // TODO: Assert here?
00239                return false;
00240           }
00241      }
00242 
00243      // this ugly helper is for the CHTHML Writer -- we are having there
00244      // a list which embeddes a array. The entries are anonymous, that is without
00245      // a destinctive name.
00246      // I currently wonder how a bettter access function should look like ;-O)
00247      // TODO make this more elegant and reusable...
00248      //
00249      // so, well, this function looks up an entry in the embedded array,
00250      // with param i giving the row, index the column.
00251 template<class T>
00252      bool GetConfigArray( const int i, int index, T &store )
00253      {
00254           libconfig::Setting & set
00255                = Registry::Instance().GetSettingsForObject(cfgpath);
00256 
00257           try {
00258                store = (const char *) set[i][index];
00259                return true;
00260           } catch (libconfig::SettingNotFoundException e) {
00261                return false;
00262           } catch (libconfig::SettingTypeException e) {
00263                // TODO: Assert here?
00264                return false;
00265           }
00266      }
00267 
00268 bool GetConfigArray( const int i, int index, string &store )
00269 {
00270      libconfig::Setting & set
00271           = Registry::Instance().GetSettingsForObject(cfgpath);
00272 
00273      try {
00274           store = (const char *) set[i][index];
00275           return true;
00276      } catch (libconfig::SettingNotFoundException e) {
00277           return false;
00278      } catch (libconfig::SettingTypeException e) {
00279           // TODO: Assert here?
00280           return false;
00281      }
00282 }
00283 
00284 
00285 private:
00286      string cfgpath;
00287 
00288 };
00289 
00290 #endif /* CCONFIGHELPER_H_ */