summaryrefslogtreecommitdiffstats
path: root/host/include/uhd/dict.hpp
blob: 2224a0063e5b61b018c749b8f54c6f787512e743 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// Copyright 2010 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#ifndef INCLUDED_UHD_DICT_HPP
#define INCLUDED_UHD_DICT_HPP

#include <map>
#include <vector>
#include <stdexcept>
#include <boost/foreach.hpp>

namespace uhd{

    /*!
     * A templated dictionary class with a python-like interface.
     * Its wraps around a std::map internally.
     */
    template <class Key, class Val> class dict{
    public:
        typedef std::map<Key, Val> map_t;
        typedef std::pair<Key, Val> pair_t;

        /*!
         * Create a new empty dictionary.
         */
        dict(void){
            /* NOP */
        }

        /*!
         * Create a dictionary from a map.
         * \param map a map with key value pairs
         */
        dict(const map_t &map){
            _map = map;
        }

        /*!
         * Destroy this dict.
         */
        ~dict(void){
            /* NOP */
        }

        /*!
         * Get the number of elements in this dict.
         * \param the number of elements
         */
        std::size_t size(void) const{
            return _map.size();
        }

        /*!
         * Get a list of the keys in this dict.
         * \return vector of keys
         */
        std::vector<Key> get_keys(void) const{
            std::vector<Key> keys;
            BOOST_FOREACH(pair_t p, _map){
                keys.push_back(p.first);
            }
            return keys;
        }

        /*!
         * Get a list of the values in this dict.
         * \return vector of values
         */
        std::vector<Val> get_vals(void) const{
            std::vector<Val> vals;
            BOOST_FOREACH(pair_t p, _map){
                vals.push_back(p.second);
            }
            return vals;
        }

        /*!
         * Does the dictionary contain this key?
         * \param key the key to look for
         * \return true if found
         */
        bool has_key(const Key &key) const{
            BOOST_FOREACH(pair_t p, _map){
                if (p.first == key) return true;
            }
            return false;
        }

        /*!
         * Get a value for the given key if it exists.
         * If the key is not found throw an error.
         * \param key the key to look for
         * \return the value at the key
         * \throw an exception when not found
         */
        const Val &operator[](const Key &key) const{
            if (has_key(key)){
                return _map.find(key)->second;
            }
            throw std::invalid_argument("key not found in dict");
        }

        /*!
         * Set a value for the given key, however, in reality
         * it really returns a reference which can be assigned to.
         * \param key the key to set to
         * \return a reference to the value
         */
        Val &operator[](const Key &key){
            return _map[key];
        }

        /*!
         * Pop an item out of the dictionary.
         * \param key the item key
         * \return the value of the item
         * \throw an exception when not found
         */
        Val pop_key(const Key &key){
            if (has_key(key)){
                Val val = _map.find(key)->second;
                _map.erase(key);
                return val;
            }
            throw std::invalid_argument("key not found in dict");
        }

    private:
        map_t _map; //private container
    };

} //namespace uhd

#endif /* INCLUDED_UHD_DICT_HPP */