util.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. //
  5. // Copyright (C) 2018-2019 Intel Corporation
  6. #ifndef OPENCV_GAPI_UTIL_HPP
  7. #define OPENCV_GAPI_UTIL_HPP
  8. #include <tuple>
  9. // \cond HIDDEN_SYMBOLS
  10. // This header file contains some generic utility functions which are
  11. // used in other G-API Public API headers.
  12. //
  13. // PLEASE don't put any stuff here if it is NOT used in public API headers!
  14. namespace cv
  15. {
  16. namespace detail
  17. {
  18. // Recursive integer sequence type, useful for enumerating elements of
  19. // template parameter packs.
  20. template<int... I> struct Seq { using next = Seq<I..., sizeof...(I)>; };
  21. template<int Sz> struct MkSeq { using type = typename MkSeq<Sz-1>::type::next; };
  22. template<> struct MkSeq<0>{ using type = Seq<>; };
  23. // Checks if elements of variadic template satisfy the given Predicate.
  24. // Implemented via tuple, with an interface to accept plain type lists
  25. template<template<class> class, typename, typename...> struct all_satisfy;
  26. template<template<class> class F, typename T, typename... Ts>
  27. struct all_satisfy<F, std::tuple<T, Ts...> >
  28. {
  29. static const constexpr bool value = F<T>::value
  30. && all_satisfy<F, std::tuple<Ts...> >::value;
  31. };
  32. template<template<class> class F, typename T>
  33. struct all_satisfy<F, std::tuple<T> >
  34. {
  35. static const constexpr bool value = F<T>::value;
  36. };
  37. template<template<class> class F, typename T, typename... Ts>
  38. struct all_satisfy: public all_satisfy<F, std::tuple<T, Ts...> > {};
  39. // Permute given tuple type C with given integer sequence II
  40. // Sequence may be less than tuple C size.
  41. template<class, class> struct permute_tuple;
  42. template<class C, int... IIs>
  43. struct permute_tuple<C, Seq<IIs...> >
  44. {
  45. using type = std::tuple< typename std::tuple_element<IIs, C>::type... >;
  46. };
  47. // Given T..., generates a type sequence of sizeof...(T)-1 elements
  48. // which is T... without its last element
  49. // Implemented via tuple, with an interface to accept plain type lists
  50. template<typename T, typename... Ts> struct all_but_last;
  51. template<typename T, typename... Ts>
  52. struct all_but_last<std::tuple<T, Ts...> >
  53. {
  54. using C = std::tuple<T, Ts...>;
  55. using S = typename MkSeq<std::tuple_size<C>::value - 1>::type;
  56. using type = typename permute_tuple<C, S>::type;
  57. };
  58. template<typename T, typename... Ts>
  59. struct all_but_last: public all_but_last<std::tuple<T, Ts...> > {};
  60. template<typename... Ts>
  61. using all_but_last_t = typename all_but_last<Ts...>::type;
  62. // NB.: This is here because there's no constexpr std::max in C++11
  63. template<std::size_t S0, std::size_t... SS> struct max_of_t
  64. {
  65. static constexpr const std::size_t rest = max_of_t<SS...>::value;
  66. static constexpr const std::size_t value = rest > S0 ? rest : S0;
  67. };
  68. template<std::size_t S> struct max_of_t<S>
  69. {
  70. static constexpr const std::size_t value = S;
  71. };
  72. template <typename...>
  73. struct contains : std::false_type{};
  74. template <typename T1, typename T2, typename... Ts>
  75. struct contains<T1, T2, Ts...> : std::integral_constant<bool, std::is_same<T1, T2>::value ||
  76. contains<T1, Ts...>::value> {};
  77. template<typename T, typename... Types>
  78. struct contains<T, std::tuple<Types...>> : std::integral_constant<bool, contains<T, Types...>::value> {};
  79. template <typename...>
  80. struct all_unique : std::true_type{};
  81. template <typename T1, typename... Ts>
  82. struct all_unique<T1, Ts...> : std::integral_constant<bool, !contains<T1, Ts...>::value &&
  83. all_unique<Ts...>::value> {};
  84. template<typename>
  85. struct tuple_wrap_helper;
  86. template<typename T> struct tuple_wrap_helper
  87. {
  88. using type = std::tuple<T>;
  89. static type get(T&& obj) { return std::make_tuple(std::move(obj)); }
  90. };
  91. template<typename... Objs>
  92. struct tuple_wrap_helper<std::tuple<Objs...>>
  93. {
  94. using type = std::tuple<Objs...>;
  95. static type get(std::tuple<Objs...>&& objs) { return std::forward<std::tuple<Objs...>>(objs); }
  96. };
  97. template<typename... Ts>
  98. struct make_void { typedef void type;};
  99. template<typename... Ts>
  100. using void_t = typename make_void<Ts...>::type;
  101. } // namespace detail
  102. namespace util
  103. {
  104. template<typename ...L>
  105. struct overload_lamba_set;
  106. template<typename L1>
  107. struct overload_lamba_set<L1> : public L1
  108. {
  109. overload_lamba_set(L1&& lambda) : L1(std::move(lambda)) {}
  110. overload_lamba_set(const L1& lambda) : L1(lambda) {}
  111. using L1::operator();
  112. };
  113. template<typename L1, typename ...L>
  114. struct overload_lamba_set<L1, L...> : public L1, public overload_lamba_set<L...>
  115. {
  116. using base_type = overload_lamba_set<L...>;
  117. overload_lamba_set(L1 &&lambda1, L&& ...lambdas):
  118. L1(std::move(lambda1)),
  119. base_type(std::forward<L>(lambdas)...) {}
  120. overload_lamba_set(const L1 &lambda1, L&& ...lambdas):
  121. L1(lambda1),
  122. base_type(std::forward<L>(lambdas)...) {}
  123. using L1::operator();
  124. using base_type::operator();
  125. };
  126. template<typename... L>
  127. overload_lamba_set<L...> overload_lambdas(L&& ...lambdas)
  128. {
  129. return overload_lamba_set<L...>(std::forward<L>(lambdas)...);
  130. }
  131. template<typename ...T>
  132. struct find_adapter_impl;
  133. template<typename AdapterT, typename T>
  134. struct find_adapter_impl<AdapterT, T>
  135. {
  136. using type = typename std::conditional<std::is_base_of<AdapterT, T>::value,
  137. T,
  138. void>::type;
  139. static constexpr bool found = std::is_base_of<AdapterT, T>::value;
  140. };
  141. template<typename AdapterT, typename T, typename... Types>
  142. struct find_adapter_impl<AdapterT, T, Types...>
  143. {
  144. using type = typename std::conditional<std::is_base_of<AdapterT, T>::value,
  145. T,
  146. typename find_adapter_impl<AdapterT, Types...>::type>::type;
  147. static constexpr bool found = std::is_base_of<AdapterT, T>::value ||
  148. find_adapter_impl<AdapterT, Types...>::found;
  149. };
  150. } // namespace util
  151. } // namespace cv
  152. // \endcond
  153. #endif // OPENCV_GAPI_UTIL_HPP