| USRP Hardware Driver and Device Manual Version: 4.10.0.0 UHD and USRP Manual |

Loading...
Searching...
No Matches
dict.ipp
Go to the documentation of this file.
1//
2// Copyright 2010-2011 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/exception.hpp>
12#include <uhd/utils/cast.hpp>
13#include <boost/format.hpp>
14#include <iterator>
15#include <typeinfo>
16
17 namespace uhd {
18
19 namespace /*anon*/ {
20 template <typename Key, typename Val>
21 struct key_not_found : uhd::key_error
22{
23 key_not_found(const Key& key)
24 : uhd::key_error(boost::str(boost::format("key \"%s\" not found in dict(%s, %s)")
25 % uhd::cast::to_str(key) % typeid(Key).name()
26 % typeid(Val).name()))
27 {
28 /* NOP */
29 }
30};
31} // namespace
32
33 template <typename Key, typename Val>
34 dict<Key, Val>::dict(void)
35{
36 /* NOP */
37}
38
39 template <typename Key, typename Val>
40 template <typename InputIterator>
41 dict<Key, Val>::dict(InputIterator first, InputIterator last) : _map(first, last)
42{
43 /* NOP */
44}
45
46 template <typename Key, typename Val>
47 dict<Key, Val>::dict(std::initializer_list<std::pair<Key, Val>> l) : _map(l)
48{
49 /* NOP */
50}
51
52 template <typename Key, typename Val>
53std::size_t dict<Key, Val>::size(void) const
54{
55 return _map.size();
56}
57
58 template <typename Key, typename Val>
59std::vector<Key> dict<Key, Val>::keys(void) const
60{
61 std::vector<Key> keys;
62 for (const pair_t& p : _map) {
63 keys.push_back(p.first);
64 }
65 return keys;
66}
67
68 template <typename Key, typename Val>
69std::vector<Val> dict<Key, Val>::vals(void) const
70{
71 std::vector<Val> vals;
72 for (const pair_t& p : _map) {
73 vals.push_back(p.second);
74 }
75 return vals;
76}
77
78 template <typename Key, typename Val>
79 bool dict<Key, Val>::has_key(const Key& key) const
80{
81 for (const pair_t& p : _map) {
82 if (p.first == key)
83 return true;
84 }
85 return false;
86}
87
88 template <typename Key, typename Val>
89 const Val& dict<Key, Val>::get(const Key& key, const Val& other) const
90{
91 for (const pair_t& p : _map) {
92 if (p.first == key)
93 return p.second;
94 }
95 return other;
96}
97
98 template <typename Key, typename Val>
99 const Val& dict<Key, Val>::get(const Key& key) const
100{
101 for (const pair_t& p : _map) {
102 if (p.first == key)
103 return p.second;
104 }
105 throw key_not_found<Key, Val>(key);
106}
107
108 template <typename Key, typename Val>
109 void dict<Key, Val>::set(const Key& key, const Val& val)
110{
111 (*this)[key] = val;
112}
113
114 template <typename Key, typename Val>
115 const Val& dict<Key, Val>::operator[](const Key& key) const
116{
117 for (const pair_t& p : _map) {
118 if (p.first == key)
119 return p.second;
120 }
121 throw key_not_found<Key, Val>(key);
122}
123
124 template <typename Key, typename Val>
125Val& dict<Key, Val>::operator[](const Key& key)
126{
127 for (pair_t& p : _map) {
128 if (p.first == key)
129 return p.second;
130 }
131 _map.push_back(std::make_pair(key, Val()));
132 return _map.back().second;
133}
134
135 template <typename Key, typename Val>
136 bool dict<Key, Val>::operator==(const dict<Key, Val>& other) const
137{
138 if (this->size() != other.size()) {
139 return false;
140 }
141 for (const pair_t& p : _map) {
142 if (not(other.has_key(p.first) and other.get(p.first) == p.second)) {
143 return false;
144 }
145 }
146 return true;
147}
148
149 template <typename Key, typename Val>
150 bool dict<Key, Val>::operator!=(const dict<Key, Val>& other) const
151{
152 return not(*this == other);
153}
154
155 template <typename Key, typename Val>
156Val dict<Key, Val>::pop(const Key& key)
157{
158 typename std::list<pair_t>::iterator it;
159 for (it = _map.begin(); it != _map.end(); it++) {
160 if (it->first == key) {
161 Val val = it->second;
162 _map.erase(it);
163 return val;
164 }
165 }
166 throw key_not_found<Key, Val>(key);
167}
168
169 template <typename Key, typename Val>
170 void dict<Key, Val>::update(const dict<Key, Val>& new_dict, bool fail_on_conflict)
171{
172 for (const Key& key : new_dict.keys()) {
173 if (fail_on_conflict and has_key(key) and get(key) != new_dict[key]) {
174 throw uhd::value_error(
175 str(boost::format("Option merge conflict: %s:%s != %s:%s") % key
176 % get(key) % key % new_dict[key]));
177 }
178 set(key, new_dict[key]);
179 }
180}
181
182 template <typename Key, typename Val>
183 dict<Key, Val>::operator std::map<Key, Val>() const
184{
185 std::map<Key, Val> new_map;
186 for (const pair_t& p : _map) {
187 new_map[p.first] = p.second;
188 }
189 return new_map;
190}
191
192} // namespace uhd
cast.hpp
uhd::dict
Definition dict.hpp:25
uhd::dict::keys
std::vector< Key > keys(void) const
Definition dict.ipp:59
uhd::dict::vals
std::vector< Val > vals(void) const
Definition dict.ipp:69
uhd::dict::operator==
bool operator==(const dict< Key, Val > &other) const
Definition dict.ipp:136
uhd::dict::update
void update(const dict< Key, Val > &new_dict, bool fail_on_conflict=true)
Definition dict.ipp:170
uhd::dict::operator[]
const Val & operator[](const Key &key) const
Definition dict.ipp:115
uhd::dict::size
std::size_t size(void) const
Definition dict.ipp:53
uhd::dict::operator!=
bool operator!=(const dict< Key, Val > &other) const
Definition dict.ipp:150
uhd::dict::has_key
bool has_key(const Key &key) const
Definition dict.ipp:79
uhd::dict::dict
dict(void)
Definition dict.ipp:34
uhd::dict::get
const Val & get(const Key &key, const Val &other) const
Definition dict.ipp:89
uhd::dict::pop
Val pop(const Key &key)
Definition dict.ipp:156
uhd::dict::set
void set(const Key &key, const Val &val)
Definition dict.ipp:109
exception.hpp
uhd::cast::to_str
auto to_str(const T &val) -> std::enable_if_t< std::is_arithmetic_v< T > &&!std::is_floating_point_v< T > &&!std::is_same_v< T, int8_t > &&!std::is_same_v< T, uint8_t > &&!std::is_enum_v< T > &&!detail::has_to_string_method< T >::value, decltype(std::to_string(val))>
SFINAE-based template for integer types that std::to_string can handle.
Definition cast.hpp:167
uhd
Definition build_info.hpp:12
uhd::value_error
Definition exception.hpp:108