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