USRP Hardware Driver and USRP Manual Version: 4.1.0.1
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
16namespace uhd { namespace cast {
18//
19// Example:
20// uint16_t x = hexstr_cast<uint16_t>("0xDEADBEEF");
21// Uses stringstream.
22template <typename T>
23inline 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
33template <typename data_t>
34data_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`
46template <>
47UHD_API bool from_str(const std::string& val);
48
50template <>
51UHD_API double from_str(const std::string& val);
52
54template <>
55UHD_API int from_str(const std::string& val);
56
58//
59// This function simply returns the incoming string
60template <>
61UHD_API std::string from_str(const std::string& val);
62
63}} // namespace uhd::cast
#define UHD_API
Definition: config.h:70
data_t from_str(const std::string &)
Generic cast-from-string function.
Definition: cast.hpp:34
T hexstr_cast(const std::string &in)
Convert a hexadecimal string into a value.
Definition: cast.hpp:23
Definition: build_info.hpp:12
Definition: exception.hpp:133