1 // Copyright (C) 2003, Fernando Luis Cacciola Carballal.
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
10 // NOTE: This file is intended to be used ONLY by the test files
11 // from the Numeric Conversions Library
16 #include "boost/limits.hpp"
17 #include "boost/utility.hpp"
19 #include <boost/core/lightweight_test.hpp>
21 // Convenience macros to help with compilers which don't parse
22 // explicit template function instantiations (MSVC6)
23 #define MATCH_FNTPL_ARG(t) t const*
24 #define SET_FNTPL_ARG(t) (static_cast< t const* >(0))
27 // *Minimal* example of a User Defined Numeric Type
36 typedef T builtin_type
;
38 UDT ( T v_
) : v (v_
) {}
40 T
to_builtin() const { return v
; }
42 friend bool operator == ( UDT
const& lhs
, UDT
const& rhs
)
43 { return lhs
.to_builtin() == rhs
.to_builtin() ; }
45 // NOTE: This operator is *required* by the Numeric Conversion Library
46 // if Turnc<> is used as the Float2IntRounder policy.
47 friend bool operator < ( UDT
const& lhs
, UDT
const& rhs
)
48 { return lhs
.to_builtin() < rhs
.to_builtin() ; }
50 friend std::ostream
& operator << ( std::ostream
& os
, UDT
const& n
)
51 { return os
<< n
.to_builtin() ; }
56 typedef UDT
<int> MyInt
;
57 typedef UDT
<double> MyFloat
;
60 // The Float2IntRounder policies *require* a visible 'ceil' or 'floor' math function
61 // with standard semantics.
62 // In a conformant compiler, ADL can pick these functions even if they are defined
63 // within a user namespace, as below.
65 inline MyInt
ceil ( MyInt
const& x
) { return x
; }
66 inline MyInt
floor ( MyInt
const& x
) { return x
; }
68 inline MyFloat
floor ( MyFloat
const& x
)
70 #if !defined(BOOST_NO_STDC_NAMESPACE)
71 return MyFloat ( std::floor(x
.to_builtin()) ) ;
73 return MyFloat ( ::floor(x
.to_builtin()) ) ;
77 inline MyFloat
ceil ( MyFloat
const& x
)
79 #if !defined(BOOST_NO_STDC_NAMESPACE)
80 return MyFloat ( std::ceil(x
.to_builtin()) ) ;
82 return MyFloat ( ::ceil(x
.to_builtin()) ) ;
90 // The Numeric Conversion Library *requires* User Defined Numeric Types
91 // to properly specialize std::numeric_limits<>
97 class numeric_limits
<MyUDT::MyInt
> : public numeric_limits
<int>
101 BOOST_STATIC_CONSTANT(bool, is_specialized
= false);
105 class numeric_limits
<MyUDT::MyFloat
> : public numeric_limits
<double>
109 BOOST_STATIC_CONSTANT(bool, is_specialized
= false);
117 // The functions floor and ceil defined within namespace MyUDT
118 // should be found by koenig loopkup, but some compilers don't do it right
119 // so we inyect them into namespace std so ordinary overload resolution
121 #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP) || defined(BOOST_BORLANDC) || defined(__GNUC__)
129 std::string
to_string( bool arg
)
131 return arg
? "true" : "false" ;
134 std::string
to_string( ... ) { throw std::runtime_error("to_string() called with wrong type!") ; }
137 // This is used to print 'char' values as numbers instead of characters.
139 template<class T
> struct printable_number_type
{ typedef T type
; } ;
140 template<> struct printable_number_type
<signed char> { typedef int type
; } ;
141 template<> struct printable_number_type
<unsigned char> { typedef unsigned type
; } ;
142 template<> struct printable_number_type
<char> { typedef int type
; } ;
146 typename printable_number_type
<T
>::type
147 printable( T n
) { return n
; }
151 ///////////////////////////////////////////////////////////////////////////////////////////////