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