diff options
-rw-r--r-- | host/examples/rx_timed_samples.cpp | 3 | ||||
-rw-r--r-- | host/include/uhd/CMakeLists.txt | 2 | ||||
-rw-r--r-- | host/include/uhd/utils/CMakeLists.txt | 24 | ||||
-rw-r--r-- | host/include/uhd/utils/algorithm.hpp | 60 | ||||
-rw-r--r-- | host/include/uhd/utils/assert.hpp (renamed from host/include/uhd/utils.hpp) | 74 | ||||
-rw-r--r-- | host/include/uhd/utils/safe_main.hpp | 43 | ||||
-rw-r--r-- | host/include/uhd/utils/static.hpp | 35 | ||||
-rw-r--r-- | host/lib/device.cpp | 7 | ||||
-rw-r--r-- | host/lib/gain_handler.cpp | 2 | ||||
-rw-r--r-- | host/lib/load_modules.cpp | 4 | ||||
-rw-r--r-- | host/lib/simple_device.cpp | 4 | ||||
-rw-r--r-- | host/lib/usrp/dboard/basic.cpp | 5 | ||||
-rw-r--r-- | host/lib/usrp/dboard_manager.cpp | 5 | ||||
-rw-r--r-- | host/lib/usrp/usrp2/dboard_impl.cpp | 2 | ||||
-rw-r--r-- | host/lib/usrp/usrp2/dboard_interface.cpp | 2 | ||||
-rw-r--r-- | host/lib/usrp/usrp2/dsp_impl.cpp | 2 | ||||
-rw-r--r-- | host/lib/usrp/usrp2/mboard_impl.cpp | 2 | ||||
-rw-r--r-- | host/lib/usrp/usrp2/usrp2_impl.cpp | 5 | ||||
-rw-r--r-- | host/test/module_test.cpp | 4 | ||||
-rw-r--r-- | host/utils/discover_usrps.cpp | 3 | ||||
-rw-r--r-- | host/utils/usrp2_burner.cpp | 3 |
21 files changed, 203 insertions, 88 deletions
diff --git a/host/examples/rx_timed_samples.cpp b/host/examples/rx_timed_samples.cpp index 7d58187cd..5d4b5a68d 100644 --- a/host/examples/rx_timed_samples.cpp +++ b/host/examples/rx_timed_samples.cpp @@ -15,6 +15,7 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. // +#include <uhd/utils/safe_main.hpp> #include <uhd/simple_device.hpp> #include <uhd/props.hpp> #include <boost/program_options.hpp> @@ -24,7 +25,7 @@ namespace po = boost::program_options; -int main(int argc, char *argv[]){ +int UHD_SAFE_MAIN(int argc, char *argv[]){ //variables to be set by po std::string transport_args; int seconds_in_future; diff --git a/host/include/uhd/CMakeLists.txt b/host/include/uhd/CMakeLists.txt index 84e7b441b..2203ea83e 100644 --- a/host/include/uhd/CMakeLists.txt +++ b/host/include/uhd/CMakeLists.txt @@ -18,6 +18,7 @@ ADD_SUBDIRECTORY(transport) ADD_SUBDIRECTORY(usrp) +ADD_SUBDIRECTORY(utils) INSTALL(FILES config.hpp @@ -30,7 +31,6 @@ INSTALL(FILES simple_device.hpp time_spec.hpp types.hpp - utils.hpp wax.hpp DESTINATION ${INCLUDE_DIR}/uhd ) diff --git a/host/include/uhd/utils/CMakeLists.txt b/host/include/uhd/utils/CMakeLists.txt new file mode 100644 index 000000000..f6ed87701 --- /dev/null +++ b/host/include/uhd/utils/CMakeLists.txt @@ -0,0 +1,24 @@ +# +# 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/>. +# + +INSTALL(FILES + algorithm.hpp + assert.hpp + safe_main.hpp + static.hpp + DESTINATION ${INCLUDE_DIR}/uhd/utils +) diff --git a/host/include/uhd/utils/algorithm.hpp b/host/include/uhd/utils/algorithm.hpp new file mode 100644 index 000000000..6635c8a4a --- /dev/null +++ b/host/include/uhd/utils/algorithm.hpp @@ -0,0 +1,60 @@ +// +// 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_UTILS_ALGORITHM_HPP +#define INCLUDED_UHD_UTILS_ALGORITHM_HPP + +#include <algorithm> + +/*! + * Useful templated functions and classes that I like to pretend are part of stl + */ +namespace std{ + + template<class T, class InputIterator, class Function> + 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<class T, class Iterable, class Function> + T reduce(Iterable iterable, Function fcn, T init = 0){ + return reduce(iterable.begin(), iterable.end(), fcn, init); + } + + template<class T, class InputIterator> + bool has(InputIterator first, InputIterator last, const T &elem){ + return last != std::find(first, last, elem); + } + + template<class T, class Iterable> + bool has(const Iterable &iterable, const T &elem){ + return has(iterable.begin(), iterable.end(), elem); + } + + template<typename T> T signum(T n){ + if (n < 0) return -1; + if (n > 0) return 1; + return 0; + } + +}//namespace std + +#endif /* INCLUDED_UHD_UTILS_ALGORITHM_HPP */ diff --git a/host/include/uhd/utils.hpp b/host/include/uhd/utils/assert.hpp index e5333539f..842ed8dfa 100644 --- a/host/include/uhd/utils.hpp +++ b/host/include/uhd/utils/assert.hpp @@ -15,85 +15,31 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. // -#ifndef INCLUDED_UHD_UTILS_HPP -#define INCLUDED_UHD_UTILS_HPP +#ifndef INCLUDED_UHD_UTILS_ASSERT_HPP +#define INCLUDED_UHD_UTILS_ASSERT_HPP -#include <uhd/config.hpp> +#include <uhd/utils/algorithm.hpp> #include <boost/format.hpp> +#include <boost/foreach.hpp> +#include <boost/lexical_cast.hpp> #include <boost/current_function.hpp> #include <stdexcept> -#include <algorithm> - -/*! - * Defines a function that implements the "construct on first use" idiom - * \param _t the type definition for the instance - * \param _x the name of the defined function - * \return a reference to the lazy instance - */ -#define STATIC_INSTANCE(_t, _x) static _t &_x(){static _t _x; return _x;} -/*! - * Defines a static code block that will be called before main() - * \param _x the name of the defined struct (must be unique in file) - */ -#define STATIC_BLOCK(_x) static struct _x{_x();}_x;_x::_x() - -/*! - * Useful templated functions and classes that I like to pretend are part of stl - */ -namespace std{ +namespace uhd{ class assert_error : public std::logic_error{ public: - explicit assert_error(const string& what_arg) : logic_error(what_arg){ + explicit assert_error(const std::string& what_arg) : logic_error(what_arg){ /* NOP */ } }; #define ASSERT_THROW(_x) if (not (_x)) { \ - throw std::assert_error(str(boost::format( \ + throw uhd::assert_error(str(boost::format( \ "Assertion Failed:\n %s:%d\n %s\n ---> %s <---" \ ) % __FILE__ % __LINE__ % BOOST_CURRENT_FUNCTION % std::string(#_x))); \ } - template<class T, class InputIterator, class Function> - 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<class T, class Iterable, class Function> - T reduce(Iterable iterable, Function fcn, T init = 0){ - return reduce(iterable.begin(), iterable.end(), fcn, init); - } - - template<class T, class InputIterator> - bool has(InputIterator first, InputIterator last, const T &elem){ - return last != std::find(first, last, elem); - } - - template<class T, class Iterable> - bool has(const Iterable &iterable, const T &elem){ - return has(iterable.begin(), iterable.end(), elem); - } - - template<typename T> T signum(T n){ - if (n < 0) return -1; - if (n > 0) return 1; - return 0; - } - -}//namespace std - -#include <boost/format.hpp> -#include <boost/foreach.hpp> -#include <boost/lexical_cast.hpp> - -namespace uhd{ - /*! * Check that an element is found in a container. * If not, throw a meaningful assertion error. @@ -116,7 +62,7 @@ namespace uhd{ if (e != iterable.begin()[0]) possible_values += ", "; possible_values += boost::lexical_cast<std::string>(e); } - throw std::assert_error(str(boost::format( + throw uhd::assert_error(str(boost::format( "Error: %s is not a valid %s. " "Possible values are: [%s]." ) @@ -127,4 +73,4 @@ namespace uhd{ }//namespace uhd -#endif /* INCLUDED_UHD_UTILS_HPP */ +#endif /* INCLUDED_UHD_UTILS_ASSERT_HPP */ diff --git a/host/include/uhd/utils/safe_main.hpp b/host/include/uhd/utils/safe_main.hpp new file mode 100644 index 000000000..dd738f2e3 --- /dev/null +++ b/host/include/uhd/utils/safe_main.hpp @@ -0,0 +1,43 @@ +// +// 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_UTILS_SAFE_MAIN_HPP +#define INCLUDED_UHD_UTILS_SAFE_MAIN_HPP + +#include <iostream> +#include <stdexcept> + +/*! + * Defines a safe wrapper that places a catch-all around main. + * If an exception is thrown, it prints to stderr and returns. + * Usage: int UHD_SAFE_MAIN(int argc, char *argv[]){ main code here } + * \param _argc the declaration for argc + * \param _argv the declaration for argv + */ +#define UHD_SAFE_MAIN(_argc, _argv) _main(int, char*[]); \ +int main(int argc, char *argv[]){ \ + try { \ + return _main(argc, argv); \ + } catch(const std::exception &e) { \ + std::cerr << "Error: " << e.what() << std::endl; \ + } catch(...) { \ + std::cerr << "Error: unknown exception" << std::endl; \ + } \ + return ~0; \ +} int _main(_argc, _argv) + +#endif /* INCLUDED_UHD_UTILS_SAFE_MAIN_HPP */ diff --git a/host/include/uhd/utils/static.hpp b/host/include/uhd/utils/static.hpp new file mode 100644 index 000000000..63db5a247 --- /dev/null +++ b/host/include/uhd/utils/static.hpp @@ -0,0 +1,35 @@ +// +// 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_UTILS_STATIC_HPP +#define INCLUDED_UHD_UTILS_STATIC_HPP + +/*! + * Defines a function that implements the "construct on first use" idiom + * \param _t the type definition for the instance + * \param _x the name of the defined function + * \return a reference to the lazy instance + */ +#define UHD_SINGLETON_FCN(_t, _x) static _t &_x(){static _t _x; return _x;} + +/*! + * Defines a static code block that will be called before main() + * \param _x the name of the defined struct (must be unique in file) + */ +#define UHD_STATIC_BLOCK(_x) static struct _x{_x();}_x;_x::_x() + +#endif /* INCLUDED_UHD_UTILS_STATIC_HPP */ diff --git a/host/lib/device.cpp b/host/lib/device.cpp index cd8a01ab4..0bdbf5f23 100644 --- a/host/lib/device.cpp +++ b/host/lib/device.cpp @@ -17,7 +17,8 @@ #include <uhd/device.hpp> #include <uhd/dict.hpp> -#include <uhd/utils.hpp> +#include <uhd/utils/assert.hpp> +#include <uhd/utils/static.hpp> #include <boost/foreach.hpp> #include <boost/format.hpp> #include <boost/weak_ptr.hpp> @@ -59,7 +60,7 @@ static size_t hash_device_addr( typedef boost::tuple<device::discover_t, device::make_t> dev_fcn_reg_t; // instantiate the device function registry container -STATIC_INSTANCE(std::vector<dev_fcn_reg_t>, get_dev_fcn_regs) +UHD_SINGLETON_FCN(std::vector<dev_fcn_reg_t>, get_dev_fcn_regs) void device::register_device( const discover_t &discover, @@ -136,7 +137,7 @@ device::sptr device::make(const device_addr_t &hint, size_t which){ return hash_to_device[dev_hash].lock(); } //create and register a new device - catch(const std::assert_error &){ + catch(const uhd::assert_error &){ device::sptr dev = maker(dev_addr); hash_to_device[dev_hash] = dev; return dev; diff --git a/host/lib/gain_handler.cpp b/host/lib/gain_handler.cpp index 7dd1547cb..7b3dd266c 100644 --- a/host/lib/gain_handler.cpp +++ b/host/lib/gain_handler.cpp @@ -16,7 +16,7 @@ // #include <uhd/gain_handler.hpp> -#include <uhd/utils.hpp> +#include <uhd/utils/assert.hpp> #include <uhd/types.hpp> #include <uhd/props.hpp> #include <boost/assign/list_of.hpp> diff --git a/host/lib/load_modules.cpp b/host/lib/load_modules.cpp index 77426b898..babff1ca5 100644 --- a/host/lib/load_modules.cpp +++ b/host/lib/load_modules.cpp @@ -15,7 +15,7 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. // -#include <uhd/utils.hpp> +#include <uhd/utils/static.hpp> #include <boost/format.hpp> #include <boost/foreach.hpp> #include <boost/algorithm/string.hpp> @@ -101,7 +101,7 @@ static void load_path(const fs::path &path){ * Load all the modules given by the module path enviroment variable. * The path variable may be several paths split by path separators. */ -STATIC_BLOCK(load_modules){ +UHD_STATIC_BLOCK(load_modules){ //get the environment variable module path char *env_module_path = std::getenv("UHD_MODULE_PATH"); if (env_module_path == NULL) return; diff --git a/host/lib/simple_device.cpp b/host/lib/simple_device.cpp index a25cb12e0..0eb69d9fa 100644 --- a/host/lib/simple_device.cpp +++ b/host/lib/simple_device.cpp @@ -16,8 +16,8 @@ // #include <uhd/simple_device.hpp> -#include <uhd/device.hpp> -#include <uhd/utils.hpp> +#include <uhd/utils/assert.hpp> +#include <uhd/utils/algorithm.hpp> #include <uhd/props.hpp> #include <uhd/types.hpp> #include <boost/algorithm/string.hpp> diff --git a/host/lib/usrp/dboard/basic.cpp b/host/lib/usrp/dboard/basic.cpp index 849cbd764..07cb8d11c 100644 --- a/host/lib/usrp/dboard/basic.cpp +++ b/host/lib/usrp/dboard/basic.cpp @@ -15,9 +15,10 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. // -#include <uhd/utils.hpp> #include <uhd/props.hpp> #include <uhd/types.hpp> +#include <uhd/utils/assert.hpp> +#include <uhd/utils/static.hpp> #include <uhd/usrp/dboard_base.hpp> #include <uhd/usrp/dboard_manager.hpp> #include <boost/assign/list_of.hpp> @@ -74,7 +75,7 @@ static dboard_base::sptr make_lf_tx(dboard_base::ctor_args_t const& args){ return dboard_base::sptr(new basic_tx(args, 32e6)); } -STATIC_BLOCK(reg_dboards){ +UHD_STATIC_BLOCK(reg_dboards){ dboard_manager::register_dboard(0x0000, &make_basic_tx, "Basic TX"); dboard_manager::register_dboard(0x0001, &make_basic_rx, "Basic RX", list_of("ab")("a")("b")); dboard_manager::register_dboard(0x000e, &make_lf_tx, "LF TX"); diff --git a/host/lib/usrp/dboard_manager.cpp b/host/lib/usrp/dboard_manager.cpp index 6683534f1..0f2189cd1 100644 --- a/host/lib/usrp/dboard_manager.cpp +++ b/host/lib/usrp/dboard_manager.cpp @@ -17,7 +17,8 @@ #include <uhd/usrp/dboard_manager.hpp> #include <uhd/gain_handler.hpp> -#include <uhd/utils.hpp> +#include <uhd/utils/static.hpp> +#include <uhd/utils/assert.hpp> #include <uhd/dict.hpp> #include <boost/tuple/tuple.hpp> #include <boost/format.hpp> @@ -35,7 +36,7 @@ typedef boost::tuple<dboard_manager::dboard_ctor_t, std::string, prop_names_t> a //map a dboard id to a dboard constructor typedef uhd::dict<dboard_id_t, args_t> id_to_args_map_t; -STATIC_INSTANCE(id_to_args_map_t, get_id_to_args_map) +UHD_SINGLETON_FCN(id_to_args_map_t, get_id_to_args_map) void dboard_manager::register_dboard( dboard_id_t dboard_id, diff --git a/host/lib/usrp/usrp2/dboard_impl.cpp b/host/lib/usrp/usrp2/dboard_impl.cpp index 66e02d469..6b49ff29d 100644 --- a/host/lib/usrp/usrp2/dboard_impl.cpp +++ b/host/lib/usrp/usrp2/dboard_impl.cpp @@ -15,7 +15,7 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. // -#include <uhd/utils.hpp> +#include <uhd/utils/assert.hpp> #include <boost/format.hpp> #include "usrp2_impl.hpp" diff --git a/host/lib/usrp/usrp2/dboard_interface.cpp b/host/lib/usrp/usrp2/dboard_interface.cpp index d20465147..8fc7864b0 100644 --- a/host/lib/usrp/usrp2/dboard_interface.cpp +++ b/host/lib/usrp/usrp2/dboard_interface.cpp @@ -15,7 +15,7 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. // -#include <uhd/utils.hpp> +#include <uhd/utils/assert.hpp> #include "usrp2_impl.hpp" using namespace uhd::usrp; diff --git a/host/lib/usrp/usrp2/dsp_impl.cpp b/host/lib/usrp/usrp2/dsp_impl.cpp index 44f654863..654096d14 100644 --- a/host/lib/usrp/usrp2/dsp_impl.cpp +++ b/host/lib/usrp/usrp2/dsp_impl.cpp @@ -15,7 +15,7 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. // -#include <uhd/utils.hpp> +#include <uhd/utils/assert.hpp> #include <boost/format.hpp> #include <boost/assign/list_of.hpp> #include <boost/math/special_functions/round.hpp> diff --git a/host/lib/usrp/usrp2/mboard_impl.cpp b/host/lib/usrp/usrp2/mboard_impl.cpp index c56782c4b..e4706dcf0 100644 --- a/host/lib/usrp/usrp2/mboard_impl.cpp +++ b/host/lib/usrp/usrp2/mboard_impl.cpp @@ -15,7 +15,7 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. // -#include <uhd/utils.hpp> +#include <uhd/utils/assert.hpp> #include "usrp2_impl.hpp" using namespace uhd; diff --git a/host/lib/usrp/usrp2/usrp2_impl.cpp b/host/lib/usrp/usrp2/usrp2_impl.cpp index 35a4aeb20..9dce351be 100644 --- a/host/lib/usrp/usrp2/usrp2_impl.cpp +++ b/host/lib/usrp/usrp2/usrp2_impl.cpp @@ -16,7 +16,8 @@ // #include <uhd/transport/if_addrs.hpp> -#include <uhd/utils.hpp> +#include <uhd/utils/assert.hpp> +#include <uhd/utils/static.hpp> #include <boost/format.hpp> #include <boost/foreach.hpp> #include <boost/bind.hpp> @@ -28,7 +29,7 @@ using namespace uhd::usrp; using namespace uhd::transport; namespace asio = boost::asio; -STATIC_BLOCK(register_usrp2_device){ +UHD_STATIC_BLOCK(register_usrp2_device){ device::register_device(&usrp2::discover, &usrp2::make); } diff --git a/host/test/module_test.cpp b/host/test/module_test.cpp index 71721ef90..47a0e1af9 100644 --- a/host/test/module_test.cpp +++ b/host/test/module_test.cpp @@ -15,10 +15,10 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. // -#include <uhd/utils.hpp> +#include <uhd/utils/static.hpp> #include <iostream> -STATIC_BLOCK(module_test){ +UHD_STATIC_BLOCK(module_test){ std::cout << "---------------------------------------" << std::endl; std::cout << "-- Good news, everyone!" << std::endl; std::cout << "-- The test module has been loaded." << std::endl; diff --git a/host/utils/discover_usrps.cpp b/host/utils/discover_usrps.cpp index d670d1651..dc2b845bc 100644 --- a/host/utils/discover_usrps.cpp +++ b/host/utils/discover_usrps.cpp @@ -15,6 +15,7 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. // +#include <uhd/utils/safe_main.hpp> #include <uhd/device.hpp> #include <uhd/props.hpp> #include <boost/program_options.hpp> @@ -23,7 +24,7 @@ namespace po = boost::program_options; -int main(int argc, char *argv[]){ +int UHD_SAFE_MAIN(int argc, char *argv[]){ po::options_description desc("Allowed options"); desc.add_options() ("help", "help message") diff --git a/host/utils/usrp2_burner.cpp b/host/utils/usrp2_burner.cpp index 941e71d0c..ff6d4426f 100644 --- a/host/utils/usrp2_burner.cpp +++ b/host/utils/usrp2_burner.cpp @@ -15,6 +15,7 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. // +#include <uhd/utils/safe_main.hpp> #include <uhd/usrp/usrp2.hpp> #include <uhd/props.hpp> #include <boost/program_options.hpp> @@ -23,7 +24,7 @@ namespace po = boost::program_options; -int main(int argc, char *argv[]){ +int UHD_SAFE_MAIN(int argc, char *argv[]){ po::options_description desc("Allowed options"); desc.add_options() ("help", "help message") |