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_GTYPED_HPP
9 #define OPENCV_GAPI_GTYPED_HPP
10 #if !defined(GAPI_STANDALONE)
14 #include "opencv2/gapi/gcomputation.hpp"
15 #include "opencv2/gapi/gcompiled.hpp"
16 #include "opencv2/gapi/gproto.hpp"
17 #include "opencv2/gapi/gcommon.hpp"
23 // FIXME: How to prevent coolhackers from extending it by their own types?
24 // FIXME: ...Should we care?
25 template<typename T> struct ProtoToParam;
26 template<> struct ProtoToParam<cv::GMat> { using type = cv::Mat; };
27 template<> struct ProtoToParam<cv::GScalar> { using type = cv::Scalar; };
28 template<typename U> struct ProtoToParam<cv::GArray<U> > { using type = std::vector<U>; };
29 template<typename T> using ProtoToParamT = typename ProtoToParam<T>::type;
31 template<typename T> struct ProtoToMeta;
32 template<> struct ProtoToMeta<cv::GMat> { using type = cv::GMatDesc; };
33 template<> struct ProtoToMeta<cv::GScalar> { using type = cv::GScalarDesc; };
34 template<typename U> struct ProtoToMeta<cv::GArray<U> > { using type = cv::GArrayDesc; };
35 template<typename T> using ProtoToMetaT = typename ProtoToMeta<T>::type;
37 //workaround for MSVC 19.0 bug
39 auto make_default()->decltype(T{}) {return {};}
42 template<typename> class GComputationT;
44 // Single return value implementation
45 template<typename R, typename... Args> class GComputationT<R(Args...)>
48 typedef std::function<R(Args...)> Gen;
53 friend class GComputationT<R(Args...)>;
57 explicit GCompiledT(const cv::GCompiled &comp) : m_comp(comp) {}
62 void operator()(detail::ProtoToParamT<Args>... inArgs,
63 detail::ProtoToParamT<R> &outArg)
65 m_comp(cv::gin(inArgs...), cv::gout(outArg));
68 explicit operator bool() const
70 return static_cast<bool>(m_comp);
75 typedef std::pair<R, GProtoInputArgs > Captured;
77 Captured capture(const Gen& g, Args... args)
79 return Captured(g(args...), cv::GIn(args...));
83 cv::GComputation m_comp;
86 GComputationT(const Gen &generator)
87 : m_capture(capture(generator, detail::make_default<Args>()...))
88 , m_comp(cv::GProtoInputArgs(std::move(m_capture.second)),
89 cv::GOut(m_capture.first))
93 void apply(detail::ProtoToParamT<Args>... inArgs,
94 detail::ProtoToParamT<R> &outArg)
96 m_comp.apply(cv::gin(inArgs...), cv::gout(outArg));
99 GCompiledT compile(detail::ProtoToMetaT<Args>... inDescs)
101 GMetaArgs inMetas = { GMetaArg(inDescs)... };
102 return GCompiledT(m_comp.compile(std::move(inMetas), GCompileArgs()));
105 GCompiledT compile(detail::ProtoToMetaT<Args>... inDescs, GCompileArgs &&args)
107 GMetaArgs inMetas = { GMetaArg(inDescs)... };
108 return GCompiledT(m_comp.compile(std::move(inMetas), std::move(args)));
112 // Multiple (fixed) return value implementation. FIXME: How to avoid copy-paste?
113 template<typename... R, typename... Args> class GComputationT<std::tuple<R...>(Args...)>
116 typedef std::function<std::tuple<R...>(Args...)> Gen;
121 friend class GComputationT<std::tuple<R...>(Args...)>;
123 cv::GCompiled m_comp;
124 explicit GCompiledT(const cv::GCompiled &comp) : m_comp(comp) {}
129 void operator()(detail::ProtoToParamT<Args>... inArgs,
130 detail::ProtoToParamT<R>&... outArgs)
132 m_comp(cv::gin(inArgs...), cv::gout(outArgs...));
135 explicit operator bool() const
137 return static_cast<bool>(m_comp);
142 typedef std::pair<GProtoArgs, GProtoArgs> Captured;
145 Captured capture(GProtoArgs &&args, const std::tuple<R...> &rr, detail::Seq<IIs...>)
147 return Captured(cv::GOut(std::get<IIs>(rr)...).m_args, args);
150 Captured capture(const Gen& g, Args... args)
152 return capture(cv::GIn(args...).m_args, g(args...), typename detail::MkSeq<sizeof...(R)>::type());
156 cv::GComputation m_comp;
159 GComputationT(const Gen &generator)
160 : m_capture(capture(generator, detail::make_default<Args>()...))
161 , m_comp(cv::GProtoInputArgs(std::move(m_capture.second)),
162 cv::GProtoOutputArgs(std::move(m_capture.first)))
166 void apply(detail::ProtoToParamT<Args>... inArgs,
167 detail::ProtoToParamT<R>&... outArgs)
169 m_comp.apply(cv::gin(inArgs...), cv::gout(outArgs...));
172 GCompiledT compile(detail::ProtoToMetaT<Args>... inDescs)
174 GMetaArgs inMetas = { GMetaArg(inDescs)... };
175 return GCompiledT(m_comp.compile(std::move(inMetas), GCompileArgs()));
178 GCompiledT compile(detail::ProtoToMetaT<Args>... inDescs, GCompileArgs &&args)
180 GMetaArgs inMetas = { GMetaArg(inDescs)... };
181 return GCompiledT(m_comp.compile(std::move(inMetas), std::move(args)));
186 #endif // !defined(GAPI_STANDALONE)
187 #endif // OPENCV_GAPI_GTYPED_HPP