GNU Radio Manual and C++ API Reference  v3.9.2.0-89-gb7c7001e
The Free & Open Software Radio Ecosystem
prefs.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006,2013,2015 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * SPDX-License-Identifier: GPL-3.0-or-later
8  *
9  */
10 
11 #ifndef INCLUDED_GR_PREFS_H
12 #define INCLUDED_GR_PREFS_H
13 
14 #include <gnuradio/api.h>
15 #include <gnuradio/thread/thread.h>
16 #include <map>
17 #include <string>
18 
19 namespace gr {
20 
21 typedef std::map<std::string, std::map<std::string, std::string>> config_map_t;
22 typedef std::map<std::string, std::map<std::string, std::string>>::iterator
24 typedef std::map<std::string, std::string> config_map_elem_t;
25 typedef std::map<std::string, std::string>::iterator config_map_elem_itr;
26 
27 /*!
28  * \brief Base class for representing user preferences a la windows INI files.
29  * \ingroup misc
30  *
31  * The real implementation is in Python, and is accessible from C++
32  * via the magic of SWIG directors.
33  */
35 {
36 public:
37  static prefs* singleton();
38 
39  /*!
40  * \brief Creates an object to read preference files.
41  *
42  * \details
43  *
44  * If no file name is given (empty arg list or ""), this opens up
45  * the standard GNU Radio configuration files in
46  * prefix/etc/gnuradio/conf.d as well as ~/.gnuradio/config.conf.
47  *
48  * Only access this through the singleton defined here:
49  * \code
50  * prefs *p = prefs::singleton();
51  * \endcode
52  */
53  prefs();
54 
55  virtual ~prefs();
56 
57  /*!
58  * If specifying a file name, this opens that specific
59  * configuration file of the standard form containing sections and
60  * key-value pairs:
61  *
62  * \code
63  * [SectionName]
64  * key0 = value0
65  * key1 = value1
66  * \endcode
67  */
68  void add_config_file(const std::string& configfile);
69 
70  /*!
71  * \brief Returns the configuration options as a string.
72  */
73  std::string to_string();
74 
75  /*!
76  * \brief Saves the configuration settings to
77  * ${HOME}/.gnuradio/config.conf.
78  *
79  * WARNING: this will overwrite your current config.conf file.
80  */
81  void save();
82 
83  /*!
84  * \brief Does \p section exist?
85  */
86  virtual bool has_section(const std::string& section);
87 
88  /*!
89  * \brief Does \p option exist?
90  */
91  virtual bool has_option(const std::string& section, const std::string& option);
92 
93  /*!
94  * \brief If option exists return associated value; else
95  * default_val.
96  */
97  virtual const std::string get_string(const std::string& section,
98  const std::string& option,
99  const std::string& default_val);
100 
101  /*!
102  * \brief Set or add a string \p option to \p section with value
103  * \p val.
104  */
105  virtual void set_string(const std::string& section,
106  const std::string& option,
107  const std::string& val);
108 
109  /*!
110  * \brief If option exists and value can be converted to bool,
111  * return it; else default_val.
112  */
113  virtual bool
114  get_bool(const std::string& section, const std::string& option, bool default_val);
115 
116  /*!
117  * \brief Set or add a bool \p option to \p section with value \p val.
118  */
119  virtual void
120  set_bool(const std::string& section, const std::string& option, bool val);
121 
122  /*!
123  * \brief If option exists and value can be converted to long,
124  * return it; else default_val.
125  */
126  virtual long
127  get_long(const std::string& section, const std::string& option, long default_val);
128 
129  /*!
130  * \brief Set or add a long \p option to \p section with value \p val.
131  */
132  virtual void
133  set_long(const std::string& section, const std::string& option, long val);
134 
135  /*!
136  * \brief If option exists and value can be converted to double,
137  * return it; else default_val.
138  */
139  virtual double
140  get_double(const std::string& section, const std::string& option, double default_val);
141 
142  /*!
143  * \brief Set or add a double \p option to \p section with value \p val.
144  */
145  virtual void
146  set_double(const std::string& section, const std::string& option, double val);
147 
148 protected:
149  virtual std::vector<std::string> _sys_prefs_filenames();
150  virtual void _read_files(const std::vector<std::string>& filenames);
151  virtual char* option_to_env(std::string section, std::string option);
152 
153 private:
154  gr::thread::mutex d_mutex;
155  config_map_t d_config_map;
156 };
157 
158 } /* namespace gr */
159 
160 #endif /* INCLUDED_GR_PREFS_H */
Base class for representing user preferences a la windows INI files.
Definition: prefs.h:35
virtual void set_bool(const std::string &section, const std::string &option, bool val)
Set or add a bool option to section with value val.
virtual void set_long(const std::string &section, const std::string &option, long val)
Set or add a long option to section with value val.
virtual ~prefs()
prefs()
Creates an object to read preference files.
std::string to_string()
Returns the configuration options as a string.
virtual void _read_files(const std::vector< std::string > &filenames)
virtual std::vector< std::string > _sys_prefs_filenames()
virtual const std::string get_string(const std::string &section, const std::string &option, const std::string &default_val)
If option exists return associated value; else default_val.
virtual char * option_to_env(std::string section, std::string option)
virtual bool has_section(const std::string &section)
Does section exist?
virtual double get_double(const std::string &section, const std::string &option, double default_val)
If option exists and value can be converted to double, return it; else default_val.
virtual void set_string(const std::string &section, const std::string &option, const std::string &val)
Set or add a string option to section with value val.
virtual long get_long(const std::string &section, const std::string &option, long default_val)
If option exists and value can be converted to long, return it; else default_val.
static prefs * singleton()
virtual bool get_bool(const std::string &section, const std::string &option, bool default_val)
If option exists and value can be converted to bool, return it; else default_val.
virtual bool has_option(const std::string &section, const std::string &option)
Does option exist?
void add_config_file(const std::string &configfile)
virtual void set_double(const std::string &section, const std::string &option, double val)
Set or add a double option to section with value val.
void save()
Saves the configuration settings to ${HOME}/.gnuradio/config.conf.
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
boost::mutex mutex
Definition: thread.h:37
GNU Radio logging wrapper for log4cpp library (C++ port of log4j)
Definition: basic_block.h:29
std::map< std::string, std::map< std::string, std::string > >::iterator config_map_itr
Definition: prefs.h:23
std::map< std::string, std::string >::iterator config_map_elem_itr
Definition: prefs.h:25
std::map< std::string, std::string > config_map_elem_t
Definition: prefs.h:24
std::map< std::string, std::map< std::string, std::string > > config_map_t
Definition: prefs.h:21