USRP Hardware Driver and Device Manual Version: 4.10.0.0_release
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
17namespace uhd {
18
19namespace /*anon*/ {
20template <typename Key, typename Val>
21struct 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
33template <typename Key, typename Val>
35{
36 /* NOP */
37}
38
39template <typename Key, typename Val>
40template <typename InputIterator>
41dict<Key, Val>::dict(InputIterator first, InputIterator last) : _map(first, last)
42{
43 /* NOP */
44}
45
46template <typename Key, typename Val>
47dict<Key, Val>::dict(std::initializer_list<std::pair<Key, Val>> l) : _map(l)
48{
49 /* NOP */
50}
51
52template <typename Key, typename Val>
53std::size_t dict<Key, Val>::size(void) const
54{
55 return _map.size();
56}
57
58template <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
68template <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
78template <typename Key, typename Val>
79bool 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
88template <typename Key, typename Val>
89const 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
98template <typename Key, typename Val>
99const 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
108template <typename Key, typename Val>
109void dict<Key, Val>::set(const Key& key, const Val& val)
110{
111 (*this)[key] = val;
112}
113
114template <typename Key, typename Val>
115const 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
124template <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
135template <typename Key, typename Val>
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
149template <typename Key, typename Val>
151{
152 return not(*this == other);
153}
154
155template <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
169template <typename Key, typename Val>
170void 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
182template <typename Key, typename Val>
183dict<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
Definition dict.hpp:25
std::vector< Key > keys(void) const
Definition dict.ipp:59
std::vector< Val > vals(void) const
Definition dict.ipp:69
bool operator==(const dict< Key, Val > &other) const
Definition dict.ipp:136
void update(const dict< Key, Val > &new_dict, bool fail_on_conflict=true)
Definition dict.ipp:170
const Val & operator[](const Key &key) const
Definition dict.ipp:115
std::size_t size(void) const
Definition dict.ipp:53
bool operator!=(const dict< Key, Val > &other) const
Definition dict.ipp:150
bool has_key(const Key &key) const
Definition dict.ipp:79
dict(void)
Definition dict.ipp:34
const Val & get(const Key &key, const Val &other) const
Definition dict.ipp:89
Val pop(const Key &key)
Definition dict.ipp:156
void set(const Key &key, const Val &val)
Definition dict.ipp:109
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
Definition build_info.hpp:12
Definition exception.hpp:82
Definition exception.hpp:108