solarpowerlog trunk
/home/tobi/workspace/solarpowerlog/src/configuration/Registry.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 (Registry), 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 
00099 #ifndef REGISTRY_H_
00100 #define REGISTRY_H_
00101 
00102 #ifdef HAVE_CONFIG_H
00103 #include "config.h"
00104 #endif
00105 
00106 #include <assert.h>
00107 #include <list>
00108 #include <utility>
00109 #include <string>
00110 #include <libconfig.h++>
00111 
00112 #include <boost/noncopyable.hpp>
00113 #include "interfaces/CWorkScheduler.h"
00114 
00115 #include "configuration/ILogger.h"
00116 
00117 class IInverterBase;
00118 
00119 using namespace std;
00120 
00129 class Registry : boost::noncopyable
00130 {
00131 public:
00132 
00133      friend class CConfigHelper;
00134 
00136      static Registry& Instance()
00137      {
00138           static Registry Instance;
00139           return Instance;
00140      }
00141 
00142      // C O N F I G U R A T I O N
00143 
00149      inline static libconfig::Config* Configuration()
00150      {
00151           return Registry::Instance().Config;
00152      }
00153 
00154      inline static ILogger & GetMainLogger(void) {
00155           return Registry::Instance().l;
00156      };
00157 
00166      bool LoadConfig( std::string name );
00167 
00168      /* NOTE: This is obsolete! Use an Object of CConfigHelper to extract config!
00169       *
00170       *  Extract the settings-subset for a specific object,
00171       * identified by section (like inverters) and name (like inverter_solarmax_1)
00172       *
00173       * ex:
00174       *
00175       * inverters = (
00176       *                       { name = "Inverter_1";
00177       *                         type = "Solarmax_XYZ";
00178       *                         driver = "Sputnik_TCP";
00179       *                         # (...)
00180       *                       },
00181       *                       { name = "Inverter_2";
00182       *                         type = "Solarmax_XYZ";
00183       *                         driver = "Sputnik_TCP";
00184       *                         # (...)
00185       *                       }
00186       *                  );
00187       *
00188       * and  GetSettingsForObject("inverters", "Inverter_1") will return the Settings object
00189       * for the group "inverters.[0]".
00190       *
00191       * Please note, that libconfig throws some exceptions: Especially, if the section is not found.
00192       * This is not handled here, as the Factories should check if the configuration is complete on
00193       * constructing them. (They also can add their own settings (default values)...
00194       *
00195       * [code]
00196       *
00197       *   libconfig::Setting &set = Registry::Instance().GetSettingsForObject("inverters", "Inverter_1");
00198       *   libconfig::Setting &new = set.add("NewPropertyNotSet",libconfig::Setting::TypeString);
00199       *   new = "New Default Setting";
00200       *
00201       * [/code]
00202       *
00203       * Snippet to retrieve Settings:
00204       * [code]
00205       *        libconfig::Setting &set = Registry::Instance().GetSettingsForObject("inverters", "Inverter_1");
00206       * [/code]
00207       *
00208       * \warning This function is not intended to check if a setting exists!
00209       */
00210 
00211 private:
00222      libconfig::Setting & GetSettingsForObject( std::string section,
00223           std::string objname = "" );
00224 public:
00231      IInverterBase* GetInverter( const string& name ) const;
00232 
00236      void AddInverter( const IInverterBase *inverter );
00237 
00241      static CWorkScheduler *GetMainScheduler( void )
00242      {
00243           if(! Registry::Instance().mainscheduler) {
00244                Registry::Instance().mainscheduler = new CWorkScheduler;
00245           }
00246           return Registry::Instance().mainscheduler;
00247      }
00248 
00249 private:
00250      CWorkScheduler *mainscheduler;
00251 
00252 protected:
00254      Registry();
00255      virtual ~Registry();
00256 
00257 private:
00258      libconfig::Config *Config;
00259 
00260      // TODO generalize this interface, as we might also store
00261      // other types of objects here.
00262      list<IInverterBase*> inverters;
00263 
00264      ILogger l;
00265 
00266 };
00267 
00268 #endif /* REGISTRY_H_ */