infinity.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #include <string>
  11. #include <boost/static_assert.hpp>
  12. #include <boost/icl/type_traits/is_numeric.hpp>
  13. #include <boost/icl/type_traits/rep_type_of.hpp>
  14. #include <boost/icl/type_traits/size_type_of.hpp>
  15. #include <boost/mpl/and.hpp>
  16. #include <boost/mpl/if.hpp>
  17. namespace boost{ namespace icl
  18. {
  19. template<class Type> struct has_std_infinity
  20. {
  21. typedef has_std_infinity type;
  22. BOOST_STATIC_CONSTANT(bool,
  23. value = ( is_numeric<Type>::value
  24. && std::numeric_limits<Type>::has_infinity
  25. )
  26. );
  27. };
  28. template<class Type> struct has_max_infinity
  29. {
  30. typedef has_max_infinity type;
  31. BOOST_STATIC_CONSTANT(bool,
  32. value = ( is_numeric<Type>::value
  33. && ! std::numeric_limits<Type>::has_infinity
  34. )
  35. );
  36. };
  37. //------------------------------------------------------------------------------
  38. template <class Type, bool has_std_inf=false, bool has_std_max=false>
  39. struct get_numeric_infinity;
  40. template <class Type, bool has_std_max>
  41. struct get_numeric_infinity<Type, true, has_std_max>
  42. {
  43. typedef get_numeric_infinity type;
  44. static Type value()
  45. {
  46. return (std::numeric_limits<Type>::infinity)();
  47. }
  48. };
  49. template <class Type>
  50. struct get_numeric_infinity<Type, false, true>
  51. {
  52. typedef get_numeric_infinity type;
  53. static Type value()
  54. {
  55. return (std::numeric_limits<Type>::max)();
  56. }
  57. };
  58. template <class Type>
  59. struct get_numeric_infinity<Type, false, false>
  60. {
  61. typedef get_numeric_infinity type;
  62. static Type value()
  63. {
  64. return Type();
  65. }
  66. };
  67. template <class Type>
  68. struct numeric_infinity
  69. {
  70. typedef numeric_infinity type;
  71. static Type value()
  72. {
  73. return get_numeric_infinity< Type
  74. , has_std_infinity<Type>::value
  75. , has_max_infinity<Type>::value >::value();
  76. }
  77. };
  78. //------------------------------------------------------------------------------
  79. template<class Type, bool has_numeric_inf, bool has_repr_inf, bool has_size, bool has_diff>
  80. struct get_infinity;
  81. template<class Type, bool has_repr_inf, bool has_size, bool has_diff>
  82. struct get_infinity<Type, true, has_repr_inf, has_size, has_diff>
  83. {
  84. typedef get_infinity type;
  85. static Type value()
  86. {
  87. return numeric_infinity<Type>::value();
  88. }
  89. };
  90. template<class Type, bool has_size, bool has_diff>
  91. struct get_infinity<Type, false, true, has_size, has_diff>
  92. {
  93. typedef get_infinity type;
  94. static Type value()
  95. {
  96. return Type(numeric_infinity<typename Type::rep>::value());
  97. }
  98. };
  99. template<class Type, bool has_diff>
  100. struct get_infinity<Type, false, false, true, has_diff>
  101. {
  102. typedef get_infinity type;
  103. typedef typename Type::size_type size_type;
  104. static Type value()
  105. {
  106. return Type(numeric_infinity<size_type>::value());
  107. }
  108. };
  109. template<class Type>
  110. struct get_infinity<Type, false, false, false, true>
  111. {
  112. typedef get_infinity type;
  113. typedef typename Type::difference_type difference_type;
  114. static Type value()
  115. {
  116. return identity_element<difference_type>::value();
  117. }
  118. };
  119. template<class Type>
  120. struct get_infinity<Type, false, false, false, false>
  121. {
  122. typedef get_infinity type;
  123. static Type value()
  124. {
  125. return identity_element<Type>::value();
  126. }
  127. };
  128. template <class Type> struct infinity
  129. {
  130. typedef infinity type;
  131. static Type value()
  132. {
  133. return
  134. get_infinity< Type
  135. , is_numeric<Type>::value
  136. , has_rep_type<Type>::value
  137. , has_size_type<Type>::value
  138. , has_difference_type<Type>::value
  139. >::value();
  140. }
  141. };
  142. template <>
  143. struct infinity<std::string>
  144. {
  145. typedef infinity type;
  146. static std::string value()
  147. {
  148. return std::string();
  149. }
  150. };
  151. }} // namespace boost icl
  152. #endif