summaryrefslogtreecommitdiffstats
path: root/third_party/rlbox/include/rlbox_type_traits.hpp
blob: b32e677c322739e42ad76cc13577faa24cb9b128 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#pragma once
// IWYU pragma: private, include "rlbox.hpp"
// IWYU pragma: friend "rlbox_.*\.hpp"

#include <array>
#include <type_traits>

namespace rlbox::detail {

#define RLBOX_ENABLE_IF(...) std::enable_if_t<__VA_ARGS__>* = nullptr

template<typename T>
constexpr bool true_v = true;

template<typename T>
constexpr bool is_fundamental_or_enum_v =
  std::is_fundamental_v<T> || std::is_enum_v<T>;

template<typename T>
constexpr bool is_basic_type_v =
  std::is_fundamental_v<T> || std::is_enum_v<T> || std::is_pointer_v<T>;

template<typename T>
using valid_return_t =
  std::conditional_t<std::is_function_v<T>, void*, std::decay_t<T>>;

template<typename T>
using valid_param_t = std::conditional_t<std::is_void_v<T>, void*, T>;

namespace func_first_arg_detail {
  template<typename Ret, typename Arg, typename... Rest>
  Arg func_first_arg_t_helper(Ret (*)(Arg, Rest...));

  template<typename Ret, typename F, typename Arg, typename... Rest>
  Arg func_first_arg_t_helper(Ret (F::*)(Arg, Rest...));

  template<typename Ret, typename F, typename Arg, typename... Rest>
  Arg func_first_arg_t_helper(Ret (F::*)(Arg, Rest...) const);

  template<typename F>
  decltype(func_first_arg_t_helper(&F::operator())) first_argument_helper(F);
}

template<typename T>
using func_first_arg_t =
  decltype(func_first_arg_detail::first_argument_helper(std::declval<T>()));

namespace func_arg_nums_v_detail {
  template<typename T_Ret, typename... T_Args>
  constexpr size_t helper_two(T_Ret (*)(T_Args...))
  {
    return sizeof...(T_Args);
  }
  template<typename T_Func>
  constexpr size_t helper()
  {
    constexpr T_Func* ptr = nullptr;
    return helper_two(ptr);
  }
}

template<typename T_Func>
constexpr size_t func_arg_nums_v = func_arg_nums_v_detail::helper<T_Func>();

template<typename T>
using valid_array_el_t =
  std::conditional_t<std::is_void_v<T> || std::is_function_v<T>, int, T>;

template<typename T>
constexpr bool is_func_ptr_v = (std::is_pointer_v<T> &&
                                std::is_function_v<std::remove_pointer_t<T>>) ||
                               std::is_member_function_pointer_v<T>;

template<typename T>
constexpr bool is_func_or_func_ptr = std::is_function_v<T> || is_func_ptr_v<T>;

template<typename T>
constexpr bool is_one_level_ptr_v =
  std::is_pointer_v<T> && !std::is_pointer_v<std::remove_pointer_t<T>>;

template<typename T_This, typename T_Target>
using add_const_if_this_const_t =
  std::conditional_t<std::is_const_v<std::remove_pointer_t<T_This>>,
                     std::add_const_t<T_Target>,
                     T_Target>;

template<typename T>
using remove_const_from_pointer = std::conditional_t<
  std::is_pointer_v<T>,
  std::add_pointer_t<std::remove_const_t<std::remove_pointer_t<T>>>,
  T>;

template<typename T>
using add_const_from_pointer = std::conditional_t<
  std::is_pointer_v<T>,
  std::remove_pointer_t<std::add_const_t<std::remove_pointer_t<T>>>,
  T>;

template<typename T>
using remove_cv_ref_t = std::remove_cv_t<std::remove_reference_t<T>>;

template<typename T>
using c_to_std_array_t =
  std::conditional_t<std::is_array_v<T>,
                     std::array<std::remove_extent_t<T>, std::extent_v<T>>,
                     T>;

namespace std_array_to_c_arr_detail {
  template<typename T>
  struct W
  {
    using type = T;
  };

