numeric_traits.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #if !defined(BOOST_SPIRIT_NUMERIC_TRAITS_JAN_07_2011_0722AM)
  6. #define BOOST_SPIRIT_NUMERIC_TRAITS_JAN_07_2011_0722AM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <boost/config.hpp>
  11. #include <boost/mpl/bool.hpp>
  12. #include <boost/integer_traits.hpp>
  13. #include <boost/utility/enable_if.hpp>
  14. namespace boost { namespace spirit { namespace traits
  15. {
  16. ///////////////////////////////////////////////////////////////////////////
  17. // Determine if T is a boolean type
  18. ///////////////////////////////////////////////////////////////////////////
  19. template <typename T>
  20. struct is_bool : mpl::false_ {};
  21. template <typename T>
  22. struct is_bool<T const> : is_bool<T> {};
  23. template <>
  24. struct is_bool<bool> : mpl::true_ {};
  25. ///////////////////////////////////////////////////////////////////////////
  26. // Determine if T is a signed integer type
  27. ///////////////////////////////////////////////////////////////////////////
  28. template <typename T>
  29. struct is_int : mpl::false_ {};
  30. template <typename T>
  31. struct is_int<T const> : is_int<T> {};
  32. template <>
  33. struct is_int<short> : mpl::true_ {};
  34. template <>
  35. struct is_int<int> : mpl::true_ {};
  36. template <>
  37. struct is_int<long> : mpl::true_ {};
  38. #ifdef BOOST_HAS_LONG_LONG
  39. template <>
  40. struct is_int<boost::long_long_type> : mpl::true_ {};
  41. #endif
  42. ///////////////////////////////////////////////////////////////////////////
  43. // Determine if T is an unsigned integer type
  44. ///////////////////////////////////////////////////////////////////////////
  45. template <typename T>
  46. struct is_uint : mpl::false_ {};
  47. template <typename T>
  48. struct is_uint<T const> : is_uint<T> {};
  49. #if !defined(BOOST_NO_INTRINSIC_WCHAR_T)
  50. template <>
  51. struct is_uint<unsigned short> : mpl::true_ {};
  52. #endif
  53. template <>
  54. struct is_uint<unsigned int> : mpl::true_ {};
  55. template <>
  56. struct is_uint<unsigned long> : mpl::true_ {};
  57. #ifdef BOOST_HAS_LONG_LONG
  58. template <>
  59. struct is_uint<boost::ulong_long_type> : mpl::true_ {};
  60. #endif
  61. ///////////////////////////////////////////////////////////////////////////
  62. // Determine if T is a floating point type
  63. ///////////////////////////////////////////////////////////////////////////
  64. template <typename T>
  65. struct is_real : mpl::false_ {};
  66. template <typename T>
  67. struct is_real<T const> : is_uint<T> {};
  68. template <>
  69. struct is_real<float> : mpl::true_ {};
  70. template <>
  71. struct is_real<double> : mpl::true_ {};
  72. template <>
  73. struct is_real<long double> : mpl::true_ {};
  74. ///////////////////////////////////////////////////////////////////////////
  75. // customization points for numeric operations
  76. ///////////////////////////////////////////////////////////////////////////
  77. template <typename T, typename Enable = void>
  78. struct absolute_value;
  79. template <typename T, typename Enable = void>
  80. struct is_negative;
  81. template <typename T, typename Enable = void>
  82. struct is_zero;
  83. template <typename T, typename Enable = void>
  84. struct pow10_helper;
  85. template <typename T, typename Enable = void>
  86. struct is_nan;
  87. template <typename T, typename Enable = void>
  88. struct is_infinite;
  89. template <typename T, typename Enable = void>
  90. struct is_integer_wrapping : mpl::false_ {};
  91. template <typename T>
  92. struct is_integer_wrapping_default
  93. : mpl::bool_<(static_cast<T>(integer_traits<T>::const_max + 1) == integer_traits<T>::const_min)> {};
  94. template <typename T>
  95. struct is_integer_wrapping<T, typename enable_if_c<integer_traits<T>::is_integral>::type>
  96. : is_integer_wrapping_default<T> {};
  97. }}}
  98. #endif