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/*
8This header defines two traits classes, both in namespace boost::math::tools.
9
10is_distribution<D>::value is true iff D has overloaded "cdf" and
11"quantile" functions, plus member typedefs value_type and policy_type.
12It's not much of a definitive test frankly,
13but if it looks like a distribution and quacks like a distribution
14then it must be a distribution.
15
16is_scaled_distribution<D>::value is true iff D is a distribution
17as 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
30namespace boost{ namespace math{ namespace tools{
31
32namespace detail{
33
34#define BOOST_MATH_HAS_NAMED_TRAIT(trait, name) \
35template <typename T> \
36class trait \
37{ \
38private: \
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 \
48public: \
49 static constexpr bool value = (sizeof(test<T>(0)) == sizeof(char)); \
50};
51
52BOOST_MATH_HAS_NAMED_TRAIT(has_value_type, value_type)
53BOOST_MATH_HAS_NAMED_TRAIT(has_policy_type, policy_type)
54BOOST_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
58template <typename T>
59constexpr bool has_value_type_v = has_value_type<T>::value;
60
61template <typename T>
62constexpr bool has_policy_type_v = has_policy_type<T>::value;
63
64template <typename T>
65constexpr bool has_backend_type_v = has_backend_type<T>::value;
66#endif
67
68template <typename D>
69char cdf(const D& ...);
70template <typename D>
71char quantile(const D& ...);
72
73template <typename D>
74struct has_cdf
75{
76 static D d;
77 static constexpr bool value = sizeof(cdf(d, 0.0f)) != 1;
78};
79
80template <typename D>
81struct has_quantile
82{
83 static D d;
84 static constexpr bool value = sizeof(quantile(d, 0.0f)) != 1;
85};
86
87template <typename D>
88struct 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
97template <typename sig, sig val>
98struct result_tag{};
99
100template <typename D>
101double test_has_location(const volatile result_tag<typename D::value_type (D::*)()const, &D::location>*);
102template <typename D>
103char test_has_location(...);
104
105template <typename D>
106double test_has_scale(const volatile result_tag<typename D::value_type (D::*)()const, &D::scale>*);
107template <typename D>
108char test_has_scale(...);
109
110template <typename D, bool b>
111struct is_scaled_distribution_helper
112{
113 static constexpr bool value = false;
114};
115
116template <typename D>
117struct 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
125template <typename D>
126struct 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
133template <typename T> struct is_distribution : public std::integral_constant<bool, ::boost::math::tools::detail::is_distribution_imp<T>::value> {};
134template <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
141

source code of include/boost/math/tools/traits.hpp