From dd2c7c5140f16e7951f5e3d8529149aeac848a1e Mon Sep 17 00:00:00 2001 From: Anton Potapov Date: Mon, 13 Apr 2020 11:17:08 +0300 Subject: [PATCH] GAPI: utils - variant::get_if adding one more missing function to local version of std::variant --- .../include/opencv2/gapi/util/variant.hpp | 40 +++++++++++++++---- modules/gapi/test/util/variant_tests.cpp | 16 ++++++++ 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/modules/gapi/include/opencv2/gapi/util/variant.hpp b/modules/gapi/include/opencv2/gapi/util/variant.hpp index 22dfb2ea770f..6e6a73bd604a 100644 --- a/modules/gapi/include/opencv2/gapi/util/variant.hpp +++ b/modules/gapi/include/opencv2/gapi/util/variant.hpp @@ -150,6 +150,9 @@ namespace util protected: template friend T& get(variant &v); template friend const T& get(const variant &v); + template friend T* get_if(variant *v) noexcept; + template friend const T* get_if(const variant *v) noexcept; + template friend bool operator==(const variant &lhs, const variant &rhs); Memory memory; @@ -201,6 +204,11 @@ namespace util }; // FIMXE: visit + template + T* get_if(util::variant* v) noexcept; + + template + const T* get_if(const util::variant* v) noexcept; template T& get(util::variant &v); @@ -335,27 +343,43 @@ namespace util } template - T& get(util::variant &v) + T* get_if(util::variant* v) noexcept { const constexpr std::size_t t_index = util::type_list_index::value; - if (v.index() == t_index) - return *(T*)(&v.memory); // workaround for ICC 2019 + if (v && v->index() == t_index) + return (T*)(&v->memory); // workaround for ICC 2019 // original code: return reinterpret_cast(v.memory); - else - throw_error(bad_variant_access()); + return nullptr; } template - const T& get(const util::variant &v) + const T* get_if(const util::variant* v) noexcept { const constexpr std::size_t t_index = util::type_list_index::value; - if (v.index() == t_index) - return *(const T*)(&v.memory); // workaround for ICC 2019 + if (v && v->index() == t_index) + return (const T*)(&v->memory); // workaround for ICC 2019 // original code: return reinterpret_cast(v.memory); + return nullptr; + } + + template + T& get(util::variant &v) + { + if (auto* p = get_if(&v)) + return *p; + else + throw_error(bad_variant_access()); + } + + template + const T& get(const util::variant &v) + { + if (auto* p = get_if(&v)) + return *p; else throw_error(bad_variant_access()); } diff --git a/modules/gapi/test/util/variant_tests.cpp b/modules/gapi/test/util/variant_tests.cpp index 41aa4afa3d36..69f88b3fa486 100644 --- a/modules/gapi/test/util/variant_tests.cpp +++ b/modules/gapi/test/util/variant_tests.cpp @@ -289,6 +289,22 @@ TEST(Variant, Swap_DiffIndex) EXPECT_EQ(3.14f, util::get(tv1)); } +TEST(Variant, GetIf) +{ + const TestVar cv(42); + + // Test const& get_if() + EXPECT_EQ(nullptr, util::get_if(&cv)); + ASSERT_NE(nullptr, util::get_if(&cv)); + EXPECT_EQ(42, *util::get_if(&cv)); + + // Test &get_if + TestVar cv2(std::string("42")); + EXPECT_EQ(nullptr, util::get_if(&cv2)); + ASSERT_NE(nullptr, util::get_if(&cv2)); + EXPECT_EQ("42", *util::get_if(&cv2)); +} + TEST(Variant, Get) { const TestVar cv(42);