sl@0
|
1 |
/* Boost interval/checking.hpp template implementation file
|
sl@0
|
2 |
*
|
sl@0
|
3 |
* Copyright 2002 Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion
|
sl@0
|
4 |
*
|
sl@0
|
5 |
* Distributed under the Boost Software License, Version 1.0.
|
sl@0
|
6 |
* (See accompanying file LICENSE_1_0.txt or
|
sl@0
|
7 |
* copy at http://www.boost.org/LICENSE_1_0.txt)
|
sl@0
|
8 |
*/
|
sl@0
|
9 |
|
sl@0
|
10 |
#ifndef BOOST_NUMERIC_INTERVAL_CHECKING_HPP
|
sl@0
|
11 |
#define BOOST_NUMERIC_INTERVAL_CHECKING_HPP
|
sl@0
|
12 |
|
sl@0
|
13 |
#include <stdexcept>
|
sl@0
|
14 |
#include <string>
|
sl@0
|
15 |
#include <cassert>
|
sl@0
|
16 |
#include <boost/limits.hpp>
|
sl@0
|
17 |
|
sl@0
|
18 |
namespace boost {
|
sl@0
|
19 |
namespace numeric {
|
sl@0
|
20 |
namespace interval_lib {
|
sl@0
|
21 |
|
sl@0
|
22 |
struct exception_create_empty
|
sl@0
|
23 |
{
|
sl@0
|
24 |
void operator()()
|
sl@0
|
25 |
{
|
sl@0
|
26 |
throw std::runtime_error("boost::interval: empty interval created");
|
sl@0
|
27 |
}
|
sl@0
|
28 |
};
|
sl@0
|
29 |
|
sl@0
|
30 |
struct exception_invalid_number
|
sl@0
|
31 |
{
|
sl@0
|
32 |
void operator()()
|
sl@0
|
33 |
{
|
sl@0
|
34 |
throw std::invalid_argument("boost::interval: invalid number");
|
sl@0
|
35 |
}
|
sl@0
|
36 |
};
|
sl@0
|
37 |
|
sl@0
|
38 |
template<class T>
|
sl@0
|
39 |
struct checking_base
|
sl@0
|
40 |
{
|
sl@0
|
41 |
static T pos_inf()
|
sl@0
|
42 |
{
|
sl@0
|
43 |
assert(std::numeric_limits<T>::has_infinity);
|
sl@0
|
44 |
return std::numeric_limits<T>::infinity();
|
sl@0
|
45 |
}
|
sl@0
|
46 |
static T neg_inf()
|
sl@0
|
47 |
{
|
sl@0
|
48 |
assert(std::numeric_limits<T>::has_infinity);
|
sl@0
|
49 |
return -std::numeric_limits<T>::infinity();
|
sl@0
|
50 |
}
|
sl@0
|
51 |
static T nan()
|
sl@0
|
52 |
{
|
sl@0
|
53 |
assert(std::numeric_limits<T>::has_quiet_NaN);
|
sl@0
|
54 |
return std::numeric_limits<T>::quiet_NaN();
|
sl@0
|
55 |
}
|
sl@0
|
56 |
static bool is_nan(const T& x)
|
sl@0
|
57 |
{
|
sl@0
|
58 |
return std::numeric_limits<T>::has_quiet_NaN && (x != x);
|
sl@0
|
59 |
}
|
sl@0
|
60 |
static T empty_lower()
|
sl@0
|
61 |
{
|
sl@0
|
62 |
return (std::numeric_limits<T>::has_quiet_NaN ?
|
sl@0
|
63 |
std::numeric_limits<T>::quiet_NaN() : static_cast<T>(1));
|
sl@0
|
64 |
}
|
sl@0
|
65 |
static T empty_upper()
|
sl@0
|
66 |
{
|
sl@0
|
67 |
return (std::numeric_limits<T>::has_quiet_NaN ?
|
sl@0
|
68 |
std::numeric_limits<T>::quiet_NaN() : static_cast<T>(0));
|
sl@0
|
69 |
}
|
sl@0
|
70 |
static bool is_empty(const T& l, const T& u)
|
sl@0
|
71 |
{
|
sl@0
|
72 |
return !(l <= u); // safety for partial orders
|
sl@0
|
73 |
}
|
sl@0
|
74 |
};
|
sl@0
|
75 |
|
sl@0
|
76 |
template<class T, class Checking = checking_base<T>,
|
sl@0
|
77 |
class Exception = exception_create_empty>
|
sl@0
|
78 |
struct checking_no_empty: Checking
|
sl@0
|
79 |
{
|
sl@0
|
80 |
static T nan()
|
sl@0
|
81 |
{
|
sl@0
|
82 |
assert(false);
|
sl@0
|
83 |
return Checking::nan();
|
sl@0
|
84 |
}
|
sl@0
|
85 |
static T empty_lower()
|
sl@0
|
86 |
{
|
sl@0
|
87 |
Exception()();
|
sl@0
|
88 |
return Checking::empty_lower();
|
sl@0
|
89 |
}
|
sl@0
|
90 |
static T empty_upper()
|
sl@0
|
91 |
{
|
sl@0
|
92 |
Exception()();
|
sl@0
|
93 |
return Checking::empty_upper();
|
sl@0
|
94 |
}
|
sl@0
|
95 |
static bool is_empty(const T&, const T&)
|
sl@0
|
96 |
{
|
sl@0
|
97 |
return false;
|
sl@0
|
98 |
}
|
sl@0
|
99 |
};
|
sl@0
|
100 |
|
sl@0
|
101 |
template<class T, class Checking = checking_base<T> >
|
sl@0
|
102 |
struct checking_no_nan: Checking
|
sl@0
|
103 |
{
|
sl@0
|
104 |
static bool is_nan(const T&)
|
sl@0
|
105 |
{
|
sl@0
|
106 |
return false;
|
sl@0
|
107 |
}
|
sl@0
|
108 |
};
|
sl@0
|
109 |
|
sl@0
|
110 |
template<class T, class Checking = checking_base<T>,
|
sl@0
|
111 |
class Exception = exception_invalid_number>
|
sl@0
|
112 |
struct checking_catch_nan: Checking
|
sl@0
|
113 |
{
|
sl@0
|
114 |
static bool is_nan(const T& x)
|
sl@0
|
115 |
{
|
sl@0
|
116 |
if (Checking::is_nan(x)) Exception()();
|
sl@0
|
117 |
return false;
|
sl@0
|
118 |
}
|
sl@0
|
119 |
};
|
sl@0
|
120 |
|
sl@0
|
121 |
template<class T>
|
sl@0
|
122 |
struct checking_strict:
|
sl@0
|
123 |
checking_no_nan<T, checking_no_empty<T> >
|
sl@0
|
124 |
{};
|
sl@0
|
125 |
|
sl@0
|
126 |
} // namespace interval_lib
|
sl@0
|
127 |
} // namespace numeric
|
sl@0
|
128 |
} // namespace boost
|
sl@0
|
129 |
|
sl@0
|
130 |
#endif // BOOST_NUMERIC_INTERVAL_CHECKING_HPP
|