1 /*-----------------------------------------------------------------------------+ 2 Copyright (c) 2010-2011: Joachim Faulhaber 3 +------------------------------------------------------------------------------+ 4 Distributed under the Boost Software License, Version 1.0. 5 (See accompanying file LICENCE.txt or copy at 6 http://www.boost.org/LICENSE_1_0.txt) 7 +-----------------------------------------------------------------------------*/ 8 #ifndef BOOST_ICL_TYPE_TRAITS_INFINITY_HPP_JOFA_100322 9 #define BOOST_ICL_TYPE_TRAITS_INFINITY_HPP_JOFA_100322 10 11 #include <string> 12 #include <boost/static_assert.hpp> 13 #include <boost/icl/type_traits/is_numeric.hpp> 14 #include <boost/icl/type_traits/rep_type_of.hpp> 15 #include <boost/icl/type_traits/size_type_of.hpp> 16 #include <boost/mpl/and.hpp> 17 #include <boost/mpl/if.hpp> 18 19 namespace boost{ namespace icl 20 { 21 22 template<class Type> struct has_std_infinity 23 { 24 typedef has_std_infinity type; 25 BOOST_STATIC_CONSTANT(bool, 26 value = ( is_numeric<Type>::value 27 && std::numeric_limits<Type>::has_infinity 28 ) 29 ); 30 }; 31 32 template<class Type> struct has_max_infinity 33 { 34 typedef has_max_infinity type; 35 BOOST_STATIC_CONSTANT(bool, 36 value = ( is_numeric<Type>::value 37 && ! std::numeric_limits<Type>::has_infinity 38 ) 39 ); 40 }; 41 42 //------------------------------------------------------------------------------ 43 template <class Type, bool has_std_inf=false, bool has_std_max=false> 44 struct get_numeric_infinity; 45 46 template <class Type, bool has_std_max> 47 struct get_numeric_infinity<Type, true, has_std_max> 48 { 49 typedef get_numeric_infinity type; valueboost::icl::get_numeric_infinity50 static Type value() 51 { 52 return (std::numeric_limits<Type>::infinity)(); 53 } 54 }; 55 56 template <class Type> 57 struct get_numeric_infinity<Type, false, true> 58 { 59 typedef get_numeric_infinity type; valueboost::icl::get_numeric_infinity60 static Type value() 61 { 62 return (std::numeric_limits<Type>::max)(); 63 } 64 }; 65 66 template <class Type> 67 struct get_numeric_infinity<Type, false, false> 68 { 69 typedef get_numeric_infinity type; valueboost::icl::get_numeric_infinity70 static Type value() 71 { 72 return Type(); 73 } 74 }; 75 76 template <class Type> 77 struct numeric_infinity 78 { 79 typedef numeric_infinity type; valueboost::icl::numeric_infinity80 static Type value() 81 { 82 return get_numeric_infinity< Type 83 , has_std_infinity<Type>::value 84 , has_max_infinity<Type>::value >::value(); 85 } 86 }; 87 88 89 //------------------------------------------------------------------------------ 90 template<class Type, bool has_numeric_inf, bool has_repr_inf, bool has_size, bool has_diff> 91 struct get_infinity; 92 93 template<class Type, bool has_repr_inf, bool has_size, bool has_diff> 94 struct get_infinity<Type, true, has_repr_inf, has_size, has_diff> 95 { 96 typedef get_infinity type; 97 valueboost::icl::get_infinity98 static Type value() 99 { 100 return numeric_infinity<Type>::value(); 101 } 102 }; 103 104 template<class Type, bool has_size, bool has_diff> 105 struct get_infinity<Type, false, true, has_size, has_diff> 106 { 107 typedef get_infinity type; 108 valueboost::icl::get_infinity109 static Type value() 110 { 111 return Type(numeric_infinity<typename Type::rep>::value()); 112 } 113 }; 114 115 template<class Type, bool has_diff> 116 struct get_infinity<Type, false, false, true, has_diff> 117 { 118 typedef get_infinity type; 119 typedef typename Type::size_type size_type; 120 valueboost::icl::get_infinity121 static Type value() 122 { 123 return Type(numeric_infinity<size_type>::value()); 124 } 125 }; 126 127 template<class Type> 128 struct get_infinity<Type, false, false, false, true> 129 { 130 typedef get_infinity type; 131 typedef typename Type::difference_type difference_type; 132 valueboost::icl::get_infinity133 static Type value() 134 { 135 return identity_element<difference_type>::value(); 136 } 137 }; 138 139 template<class Type> 140 struct get_infinity<Type, false, false, false, false> 141 { 142 typedef get_infinity type; 143 valueboost::icl::get_infinity144 static Type value() 145 { 146 return identity_element<Type>::value(); 147 } 148 }; 149 150 template <class Type> struct infinity 151 { 152 typedef infinity type; 153 valueboost::icl::infinity154 static Type value() 155 { 156 return 157 get_infinity< Type 158 , is_numeric<Type>::value 159 , has_rep_type<Type>::value 160 , has_size_type<Type>::value 161 , has_difference_type<Type>::value 162 >::value(); 163 } 164 }; 165 166 template <> 167 struct infinity<std::string> 168 { 169 typedef infinity type; 170 valueboost::icl::infinity171 static std::string value() 172 { 173 return std::string(); 174 } 175 }; 176 177 }} // namespace boost icl 178 179 #endif 180 181 182