traits.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. // C++17-esque helpers
  45. #if defined(__cpp_variable_templates) && __cpp_variable_templates >= 201304L
  46. template <typename T>
  47. constexpr bool has_value_type_v = has_value_type<T>::value;
  48. template <typename T>
  49. constexpr bool has_policy_type_v = has_policy_type<T>::value;
  50. template <typename T>
  51. constexpr bool has_backend_type_v = has_backend_type<T>::value;
  52. #endif
  53. template <typename D>
  54. char cdf(const D& ...);
  55. template <typename D>
  56. char quantile(const D& ...);
  57. template <typename D>
  58. struct has_cdf
  59. {
  60. static D d;
  61. static constexpr bool value = sizeof(cdf(d, 0.0f)) != 1;
  62. };
  63. template <typename D>
  64. struct has_quantile
  65. {
  66. static D d;
  67. static constexpr bool value = sizeof(quantile(d, 0.0f)) != 1;
  68. };
  69. template <typename D>
  70. struct is_distribution_imp
  71. {
  72. static constexpr bool value =
  73. has_quantile<D>::value
  74. && has_cdf<D>::value
  75. && has_value_type<D>::value
  76. && has_policy_type<D>::value;
  77. };
  78. template <typename sig, sig val>
  79. struct result_tag{};
  80. template <typename D>
  81. double test_has_location(const volatile result_tag<typename D::value_type (D::*)()const, &D::location>*);
  82. template <typename D>
  83. char test_has_location(...);
  84. template <typename D>
  85. double test_has_scale(const volatile result_tag<typename D::value_type (D::*)()const, &D::scale>*);
  86. template <typename D>
  87. char test_has_scale(...);
  88. template <typename D, bool b>
  89. struct is_scaled_distribution_helper
  90. {
  91. static constexpr bool value = false;
  92. };
  93. template <typename D>
  94. struct is_scaled_distribution_helper<D, true>
  95. {
  96. static constexpr bool value =
  97. (sizeof(test_has_location<D>(0)) != 1)
  98. &&
  99. (sizeof(test_has_scale<D>(0)) != 1);
  100. };
  101. template <typename D>
  102. struct is_scaled_distribution_imp
  103. {
  104. static constexpr bool value = (::boost::math::tools::detail::is_scaled_distribution_helper<D, ::boost::math::tools::detail::is_distribution_imp<D>::value>::value);
  105. };
  106. } // namespace detail
  107. template <typename T> struct is_distribution : public std::integral_constant<bool, ::boost::math::tools::detail::is_distribution_imp<T>::value> {};
  108. template <typename T> struct is_scaled_distribution : public std::integral_constant<bool, ::boost::math::tools::detail::is_scaled_distribution_imp<T>::value> {};
  109. }}}
  110. #endif