  template<typename T, size_t N>
  W<T[N]> std_array_to_c_arr_helper(std::array<T, N>);

  template<typename T>
  W<T> std_array_to_c_arr_helper(T&&);
}

template<typename T>
using std_array_to_c_arr_t =
  typename decltype(std_array_to_c_arr_detail::std_array_to_c_arr_helper(
    std::declval<T>()))::type;

template<typename T>
using dereference_result_t =
  std::conditional_t<std::is_pointer_v<T>,
                     std::remove_pointer_t<T>,
                     std::remove_extent_t<std_array_to_c_arr_t<T>> // is_array
                     >;

template<typename T>
using value_type_t =
  std::conditional_t<std::is_array_v<T>, c_to_std_array_t<T>, T>;

template<typename T>
using function_ptr_t =
  std::conditional_t<std::is_pointer_v<T> &&
                       std::is_function_v<std::remove_pointer_t<T>>,
                     T,
                     int (*)(int)>;

namespace is_c_or_std_array_detail {
  template<typename T, typename T_Enable = void>
  struct is_c_or_std_array_helper;

  template<typename T>
  struct is_c_or_std_array_helper<T, std::enable_if_t<std::is_array_v<T>>>
    : std::true_type
  {};

  template<typename T, size_t N>
  std::true_type is_std_array_helper(std::array<T, N>*);

  template<typename T>
  std::false_type is_std_array_helper(T*);

  template<typename T>
  constexpr bool is_std_array_v =
    decltype(is_std_array_helper(std::declval<std::add_pointer_t<T>>()))::value;

  template<typename T>
  struct is_c_or_std_array_helper<T, std::enable_if_t<is_std_array_v<T>>>
    : std::true_type
  {};

  template<typename T>
  struct is_c_or_std_array_helper<
    T,
    std::enable_if_t<!std::is_array_v<T> && !is_std_array_v<T>>>
    : std::false_type
  {};
}

template<typename T>
constexpr bool is_std_array_v = is_c_or_std_array_detail::is_std_array_v<T>;

template<typename T>
constexpr bool is_c_or_std_array_v =
  is_c_or_std_array_detail::is_c_or_std_array_helper<T>::value;

namespace std_array_el_detail {
  template<typename T>
  struct W
  {
    using type = T;
  };

  template<typename T, size_t N>
  W<T> is_std_array_helper(std::array<T, N>*);

  template<typename T>
  W<void> is_std_array_helper(T*);

  template<typename T>
  using std_array_el_t = decltype(std_array_el_detail::is_std_array_helper(
    std::declval<std::add_pointer_t<T>>));
}

template<typename T>
using std_array_el_t = typename std_array_el_detail::std_array_el_t<T>::type;

namespace all_extents_same_detail {

  template<typename T1, typename T2, typename T_Enable = void>
  struct all_extents_same_helper;

  template<typename T1, typename T2>
  struct all_extents_same_helper<
    T1,
    T2,
    std::enable_if_t<std::rank_v<T1> != std::rank_v<T2>>> : std::false_type
  {};

  template<typename T1, typename T2>
  struct all_extents_same_helper<
    T1,
    T2,
    std::enable_if_t<std::rank_v<T1> == std::rank_v<T2> &&
                     !std::is_array_v<T1> && !std::is_array_v<T2>>>
    : std::true_type
  {};

  template<typename T1, typename T2>
  struct all_extents_same_helper<
    T1,
    T2,
    std::enable_if_t<std::rank_v<T1> == std::rank_v<T2> &&
                     std::is_array_v<T1> && std::is_array_v<T2> &&
                     std::extent_v<T1> != std::extent_v<T2>>> : std::false_type
  {};

