mirror of
https://github.com/opencv/opencv.git
synced 2025-06-12 20:42:53 +08:00
GAPI: utils - variant::get_if
adding one more missing function to local version of std::variant
This commit is contained in:
parent
f351653589
commit
dd2c7c5140
@ -150,6 +150,9 @@ namespace util
|
|||||||
protected:
|
protected:
|
||||||
template<typename T, typename... Us> friend T& get(variant<Us...> &v);
|
template<typename T, typename... Us> friend T& get(variant<Us...> &v);
|
||||||
template<typename T, typename... Us> friend const T& get(const variant<Us...> &v);
|
template<typename T, typename... Us> friend const T& get(const variant<Us...> &v);
|
||||||
|
template<typename T, typename... Us> friend T* get_if(variant<Us...> *v) noexcept;
|
||||||
|
template<typename T, typename... Us> friend const T* get_if(const variant<Us...> *v) noexcept;
|
||||||
|
|
||||||
template<typename... Us> friend bool operator==(const variant<Us...> &lhs,
|
template<typename... Us> friend bool operator==(const variant<Us...> &lhs,
|
||||||
const variant<Us...> &rhs);
|
const variant<Us...> &rhs);
|
||||||
Memory memory;
|
Memory memory;
|
||||||
@ -201,6 +204,11 @@ namespace util
|
|||||||
};
|
};
|
||||||
|
|
||||||
// FIMXE: visit
|
// FIMXE: visit
|
||||||
|
template<typename T, typename... Types>
|
||||||
|
T* get_if(util::variant<Types...>* v) noexcept;
|
||||||
|
|
||||||
|
template<typename T, typename... Types>
|
||||||
|
const T* get_if(const util::variant<Types...>* v) noexcept;
|
||||||
|
|
||||||
template<typename T, typename... Types>
|
template<typename T, typename... Types>
|
||||||
T& get(util::variant<Types...> &v);
|
T& get(util::variant<Types...> &v);
|
||||||
@ -335,14 +343,34 @@ namespace util
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename... Types>
|
template<typename T, typename... Types>
|
||||||
T& get(util::variant<Types...> &v)
|
T* get_if(util::variant<Types...>* v) noexcept
|
||||||
{
|
{
|
||||||
const constexpr std::size_t t_index =
|
const constexpr std::size_t t_index =
|
||||||
util::type_list_index<T, Types...>::value;
|
util::type_list_index<T, Types...>::value;
|
||||||
|
|
||||||
if (v.index() == t_index)
|
if (v && v->index() == t_index)
|
||||||
return *(T*)(&v.memory); // workaround for ICC 2019
|
return (T*)(&v->memory); // workaround for ICC 2019
|
||||||
// original code: return reinterpret_cast<T&>(v.memory);
|
// original code: return reinterpret_cast<T&>(v.memory);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename... Types>
|
||||||
|
const T* get_if(const util::variant<Types...>* v) noexcept
|
||||||
|
{
|
||||||
|
const constexpr std::size_t t_index =
|
||||||
|
util::type_list_index<T, Types...>::value;
|
||||||
|
|
||||||
|
if (v && v->index() == t_index)
|
||||||
|
return (const T*)(&v->memory); // workaround for ICC 2019
|
||||||
|
// original code: return reinterpret_cast<const T&>(v.memory);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename... Types>
|
||||||
|
T& get(util::variant<Types...> &v)
|
||||||
|
{
|
||||||
|
if (auto* p = get_if<T>(&v))
|
||||||
|
return *p;
|
||||||
else
|
else
|
||||||
throw_error(bad_variant_access());
|
throw_error(bad_variant_access());
|
||||||
}
|
}
|
||||||
@ -350,12 +378,8 @@ namespace util
|
|||||||
template<typename T, typename... Types>
|
template<typename T, typename... Types>
|
||||||
const T& get(const util::variant<Types...> &v)
|
const T& get(const util::variant<Types...> &v)
|
||||||
{
|
{
|
||||||
const constexpr std::size_t t_index =
|
if (auto* p = get_if<T>(&v))
|
||||||
util::type_list_index<T, Types...>::value;
|
return *p;
|
||||||
|
|
||||||
if (v.index() == t_index)
|
|
||||||
return *(const T*)(&v.memory); // workaround for ICC 2019
|
|
||||||
// original code: return reinterpret_cast<const T&>(v.memory);
|
|
||||||
else
|
else
|
||||||
throw_error(bad_variant_access());
|
throw_error(bad_variant_access());
|
||||||
}
|
}
|
||||||
|
@ -289,6 +289,22 @@ TEST(Variant, Swap_DiffIndex)
|
|||||||
EXPECT_EQ(3.14f, util::get<float>(tv1));
|
EXPECT_EQ(3.14f, util::get<float>(tv1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(Variant, GetIf)
|
||||||
|
{
|
||||||
|
const TestVar cv(42);
|
||||||
|
|
||||||
|
// Test const& get_if()
|
||||||
|
EXPECT_EQ(nullptr, util::get_if<std::string>(&cv));
|
||||||
|
ASSERT_NE(nullptr, util::get_if<int>(&cv));
|
||||||
|
EXPECT_EQ(42, *util::get_if<int>(&cv));
|
||||||
|
|
||||||
|
// Test &get_if
|
||||||
|
TestVar cv2(std::string("42"));
|
||||||
|
EXPECT_EQ(nullptr, util::get_if<int>(&cv2));
|
||||||
|
ASSERT_NE(nullptr, util::get_if<std::string>(&cv2));
|
||||||
|
EXPECT_EQ("42", *util::get_if<std::string>(&cv2));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(Variant, Get)
|
TEST(Variant, Get)
|
||||||
{
|
{
|
||||||
const TestVar cv(42);
|
const TestVar cv(42);
|
||||||
|
Loading…
Reference in New Issue
Block a user