Fawkes API  Fawkes Development Version
config.h
00001 
00002 /***************************************************************************
00003  *  config.h - Fawkes configuration interface
00004  *
00005  *  Created: Mon Dec 04 17:38:32 2006
00006  *  Copyright  2006  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #ifndef __CONFIG_CONFIG_H_
00025 #define __CONFIG_CONFIG_H_
00026 
00027 #include <core/exception.h>
00028 #include <utils/misc/string_compare.h>
00029 #include <string>
00030 #include <list>
00031 #include <map>
00032 
00033 namespace fawkes {
00034 
00035 class ConfigurationChangeHandler;
00036 
00037 class ConfigurationException : public Exception
00038 {
00039  public:
00040   ConfigurationException(const char *msg);
00041   ConfigurationException(const char *prefix, const char *msg);
00042 };
00043 
00044 class ConfigEntryNotFoundException : public Exception
00045 {
00046  public:
00047   ConfigEntryNotFoundException(const char *path);
00048 };
00049 
00050 class ConfigTypeMismatchException : public Exception
00051 {
00052  public:
00053   ConfigTypeMismatchException(const char *path,
00054                                const char *actual, const char *requested);
00055 };
00056 
00057 class CouldNotOpenConfigException : public Exception
00058 {
00059  public:
00060   CouldNotOpenConfigException(const char *format, ...);
00061 };
00062 
00063 class Configuration
00064 {
00065  public:
00066   virtual ~Configuration() {}
00067 
00068   class ValueIterator
00069   {
00070    public:
00071     virtual ~ValueIterator() {}
00072     virtual bool          next()                                          = 0;
00073     virtual bool          valid() const                                   = 0;
00074     
00075     virtual const char *  path() const                                    = 0;
00076     virtual const char *  type() const                                    = 0;
00077     
00078     virtual bool          is_float() const                                = 0;
00079     virtual bool          is_uint() const                                 = 0;
00080     virtual bool          is_int() const                                  = 0;
00081     virtual bool          is_bool() const                                 = 0;
00082     virtual bool          is_string() const                               = 0;
00083 
00084     virtual float         get_float() const                               = 0;
00085     virtual unsigned int  get_uint() const                                = 0;
00086     virtual int           get_int() const                                 = 0;
00087     virtual bool          get_bool() const                                = 0;
00088     virtual std::string   get_string() const                              = 0;
00089     virtual std::string   get_as_string() const                           = 0;
00090 
00091     virtual std::string   get_comment() const                             = 0;
00092 
00093     virtual bool          is_default() const                              = 0;
00094   };
00095 
00096   virtual void          copy(Configuration *copyconf)                     = 0;
00097 
00098   virtual void          add_change_handler(ConfigurationChangeHandler *h);
00099   virtual void          rem_change_handler(ConfigurationChangeHandler *h);
00100 
00101   virtual void          load(const char *name, const char *defaults_name,
00102                              const char *tag = NULL)                      = 0;
00103 
00104   virtual void          tag(const char *tag)                              = 0;
00105   virtual std::list<std::string> tags()                                   = 0;
00106 
00107   virtual bool          exists(const char *path)                          = 0;
00108   virtual bool          is_float(const char *path)                        = 0;
00109   virtual bool          is_uint(const char *path)                         = 0;
00110   virtual bool          is_int(const char *path)                          = 0;
00111   virtual bool          is_bool(const char *path)                         = 0;
00112   virtual bool          is_string(const char *path)                       = 0;
00113 
00114   virtual bool          is_default(const char *path)                      = 0;
00115 
00116   virtual float           get_float(const char *path)                     = 0;
00117   virtual unsigned int    get_uint(const char *path)                      = 0;
00118   virtual int             get_int(const char *path)                       = 0;
00119   virtual bool            get_bool(const char *path)                      = 0;
00120   virtual std::string     get_string(const char *path)                    = 0;
00121   virtual ValueIterator * get_value(const char *path)                     = 0;
00122   virtual std::string     get_type(const char *path)                      = 0;
00123   virtual std::string     get_comment(const char *path)                   = 0;
00124   virtual std::string     get_default_comment(const char *path)           = 0;
00125 
00126   virtual void          set_float(const char *path, float f)              = 0;
00127   virtual void          set_uint(const char *path, unsigned int uint)     = 0;
00128   virtual void          set_int(const char *path, int i)                  = 0;
00129   virtual void          set_bool(const char *path, bool b)                = 0;
00130   virtual void          set_string(const char *path, std::string &s)      = 0;
00131   virtual void          set_string(const char *path, const char *s)       = 0;
00132   virtual void          set_comment(const char *path,
00133                                     const char *comment)                  = 0;
00134   virtual void          set_comment(const char *path,
00135                                     std::string &comment)                 = 0;
00136 
00137   virtual void          erase(const char *path)                           = 0;
00138 
00139   virtual void          set_default_float(const char *path, float f)      = 0;
00140   virtual void          set_default_uint(const char *path,
00141                                          unsigned int uint)               = 0;
00142   virtual void          set_default_int(const char *path, int i)          = 0;
00143   virtual void          set_default_bool(const char *path, bool b)        = 0;
00144   virtual void          set_default_string(const char *path,
00145                                            std::string &s)                 = 0;
00146   virtual void          set_default_string(const char *path,
00147                                            const char *s)                 = 0;
00148 
00149   virtual void          set_default_comment(const char *path,
00150                                             const char *comment)          = 0;
00151   virtual void          set_default_comment(const char *path,
00152                                             std::string &comment)         = 0;
00153 
00154   virtual void          erase_default(const char *path)                   = 0;
00155 
00156   virtual ValueIterator * iterator()                                      = 0;
00157   virtual ValueIterator * iterator_default()                              = 0;
00158   virtual ValueIterator * iterator_hostspecific()                         = 0;
00159 
00160   virtual ValueIterator * search(const char *path)                        = 0;
00161 
00162   virtual void            lock()                                          = 0;
00163   virtual bool            try_lock()                                      = 0;
00164   virtual void            unlock()                                        = 0;
00165 
00166  protected:
00167   /** List that contains pointers to ConfigurationChangeHandler */
00168   typedef std::list<ConfigurationChangeHandler *> ChangeHandlerList;
00169 
00170   /** Multimap string to config change handlers. */
00171   typedef std::multimap<const char *, ConfigurationChangeHandler *, StringLess >
00172           ChangeHandlerMultimap;
00173 
00174   /** Config change handler multimap range. */
00175   typedef std::pair<ChangeHandlerMultimap::iterator,
00176                     ChangeHandlerMultimap::iterator>
00177           ChangeHandlerMultimapRange;
00178 
00179   /** Registered change handlers. */
00180   ChangeHandlerMultimap                  _change_handlers;
00181   /** Change handler range. */
00182   ChangeHandlerMultimapRange             _ch_range;
00183 
00184   ChangeHandlerList * find_handlers(const char *path);
00185   void notify_handlers(const char *path, bool comment_changed = false);
00186 
00187 };
00188 
00189 } // end namespace fawkes
00190 
00191 #endif