Skip to content
Snippets Groups Projects
Commit 4e971ee2 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

introduce ext::visit

parent 2f61fec5
No related branches found
No related tags found
1 merge request!95Many clang-tidy fixes
......@@ -44,7 +44,7 @@ class Object {
return Object ( std::forward < decltype ( element ) > ( element ) );
};
 
return std::visit ( visitor, std::forward < Variant > ( data ) );
return ext::visit ( visitor, std::forward < Variant > ( data ) );
}
 
/**
......
......@@ -38,6 +38,24 @@
 
namespace ext {
 
template < class Visitor, class... Variants >
constexpr auto visit ( Visitor && vis, Variants && ... vars ) {
auto extToStd = [] ( auto && variant ) {
using __std__variant = typename std::decay_t < decltype ( variant ) >::__std__variant;
if constexpr ( std::is_lvalue_reference_v < decltype ( variant ) > && std::is_const_v < std::remove_reference_t < decltype ( variant ) > > ) {
return static_cast < const __std__variant & > ( variant );
} else if constexpr ( std::is_lvalue_reference_v < decltype ( variant ) > ) {
return static_cast < __std__variant & > ( variant );
} else if constexpr ( std::is_rvalue_reference_v < decltype ( variant ) > ) {
return static_cast < __std__variant && > ( variant );
} else {
static_assert ( "Unhandled option" );
}
};
return std::visit ( std::forward < Visitor > ( vis ), extToStd ( std::forward < Variants > ( vars ) ) ... );
}
/**
* \brief
* Class to help building of the variant type or, in case variant is requested to be constructed from single type or more types but all the same, that concrete type.
......@@ -243,7 +261,7 @@ public:
* \return negative value if this is smaller than the other instance, positive if this is greater than the other instance, zero othervise
*/
int compare ( const variant < Ts ... > & other ) const {
return std::visit ( VariantCompare ( other ), * this );
return ext::visit ( VariantCompare ( other ), * this );
}
 
/**
......@@ -258,7 +276,7 @@ public:
*/
template < typename T, typename std::enable_if < ext::is_in < T, Ts ... >::value >::type * = nullptr >
int compare ( const T & other ) const {
return std::visit ( VariantCompare2 < T > ( other ), * this );
return ext::visit ( VariantCompare2 < T > ( other ), * this );
}
 
/**
......@@ -358,7 +376,7 @@ public:
*/
template < class Result, class Callable >
Result call ( Callable callable ) const {
return std::visit ( callable, * this );
return ext::visit ( callable, * this );
}
 
/**
......
......@@ -59,7 +59,7 @@ bool stringApi < ext::variant < Types ... > >::first ( std::istream & input ) {
 
template < class ... Types >
void stringApi < ext::variant < Types ... > >::compose ( std::ostream & output, const ext::variant < Types ... > & container ) {
std::visit ( VariantStringApiCallback ( output ), container );
ext::visit ( VariantStringApiCallback ( output ), container );
}
 
} /* namespace core */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment