Debian Codegraph @

third-party/boost-math/include/boost/math/tools/traits.hpp

raw
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 /*
8 This header defines two traits classes, both in namespace boost::math::tools.
9
10 is_distribution<D>::value is true iff D has overloaded "cdf" and
11 "quantile" functions, plus member typedefs value_type and policy_type.
12 It's not much of a definitive test frankly,
13 but if it looks like a distribution and quacks like a distribution
14 then it must be a distribution.
15
16 is_scaled_distribution<D>::value is true iff D is a distribution
17 as defined above, and has member functions "scale" and "location".
18
19 */
20
21 #ifndef BOOST_STATS_IS_DISTRIBUTION_HPP
22 #define BOOST_STATS_IS_DISTRIBUTION_HPP
23
24 #ifdef _MSC_VER
25 #pragma once
26 #endif
27
28 #include <type_traits>
29
30 namespace boost{ namespace math{ namespace tools{
31
32 namespace detail{
33
34 #define BOOST_MATH_HAS_NAMED_TRAIT(trait, name) \
35 template <typename T> \
36 class trait \
37 { \
38 private: \
39 using yes = char; \
40 struct no { char x[2]; }; \
41 \
42 template <typename U> \
43 static yes test(typename U::name* = nullptr); \
44 \
45 template <typename U> \
46 static no test(...); \
47 \
48 public: \
49 static constexpr bool value = (sizeof(test<T>(0)) == sizeof(char)); \
50 };
51
52 BOOST_MATH_HAS_NAMED_TRAIT(has_value_type, value_type)
53 BOOST_MATH_HAS_NAMED_TRAIT(has_policy_type, policy_type)
54 BOOST_MATH_HAS_NAMED_TRAIT(has_backend_type, backend_type)
55
56 // C++17-esque helpers
57 #if defined(__cpp_variable_templates) && __cpp_variable_templates >= 201304L
58 template <typename T>
59 constexpr bool has_value_type_v = has_value_type<T>::value;
60
61 template <typename T>
62 constexpr bool has_policy_type_v = has_policy_type<T>::value;
63
64 template <typename T>
65 constexpr bool has_backend_type_v = has_backend_type<T>::value;
66 #endif
67
68 template <typename D>
69 char cdf(const D&, ...);
70 template <typename D>
71 char quantile(const D&, ...);
72
73 template <typename D>
74 struct has_cdf
75 {
76 static D d;
77 static constexpr bool value = sizeof(cdf(d, 0.0f)) != 1;
78 };
79
80 template <typename D>
81 struct has_quantile
82 {
83 static D d;
84 static constexpr bool value = sizeof(quantile(d, 0.0f)) != 1;
85 };
86
87 template <typename D>
88 struct is_distribution_imp
89 {
90 static constexpr bool value =
91 has_quantile<D>::value
92 && has_cdf<D>::value
93 && has_value_type<D>::value
94 && has_policy_type<D>::value;
95 };
96
97 template <typename sig, sig val>
98 struct result_tag{};
99
100 template <typename D>
101 double test_has_location(const volatile result_tag<typename D::value_type (D::*)()const, &D::location>*);
102 template <typename D>
103 char test_has_location(...);
104
105 template <typename D>
106 double test_has_scale(const volatile result_tag<typename D::value_type (D::*)()const, &D::scale>*);
107 template <typename D>
108 char test_has_scale(...);
109
110 template <typename D, bool b>
111 struct is_scaled_distribution_helper
112 {
113 static constexpr bool value = false;
114 };
115
116 template <typename D>
117 struct is_scaled_distribution_helper<D, true>
118 {
119 static constexpr bool value =
120 (sizeof(test_has_location<D>(0)) != 1)
121 &&
122 (sizeof(test_has_scale<D>(0)) != 1);
123 };
124
125 template <typename D>
126 struct is_scaled_distribution_imp
127 {
128 static constexpr bool value = (::boost::math::tools::detail::is_scaled_distribution_helper<D, ::boost::math::tools::detail::is_distribution_imp<D>::value>::value);
129 };
130
131 } // namespace detail
132
133 template <typename T> struct is_distribution : public std::integral_constant<bool, ::boost::math::tools::detail::is_distribution_imp<T>::value> {};
134 template <typename T> struct is_scaled_distribution : public std::integral_constant<bool, ::boost::math::tools::detail::is_scaled_distribution_imp<T>::value> {};
135
136 }}}
137
138 #endif
139
140