From 99d55ca11eb67cbbec72660d10a94ea0e9c50f39 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Sat, 1 May 2010 21:35:11 -0700 Subject: Code tweaks and added unit test for dict and error message. --- host/include/uhd/config.hpp | 4 ++-- host/include/uhd/types/dict.hpp | 4 ++-- host/include/uhd/utils/algorithm.hpp | 30 ++++++------------------------ host/include/uhd/utils/assert.hpp | 18 +++++++++--------- 4 files changed, 19 insertions(+), 37 deletions(-) (limited to 'host/include') diff --git a/host/include/uhd/config.hpp b/host/include/uhd/config.hpp index 941219ac7..227c473ce 100644 --- a/host/include/uhd/config.hpp +++ b/host/include/uhd/config.hpp @@ -46,12 +46,12 @@ // http://gcc.gnu.org/wiki/Visibility // Generic helper definitions for shared library support -#if defined _WIN32 || defined __CYGWIN__ +#if BOOST_HAS_DECLSPEC #define UHD_HELPER_DLL_IMPORT __declspec(dllimport) #define UHD_HELPER_DLL_EXPORT __declspec(dllexport) #define UHD_HELPER_DLL_LOCAL #else - #if __GNUC__ >= 4 + #if __GNUG__ >= 4 #define UHD_HELPER_DLL_IMPORT __attribute__ ((visibility("default"))) #define UHD_HELPER_DLL_EXPORT __attribute__ ((visibility("default"))) #define UHD_HELPER_DLL_LOCAL __attribute__ ((visibility("hidden"))) diff --git a/host/include/uhd/types/dict.hpp b/host/include/uhd/types/dict.hpp index c8fbc5a9f..b5fb11120 100644 --- a/host/include/uhd/types/dict.hpp +++ b/host/include/uhd/types/dict.hpp @@ -29,7 +29,7 @@ namespace uhd{ /*! * A templated dictionary class with a python-like interface. */ - template class dict{ + template class dict{ public: typedef std::pair pair_t; @@ -130,7 +130,7 @@ namespace uhd{ BOOST_FOREACH(pair_t &p, _map){ if (p.first == key) return p.second; } - _map.push_back(pair_t(key, Val())); + _map.push_back(std::make_pair(key, Val())); return _map.back().second; } diff --git a/host/include/uhd/utils/algorithm.hpp b/host/include/uhd/utils/algorithm.hpp index 8fe9cde82..72b655745 100644 --- a/host/include/uhd/utils/algorithm.hpp +++ b/host/include/uhd/utils/algorithm.hpp @@ -19,43 +19,25 @@ #define INCLUDED_UHD_UTILS_ALGORITHM_HPP #include +#include /*! * Useful templated functions and classes that I like to pretend are part of stl */ namespace std{ - 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 inline + bool has(const Range &range, const T &value){ + return boost::end(range) != std::find(boost::begin(range), boost::end(range), value); } - template - T reduce(Iterable iterable, Function fcn, T init = 0){ - return reduce(iterable.begin(), iterable.end(), fcn, init); - } - - template - bool has(InputIterator first, InputIterator last, const T &elem){ - return last != std::find(first, last, elem); - } - - template - bool has(const Iterable &iterable, const T &elem){ - return has(iterable.begin(), iterable.end(), elem); - } - - template T signum(T n){ + template inline T signum(T n){ if (n < 0) return -1; if (n > 0) return 1; return 0; } - template T clip(T val, T minVal, T maxVal){ + template inline T clip(T val, T minVal, T maxVal){ return std::min(std::max(val, minVal), maxVal); } diff --git a/host/include/uhd/utils/assert.hpp b/host/include/uhd/utils/assert.hpp index 01beed757..ed0a95535 100644 --- a/host/include/uhd/utils/assert.hpp +++ b/host/include/uhd/utils/assert.hpp @@ -46,28 +46,28 @@ namespace uhd{ * The "what" in the error will show what is * being set and a list of known good values. * - * \param iterable a list of possible settings - * \param elem an element that may be in the list + * \param range a list of possible settings + * \param value an element that may be in the list * \param what a description of what is being set * \throw assertion_error when elem not in list */ - template void assert_has( - const Iterable &iterable, - const T &elem, + template void assert_has( + const Range &range, + const T &value, const std::string &what = "unknown" ){ - if (std::has(iterable, elem)) return; + if (std::has(range, value)) return; std::string possible_values = ""; size_t i = 0; - BOOST_FOREACH(const T &e, iterable){ + BOOST_FOREACH(const T &v, range){ if (i++ > 0) possible_values += ", "; - possible_values += boost::lexical_cast(e); + possible_values += boost::lexical_cast(v); } boost::throw_exception(uhd::assert_error() << assert_info(str(boost::format( "Error: %s is not a valid %s. " "Possible values are: [%s]." ) - % boost::lexical_cast(elem) + % boost::lexical_cast(value) % what % possible_values ))); } -- cgit v1.2.3