1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
//
// 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_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/throw_exception.hpp>
#include <boost/exception/info.hpp>
#include <stdexcept>
#include <string>
#ifndef BOOST_THROW_EXCEPTION
#include <boost/exception/exception.hpp>
#include <boost/current_function.hpp>
#define BOOST_THROW_EXCEPTION(x)\
::boost::throw_exception( ::boost::enable_error_info(x) <<\
::boost::throw_function(BOOST_CURRENT_FUNCTION) <<\
::boost::throw_file(__FILE__) <<\
::boost::throw_line((int)__LINE__) )
#endif
namespace uhd{
//! The exception to throw when assertions fail
struct UHD_API assert_error : virtual std::exception, virtual boost::exception{};
//! 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 UHD_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.
* If not, throw a meaningful assertion error.
* The "what" in the error will show what is
* being set and a list of known good values.
*
* \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<typename T, typename Range> void assert_has(
const Range &range,
const T &value,
const std::string &what = "unknown"
){
if (std::has(range, value)) return;
std::string possible_values = "";
size_t i = 0;
BOOST_FOREACH(const T &v, range){
if (i++ > 0) possible_values += ", ";
possible_values += boost::lexical_cast<std::string>(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<std::string>(value)
% what % possible_values
)));
}
}//namespace uhd
#endif /* INCLUDED_UHD_UTILS_ASSERT_HPP */
|