  template<typename T1, typename T2>
  struct all_extents_same_helper<
    T1,
    T2,
    std::enable_if_t<std::rank_v<T1> == std::rank_v<T2> &&
                     std::is_array_v<T1> && std::is_array_v<T2> &&
                     std::extent_v<T1> == std::extent_v<T2>>>
  {
    static constexpr bool value =
      all_extents_same_helper<std::remove_extent_t<T1>,
                              std::remove_extent_t<T2>>::value;
  };
}

template<typename T1, typename T2>
constexpr bool all_extents_same =
  all_extents_same_detail::all_extents_same_helper<T1, T2>::value;

// remove all pointers/extent types
namespace remove_all_pointers_detail {
  template<typename T>
  struct remove_all_pointers
  {
    typedef T type;
  };

  template<typename T>
  struct remove_all_pointers<T*>
  {
    typedef typename remove_all_pointers<T>::type type;
  };
}

template<typename T>
using remove_all_pointers_t =
  typename remove_all_pointers_detail::remove_all_pointers<T>::type;

// remove all pointers/extent types
namespace base_type_detail {
  template<typename T>
  struct base_type
  {
    typedef T type;
  };

  template<typename T>
  struct base_type<T*>
  {
    typedef typename base_type<T>::type type;
  };

  template<typename T>
  struct base_type<T[]>
  {
    typedef typename base_type<T>::type type;
  };

  template<typename T, std::size_t N>
  struct base_type<T[N]>
  {
    typedef typename base_type<T>::type type;
  };
}

template<typename T>
using base_type_t = typename base_type_detail::base_type<T>::type;

// convert types
namespace convert_detail {
  template<typename T,
           typename T_ShortType,
           typename T_IntType,
           typename T_LongType,
           typename T_LongLongType,
           typename T_PointerType,
           typename T_Enable = void>
  struct convert_base_types_t_helper;

  template<typename T,
           typename T_ShortType,
           typename T_IntType,
           typename T_LongType,
           typename T_LongLongType,
           typename T_PointerType>
  struct convert_base_types_t_helper<
    T,
    T_ShortType,
    T_IntType,
    T_LongType,
    T_LongLongType,
    T_PointerType,
    std::enable_if_t<std::is_same_v<short, T> && !std::is_const_v<T>>>
  {
    using type = T_ShortType;
  };

  template<typename T,
           typename T_ShortType,
           typename T_IntType,
           typename T_LongType,
           typename T_LongLongType,
           typename T_PointerType>
  struct convert_base_types_t_helper<
    T,
    T_ShortType,
    T_IntType,
    T_LongType,
    T_LongLongType,
    T_PointerType,
    std::enable_if_t<std::is_same_v<int, T> && !std::is_const_v<T>>>
  {
    using type = T_IntType;
  };

  template<typename T,
           typename T_ShortType,
           typename T_IntType,
           typename T_LongType,
           typename T_LongLongType,
           typename T_PointerType>
  struct convert_base_types_t_helper<
    T,
    T_ShortType,
    T_IntType,
    T_LongType,
    T_LongLongType,
    T_PointerType,
    std::enable_if_t<std::is_same_v<long, T> && !std::is_const_v<T>>>
  {
    using type = T_LongType;
  };

  template<typename T,
           typename T_ShortType,
           typename T_IntType,
           typename T_LongType,
           typename T_LongLongType,
           typename T_PointerType>
  struct convert_base_types_t_helper<
    T,
    T_ShortType,
    T_IntType,
    T_LongType,
    T_LongLongType,
    T_PointerType,
    std::enable_if_t<std::is_same_v<long long, T> && !std::is_const_v<T>>>
  {
    using type = T_LongLongType;
  };

  template<typename T,
           typename T_ShortType,
           typename T_IntType,
           typename T_LongType,
           typename T_LongLongType,
           typename T_PointerType>
  struct convert_base_types_t_helper<
    T,
    T_ShortType,
    T_IntType,
    T_LongType,
    T_LongLongType,
    T_PointerType,
    std::enable_if_t<std::is_pointer_v<T> && !std::is_const_v<T>>>
  {
    using type = T_PointerType;
  };

