GNU Radio 3.7.1 C++ API
prefs.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2006,2013 Free Software Foundation, Inc.
00004  *
00005  * This file is part of GNU Radio
00006  *
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  *
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifndef INCLUDED_GR_PREFS_H
00024 #define INCLUDED_GR_PREFS_H
00025 
00026 #include <gnuradio/api.h>
00027 #include <string>
00028 #include <map>
00029 #include <gnuradio/thread/thread.h>
00030 
00031 namespace gr {
00032 
00033   typedef std::map< std::string, std::map<std::string, std::string> > config_map_t;
00034   typedef std::map< std::string, std::map<std::string, std::string> >::iterator config_map_itr;
00035   typedef std::map<std::string, std::string> config_map_elem_t;
00036   typedef std::map<std::string, std::string>::iterator config_map_elem_itr;
00037 
00038   /*!
00039    * \brief Base class for representing user preferences a la windows INI files.
00040    * \ingroup misc
00041    *
00042    * The real implementation is in Python, and is accessable from C++
00043    * via the magic of SWIG directors.
00044    */
00045   class GR_RUNTIME_API prefs
00046   {
00047   public:
00048     static prefs *singleton();
00049     static void set_singleton(prefs *p);
00050 
00051     prefs();
00052     virtual ~prefs();
00053 
00054     /*!
00055      * \brief Returns the configuration options as a string.
00056      */
00057     std::string to_string();
00058 
00059     /*!
00060      * \brief Saves the configuration settings to
00061      * ${HOME}/.gnuradio/config.conf.
00062      *
00063      * WARNING: this will overwrite your current config.conf file.
00064      */
00065     void save();
00066 
00067     /*!
00068      * \brief Does \p section exist?
00069      */
00070     virtual bool has_section(const std::string &section);
00071 
00072     /*!
00073      * \brief Does \p option exist?
00074      */
00075     virtual bool has_option(const std::string &section,
00076                             const std::string &option);
00077 
00078     /*!
00079      * \brief If option exists return associated value; else
00080      * default_val.
00081      */
00082     virtual const std::string get_string(const std::string &section,
00083                                          const std::string &option,
00084                                          const std::string &default_val);
00085 
00086     /*!
00087      * \brief Set or add a string \p option to \p section with value
00088      * \p val.
00089      */
00090     virtual void set_string(const std::string &section,
00091                             const std::string &option,
00092                             const std::string &val);
00093 
00094     /*!
00095      * \brief If option exists and value can be converted to bool,
00096      * return it; else default_val.
00097      */
00098     virtual bool get_bool(const std::string &section,
00099                           const std::string &option,
00100                           bool default_val);
00101 
00102     /*!
00103      * \brief Set or add a bool \p option to \p section with value \p val.
00104      */
00105     virtual void set_bool(const std::string &section,
00106                           const std::string &option,
00107                           bool val);
00108 
00109     /*!
00110      * \brief If option exists and value can be converted to long,
00111      * return it; else default_val.
00112      */
00113     virtual long get_long(const std::string &section,
00114                           const std::string &option,
00115                           long default_val);
00116 
00117     /*!
00118      * \brief Set or add a long \p option to \p section with value \p val.
00119      */
00120     virtual void set_long(const std::string &section,
00121                           const std::string &option,
00122                           long val);
00123 
00124     /*!
00125      * \brief If option exists and value can be converted to double,
00126      * return it; else default_val.
00127      */
00128     virtual double get_double(const std::string &section,
00129                               const std::string &option,
00130                               double default_val);
00131 
00132     /*!
00133      * \brief Set or add a double \p option to \p section with value \p val.
00134      */
00135     virtual void set_double(const std::string &section,
00136                             const std::string &option,
00137                             double val);
00138 
00139   protected:
00140     virtual std::vector<std::string> _sys_prefs_filenames();
00141     virtual void _read_files();
00142     virtual void _convert_to_map(const std::string &conf);
00143     virtual char * option_to_env(std::string section, std::string option);
00144 
00145   private:
00146     gr::thread::mutex d_mutex;
00147     config_map_t d_config_map;
00148   };
00149 
00150 } /* namespace gr */
00151 
00152 #endif /* INCLUDED_GR_PREFS_H */