GNU Radio Manual and C++ API Reference  v3.9.2.0-89-gb7c7001e
The Free & Open Software Radio Ecosystem
setting_string_conversion.h
Go to the documentation of this file.
1 /*
2  * Copyright 2021 Nicholas Corgan
3  * SPDX-License-Identifier: GPL-3.0-or-later
4  */
5 
6 #ifndef INCLUDED_GR_SOAPY_SETTING_STRING_CONVERSION_H
7 #define INCLUDED_GR_SOAPY_SETTING_STRING_CONVERSION_H
8 
9 #include <SoapySDR/Types.hpp>
10 #include <SoapySDR/Version.h>
11 
12 #include <sstream>
13 
14 namespace gr {
15 namespace soapy {
16 
17 // SoapySDR doesn't have an API define for SettingToString, so we need
18 // to check the version. 0.8 is the first tagged version to have this
19 // functionality.
20 #if SOAPY_SDR_API_VERSION >= 0x080000
21 
22 template <typename T>
23 static inline T string_to_setting(const std::string& str)
24 {
25  return SoapySDR::StringToSetting<T>(str);
26 }
27 
28 template <typename T>
29 static inline std::string setting_to_string(const T& setting)
30 {
31  return SoapySDR::SettingToString<T>(setting);
32 }
33 
34 #else
35 
36 // Copied from SoapySDR 0.8
37 #define SOAPY_SDR_TRUE "true"
38 #define SOAPY_SDR_FALSE "false"
39 
40 template <typename T>
41 static inline T string_to_setting(const std::string& str)
42 {
43  std::stringstream sstream(str);
44  T setting;
45 
46  sstream >> setting;
47 
48  return setting;
49 }
50 
51 // Copied from SoapySDR 0.8
52 //! convert empty and "false" strings to false, integers to their truthness
53 template <>
54 inline bool string_to_setting<bool>(const std::string& str)
55 {
56  if (str.empty() || str == SOAPY_SDR_FALSE) {
57  return false;
58  }
59  if (str == SOAPY_SDR_TRUE) {
60  return true;
61  }
62  try {
63  return static_cast<bool>(std::stod(str));
64  } catch (std::invalid_argument& e) {
65  }
66  // other values are true
67  return true;
68 }
69 
70 template <typename T>
71 static inline std::string setting_to_string(const T& setting)
72 {
73  return std::to_string(setting);
74 }
75 
76 template <>
77 inline std::string setting_to_string<bool>(const bool& setting)
78 {
79  return setting ? SOAPY_SDR_TRUE : SOAPY_SDR_FALSE;
80 }
81 
82 #endif
83 
84 } // namespace soapy
85 } // namespace gr
86 
87 #endif /* INCLUDED_GR_SOAPY_SETTING_STRING_CONVERSION_H */
static std::string setting_to_string(const T &setting)
Definition: setting_string_conversion.h:71
std::string setting_to_string< bool >(const bool &setting)
Definition: setting_string_conversion.h:77
static T string_to_setting(const std::string &str)
Definition: setting_string_conversion.h:41
bool string_to_setting< bool >(const std::string &str)
convert empty and "false" strings to false, integers to their truthness
Definition: setting_string_conversion.h:54
GNU Radio logging wrapper for log4cpp library (C++ port of log4j)
Definition: basic_block.h:29
#define SOAPY_SDR_FALSE
Definition: setting_string_conversion.h:38
#define SOAPY_SDR_TRUE
Definition: setting_string_conversion.h:37