  template<typename T,
           typename T_ShortType,
           typename T_IntType,
           typename T_LongType,
           typename T_LongLongType,
           typename T_PointerType>
  struct convert_base_types_t_helper<
    T,
    T_ShortType,
    T_IntType,
    T_LongType,
    T_LongLongType,
    T_PointerType,
    std::enable_if_t<std::is_unsigned_v<T> && !std::is_same_v<T, bool> &&
                     !std::is_same_v<T, char> && !std::is_const_v<T> &&
                     !std::is_enum_v<T>>>
  {
    using type = std::make_unsigned_t<
      typename convert_base_types_t_helper<std::make_signed_t<T>,
                                           T_ShortType,
                                           T_IntType,
                                           T_LongType,
                                           T_LongLongType,
                                           T_PointerType>::type>;
  };

  template<typename T,
           typename T_ShortType,
           typename T_IntType,
           typename T_LongType,
           typename T_LongLongType,
           typename T_PointerType>
  struct convert_base_types_t_helper<
    T,
    T_ShortType,
    T_IntType,
    T_LongType,
    T_LongLongType,
    T_PointerType,
    std::enable_if_t<(
      std::is_same_v<bool, T> || std::is_same_v<void, T> ||
      std::is_same_v<char, T> || std::is_same_v<signed char, T> ||
      std::is_floating_point_v<T> || std::is_enum_v<T>)&&!std::is_const_v<T>>>
  {
    using type = T;
  };

  template<typename T,
           typename T_ShortType,
           typename T_IntType,
           typename T_LongType,
           typename T_LongLongType,
           typename T_PointerType>
  struct convert_base_types_t_helper<
    T,
    T_ShortType,
    T_IntType,
    T_LongType,
    T_LongLongType,
    T_PointerType,
    std::enable_if_t<std::is_array_v<T> && !std::is_const_v<T>>>
  {
    using type = typename convert_base_types_t_helper<
      std::remove_extent_t<T>,
      T_ShortType,
      T_IntType,
      T_LongType,
      T_LongLongType,
      T_PointerType>::type[std::extent_v<T>];
  };

  template<typename T,
           typename T_ShortType,
           typename T_IntType,
           typename T_LongType,
           typename T_LongLongType,
           typename T_PointerType>
  struct convert_base_types_t_helper<T,
                                     T_ShortType,
                                     T_IntType,
                                     T_LongType,
                                     T_LongLongType,
                                     T_PointerType,
                                     std::enable_if_t<std::is_const_v<T>>>
  {
    using type = std::add_const_t<
      typename convert_base_types_t_helper<std::remove_const_t<T>,
                                           T_ShortType,
                                           T_IntType,
                                           T_LongType,
                                           T_LongLongType,
                                           T_PointerType>::type>;
  };
}

template<typename T,
         typename T_ShortType,
         typename T_IntType,
         typename T_LongType,
         typename T_LongLongType,
         typename T_PointerType>
using convert_base_types_t =
  typename convert_detail::convert_base_types_t_helper<T,
                                                       T_ShortType,
                                                       T_IntType,
                                                       T_LongType,
                                                       T_LongLongType,
                                                       T_PointerType>::type;

namespace unsigned_int_of_size_t_detail {
  template<typename T, typename T_Enable = void>
  struct unsigned_int_of_size_t_helper;

  template<typename T>
  struct unsigned_int_of_size_t_helper<T, std::enable_if_t<sizeof(T) == 1>>
  {
    using type = uint8_t;
  };

  template<typename T>
  struct unsigned_int_of_size_t_helper<T, std::enable_if_t<sizeof(T) == 2>>
  {
    using type = uint16_t;
  };

  template<typename T>
  struct unsigned_int_of_size_t_helper<T, std::enable_if_t<sizeof(T) == 4>>
  {
    using type = uint32_t;
  };

  template<typename T>
  struct unsigned_int_of_size_t_helper<T, std::enable_if_t<sizeof(T) == 8>>
  {
    using type = uint64_t;
  };
}

template<typename T>
using unsigned_int_of_size_t =
  typename unsigned_int_of_size_t_detail::unsigned_int_of_size_t_helper<
    T>::type;

}