diff options
author | Josh Blum <josh@joshknows.com> | 2010-04-25 22:05:50 -0700 |
---|---|---|
committer | Josh Blum <josh@joshknows.com> | 2010-04-25 22:05:50 -0700 |
commit | 61ec6711bb4bbae7a8a26cc631eeb88fb3e7d688 (patch) | |
tree | 12cfaf308bcbd779dd5a1254b12a6104088629cc /host/include | |
parent | d1d6c859f0dbae5f044519d589d5ad3fcbb8643e (diff) | |
download | uhd-61ec6711bb4bbae7a8a26cc631eeb88fb3e7d688.tar.gz uhd-61ec6711bb4bbae7a8a26cc631eeb88fb3e7d688.tar.bz2 uhd-61ec6711bb4bbae7a8a26cc631eeb88fb3e7d688.zip |
Work on exceptions.
Added props exception macro to make the set/get prop switch statements easier.
Made use of boost throw exception macro for throw-site information in throw assert.
Diffstat (limited to 'host/include')
-rw-r--r-- | host/include/uhd/utils/assert.hpp | 33 | ||||
-rw-r--r-- | host/include/uhd/utils/props.hpp | 29 | ||||
-rw-r--r-- | host/include/uhd/utils/safe_main.hpp | 3 |
3 files changed, 47 insertions, 18 deletions
diff --git a/host/include/uhd/utils/assert.hpp b/host/include/uhd/utils/assert.hpp index 842ed8dfa..773acd634 100644 --- a/host/include/uhd/utils/assert.hpp +++ b/host/include/uhd/utils/assert.hpp @@ -18,27 +18,27 @@ #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 <boost/throw_exception.hpp> +#include <boost/exception/info.hpp> #include <stdexcept> +#include <string> namespace uhd{ - class assert_error : public std::logic_error{ - public: - explicit assert_error(const std::string& what_arg) : logic_error(what_arg){ - /* NOP */ - } - }; + //! The exception to throw when assertions fail + struct UHD_API assert_error : virtual std::exception, virtual boost::exception{}; - #define ASSERT_THROW(_x) if (not (_x)) { \ - throw uhd::assert_error(str(boost::format( \ - "Assertion Failed:\n %s:%d\n %s\n ---> %s <---" \ - ) % __FILE__ % __LINE__ % BOOST_CURRENT_FUNCTION % std::string(#_x))); \ - } + //! The assertion info, the code that failed + typedef boost::error_info<struct tag_assert_info, std::string> assert_info; + + //! Throw an assert error with throw-site information + #define ASSERT_THROW(_x) if (not (_x)) \ + BOOST_THROW_EXCEPTION(uhd::assert_error() << uhd::assert_info(#_x)) /*! * Check that an element is found in a container. @@ -58,17 +58,18 @@ namespace uhd{ ){ if (std::has(iterable, elem)) return; std::string possible_values = ""; - BOOST_FOREACH(T e, iterable){ - if (e != iterable.begin()[0]) possible_values += ", "; + size_t i = 0; + BOOST_FOREACH(const T &e, iterable){ + if (i++ > 0) possible_values += ", "; possible_values += boost::lexical_cast<std::string>(e); } - throw uhd::assert_error(str(boost::format( + 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<std::string>(elem) % what % possible_values - )); + ))); } }//namespace uhd diff --git a/host/include/uhd/utils/props.hpp b/host/include/uhd/utils/props.hpp index 6be0b2ce5..bfbca4273 100644 --- a/host/include/uhd/utils/props.hpp +++ b/host/include/uhd/utils/props.hpp @@ -21,6 +21,9 @@ #include <uhd/config.hpp> #include <uhd/wax.hpp> #include <boost/tuple/tuple.hpp> +#include <boost/throw_exception.hpp> +#include <boost/exception/info.hpp> +#include <stdexcept> #include <vector> #include <string> @@ -35,14 +38,36 @@ namespace uhd{ * \param key a reference to the prop object * \param name a reference to the name object */ - inline UHD_API named_prop_t //must be exported as part of the api to work (TODO move guts to cpp file) - extract_named_prop(const wax::obj &key, const std::string &name = ""){ + inline UHD_API named_prop_t extract_named_prop( + const wax::obj &key, + const std::string &name = "" + ){ if (key.type() == typeid(named_prop_t)){ return key.as<named_prop_t>(); } return named_prop_t(key, name); } + //! The exception to throw for property errors + struct UHD_API prop_error : virtual std::exception, virtual boost::exception{}; + + //! The property error info (verbose or message) + typedef boost::error_info<struct tag_prop_info, std::string> prop_info; + + /*! + * Throw an error when trying to get a write only property. + * Throw-site information will be included with this error. + */ + #define UHD_THROW_PROP_WRITE_ONLY() \ + BOOST_THROW_EXCEPTION(uhd::prop_error() << uhd::prop_info("cannot get write-only property")) + + /*! + * Throw an error when trying to set a read only property. + * Throw-site information will be included with this error. + */ + #define UHD_THROW_PROP_READ_ONLY() \ + BOOST_THROW_EXCEPTION(uhd::prop_error() << uhd::prop_info("cannot set read-only property")) + } //namespace uhd #endif /* INCLUDED_UHD_UTILS_PROPS_HPP */ diff --git a/host/include/uhd/utils/safe_main.hpp b/host/include/uhd/utils/safe_main.hpp index b682aa540..a4e4e06e8 100644 --- a/host/include/uhd/utils/safe_main.hpp +++ b/host/include/uhd/utils/safe_main.hpp @@ -19,6 +19,7 @@ #define INCLUDED_UHD_UTILS_SAFE_MAIN_HPP #include <uhd/config.hpp> +#include <boost/exception/diagnostic_information.hpp> #include <iostream> #include <stdexcept> @@ -33,6 +34,8 @@ int main(int argc, char *argv[]){ \ try { \ return _main(argc, argv); \ + } catch(const boost::exception &e){ \ + std::cerr << "Error: " << boost::diagnostic_information(e) << std::endl; \ } catch(const std::exception &e) { \ std::cerr << "Error: " << e.what() << std::endl; \ } catch(...) { \ |