1#ifndef COMMON_HPP
2#define COMMON_HPP
3
4#include <type_traits>
5#include <random>
6#include "memory/memory.hpp"
7#include "util/for_each_ref.hpp"
8#include "util/variadic_to_vmpl.hpp"
9
10constexpr int RUN_ON_DEVICE = 1024;
11
12#define GCC_VERSION (__GNUC__ * 10000 \
13 + __GNUC_MINOR__ * 100 \
14 + __GNUC_PATCHLEVEL__)
15
16template<unsigned int N,typename T>
17struct static_array
18{
19 T sa[N];
20};
21
22namespace std
23{
24 // We need the definition of std::to_string that work on string
25 inline static std::string to_string(std::string s)
26 {
27 return s;
28 }
29}
30
31
32/*! \brief convert a type into constant type
33 *
34 * \param T type tp convert
35 *
36 * \return a constant of the type T
37 *
38 */
39namespace openfpm
40{
41 template <class T>
42 constexpr typename std::add_const<T>::type & as_const(T& t) noexcept
43 {
44 return t;
45 }
46
47 template <typename> struct Debug;
48}
49
50 //! Compile time array functor needed to generate array at compile-time of type
51 // {3,3,3,3,3,3,.....}
52 template<size_t index, size_t N> struct Fill_three {
53 enum { value = 3 };
54 };
55
56 //! {0,0,0,0,....}
57 template<size_t index, size_t N> struct Fill_zero {
58 enum { value = 0 };
59 };
60
61 //! {2,2,2,2,....}
62 template<size_t index, size_t N> struct Fill_two {
63 enum { value = 2 };
64 };
65
66 //! {1,1,1,1,....}
67 template<size_t index, size_t N> struct Fill_one {
68 enum { value = 1 };
69 };
70
71//! Void structure
72template<typename> struct Void
73{
74 //! define void type
75 typedef void type;
76};
77
78template<typename T, typename Sfinae = void>
79struct has_attributes: std::false_type {};
80
81
82/*! \brief has_attributes check if a type has defined an
83 * internal structure with attributes
84 *
85 * ### Example
86 * \snippet util_test.hpp Declaration of struct with attributes and without
87 * \snippet util_test.hpp Check has_attributes
88 *
89 * return true if T::attributes::name[0] is a valid expression
90 * and produce a defined type
91 *
92 */
93template<typename T>
94struct has_attributes<T, typename Void<decltype( T::attributes::name[0] )>::type> : std::true_type
95{};
96
97template<typename T, typename Sfinae = void>
98struct has_typedef_type: std::false_type {};
99
100/*! \brief has_typedef_type check if a typedef ... type inside the structure is
101 * defined
102 *
103 * ### Example
104 *
105 * \snippet util_test.hpp Check has_typedef_type
106 *
107 * return true if T::type is a valid type
108 *
109 */
110template<typename T>
111struct has_typedef_type<T, typename Void< typename T::type>::type> : std::true_type
112{};
113
114template<typename T, typename Sfinae = void>
115struct has_vector_kernel: std::false_type {};
116
117/*! \brief has_vector_kernel check if a type has defined a member data
118 *
119 * ### Example
120 *
121 * \snippet util_test.hpp Check has_data
122 *
123 * return true if T::vector_kernel is a valid type
124 *
125 */
126template<typename T>
127struct has_vector_kernel<T, typename Void< typename T::vector_kernel >::type> : std::true_type
128{};
129
130template<typename T, typename Sfinae = void>
131struct has_set_d: std::false_type {};
132
133/*! \brief has_move check if a type has defined a method function move
134 *
135 */
136template<typename T>
137struct has_set_d<T, typename Void<decltype( std::declval<T>().set_d(0,0) )>::type> : std::true_type
138{};
139
140template<typename T, typename Sfinae = void>
141struct has_data: std::false_type {};
142
143/*! \brief has_data check if a type has defined a member data
144 *
145 * ### Example
146 *
147 * \snippet util_test.hpp Check has_data
148 *
149 * return true if T::type is a valid type
150 *
151 */
152template<typename T>
153struct has_data<T, typename Void<decltype( T::data )>::type> : std::true_type
154{};
155
156template<typename T, typename Sfinae = void>
157struct has_posMask: std::false_type {};
158
159/*! \brief has_posMask check if a type has defined a member stag_mask
160 *
161 * It is used to indicate a staggered grid
162 *
163 * return true if T::stag_mask is a valid type
164 *
165 */
166template<typename T>
167struct has_posMask<T, typename Void<decltype( T::stag_mask )>::type> : std::true_type
168{};
169
170
171/*! \brief check if T::type and T.data has the same type
172 *
173 * \tparam i when different from 0 a check is performed otherwise not, the reason of
174 * this is that the typedef and data could also not exist producing
175 * compilation error, this flag avoid this, it perform the check only if it
176 * is safe
177 *
178 * \tparam T
179 *
180 * ### Example
181 *
182 * \snippet util_test.hpp Check is_typedef_and_data_same
183 *
184 * return true if the type of T::data is the same of T::type, false otherwise
185 *
186 */
187template<bool cond, typename T>
188struct is_typedef_and_data_same
189{
190 enum
191 {
192 value = std::is_same<decltype(std::declval<T>().data),typename T::type>::value
193 };
194};
195
196
197template<typename T>
198struct is_typedef_and_data_same<false,T>
199{
200 enum
201 {
202 value = false
203 };
204};
205
206
207template<typename T, typename Sfinae = void>
208struct has_noPointers: std::false_type {};
209
210
211/*! \brief has_noPointers check if a type has defined a
212 * method called noPointers
213 *
214 * ### Example
215 *
216 * \snippet util_test.hpp Check no pointers
217 *
218 * return true if T::noPointers() is a valid expression (function pointers)
219 * and produce a defined type
220 *
221 */
222template<typename T>
223struct has_noPointers<T, typename Void<decltype( T::noPointers() )>::type> : std::true_type
224{};
225
226/*! \brief has_Pack check if a type has defined a
227 * method called Pack
228 *
229 * ### Example
230 *
231 * \snippet
232 *
233 * return true if T::pack() is a valid expression (function pointers)
234 * and produce a defined type
235 *
236 */
237
238template<typename ObjType, typename Sfinae = void>
239struct has_pack: std::false_type {};
240
241template<typename ObjType>
242struct has_pack<ObjType, typename Void<decltype( ObjType::pack() )>::type> : std::true_type
243{};
244
245/*! \brief has_toKernel check if a type has defined a
246 * method called toKernel
247 *
248 * ### Example
249 *
250 * \snippet
251 *
252 * return true if T.toKernel() is a valid expression
253 * and produce a defined type
254 *
255 */
256
257template<typename ObjType, typename Sfinae = void>
258struct has_toKernel: std::false_type {};
259
260template<typename ObjType>
261struct has_toKernel<ObjType, typename Void<decltype( std::declval<ObjType>().toKernel() )>::type> : std::true_type
262{};
263
264/*! \brief has_packRequest check if a type has defined a
265 * method called packRequest
266 *
267 * ### Example
268 *
269 * \snippet util_test.hpp Check has pack
270 *
271 * return true if T::packRequest() is a valid expression (function pointers)
272 * and produce a defined type
273 *
274 */
275
276template<typename ObjType, typename Sfinae = void>
277struct has_packRequest: std::false_type {};
278
279template<typename ObjType>
280struct has_packRequest<ObjType, typename Void<decltype( ObjType::packRequest() )>::type> : std::true_type
281{};
282
283
284/*! \brief has_calculateMem check if a type has defined a
285 * method called calculateMem
286 *
287 * ### Example
288 *
289 * \snippet util_test.hpp Check has packRequest
290 *
291 * return true if T::calculateMem() is a valid expression (function pointers)
292 * and produce a defined type
293 *
294 */
295
296template<typename ObjType, typename Sfinae = void>
297struct has_packMem: std::false_type {};
298
299/*! \brief has_PackMem check if a type has packMem() member function
300 *
301 * ### Example
302 * \snippet util_test.hpp Check has packMem
303 *
304 * return true if ObjType::packMem() is a valid expression
305 *
306 */
307template<typename ObjType>
308struct has_packMem<ObjType, typename Void<decltype( ObjType::packMem() )>::type> : std::true_type
309{};
310
311/*! \brief is_openfpm_native check if a type is an openfpm native structure type
312 *
313 * ### Example
314 * \snippet util_test.hpp Declaration of an openfpm native structure
315 * \snippet util_test.hpp Usage with a non openfpm native structure
316 *
317 * return true if T::attributes::name[0] is a valid expression
318 * and produce a defined type
319 *
320 */
321template<typename T, bool = is_typedef_and_data_same<has_typedef_type<T>::value && has_data<T>::value,T>::value>
322struct is_openfpm_native : std::false_type
323{};
324
325
326template<typename T>
327struct is_openfpm_native<T,true> : std::true_type
328{};
329
330template<typename T, typename Sfinae = void>
331struct has_value_type_ofp: std::false_type {};
332
333/*! \brief has_value_type check if a type has defined a member value_type
334 *
335 * ### Example
336 *
337 * \snippet util_test.hpp Check has_value_type
338 *
339 * return true if T::value_type is a valid type
340 *
341 */
342template<typename T>
343//struct has_value_type<T, typename Void<decltype( typename T::value_type )>::type> : std::true_type
344struct has_value_type_ofp<T, typename Void< typename T::value_type>::type> : std::true_type
345{};
346
347
348//! [Metafunction definition]
349template<size_t index, size_t N> struct MetaFunc {
350 enum { value = index + N };
351};
352
353
354template<size_t index, size_t N> struct MetaFuncOrd {
355 enum { value = index };
356};
357
358///////////// Check if the
359
360template<typename ObjType, typename Sfinae = void>
361struct isDynStruct: std::false_type
362{
363 constexpr static bool value()
364 {
365 return false;
366 }
367};
368
369template<typename ObjType>
370struct isDynStruct<ObjType, typename Void<decltype( ObjType::isCompressed() )>::type> : std::true_type
371{
372 constexpr static bool value()
373 {
374 return ObjType::isCompressed();
375 }
376};
377
378
379#endif
380