USRP Hardware Driver and USRP Manual  Version: 4.4.0.HEAD-0-g5fac246b
UHD and USRP Manual
cast.hpp
Go to the documentation of this file.
1 //
2 // Copyright 2014-2015 Ettus Research LLC
3 // Copyright 2018 Ettus Research, a National Instruments Company
4 // Copyright 2019 Ettus Research, a National Instruments Brand
5 //
6 // SPDX-License-Identifier: GPL-3.0-or-later
7 //
8 
9 #pragma once
10 
11 #include <uhd/config.hpp>
12 #include <uhd/exception.hpp>
13 #include <sstream>
14 #include <string>
15 
16 namespace uhd { namespace cast {
18 //
19 // Example:
20 // uint16_t x = hexstr_cast<uint16_t>("0xDEADBEEF");
21 // Uses stringstream.
22 template <typename T>
23 inline T hexstr_cast(const std::string& in)
24 {
25  T x;
26  std::stringstream ss;
27  ss << std::hex << in;
28  ss >> x;
29  return x;
30 }
31 
33 template <typename data_t>
34 data_t from_str(const std::string&)
35 {
36  throw uhd::runtime_error("Cannot convert from string!");
37 }
38 
39 // Specializations of `uhd::cast::from_str()` for supported data types
40 
42 //
43 // Examples evaluating to `true`: 'True', 'Yes', 'y', '1', empty string
44 // Examples evaluating to `false`: 'false', 'NO', 'n', '0'
45 // Throws `uhd::runtime_error` if the string can't be converted to `bool`
46 template <>
47 UHD_API bool from_str(const std::string& val);
48 
50 template <>
51 UHD_API double from_str(const std::string& val);
52 
54 template <>
55 UHD_API int from_str(const std::string& val);
56 
58 //
59 // This function simply returns the incoming string
60 template <>
61 UHD_API std::string from_str(const std::string& val);
62 
63 }} // namespace uhd::cast
config.hpp
uhd::cast::from_str
data_t from_str(const std::string &)
Generic cast-from-string function.
Definition: cast.hpp:34
UHD_API
#define UHD_API
Definition: config.h:87
uhd::cast::hexstr_cast
T hexstr_cast(const std::string &in)
Convert a hexadecimal string into a value.
Definition: cast.hpp:23
uhd
Definition: build_info.hpp:12
uhd::runtime_error
Definition: exception.hpp:131
exception.hpp