traits.hpp 4.1 KB

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