From c5b8b5687f2ec7fe73d6e2265eba46804114dcd9 Mon Sep 17 00:00:00 2001 From: Sergey Ivanov Date: Fri, 3 Dec 2021 15:30:05 +0300 Subject: [PATCH] Merge pull request #21041 from sivanov-work:gin_gout_concept G-API: GAPI introduce compile guard for some types for gin/gout params passing * Initial for taged solution * Move out tags to gtags.hpp & add protection for own::Mat * Add compile guard to proper place * Fix MACRO concat * Add unit tests * Remove class MACRO injection due to Python3 * Revert back unproper changes * Apply comments: reuse shape from traits * Throw away unused gtags * Apply comments * Handle own::* * Fix test * Fix test(1) * Fix unix build * Try on type list * Apply comments * Apply comments * Fix warning --- modules/gapi/include/opencv2/gapi/gtype_traits.hpp | 20 +++++++++++ .../gapi/include/opencv2/gapi/opencv_includes.hpp | 8 +++++ modules/gapi/include/opencv2/gapi/own/types.hpp | 1 + modules/gapi/include/opencv2/gapi/util/util.hpp | 7 ++++ modules/gapi/test/gapi_util_tests.cpp | 40 +++++++++++++++++++--- .../gapi/test/internal/gapi_int_gmetaarg_test.cpp | 16 +++------ .../gapi/test/internal/gapi_int_proto_tests.cpp | 1 - 7 files changed, 76 insertions(+), 17 deletions(-) diff --git a/modules/gapi/include/opencv2/gapi/gtype_traits.hpp b/modules/gapi/include/opencv2/gapi/gtype_traits.hpp index 2e8dcb1..2b43421 100644 --- a/modules/gapi/include/opencv2/gapi/gtype_traits.hpp +++ b/modules/gapi/include/opencv2/gapi/gtype_traits.hpp @@ -19,11 +19,25 @@ #include #include #include +#include +#include namespace cv { namespace detail { + template + struct contains_shape_field : std::false_type {}; + + template + struct contains_shape_field> : + std::is_same::type, GShape> + {}; + + template + struct has_gshape : contains_shape_field {}; + // FIXME: These traits and enum and possible numerous switch(kind) // block may be replaced with a special Handler object or with // a double dispatch @@ -181,10 +195,16 @@ namespace detail } template static auto wrap_in (const U &u) -> typename GTypeTraits::strip_type { + static_assert(!(cv::detail::has_gshape>::value + || cv::detail::contains::type, GAPI_OWN_TYPES_LIST>::value), + "gin/gout must not be used with G* classes or cv::gapi::own::*"); return GTypeTraits::wrap_in(u); } template static auto wrap_out(U &u) -> typename GTypeTraits::strip_type { + static_assert(!(cv::detail::has_gshape>::value + || cv::detail::contains::type, GAPI_OWN_TYPES_LIST>::value), + "gin/gout must not be used with G* classses or cv::gapi::own::*"); return GTypeTraits::wrap_out(u); } }; diff --git a/modules/gapi/include/opencv2/gapi/opencv_includes.hpp b/modules/gapi/include/opencv2/gapi/opencv_includes.hpp index 08b2d6e..25a67d6 100644 --- a/modules/gapi/include/opencv2/gapi/opencv_includes.hpp +++ b/modules/gapi/include/opencv2/gapi/opencv_includes.hpp @@ -14,6 +14,12 @@ # include # include # include +#define GAPI_OWN_TYPES_LIST cv::gapi::own::Rect, \ + cv::gapi::own::Size, \ + cv::gapi::own::Point, \ + cv::gapi::own::Point2f, \ + cv::gapi::own::Scalar, \ + cv::gapi::own::Mat #else // Without OpenCV # include # include // cv::gapi::own::Rect/Size/Point @@ -28,6 +34,8 @@ namespace cv { using Scalar = gapi::own::Scalar; using Mat = gapi::own::Mat; } // namespace cv +#define GAPI_OWN_TYPES_LIST cv::gapi::own::VoidType + #endif // !defined(GAPI_STANDALONE) #endif // OPENCV_GAPI_OPENCV_INCLUDES_HPP diff --git a/modules/gapi/include/opencv2/gapi/own/types.hpp b/modules/gapi/include/opencv2/gapi/own/types.hpp index 6bd6880..3814366 100644 --- a/modules/gapi/include/opencv2/gapi/own/types.hpp +++ b/modules/gapi/include/opencv2/gapi/own/types.hpp @@ -143,6 +143,7 @@ inline std::ostream& operator<<(std::ostream& o, const Size& s) return o; } +struct VoidType {}; } // namespace own } // namespace gapi } // namespace cv diff --git a/modules/gapi/include/opencv2/gapi/util/util.hpp b/modules/gapi/include/opencv2/gapi/util/util.hpp index eb435a3..3be46d7 100644 --- a/modules/gapi/include/opencv2/gapi/util/util.hpp +++ b/modules/gapi/include/opencv2/gapi/util/util.hpp @@ -116,6 +116,13 @@ namespace detail using type = std::tuple; static type get(std::tuple&& objs) { return std::forward>(objs); } }; + + template + struct make_void { typedef void type;}; + + template + using void_t = typename make_void::type; + } // namespace detail namespace util diff --git a/modules/gapi/test/gapi_util_tests.cpp b/modules/gapi/test/gapi_util_tests.cpp index e080f54..b95c12f 100644 --- a/modules/gapi/test/gapi_util_tests.cpp +++ b/modules/gapi/test/gapi_util_tests.cpp @@ -4,13 +4,32 @@ // // Copyright (C) 2018 Intel Corporation - -#include "test_precomp.hpp" - #include - +#include "test_precomp.hpp" +#include #include +namespace cv +{ +struct Own {}; +namespace gapi +{ +namespace own +{ +struct ConvertibleToOwn{}; +struct NotConvertibleToOwn{}; +} // own +} // gapi +} // cv + +struct NoGhape {}; +struct HasGShape { + static constexpr cv::GShape shape = cv::GShape::GFRAME; +}; +struct MimicGShape { + static constexpr int shape = 0; +}; +#define DISALLOWED_LIST cv::gapi::own::NotConvertibleToOwn namespace opencv_test { @@ -40,4 +59,17 @@ TEST(GAPIUtil, AllButLast) "[int, float] are NOT all integral types"); } +TEST(GAPIUtil, GShaped) +{ + static_assert(!cv::detail::has_gshape::value, "NoGhape hasn't got any shape"); + static_assert(cv::detail::has_gshape::value, "HasGShape has got GShape shape"); + static_assert(!cv::detail::has_gshape::value, "MimicGShape hasn't got right shape"); +} + +TEST(GAPIUtil, GTypeList) +{ + static_assert(cv::detail::contains::value, "NotConvertibleToOwn is in denial list"); + static_assert(!cv::detail::contains::value, "ConvertibleToOwn is not in empty denial list"); + static_assert(!cv::detail::contains::value, "ConvertibleToOwn is not in denial list"); +} } // namespace opencv_test diff --git a/modules/gapi/test/internal/gapi_int_gmetaarg_test.cpp b/modules/gapi/test/internal/gapi_int_gmetaarg_test.cpp index 78048cf..bcf014a 100644 --- a/modules/gapi/test/internal/gapi_int_gmetaarg_test.cpp +++ b/modules/gapi/test/internal/gapi_int_gmetaarg_test.cpp @@ -138,22 +138,18 @@ TEST(GMetaArg, Can_Describe_RunArg) cv::Mat m(3, 3, CV_8UC3); cv::UMat um(3, 3, CV_8UC3); cv::Scalar s; - constexpr int w = 3, h = 3, c = 3; - uchar data[w*h*c]; - cv::gapi::own::Mat om(h, w, CV_8UC3, data); cv::Scalar os; std::vector v; GMetaArgs metas = {GMetaArg(descr_of(m)), GMetaArg(descr_of(um)), GMetaArg(descr_of(s)), - GMetaArg(descr_of(om)), GMetaArg(descr_of(os)), GMetaArg(descr_of(v))}; - auto in_run_args = cv::gin(m, um, s, om, os, v); + auto in_run_args = cv::gin(m, um, s, os, v); - for (int i = 0; i < 3; i++) { + for (size_t i = 0; i < metas.size(); i++) { EXPECT_TRUE(can_describe(metas[i], in_run_args[i])); } } @@ -178,22 +174,18 @@ TEST(GMetaArg, Can_Describe_RunArgP) cv::Mat m(3, 3, CV_8UC3); cv::UMat um(3, 3, CV_8UC3); cv::Scalar s; - constexpr int w = 3, h = 3, c = 3; - uchar data[w*h*c]; - cv::gapi::own::Mat om(h, w, CV_8UC3, data); cv::Scalar os; std::vector v; GMetaArgs metas = {GMetaArg(descr_of(m)), GMetaArg(descr_of(um)), GMetaArg(descr_of(s)), - GMetaArg(descr_of(om)), GMetaArg(descr_of(os)), GMetaArg(descr_of(v))}; - auto out_run_args = cv::gout(m, um, s, om, os, v); + auto out_run_args = cv::gout(m, um, s, os, v); - for (int i = 0; i < 3; i++) { + for (size_t i = 0; i < metas.size(); i++) { EXPECT_TRUE(can_describe(metas[i], out_run_args[i])); } } diff --git a/modules/gapi/test/internal/gapi_int_proto_tests.cpp b/modules/gapi/test/internal/gapi_int_proto_tests.cpp index 3dd7221..67b9fa4 100644 --- a/modules/gapi/test/internal/gapi_int_proto_tests.cpp +++ b/modules/gapi/test/internal/gapi_int_proto_tests.cpp @@ -15,7 +15,6 @@ struct ProtoPtrTest : public ::testing::Test { using Type = T; }; using ProtoPtrTestTypes = ::testing::Types< cv::Mat , cv::UMat - , cv::gapi::own::Mat , cv::RMat , cv::Scalar , std::vector -- 2.7.4