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

allow comparison of variant with its content types

parent e497928d
Branches
Tags
No related merge requests found
......@@ -635,7 +635,7 @@ public:
* \brief
* Implementation of the three way comparison for varant.
*
* \param other the value compared to compared this
* \param other the value compared to this
*
* \return negative value if this is smaller than the other instance, positive if this is greater than the other instance, zero othervise
*/
......@@ -646,6 +646,25 @@ public:
return helper_t::compareHelper(this->type_id, &this->data, other.type_id, &other.data);
}
 
/**
* \brief
* Implementation of the three way comparison for varant.
*
* \tparam T the type of the other compared value
*
* \param other the value compared to this
*
* \return negative value if this is smaller than the other instance, positive if this is greater than the other instance, zero othervise
*/
template < typename T, typename std::enable_if < ext::is_in < T, Ts ... >::value >::type * = nullptr >
int compare ( const T & other ) const {
if ( this->type_id < ext::type_index ( typeid ( T ) ) ) return -1;
if ( this->type_id > ext::type_index ( typeid ( T ) ) ) return 1;
static ext::compare < typename std::decay < T >::type > comp;
return comp ( this->template get < T > ( ), other );
}
/**
* \brief
* Function to test whether the variant currently contains type T.
......@@ -932,7 +951,7 @@ public:
* \brief
* Implementation of the three way comparison for varant.
*
* \param other the value compared to compared this
* \param other the value compared to this
*
* \return negative value if this is smaller than the other instance, positive if this is greater than the other instance, zero othervise
*/
......@@ -941,6 +960,19 @@ public:
return comp ( m_value, other.m_value );
}
 
/**
* \brief
* Implementation of the three way comparison for varant.
*
* \param other the value compared to this
*
* \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 T & other ) const {
static ext::compare < typename std::decay < T >::type > comp;
return comp ( m_value, other );
}
/**
* \brief
* Function to test whether the variant currently contains type T.
......@@ -1174,7 +1206,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 < void > & ) const {
return true;
return 0;
}
 
/**
......@@ -1284,11 +1316,71 @@ struct compare < ext::variant < Ts ... > > {
*
* \return string representation
*/
template < class ... Ts >
template < class ... Ts >
std::string to_string ( const ext::variant < Ts ... > & value ) {
return ( std::string ) value;
}
 
template < class ... Ts, class T >
bool operator < ( const ext::variant < Ts ... > & first, const T & second ) {
return first.compare ( second ) < 0;
}
template < class ... Ts, class T >
bool operator > ( const ext::variant < Ts ... > & first, const T & second ) {
return first.compare ( second ) > 0;
}
template < class ... Ts, class T >
bool operator <= ( const ext::variant < Ts ... > & first, const T & second ) {
return first.compare ( second ) <= 0;
}
template < class ... Ts, class T >
bool operator >= ( const ext::variant < Ts ... > & first, const T & second ) {
return first.compare ( second ) >= 0;
}
template < class ... Ts, class T >
bool operator == ( const ext::variant < Ts ... > & first, const T & second ) {
return first.compare ( second ) == 0;
}
template < class ... Ts, class T >
bool operator != ( const ext::variant < Ts ... > & first, const T & second ) {
return first.compare ( second ) != 0;
}
template < class ... Ts, class T >
bool operator < ( const T & first, const ext::variant < Ts ... > & second ) {
return second > first;
}
template < class ... Ts, class T >
bool operator > ( const T & first, const ext::variant < Ts ... > & second ) {
return second < first;
}
template < class ... Ts, class T >
bool operator <= ( const T & first, const ext::variant < Ts ... > & second ) {
return second >= first;
}
template < class ... Ts, class T >
bool operator >= ( const T & first, const ext::variant < Ts ... > & second ) {
return second <= first;
}
template < class ... Ts, class T >
bool operator == ( const T & first, const ext::variant < Ts ... > & second ) {
return second == first;
}
template < class ... Ts, class T >
bool operator != ( const T & first, const ext::variant < Ts ... > & second ) {
return second != first;
}
/**
* \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.
......
......@@ -18,6 +18,8 @@ void VariantTest::testVariant() {
ext::variant<int, std::string, VariantTest::test> d(10);
CPPUNIT_ASSERT( d.is<int>() );
CPPUNIT_ASSERT( d.get<int>() == 10 );
CPPUNIT_ASSERT( d == 10 );
CPPUNIT_ASSERT( 10 == d );
 
std::string str = "abcde";
d.set<std::string>(str);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment