traits.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // should be the last #include
  22. #include <boost/type_traits/detail/bool_trait_def.hpp>
  23. namespace boost{ namespace math{ namespace tools{
  24. namespace detail{
  25. BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(has_value_type, value_type, true)
  26. BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(has_policy_type, policy_type, true)
  27. template<class D>
  28. char cdf(const D& ...);
  29. template<class D>
  30. char quantile(const D& ...);
  31. template <class D>
  32. struct has_cdf
  33. {
  34. static D d;
  35. BOOST_STATIC_CONSTANT(bool, value = sizeof(cdf(d, 0.0f)) != 1);
  36. };
  37. template <class D>
  38. struct has_quantile
  39. {
  40. static D d;
  41. BOOST_STATIC_CONSTANT(bool, value = sizeof(quantile(d, 0.0f)) != 1);
  42. };
  43. template <class D>
  44. struct is_distribution_imp
  45. {
  46. BOOST_STATIC_CONSTANT(bool, value =
  47. has_quantile<D>::value
  48. && has_cdf<D>::value
  49. && has_value_type<D>::value
  50. && has_policy_type<D>::value);
  51. };
  52. template <class sig, sig val>
  53. struct result_tag{};
  54. template <class D>
  55. double test_has_location(const volatile result_tag<typename D::value_type (D::*)()const, &D::location>*);
  56. template <class D>
  57. char test_has_location(...);
  58. template <class D>
  59. double test_has_scale(const volatile result_tag<typename D::value_type (D::*)()const, &D::scale>*);
  60. template <class D>
  61. char test_has_scale(...);
  62. template <class D, bool b>
  63. struct is_scaled_distribution_helper
  64. {
  65. BOOST_STATIC_CONSTANT(bool, value = false);
  66. };
  67. template <class D>
  68. struct is_scaled_distribution_helper<D, true>
  69. {
  70. BOOST_STATIC_CONSTANT(bool, value =
  71. (sizeof(test_has_location<D>(0)) != 1)
  72. &&
  73. (sizeof(test_has_scale<D>(0)) != 1));
  74. };
  75. template <class D>
  76. struct is_scaled_distribution_imp
  77. {
  78. BOOST_STATIC_CONSTANT(bool, value = (::boost::math::tools::detail::is_scaled_distribution_helper<D, ::boost::math::tools::detail::is_distribution_imp<D>::value>::value));
  79. };
  80. } // namespace detail
  81. BOOST_TT_AUX_BOOL_TRAIT_DEF1(is_distribution,T,::boost::math::tools::detail::is_distribution_imp<T>::value)
  82. BOOST_TT_AUX_BOOL_TRAIT_DEF1(is_scaled_distribution,T,::boost::math::tools::detail::is_scaled_distribution_imp<T>::value)
  83. }}}
  84. #endif