Skip to content

Commit 272c3bc

Browse files
committed
Refactor of is() - part 3 (std::any)
1 parent da9c66c commit 272c3bc

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

include/cpp2util.h

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,40 +1856,38 @@ auto as( std::variant<Ts...> const& x ) -> decltype(auto) {
18561856
// std::any is and as
18571857
//
18581858

1859-
// is Type
1859+
// std::any variable is Type
18601860
//
1861-
template<typename T, typename X>
1862-
requires (std::is_same_v<X,std::any> && !std::is_same_v<T,std::any> && !std::is_same_v<T,empty>)
1863-
constexpr auto is( X const& x ) -> bool
1861+
template<typename T>
1862+
constexpr auto is( same_type_as<std::any> auto && x ) -> bool
18641863
{ return x.type() == Typeid<T>(); }
18651864

1866-
template<typename T, typename X>
1867-
requires (std::is_same_v<X,std::any> && std::is_same_v<T,empty>)
1868-
constexpr auto is( X const& x ) -> bool
1865+
template<std::same_as<empty> T>
1866+
constexpr auto is( same_type_as<std::any> auto && x ) -> bool
18691867
{ return !x.has_value(); }
18701868

18711869

1872-
// is Value
1870+
// std::any variable is Value
18731871
//
1874-
inline constexpr auto is( std::any const& x, auto&& value ) -> bool
1875-
{
1876-
// Predicate case
1877-
if constexpr (requires{ bool{ value(x) }; }) {
1878-
return value(x);
1879-
}
1880-
else if constexpr (std::is_function_v<decltype(value)> || requires{ &value.operator(); }) {
1881-
return false;
1882-
}
18831872

1884-
// Value case
1885-
else if constexpr (requires{ bool{ *std::any_cast<CPP2_TYPEOF(value)>(&x) == value }; }) {
1886-
auto pvalue = std::any_cast<CPP2_TYPEOF(value)>(&x);
1887-
return pvalue && *pvalue == value;
1888-
}
1889-
// else
1890-
return false;
1873+
template <std::same_as<std::any> X, has_defined_argument V>
1874+
requires not_same_as<std::any, argument_of_t<V>>
1875+
constexpr bool is( X const& x, V && value ) {
1876+
auto* ptr = std::any_cast<argument_of_t<V>>(&x);
1877+
return ptr && value(*ptr);
18911878
}
18921879

1880+
template <std::same_as<std::any> X, std::equality_comparable V>
1881+
requires (!has_defined_argument<V>)
1882+
constexpr bool is( X const& x, V && value ) {
1883+
if constexpr (pointer_like<V>) {
1884+
auto* ptr = std::any_cast<pointee_t<V>>(&x);
1885+
return ptr && !is<empty>(value) && is(*ptr, *value);
1886+
} else {
1887+
auto* ptr = std::any_cast<std::remove_cvref_t<V>>(&x);
1888+
return ptr && is(*ptr, std::forward<V>(value));
1889+
}
1890+
}
18931891

18941892
// as
18951893
//

0 commit comments

Comments
 (0)