| 1 | // Copyright (C) 2003, Fernando Luis Cacciola Carballal. |
| 2 | // |
| 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) |
| 6 | // |
| 7 | |
| 8 | |
| 9 | // |
| 10 | // NOTE: This file is intended to be used ONLY by the test files |
| 11 | // from the Numeric Conversions Library |
| 12 | // |
| 13 | // |
| 14 | #include <cmath> |
| 15 | |
| 16 | #include "boost/limits.hpp" |
| 17 | #include "boost/utility.hpp" |
| 18 | |
| 19 | #include <boost/core/lightweight_test.hpp> |
| 20 | #include <stdexcept> // std::runtime_error |
| 21 | |
| 22 | // Convenience macros to help with compilers which don't parse |
| 23 | // explicit template function instantiations (MSVC6) |
| 24 | #define MATCH_FNTPL_ARG(t) t const* |
| 25 | #define SET_FNTPL_ARG(t) (static_cast< t const* >(0)) |
| 26 | |
| 27 | // |
| 28 | // *Minimal* example of a User Defined Numeric Type |
| 29 | // |
| 30 | // |
| 31 | namespace MyUDT |
| 32 | { |
| 33 | |
| 34 | template<class T> |
| 35 | struct UDT |
| 36 | { |
| 37 | typedef T builtin_type ; |
| 38 | |
| 39 | UDT ( T v_ ) : v (v_) {} |
| 40 | |
| 41 | T to_builtin() const { return v ; } |
| 42 | |
| 43 | friend bool operator == ( UDT const& lhs, UDT const& rhs ) |
| 44 | { return lhs.to_builtin() == rhs.to_builtin() ; } |
| 45 | |
| 46 | // NOTE: This operator is *required* by the Numeric Conversion Library |
| 47 | // if Turnc<> is used as the Float2IntRounder policy. |
| 48 | friend bool operator < ( UDT const& lhs, UDT const& rhs ) |
| 49 | { return lhs.to_builtin() < rhs.to_builtin() ; } |
| 50 | |
| 51 | friend std::ostream& operator << ( std::ostream& os, UDT const& n ) |
| 52 | { return os << n.to_builtin() ; } |
| 53 | |
| 54 | T v ; |
| 55 | } ; |
| 56 | |
| 57 | typedef UDT<int> MyInt ; |
| 58 | typedef UDT<double> MyFloat ; |
| 59 | |
| 60 | // |
| 61 | // The Float2IntRounder policies *require* a visible 'ceil' or 'floor' math function |
| 62 | // with standard semantics. |
| 63 | // In a conformant compiler, ADL can pick these functions even if they are defined |
| 64 | // within a user namespace, as below. |
| 65 | // |
| 66 | inline MyInt ceil ( MyInt const& x ) { return x ; } |
| 67 | inline MyInt floor ( MyInt const& x ) { return x ; } |
| 68 | |
| 69 | inline MyFloat floor ( MyFloat const& x ) |
| 70 | { |
| 71 | #if !defined(BOOST_NO_STDC_NAMESPACE) |
| 72 | return MyFloat ( std::floor(x: x.to_builtin()) ) ; |
| 73 | #else |
| 74 | return MyFloat ( ::floor(x.to_builtin()) ) ; |
| 75 | #endif |
| 76 | } |
| 77 | |
| 78 | inline MyFloat ceil ( MyFloat const& x ) |
| 79 | { |
| 80 | #if !defined(BOOST_NO_STDC_NAMESPACE) |
| 81 | return MyFloat ( std::ceil(x: x.to_builtin()) ) ; |
| 82 | #else |
| 83 | return MyFloat ( ::ceil(x.to_builtin()) ) ; |
| 84 | #endif |
| 85 | } |
| 86 | |
| 87 | } // namespace MyUDT |
| 88 | |
| 89 | |
| 90 | // |
| 91 | // The Numeric Conversion Library *requires* User Defined Numeric Types |
| 92 | // to properly specialize std::numeric_limits<> |
| 93 | // |
| 94 | namespace std |
| 95 | { |
| 96 | |
| 97 | template<> |
| 98 | class numeric_limits<MyUDT::MyInt> : public numeric_limits<int> |
| 99 | { |
| 100 | public : |
| 101 | |
| 102 | BOOST_STATIC_CONSTANT(bool, is_specialized = false); |
| 103 | } ; |
| 104 | |
| 105 | template<> |
| 106 | class numeric_limits<MyUDT::MyFloat> : public numeric_limits<double> |
| 107 | { |
| 108 | public : |
| 109 | |
| 110 | BOOST_STATIC_CONSTANT(bool, is_specialized = false); |
| 111 | } ; |
| 112 | |
| 113 | } // namespace std |
| 114 | |
| 115 | |
| 116 | |
| 117 | // |
| 118 | // The functions floor and ceil defined within namespace MyUDT |
| 119 | // should be found by koenig loopkup, but some compilers don't do it right |
| 120 | // so we inyect them into namespace std so ordinary overload resolution |
| 121 | // can found them. |
| 122 | #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP) || defined(BOOST_BORLANDC) || defined(__GNUC__) |
| 123 | namespace std { |
| 124 | using MyUDT::floor ; |
| 125 | using MyUDT::ceil ; |
| 126 | } // namespace std |
| 127 | #endif |
| 128 | |
| 129 | |
| 130 | std::string to_string( bool arg ) |
| 131 | { |
| 132 | return arg ? "true" : "false" ; |
| 133 | } |
| 134 | |
| 135 | std::string to_string( ... ) { throw std::runtime_error("to_string() called with wrong type!" ) ; } |
| 136 | |
| 137 | // |
| 138 | // This is used to print 'char' values as numbers instead of characters. |
| 139 | // |
| 140 | template<class T> struct printable_number_type { typedef T type ; } ; |
| 141 | template<> struct printable_number_type<signed char> { typedef int type ; } ; |
| 142 | template<> struct printable_number_type<unsigned char> { typedef unsigned type ; } ; |
| 143 | template<> struct printable_number_type<char> { typedef int type ; } ; |
| 144 | |
| 145 | template<class T> |
| 146 | inline |
| 147 | typename printable_number_type<T>::type |
| 148 | printable( T n ) { return n ; } |
| 149 | |
| 150 | |
| 151 | // |
| 152 | /////////////////////////////////////////////////////////////////////////////////////////////// |
| 153 | |
| 154 | |