meta_find.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "simple/support/meta/find.hpp"
  2. #include "simple/support/meta/bind.hpp"
  3. #include "simple/support/meta/list.hpp"
  4. #include <type_traits>
  5. using namespace simple::support;
  6. template <size_t i>
  7. using size_constant = std::integral_constant<size_t, i>;
  8. // find double counting floats on the way
  9. template <typename count, typename T>
  10. struct count_float_until_double
  11. {
  12. static constexpr bool value = std::is_same_v<T, double>;
  13. using binding = size_constant<count{} + std::is_same_v<T,float>>;
  14. };
  15. // find double counting floats on the way
  16. template <typename T, typename count>
  17. struct count_float_until_double2
  18. {
  19. static constexpr bool value = std::is_same_v<T, double>;
  20. using binding = size_constant<count{} + std::is_same_v<T,float>>;
  21. };
  22. int main()
  23. {
  24. static_assert(meta::find_v<bool,
  25. meta::list<char, int, bool, double>>
  26. == 2
  27. );
  28. static_assert(meta::find_v<short,
  29. meta::list<char, int, bool, double>>
  30. == 4
  31. );
  32. static_assert(meta::find_if_v<std::is_floating_point,
  33. meta::list<char, int, bool, double>>
  34. == 3
  35. );
  36. static_assert(meta::find_if_v<std::is_array,
  37. meta::list<char, int, bool, double>>
  38. == 4
  39. );
  40. static_assert(std::is_same_v<
  41. meta::find_if_t<std::is_floating_point,
  42. meta::list<char, bool, double, int>>,
  43. double
  44. >);
  45. using found = meta::find_t
  46. <
  47. meta::bind<count_float_until_double, size_constant<0>>,
  48. meta::list<char, float, bool, float, double, int, float>
  49. >;
  50. // found double
  51. static_assert(std::is_same_v<found::type, double>);
  52. // at index 4
  53. static_assert(found::value == 4);
  54. // counted 2 floats
  55. static_assert(found::functor::binding{} == 2);
  56. using found2 = meta::find_t
  57. <
  58. meta::bind_after<count_float_until_double2, size_constant<0>>,
  59. meta::list<char, float, bool, float, double, int, float>
  60. >;
  61. // found double
  62. static_assert(std::is_same_v<found2::type, double>);
  63. // at index 4
  64. static_assert(found2::value == 4);
  65. // counted 2 floats
  66. static_assert(found2::functor::binding{} == 2);
  67. return 0;
  68. }