Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / fluid / modules / gapi / include / opencv2 / gapi / util / util.hpp
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
7
8 #ifndef OPENCV_GAPI_UTIL_HPP
9 #define OPENCV_GAPI_UTIL_HPP
10
11 #include <utility> // std::tuple
12
13 // \cond HIDDEN_SYMBOLS
14 // This header file contains some generic utility functions which are
15 // used in other G-API Public API headers.
16 //
17 // PLEASE don't put any stuff here if it is NOT used in public API headers!
18
19 namespace cv
20 {
21 namespace detail
22 {
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<>; };
28
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;
32
33     template<template<class> class F, typename T, typename... Ts>
34     struct all_satisfy<F, std::tuple<T, Ts...> >
35     {
36         static const constexpr bool value = F<T>::value
37             && all_satisfy<F, std::tuple<Ts...> >::value;
38     };
39     template<template<class> class F, typename T>
40     struct all_satisfy<F, std::tuple<T> >
41     {
42         static const constexpr bool value = F<T>::value;
43     };
44
45     template<template<class> class F, typename T, typename... Ts>
46     struct all_satisfy: public all_satisfy<F, std::tuple<T, Ts...> > {};
47
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;
51
52     template<class C, int... IIs>
53     struct permute_tuple<C, Seq<IIs...> >
54     {
55         using type = std::tuple< typename std::tuple_element<IIs, C>::type... >;
56     };
57
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;
62
63     template<typename T, typename... Ts>
64     struct all_but_last<std::tuple<T, Ts...> >
65     {
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;
69     };
70
71     template<typename T, typename... Ts>
72     struct all_but_last: public all_but_last<std::tuple<T, Ts...> > {};
73
74     template<typename... Ts>
75     using all_but_last_t = typename all_but_last<Ts...>::type;
76
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
79     {
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;
82     };
83     template<std::size_t S> struct max_of_t<S>
84     {
85         static constexpr const std::size_t value = S;
86     };
87 } // namespace detail
88 } // namespace cv
89
90 // \endcond
91
92 #endif //  OPENCV_GAPI_UTIL_HPP