mn_functional.hpp
Go to the documentation of this file.
1 
20 #ifndef _MINLIB_423a6056_5421_4f39_85b0_41c5bada4730_H_
21 #define _MINLIB_423a6056_5421_4f39_85b0_41c5bada4730_H_
22 
23 #include "mn_config.hpp"
24 #include "mn_defines.hpp"
25 #include "mn_def.hpp"
26 #include "mn_typetraits.hpp"
27 
28 
29 using nullptr_t = decltype(nullptr);
30 
31 namespace mn {
32  template <typename T> struct tag { using type = T; };
33  template <typename Tag> using type_t = typename Tag::type;
34 
35  template< bool B, typename T, typename F > struct conditional { using type = T; };
36  template< typename T, typename F > struct conditional<false, T, F> { using type = F; };
37 
38 
39 
40  template<typename...>
41  struct bit_or;
42 
43  template<>
44  struct bit_or<> : public false_type { };
45 
46  template<typename A>
47  struct bit_or<A>
48  : public A { };
49 
50  template<typename A, typename B>
51  struct bit_or<A, B>
53 
54  template<typename A, typename B, typename C, typename... N>
55  struct bit_or<A, B, C, N...>
56  : public conditional<A::value, B, bit_or<B, C, N...>>::type { };
57 
58 
59  template <typename T> T &&move(T &t) {
60  return static_cast<T &&>(t); }
61 
62  template <typename T>
64  : tag<T> { };
65 
66  template <typename T>
67  struct remove_reference<T &>
68  : tag<T> { };
69 
70  template <typename T>
72 
73  template <typename T>
75  return static_cast<T &&>(t);
76  }
77  template <typename T>
79  return static_cast<T &&>(t);
80  }
81 
82  template <typename T>
83  struct remove_const
84  : tag<T> { };
85 
86  template <typename T>
87  struct remove_const<T const>
88  : tag<T> {};
89 
90  template <typename T>
92  : tag<T> {};
93 
94  template <typename T>
95  struct remove_volatile<T volatile>
96  : tag<T> {};
97 
98  template <typename T>
99  struct remove_cv
100  : remove_const<type_t<remove_volatile<T>>> { };
101 
102  template< typename T >
104  : remove_cv<type_t<remove_reference<T>>> { };
105 
106  template< class T >
107  struct add_const
108  : tag<const T> {};
109 
110  template< class T >
112  : tag<volatile T> {};
113 
114  template< class T >
115  struct add_cv {
116  using type = const volatile typename remove_reference<T>::type;
117  };
118 
119  template <typename T>
121  { using type = typename remove_reference<T>::type&; };
122 
123  template< class T >
124  struct add_pointer {
125  using type = typename remove_reference<T>::type*;
126  };
127 
128  template<class T>
130  : tag<T&> { };
131 
132  template<class T>
134  : tag<T&&> { };
135 
136  template <typename T>
137  struct decay3 : remove_cv<T> {};
138 
139  template <typename R, typename... Args>
140  struct decay3<R(Args...)> : tag<R (*)(Args...)> {};
141 
142  template <typename T>
143  struct decay2 : decay3<T> {};
144 
145  template <typename T, size_t N>
146  struct decay2<T[N]> : tag<T *> {};
147 
148  template <typename T>
149  struct decay : decay2<remove_reference_t<T>> {};
150 
151  template <typename T>
153 
154 
155  template <typename T> T declval();
156 
157 
158  namespace internal {
159  template <template <class...> typename Z, typename, typename... Ts>
160  struct can_apply : false_type {};
161 
162  template <template <class...> typename Z, typename... Ts>
163  struct can_apply<Z, void_t<Z<Ts...>>, Ts...> : true_type {};
164 
165  template <typename From, typename To>
166  using try_convert = decltype(To{declval<From>()});
167 
168  template <class G, class... Args>
169  using invoke_t = decltype(declval<G>()(declval<Args>()...));
170 
171  template <class Sig, class = void>
172  struct res_of {};
173  template <class G, class... Args>
174  struct res_of<G(Args...), void_t<invoke_t<G, Args...>>> : tag<invoke_t<G, Args...>> {};
175 
176  }
177 
178  template <template <typename...> typename Z, typename... Ts>
179  using can_apply = internal::can_apply<Z, void, Ts...>;
180 
181 
182  template <typename From, typename To>
183  struct is_convertible : internal::can_apply<internal::try_convert, From, To> {};
184 
185  template <> struct is_convertible<void, void> : true_type {};
186 
187  template <bool, class = void>
188  struct enable_if {};
189 
190  template <class T>
191  struct enable_if<true, T> : tag<T> {};
192 
193  template <bool b, class T = void>
195 
196 
197  template <class Sig>
199 
200  template <class Sig>
202 
203  template <class Sig>
205 
206  template <class Sig>
208 
209  // is_same
210  template <class A, class B>
211  struct is_same : false_type {};
212 
213  template <class A>
214  struct is_same<A, A> : true_type {};
215 
216  template<typename TBase, typename TDerived>
217  struct is_base_of : public integral_constant<bool, __is_base_of(TBase, TDerived)> { };
218 
219 
220  template<typename T>
221  struct logic_and {
222  bool operator()(const T& a, const T& b) const noexcept { return a && b; }
223  };
224 
225  template<typename T>
226  struct logic_or {
227  bool operator()(const T& a, const T& b) const noexcept { return a || b; }
228  };
229 
230  template<typename T>
231  struct logic_not {
232  bool operator()(const T& a) const noexcept { return !a; }
233  };
234 
235  template <size_t VAL, size_t X = 1>
236  struct sqrt{
237  using type = typename mn::conditional<
238  ((X * X) > VAL), mn::integral_constant<intmax_t, X - 1>, mn::sqrt<VAL, X + 1> >::type;
239  static constexpr size_t value = type::value;
240  };
241 
246  template <size_t N>
247  struct factorial {
248  static constexpr size_t value = N * factorial<N - 1>::value;
249  };
253  template <>
254  struct factorial<0> {
255  static constexpr size_t value = 1;
256  };
257 
262  template <size_t N>
263  struct fibonacci {
264  static constexpr size_t value = fibonacci<N - 1>::value + fibonacci<N - 2>::value;
265  };
269  template <>
270  struct fibonacci<0> {
271  static constexpr size_t value = 0;
272  };
276  template <>
277  struct fibonacci<1> {
278  static constexpr size_t value = 1;
279  };
280 }
281 
282 #endif
decltype(nullptr) nullptr_t
Definition: mn_functional.hpp:29
decltype(declval< G >()(declval< Args >()...)) invoke_t
Definition: mn_functional.hpp:169
decltype(To{declval< From >()}) try_convert
Definition: mn_functional.hpp:166
Definition: mn_functional.hpp:172
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
struct mn::memory::detail::ptr_difference type
Definition: mn_allocator_typetraits.hpp:25
T declval()
typename remove_reference< T >::type & type
Definition: mn_functional.hpp:121
type_t< res_of< Sig > > res_of_t
Definition: mn_functional.hpp:204
type_t< remove_reference< T > > remove_reference_t
Definition: mn_functional.hpp:71
F type
Definition: mn_functional.hpp:36
T type
Definition: mn_functional.hpp:35
type_t< enable_if< b, T > > enable_if_t
Definition: mn_functional.hpp:194
type_t< decay< T > > decay_t
Definition: mn_functional.hpp:152
typename remove_reference< T >::type * type
Definition: mn_functional.hpp:125
signed long intmax_t
Definition: mn_def.hpp:51
typename make_void< ts ... >::type void_t
Definition: mn_void.hpp:65
const volatile typename remove_reference< T >::type type
Definition: mn_functional.hpp:116
void move(const T *src, const T *last, T *dest)
Definition: mn_algorithm.hpp:100
T && forward(remove_reference_t< T > &t)
Definition: mn_functional.hpp:74
typename Tag::type type_t
Definition: mn_functional.hpp:33
Definition: mn_functional.hpp:115
Definition: mn_functional.hpp:124
Definition: mn_functional.hpp:121
Definition: mn_functional.hpp:41
Definition: mn_functional.hpp:35
Definition: mn_functional.hpp:188
Definition: mn_functional.hpp:108
Definition: mn_functional.hpp:130
Definition: mn_functional.hpp:134
Definition: mn_functional.hpp:112
Definition: mn_functional.hpp:52
Definition: mn_functional.hpp:48
Definition: mn_functional.hpp:44
Definition: mn_functional.hpp:143
Definition: mn_functional.hpp:137
Definition: mn_functional.hpp:149
Calculates the Nth factorial value.
Definition: mn_functional.hpp:247
static constexpr size_t value
Definition: mn_functional.hpp:248
Calculates the Nth Fibonacci value.
Definition: mn_functional.hpp:263
static constexpr size_t value
Definition: mn_functional.hpp:264
Definition: mn_functional.hpp:160
Definition: mn_functional.hpp:217
Definition: mn_functional.hpp:183
Definition: mn_functional.hpp:211
Definition: mn_functional.hpp:221
bool operator()(const T &a, const T &b) const noexcept
Definition: mn_functional.hpp:222
Definition: mn_functional.hpp:231
bool operator()(const T &a) const noexcept
Definition: mn_functional.hpp:232
Definition: mn_functional.hpp:226
bool operator()(const T &a, const T &b) const noexcept
Definition: mn_functional.hpp:227
Definition: mn_functional.hpp:84
Definition: mn_functional.hpp:100
Definition: mn_functional.hpp:104
Definition: mn_functional.hpp:64
Definition: mn_functional.hpp:92
Definition: mn_functional.hpp:236
typename mn::conditional<((X *X) > VAL), mn::integral_constant< intmax_t, X - 1 >, mn::sqrt< VAL, X+1 > >::type type
Definition: mn_functional.hpp:238
static constexpr size_t value
Definition: mn_functional.hpp:239
Definition: mn_functional.hpp:32
T type
Definition: mn_functional.hpp:32