Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / thirdparty / fluid / modules / gapi / include / opencv2 / gapi / gcommon.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_GCOMMON_HPP
9 #define OPENCV_GAPI_GCOMMON_HPP
10
11 #include <functional>   // std::hash
12 #include <vector>       // std::vector
13 #include <type_traits>  // decay
14
15 #include <opencv2/gapi/opencv_includes.hpp>
16
17 #include "opencv2/gapi/util/any.hpp"
18 #include "opencv2/gapi/own/exports.hpp"
19 #include "opencv2/gapi/own/assert.hpp"
20
21 namespace cv {
22
23 namespace detail
24 {
25     // This is a trait-like structure to mark backend-specific compile arguments
26     // with tags
27     template<typename T> struct CompileArgTag;
28     template<typename T> struct CompileArgTag
29     {
30         static const char* tag() { return ""; };
31     };
32 }
33
34 // This definition is here because it is reused by both public(?) and internal
35 // modules. Keeping it here wouldn't expose public details (e.g., API-level)
36 // to components which are internal and operate on a lower-level entities
37 // (e.g., compiler, backends).
38 // FIXME: merge with ArgKind?
39 // FIXME: replace with variant[format desc]?
40 enum class GShape: int
41 {
42     GMAT,
43     GSCALAR,
44     GARRAY,
45 };
46
47 struct GCompileArg;
48
49 namespace detail {
50     template<typename T>
51     using is_compile_arg = std::is_same<GCompileArg, typename std::decay<T>::type>;
52 }
53 // CompileArg is an unified interface over backend-specific compilation
54 // information
55 // FIXME: Move to a separate file?
56 /** \addtogroup gapi_compile_args
57  * @{
58  *
59  * @brief Compilation arguments: a set of data structures which can be
60  * passed to control compilation process
61  *
62  * G-API comes with a number of graph compilation options which can be
63  * passed to cv::GComputation::apply() or
64  * cv::GComputation::compile(). Known compilation options are listed
65  * in this page, while extra backends may introduce their own
66  * compilation options (G-API transparently accepts _everything_ which
67  * can be passed to cv::compile_args(), it depends on underlying
68  * backends if an option would be interpreted or not).
69  *
70  * For example, if an example computation is executed like this:
71  *
72  * @snippet modules/gapi/samples/api_ref_snippets.cpp graph_decl_apply
73  *
74  * Extra parameter specifying which kernels to compile with can be
75  * passed like this:
76  *
77  * @snippet modules/gapi/samples/api_ref_snippets.cpp apply_with_param
78  */
79
80 /**
81  * @brief Represents an arbitrary compilation argument.
82  *
83  * Any value can be wrapped into cv::GCompileArg, but only known ones
84  * (to G-API or its backends) can be interpreted correctly.
85  *
86  * Normally objects of this class shouldn't be created manually, use
87  * cv::compile_args() function which automatically wraps everything
88  * passed in (a variadic template parameter pack) into a vector of
89  * cv::GCompileArg objects.
90  */
91 struct GAPI_EXPORTS GCompileArg
92 {
93 public:
94     std::string tag;
95
96     // FIXME: use decay in GArg/other trait-based wrapper before leg is shot!
97     template<typename T, typename std::enable_if<!detail::is_compile_arg<T>::value, int>::type = 0>
98     explicit GCompileArg(T &&t)
99         : tag(detail::CompileArgTag<typename std::decay<T>::type>::tag())
100         , arg(t)
101     {
102     }
103
104     template<typename T> T& get()
105     {
106         return util::any_cast<T>(arg);
107     }
108
109     template<typename T> const T& get() const
110     {
111         return util::any_cast<T>(arg);
112     }
113
114 private:
115     util::any arg;
116 };
117
118 using GCompileArgs = std::vector<GCompileArg>;
119
120 /**
121  * Wraps a list of arguments (a parameter pack) into a vector of
122  * compilation arguments (cv::GCompileArg).
123  */
124 template<typename... Ts> GCompileArgs compile_args(Ts&&... args)
125 {
126     return GCompileArgs{ GCompileArg(args)... };
127 }
128
129 /**
130  * @brief Ask G-API to dump compiled graph in Graphviz format under
131  * the given file name.
132  *
133  * Specifies a graph dump path (path to .dot file to be generated).
134  * G-API will dump a .dot file under specified path during a
135  * compilation process if this flag is passed.
136  */
137 struct graph_dump_path
138 {
139     std::string m_dump_path;
140 };
141 /** @} */
142
143 namespace detail
144 {
145     template<> struct CompileArgTag<cv::graph_dump_path>
146     {
147         static const char* tag() { return "gapi.graph_dump_path"; }
148     };
149 }
150
151 } // namespace cv
152
153 // std::hash overload for GShape
154 namespace std
155 {
156 template<> struct hash<cv::GShape>
157 {
158     size_t operator() (cv::GShape sh) const
159     {
160         return std::hash<int>()(static_cast<int>(sh));
161     }
162 };
163 } // namespace std
164
165
166 #endif // OPENCV_GAPI_GCOMMON_HPP