From 259f5babf1e1bc1595ad54c6588c1ff5117dc2e3 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Fri, 12 Feb 2010 18:43:21 -0800 Subject: Made use of templated dict to replace used of map and to get rid of utility call. --- include/uhd/dict.hpp | 18 +++---- include/uhd/usrp/dboard/manager.hpp | 6 +-- include/uhd/usrp/mboard/test.hpp | 4 +- include/uhd/usrp/mboard/usrp2.hpp | 4 +- include/uhd/usrp/usrp.hpp | 4 +- include/uhd/utils.hpp | 94 +++++++++++++++++-------------------- 6 files changed, 60 insertions(+), 70 deletions(-) (limited to 'include') diff --git a/include/uhd/dict.hpp b/include/uhd/dict.hpp index 3abc4273c..1ed28551a 100644 --- a/include/uhd/dict.hpp +++ b/include/uhd/dict.hpp @@ -20,8 +20,8 @@ #include #include -#include #include +#include namespace uhd{ @@ -31,6 +31,9 @@ namespace uhd{ */ template class dict{ public: + typedef std::map map_t; + typedef std::pair pair_t; + /*! * Create a new empty dictionary. */ @@ -42,7 +45,7 @@ namespace uhd{ * Create a dictionary from a map. * \param map a map with key value pairs */ - dict(const std::map &map){ + dict(const map_t &map){ _map = map; } @@ -59,8 +62,7 @@ namespace uhd{ */ std::vector get_keys(void) const{ std::vector keys; - std::pair p; - BOOST_FOREACH(p, _map){ + BOOST_FOREACH(pair_t p, _map){ keys.push_back(p.first); } return keys; @@ -72,8 +74,7 @@ namespace uhd{ */ std::vector get_vals(void) const{ std::vector vals; - std::pair p; - BOOST_FOREACH(p, _map){ + BOOST_FOREACH(pair_t p, _map){ vals.push_back(p.second); } return vals; @@ -85,8 +86,7 @@ namespace uhd{ * \return true if found */ bool has_key(const Key &key) const{ - std::pair p; - BOOST_FOREACH(p, _map){ + BOOST_FOREACH(pair_t p, _map){ if (p.first == key) return true; } return false; @@ -132,7 +132,7 @@ namespace uhd{ } private: - std::map _map; //private container + map_t _map; //private container }; } //namespace uhd diff --git a/include/uhd/usrp/dboard/manager.hpp b/include/uhd/usrp/dboard/manager.hpp index 5934efedc..e53ba8e52 100644 --- a/include/uhd/usrp/dboard/manager.hpp +++ b/include/uhd/usrp/dboard/manager.hpp @@ -18,7 +18,7 @@ #ifndef INCLUDED_UHD_USRP_DBOARD_MANAGER_HPP #define INCLUDED_UHD_USRP_DBOARD_MANAGER_HPP -#include +#include #include #include #include @@ -73,8 +73,8 @@ private: //list of rx and tx dboards in this manager //each dboard here is actually a subdevice proxy //the subdevice proxy is internal to the cpp file - std::map _rx_dboards; - std::map _tx_dboards; + uhd::dict _rx_dboards; + uhd::dict _tx_dboards; }; }}} //namespace diff --git a/include/uhd/usrp/mboard/test.hpp b/include/uhd/usrp/mboard/test.hpp index be435930b..04d0ff4c4 100644 --- a/include/uhd/usrp/mboard/test.hpp +++ b/include/uhd/usrp/mboard/test.hpp @@ -21,7 +21,7 @@ #include #include #include -#include +#include namespace uhd{ namespace usrp{ namespace mboard{ @@ -38,7 +38,7 @@ private: void get(const wax::obj &, wax::obj &); void set(const wax::obj &, const wax::obj &); - std::map _dboard_managers; + uhd::dict _dboard_managers; }; }}} //namespace diff --git a/include/uhd/usrp/mboard/usrp2.hpp b/include/uhd/usrp/mboard/usrp2.hpp index 4950f4cd1..91f729f6f 100644 --- a/include/uhd/usrp/mboard/usrp2.hpp +++ b/include/uhd/usrp/mboard/usrp2.hpp @@ -22,7 +22,7 @@ #include #include #include -#include +#include namespace uhd{ namespace usrp{ namespace mboard{ @@ -46,7 +46,7 @@ private: void get(const wax::obj &, wax::obj &); void set(const wax::obj &, const wax::obj &); - std::map _dboard_managers; + uhd::dict _dboard_managers; uhd::transport::udp::sptr _udp_ctrl_transport; uhd::transport::udp::sptr _udp_data_transport; }; diff --git a/include/uhd/usrp/usrp.hpp b/include/uhd/usrp/usrp.hpp index 0dca36f62..98c357b77 100644 --- a/include/uhd/usrp/usrp.hpp +++ b/include/uhd/usrp/usrp.hpp @@ -17,7 +17,7 @@ #include #include -#include +#include #ifndef INCLUDED_UHD_USRP_USRP_HPP #define INCLUDED_UHD_USRP_USRP_HPP @@ -43,7 +43,7 @@ private: void get(const wax::obj &, wax::obj &); void set(const wax::obj &, const wax::obj &); - std::map _mboards; + uhd::dict _mboards; boost::function &)> _send_raw_cb; boost::function _recv_raw_cb; }; diff --git a/include/uhd/utils.hpp b/include/uhd/utils.hpp index b080516a5..13367990e 100644 --- a/include/uhd/utils.hpp +++ b/include/uhd/utils.hpp @@ -27,23 +27,50 @@ #ifndef INCLUDED_UHD_UTILS_HPP #define INCLUDED_UHD_UTILS_HPP -namespace uhd{ +/*! + * Useful templated functions and classes that I like to pretend are part of stl + */ +namespace std{ + + class assert_error : public std::logic_error{ + public: + explicit assert_error(const string& what_arg) : logic_error(what_arg){ + /* NOP */ + } + }; -template //TODO template this better -std::vector get_map_keys(const std::map &m){ - std::vector v; - std::pair p; - BOOST_FOREACH(p, m){ - v.push_back(p.first); + #define ASSERT_THROW(_x) if (not (_x)) { \ + throw std::assert_error("Assertion Failed: " + std::string(#_x)); \ } - return v; -} -template T signum(T n){ - if (n < 0) return -1; - if (n > 0) return 1; - return 0; -} + template + T reduce(InputIterator first, InputIterator last, Function fcn, T init = 0){ + T tmp = init; + for ( ; first != last; ++first ){ + tmp = fcn(tmp, *first); + } + return tmp; + } + + template + bool has(InputIterator first, InputIterator last, const T &elem){ + return last != std::find(first, last, elem); + } + + template + T sum(const T &a, const T &b){ + return a + b; + } + + template T signum(T n){ + if (n < 0) return -1; + if (n > 0) return 1; + return 0; + } + +}//namespace std + +namespace uhd{ inline void tune( freq_t target_freq, @@ -62,7 +89,7 @@ inline void tune( // Calculate the DDC setting that will downconvert the baseband from the // daughterboard to our target frequency. freq_t delta_freq = target_freq - inter_freq; - int delta_sign = signum(delta_freq); + int delta_sign = std::signum(delta_freq); delta_freq *= delta_sign; delta_freq = fmod(delta_freq, dsp_sample_rate); bool inverted = delta_freq > dsp_sample_rate/2.0; @@ -91,41 +118,4 @@ inline void tune( } //namespace uhd -/*! - * Useful templated functions and classes that I like to pretend are part of stl - */ -namespace std{ - - class assert_error : public std::logic_error{ - public: - explicit assert_error(const string& what_arg) : logic_error(what_arg){ - /* NOP */ - } - }; - - #define ASSERT_THROW(_x) if (not (_x)) { \ - throw std::assert_error("Assertion Failed: " + std::string(#_x)); \ - } - - template - T reduce(InputIterator first, InputIterator last, Function fcn, T init = 0){ - T tmp = init; - for ( ; first != last; ++first ){ - tmp = fcn(tmp, *first); - } - return tmp; - } - - template - bool has(InputIterator first, InputIterator last, const T &elem){ - return last != std::find(first, last, elem); - } - - template - T sum(const T &a, const T &b){ - return a + b; - } - -}//namespace std - #endif /* INCLUDED_UHD_UTILS_HPP */ -- cgit v1.2.3