uhd: dict: Add typecast operator to std::map<>

This will now allow calls like this:

uhd::dict<k, v> d = /* ... */;
auto m = static_cast<std::map<k, v>>(d);
This commit is contained in:
Martin Braun 2019-11-01 14:48:06 -07:00
parent ebb06c983a
commit 0014f5ba29
3 changed files with 19 additions and 0 deletions

View File

@ -10,6 +10,7 @@
#include <uhd/config.hpp>
#include <list>
#include <map>
#include <vector>
namespace uhd {
@ -138,6 +139,10 @@ public:
*/
void update(const dict<Key, Val>& new_dict, bool fail_on_conflict = true);
/*! Typecast operator to std::map<>
*/
operator std::map<Key, Val>() const;
private:
typedef std::pair<Key, Val> pair_t;
std::list<pair_t> _map; // private container

View File

@ -157,6 +157,16 @@ namespace uhd{
}
}
template <typename Key, typename Val>
dict<Key, Val>::operator std::map<Key, Val>() const
{
std::map<Key, Val> new_map;
BOOST_FOREACH (const pair_t& p, _map) {
new_map[p.first] = p.second;
}
return new_map;
}
} //namespace uhd
#endif /* INCLUDED_UHD_TYPES_DICT_IPP */

View File

@ -8,6 +8,7 @@
#include <uhd/types/dict.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/test/unit_test.hpp>
#include <map>
BOOST_AUTO_TEST_CASE(test_dict_init)
{
@ -41,6 +42,9 @@ BOOST_AUTO_TEST_CASE(test_const_dict)
BOOST_CHECK(d.vals()[1] == 4);
BOOST_CHECK_EQUAL(d[-1], 3);
BOOST_CHECK_THROW(d[2], std::exception);
std::map<int, int> m = static_cast<std::map<int, int>>(d);
BOOST_CHECK_EQUAL(m[-1], 3);
}
BOOST_AUTO_TEST_CASE(test_dict_pop)