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