traits.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright John Maddock 2007.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. /*
  6. This header defines two traits classes, both in namespace boost::math::tools.
  7. is_distribution<D>::value is true iff D has overloaded "cdf" and
  8. "quantile" functions, plus member typedefs value_type and policy_type.
  9. It's not much of a definitive test frankly,
  10. but if it looks like a distribution and quacks like a distribution
  11. then it must be a distribution.
  12. is_scaled_distribution<D>::value is true iff D is a distribution
  13. as defined above, and has member functions "scale" and "location".
  14. */
  15. #ifndef BOOST_STATS_IS_DISTRIBUTION_HPP
  16. #define BOOST_STATS_IS_DISTRIBUTION_HPP
  17. #ifdef _MSC_VER
  18. #pragma once
  19. #endif
  20. #include <boost/mpl/has_xxx.hpp>
  21. #include <boost/type_traits/integral_constant.hpp>
  22. namespace boost{ namespace math{ namespace tools{
  23. namespace detail{
  24. BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(has_value_type, value_type, true)
  25. BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(has_policy_type, policy_type, true)
  26. template<class D>
  27. char cdf(const D& ...);
  28. template<class D>
  29. char quantile(const D& ...);
  30. template <class D>
  31. struct has_cdf
  32. {
  33. static D d;
  34. BOOST_STATIC_CONSTANT(bool, value = sizeof(cdf(d, 0.0f)) != 1);
  35. };
  36. template <class D>
  37. struct has_quantile
  38. {
  39. static D d;
  40. BOOST_STATIC_CONSTANT(bool, value = sizeof(quantile(d, 0.0f)) != 1);
  41. };
  42. template <class D>
  43. struct is_distribution_imp
  44. {
  45. BOOST_STATIC_CONSTANT(bool, value =
  46. has_quantile<D>::value
  47. && has_cdf<D>::value
  48. && has_value_type<D>::value
  49. && has_policy_type<D>::value);
  50. };
  51. template <class sig, sig val>
  52. struct result_tag{};
  53. template <class D>
  54. double test_has_location(const volatile result_tag<typename D::value_type (D::*)()const, &D::location>*);
  55. template <class D>
  56. char test_has_location(...);
  57. template <class D>
  58. double test_has_scale(const volatile result_tag<typename D::value_type (D::*)()const, &D::scale>*);
  59. template <class D>
  60. char test_has_scale(...);
  61. template <class D, bool b>
  62. struct is_scaled_distribution_helper
  63. {
  64. BOOST_STATIC_CONSTANT(bool, value = false);
  65. };
  66. template <class D>
  67. struct is_scaled_distribution_helper<D, true>
  68. {
  69. BOOST_STATIC_CONSTANT(bool, value =
  70. (sizeof(test_has_location<D>(0)) != 1)
  71. &&
  72. (sizeof(test_has_scale<D>(0)) != 1));
  73. };
  74. template <class D>
  75. struct is_scaled_distribution_imp
  76. {
  77. BOOST_STATIC_CONSTANT(bool, value = (::boost::math::tools::detail::is_scaled_distribution_helper<D, ::boost::math::tools::detail::is_distribution_imp<D>::value>::value));
  78. };
  79. } // namespace detail
  80. template <class T> struct is_distribution : public boost::integral_constant<bool, ::boost::math::tools::detail::is_distribution_imp<T>::value> {};
  81. template <class T> struct is_scaled_distribution : public boost::integral_constant<bool, ::boost::math::tools::detail::is_scaled_distribution_imp<T>::value> {};
  82. }}}
  83. #endif