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_GCOMMON_HPP
9 #define OPENCV_GAPI_GCOMMON_HPP
11 #include <functional> // std::hash
12 #include <vector> // std::vector
13 #include <type_traits> // decay
15 #include <opencv2/gapi/opencv_includes.hpp>
17 #include "opencv2/gapi/util/any.hpp"
18 #include "opencv2/gapi/own/exports.hpp"
19 #include "opencv2/gapi/own/assert.hpp"
25 // This is a trait-like structure to mark backend-specific compile arguments
27 template<typename T> struct CompileArgTag;
28 template<typename T> struct CompileArgTag
30 static const char* tag() { return ""; };
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
51 using is_compile_arg = std::is_same<GCompileArg, typename std::decay<T>::type>;
53 // CompileArg is an unified interface over backend-specific compilation
55 // FIXME: Move to a separate file?
56 /** \addtogroup gapi_compile_args
59 * @brief Compilation arguments: a set of data structures which can be
60 * passed to control compilation process
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).
70 * For example, if an example computation is executed like this:
72 * @snippet modules/gapi/samples/api_ref_snippets.cpp graph_decl_apply
74 * Extra parameter specifying which kernels to compile with can be
77 * @snippet modules/gapi/samples/api_ref_snippets.cpp apply_with_param
81 * @brief Represents an arbitrary compilation argument.
83 * Any value can be wrapped into cv::GCompileArg, but only known ones
84 * (to G-API or its backends) can be interpreted correctly.
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.
91 struct GAPI_EXPORTS GCompileArg
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())
104 template<typename T> T& get()
106 return util::any_cast<T>(arg);
109 template<typename T> const T& get() const
111 return util::any_cast<T>(arg);
118 using GCompileArgs = std::vector<GCompileArg>;
121 * Wraps a list of arguments (a parameter pack) into a vector of
122 * compilation arguments (cv::GCompileArg).
124 template<typename... Ts> GCompileArgs compile_args(Ts&&... args)
126 return GCompileArgs{ GCompileArg(args)... };
130 * @brief Ask G-API to dump compiled graph in Graphviz format under
131 * the given file name.
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.
137 struct graph_dump_path
139 std::string m_dump_path;
145 template<> struct CompileArgTag<cv::graph_dump_path>
147 static const char* tag() { return "gapi.graph_dump_path"; }
153 // std::hash overload for GShape
156 template<> struct hash<cv::GShape>
158 size_t operator() (cv::GShape sh) const
160 return std::hash<int>()(static_cast<int>(sh));
166 #endif // OPENCV_GAPI_GCOMMON_HPP