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.
5 // Copyright (C) 2018-2019 Intel Corporation
8 #ifndef OPENCV_GAPI_UTIL_HPP
9 #define OPENCV_GAPI_UTIL_HPP
11 #include <utility> // std::tuple
13 // \cond HIDDEN_SYMBOLS
14 // This header file contains some generic utility functions which are
15 // used in other G-API Public API headers.
17 // PLEASE don't put any stuff here if it is NOT used in public API headers!
23 // Recursive integer sequence type, useful for enumerating elements of
24 // template parameter packs.
25 template<int... I> struct Seq { using next = Seq<I..., sizeof...(I)>; };
26 template<int Sz> struct MkSeq { using type = typename MkSeq<Sz-1>::type::next; };
27 template<> struct MkSeq<0>{ using type = Seq<>; };
29 // Checks if elements of variadic template satisfy the given Predicate.
30 // Implemented via tuple, with an interface to accept plain type lists
31 template<template<class> class, typename, typename...> struct all_satisfy;
33 template<template<class> class F, typename T, typename... Ts>
34 struct all_satisfy<F, std::tuple<T, Ts...> >
36 static const constexpr bool value = F<T>::value
37 && all_satisfy<F, std::tuple<Ts...> >::value;
39 template<template<class> class F, typename T>
40 struct all_satisfy<F, std::tuple<T> >
42 static const constexpr bool value = F<T>::value;
45 template<template<class> class F, typename T, typename... Ts>
46 struct all_satisfy: public all_satisfy<F, std::tuple<T, Ts...> > {};
48 // Permute given tuple type C with given integer sequence II
49 // Sequence may be less than tuple C size.
50 template<class, class> struct permute_tuple;
52 template<class C, int... IIs>
53 struct permute_tuple<C, Seq<IIs...> >
55 using type = std::tuple< typename std::tuple_element<IIs, C>::type... >;
58 // Given T..., generates a type sequence of sizeof...(T)-1 elements
59 // which is T... without its last element
60 // Implemented via tuple, with an interface to accept plain type lists
61 template<typename T, typename... Ts> struct all_but_last;
63 template<typename T, typename... Ts>
64 struct all_but_last<std::tuple<T, Ts...> >
66 using C = std::tuple<T, Ts...>;
67 using S = typename MkSeq<std::tuple_size<C>::value - 1>::type;
68 using type = typename permute_tuple<C, S>::type;
71 template<typename T, typename... Ts>
72 struct all_but_last: public all_but_last<std::tuple<T, Ts...> > {};
74 template<typename... Ts>
75 using all_but_last_t = typename all_but_last<Ts...>::type;
77 // NB.: This is here because there's no constexpr std::max in C++11
78 template<std::size_t S0, std::size_t... SS> struct max_of_t
80 static constexpr const std::size_t rest = max_of_t<SS...>::value;
81 static constexpr const std::size_t value = rest > S0 ? rest : S0;
83 template<std::size_t S> struct max_of_t<S>
85 static constexpr const std::size_t value = S;
92 #endif // OPENCV_GAPI_UTIL_HPP