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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
//
//! Copyright (c) 2011
//! Brandon Kohn
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <boost/operators.hpp>
#include <boost/numeric/conversion/cast.hpp>
#include <boost/mpl/for_each.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/cstdint.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/static_assert.hpp>
//! Define a simple custom number
struct Double
{
Double()
: v(0)
{}
template <typename T>
explicit Double( T v )
: v(static_cast<double>(v))
{}
template <typename T>
Double& operator= ( T t )
{
v = static_cast<double>(t);
return *this;
}
bool operator < ( const Double& rhs ) const
{
return v < rhs.v;
}
template <typename T>
bool operator < ( T rhs ) const
{
return v < static_cast<double>(rhs);
}
template <typename LHS>
friend bool operator < ( const LHS& lhs, const Double& rhs )
{
return lhs < rhs.v;
}
bool operator > ( const Double& rhs ) const
{
return v > rhs.v;
}
template <typename LHS>
friend bool operator > ( const LHS& lhs, const Double& rhs )
{
return lhs > rhs.v;
}
template <typename T>
bool operator > ( T rhs ) const
{
return v > static_cast<double>(rhs);
}
bool operator == ( const Double& rhs ) const
{
return v == rhs.v;
}
template <typename T>
bool operator == ( T rhs ) const
{
return v == static_cast<double>(rhs);
}
template <typename LHS>
friend bool operator == ( const LHS& lhs, const Double& rhs )
{
return lhs == rhs.v;
}
bool operator !() const
{
return v == 0;
}
Double operator -() const
{
return Double(-v);
}
Double& operator +=( const Double& t )
{
v += t.v;
return *this;
}
template <typename T>
Double& operator +=( T t )
{
v += static_cast<double>(t);
return *this;
}
Double& operator -=( const Double& t )
{
v -= t.v;
return *this;
}
template <typename T>
Double& operator -=( T t )
{
v -= static_cast<double>(t);
return *this;
}
Double& operator *= ( const Double& factor )
{
v *= factor.v;
return *this;
}
template <typename T>
Double& operator *=( T t )
{
v *= static_cast<double>(t);
return *this;
}
Double& operator /= (const Double& divisor)
{
v /= divisor.v;
return *this;
}
template <typename T>
Double& operator /=( T t )
{
v /= static_cast<double>(t);
return (*this);
}
double v;
};
//! Define numeric_limits for the custom type.
namespace std
{
template<>
class numeric_limits< Double > : public numeric_limits<double>
{
public:
//! Limit our Double to a range of +/- 100.0
static Double (min)()
{
return Double(1.e-2);
}
static Double (max)()
{
return Double(1.e2);
}
static Double epsilon()
{
return Double( std::numeric_limits<double>::epsilon() );
}
};
}
//! Define range checking and overflow policies.
namespace custom
{
//! Define a custom range checker
template<typename Traits, typename OverFlowHandler>
struct range_checker
{
typedef typename Traits::argument_type argument_type ;
typedef typename Traits::source_type S;
typedef typename Traits::target_type T;
//! Check range of integral types.
static boost::numeric::range_check_result out_of_range( argument_type s )
{
using namespace boost::numeric;
if( s > bounds<T>::highest() )
return cPosOverflow;
else if( s < bounds<T>::lowest() )
return cNegOverflow;
else
return cInRange;
}
static void validate_range ( argument_type s )
{
BOOST_STATIC_ASSERT( std::numeric_limits<T>::is_bounded );
OverFlowHandler()( out_of_range(s) );
}
};
//! Overflow handler
struct positive_overflow{};
struct negative_overflow{};
struct overflow_handler
{
void operator() ( boost::numeric::range_check_result r )
{
using namespace boost::numeric;
if( r == cNegOverflow )
throw negative_overflow() ;
else if( r == cPosOverflow )
throw positive_overflow() ;
}
};
//! Define a rounding policy and specialize on the custom type.
template<class S>
struct Ceil : boost::numeric::Ceil<S>{};
template<>
struct Ceil<Double>
{
typedef Double source_type;
typedef Double const& argument_type;
static source_type nearbyint ( argument_type s )
{
#if !defined(BOOST_NO_STDC_NAMESPACE)
using std::ceil ;
#endif
return Double( ceil(s.v) );
}
typedef boost::mpl::integral_c< std::float_round_style, std::round_toward_infinity> round_style;
};
//! Define a rounding policy and specialize on the custom type.
template<class S>
struct Trunc: boost::numeric::Trunc<S>{};
template<>
struct Trunc<Double>
{
typedef Double source_type;
typedef Double const& argument_type;
static source_type nearbyint ( argument_type s )
{
#if !defined(BOOST_NO_STDC_NAMESPACE)
using std::floor;
#endif
return Double( floor(s.v) );
}
typedef boost::mpl::integral_c< std::float_round_style, std::round_toward_zero> round_style;
};
}//namespace custom;
namespace boost { namespace numeric {
//! Define the numeric_cast_traits specializations on the custom type.
template <typename S>
struct numeric_cast_traits<Double, S>
{
typedef custom::overflow_handler overflow_policy;
typedef custom::range_checker
<
boost::numeric::conversion_traits<Double, S>
, overflow_policy
> range_checking_policy;
typedef boost::numeric::Trunc<S> rounding_policy;
};
template <typename T>
struct numeric_cast_traits<T, Double>
{
typedef custom::overflow_handler overflow_policy;
typedef custom::range_checker
<
boost::numeric::conversion_traits<T, Double>
, overflow_policy
> range_checking_policy;
typedef custom::Trunc<Double> rounding_policy;
};
//! Define the conversion from the custom type to built-in types and vice-versa.
template<typename T>
struct raw_converter< conversion_traits< T, Double > >
{
static T low_level_convert ( const Double& n )
{
return static_cast<T>( n.v );
}
};
template<typename S>
struct raw_converter< conversion_traits< Double, S > >
{
static Double low_level_convert ( const S& n )
{
return Double(n);
}
};
}}//namespace boost::numeric;
#define BOOST_TEST_CATCH_CUSTOM_POSITIVE_OVERFLOW( CastCode ) \
BOOST_TEST_THROWS( CastCode, custom::positive_overflow )
/***/
#define BOOST_TEST_CATCH_CUSTOM_NEGATIVE_OVERFLOW( CastCode ) \
BOOST_TEST_THROWS( CastCode, custom::negative_overflow )
/***/
struct test_cast_traits
{
template <typename T>
void operator()(T) const
{
Double d = boost::numeric_cast<Double>( static_cast<T>(50) );
BOOST_TEST( d.v == 50. );
T v = boost::numeric_cast<T>( d );
BOOST_TEST( v == 50 );
}
};
void test_numeric_cast_traits()
{
typedef boost::mpl::vector
<
boost::int8_t
, boost::uint8_t
, boost::int16_t
, boost::uint16_t
, boost::int32_t
, boost::uint32_t
#if !defined( BOOST_NO_INT64_T )
, boost::int64_t
, boost::uint64_t
#endif
, float
, double
, long double
> types;
boost::mpl::for_each<types>( test_cast_traits() );
//! Check overflow handler.
Double d( 56.0 );
BOOST_TEST_CATCH_CUSTOM_POSITIVE_OVERFLOW( d = boost::numeric_cast<Double>( 101 ) );
BOOST_TEST( d.v == 56. );
BOOST_TEST_CATCH_CUSTOM_NEGATIVE_OVERFLOW( d = boost::numeric_cast<Double>( -101 ) );
BOOST_TEST( d.v == 56.);
//! Check custom round policy.
d = 5.9;
int five = boost::numeric_cast<int>( d );
BOOST_TEST( five == 5 );
}
int main()
{
test_numeric_cast_traits();
return boost::report_errors();
}
#undef BOOST_TEST_CATCH_CUSTOM_POSITIVE_OVERFLOW
#undef BOOST_TEST_CATCH_CUSTOM_NEGATIVE_OVERFLOW
|