MCKL
Monte Carlo Kernel Library
traits.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/internal/traits.hpp
3 //----------------------------------------------------------------------------
4 // MCKL: Monte Carlo Kernel Library
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2018, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef MCKL_INTERNAL_TRAITS_HPP
33 #define MCKL_INTERNAL_TRAITS_HPP
34 
35 #include <mckl/internal/config.h>
37 #include <type_traits>
38 
39 #define MCKL_DEFINE_TYPE_DISPATCH_TRAIT(Outer, Inner, Default) \
40  template <typename T> \
41  class Outer##Trait; \
42  \
43  namespace internal { \
44  \
45  template <typename T> \
46  class Has##Outer##Impl \
47  { \
48  class char2 \
49  { \
50  char c1; \
51  char c2; \
52  }; \
53  \
54  template <typename U> \
55  static char test(typename U::Inner *); \
56  \
57  template <typename U> \
58  static char2 test(...); \
59  \
60  public: \
61  static constexpr bool value = \
62  sizeof(test<T>(nullptr)) == sizeof(char); \
63  }; \
64  \
65  template <typename T> \
66  class Has##Outer \
67  : public std::integral_constant<bool, Has##Outer##Impl<T>::value> \
68  { \
69  }; \
70  \
71  template <typename T, bool> \
72  class Outer##Dispatch; \
73  \
74  template <typename T> \
75  class Outer##Dispatch<T, false> \
76  { \
77  public: \
78  using type = Default; \
79  }; \
80  \
81  template <typename T> \
82  class Outer##Dispatch<T, true> \
83  { \
84  public: \
85  using type = typename T::Inner; \
86  }; \
87  \
88  } /* namespace interanl */ \
89  \
90  template <typename T> \
91  class Outer##Trait \
92  { \
93  public: \
94  static constexpr bool value = internal::Has##Outer<T>::value; \
95  using type = typename internal::Outer##Dispatch<T, value>::type; \
96  }; \
97  \
98  template <typename T> \
99  using Outer = typename Outer##Trait<T>::type;
100 
101 #define MCKL_DEFINE_TYPE_TEMPLATE_DISPATCH_TRAIT(Outer, Inner, Default) \
102  template <typename T> \
103  class Outer##Trait; \
104  \
105  namespace internal { \
106  \
107  template <typename T> \
108  class Has##Outer##Impl \
109  { \
110  class char2 \
111  { \
112  char c1; \
113  char c2; \
114  }; \
115  \
116  template <typename U> \
117  static char test(typename U::template Inner<T> *); \
118  \
119  template <typename U> \
120  static char2 test(...); \
121  \
122  public: \
123  static constexpr bool value = \
124  sizeof(test<T>(nullptr)) == sizeof(char); \
125  }; \
126  \
127  template <typename T> \
128  class Has##Outer \
129  : public std::integral_constant<bool, Has##Outer##Impl<T>::value> \
130  { \
131  }; \
132  \
133  template <typename T, bool> \
134  class Outer##Dispatch; \
135  \
136  template <typename T> \
137  class Outer##Dispatch<T, false> \
138  { \
139  public: \
140  using type = Default<T>; \
141  }; \
142  \
143  template <typename T> \
144  class Outer##Dispatch<T, true> \
145  { \
146  public: \
147  using type = typename T::template Inner<T>; \
148  }; \
149  } \
150  \
151  template <typename T> \
152  class Outer##Trait \
153  { \
154  public: \
155  static constexpr bool value = internal::Has##Outer<T>::value; \
156  using type = typename internal::Outer##Dispatch<T, value>::type; \
157  }; \
158  \
159  template <typename T> \
160  using Outer = typename Outer##Trait<T>::type;
161 
162 namespace mckl {
163 
167 
168 namespace internal {
169 
170 template <typename T, std::size_t K = 1>
172  : public std::integral_constant<std::size_t,
173  8192 / (sizeof(T) * K) == 0 ? 1 : 8192 / (sizeof(T) * K)>
174 {
175 }; // class BufferSize;
176 
177 template <typename T>
178 using is_blas_floating_point = std::integral_constant<bool,
179  std::is_same<typename std::remove_cv<T>::type, float>::value ||
180  std::is_same<typename std::remove_cv<T>::type, double>::value>;
181 
182 template <typename T, typename RNGType,
183  typename KeyType = typename RNGType::result_type>
184 using is_seed_seq = std::integral_constant<bool,
185  std::is_class<T>::value && !std::is_convertible<T, RNGType>::value &&
186  !std::is_convertible<T, typename RNGType::result_type>::value &&
187  !std::is_convertible<T, KeyType>::value>;
188 
189 } // namespace internal
190 
191 } // namespace mckl
192 
193 #endif // MCKL_INTERNAL_TRAITS_HPP
std::integral_constant< bool, std::is_same< typename std::remove_cv< T >::type, float >::value||std::is_same< typename std::remove_cv< T >::type, double >::value > is_blas_floating_point
Definition: traits.hpp:180
#define MCKL_DEFINE_TYPE_DISPATCH_TRAIT(Outer, Inner, Default)
Definition: traits.hpp:39
std::integral_constant< bool, std::is_class< T >::value &&!std::is_convertible< T, RNGType >::value &&!std::is_convertible< T, typename RNGType::result_type >::value &&!std::is_convertible< T, KeyType >::value > is_seed_seq
Definition: traits.hpp:187
Definition: mcmc.hpp:40
typename SizeTypeTrait< T >::type SizeType
Definition: traits.hpp:166