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