Skip to content

Commit

Permalink
Support comparison of integer and double values
Browse files Browse the repository at this point in the history
  • Loading branch information
d-frey committed Apr 24, 2016
1 parent 6ad1d60 commit 0949d2b
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 1 deletion.
139 changes: 138 additions & 1 deletion include/tao/json/value.hh
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ namespace tao
}
};


template< typename T >
struct totally_ordered< T, null_t, type::NULL_ >
: operators::totally_ordered< T, null_t >
Expand Down Expand Up @@ -147,6 +148,130 @@ namespace tao
return lhs.type() > type::NULL_;
}
};

template< typename T, typename U >
struct totally_ordered< T, U, type::INTEGER >
: operators::totally_ordered< T, U >
{
friend bool operator==( const T & lhs, const U & rhs ) noexcept
{
if ( lhs.type() == type::POINTER ) {
if ( const auto * p = lhs.unsafe_get_pointer() ) {
return * p == rhs;
}
else {
return false;
}
}
if ( lhs.type() == type::INTEGER ) {
return lhs.unsafe_get_integer() == rhs;
}
if ( lhs.type() == type::DOUBLE ) {
return lhs.unsafe_get_double() == rhs;
}
return false;
}

friend bool operator<( const T & lhs, const U & rhs ) noexcept
{
if ( lhs.type() == type::POINTER ) {
if ( const auto * p = lhs.unsafe_get_pointer() ) {
return * p < rhs;
}
else {
return true;
}
}
if ( lhs.type() == type::INTEGER ) {
return lhs.unsafe_get_integer() < rhs;
}
if ( lhs.type() == type::DOUBLE ) {
return lhs.unsafe_get_double() < rhs;
}
return lhs.type() < type::INTEGER;
}

friend bool operator>( const T & lhs, const U & rhs ) noexcept
{
if ( lhs.type() == type::POINTER ) {
if ( const auto * p = lhs.unsafe_get_pointer() ) {
return * p > rhs;
}
else {
return false;
}
}
if ( lhs.type() == type::INTEGER ) {
return lhs.unsafe_get_integer() > rhs;
}
if ( lhs.type() == type::DOUBLE ) {
return lhs.unsafe_get_double() > rhs;
}
return lhs.type() > type::INTEGER;
}
};

template< typename T, typename U >
struct totally_ordered< T, U, type::DOUBLE >
: operators::totally_ordered< T, U >
{
friend bool operator==( const T & lhs, const U & rhs ) noexcept
{
if ( lhs.type() == type::POINTER ) {
if ( const auto * p = lhs.unsafe_get_pointer() ) {
return * p == rhs;
}
else {
return false;
}
}
if ( lhs.type() == type::INTEGER ) {
return lhs.unsafe_get_integer() == rhs;
}
if ( lhs.type() == type::DOUBLE ) {
return lhs.unsafe_get_double() == rhs;
}
return false;
}

friend bool operator<( const T & lhs, const U & rhs ) noexcept
{
if ( lhs.type() == type::POINTER ) {
if ( const auto * p = lhs.unsafe_get_pointer() ) {
return * p < rhs;
}
else {
return true;
}
}
if ( lhs.type() == type::INTEGER ) {
return lhs.unsafe_get_integer() < rhs;
}
if ( lhs.type() == type::DOUBLE ) {
return lhs.unsafe_get_double() < rhs;
}
return lhs.type() < type::DOUBLE;
}

friend bool operator>( const T & lhs, const U & rhs ) noexcept
{
if ( lhs.type() == type::POINTER ) {
if ( const auto * p = lhs.unsafe_get_pointer() ) {
return * p > rhs;
}
else {
return false;
}
}
if ( lhs.type() == type::INTEGER ) {
return lhs.unsafe_get_integer() > rhs;
}
if ( lhs.type() == type::DOUBLE ) {
return lhs.unsafe_get_double() > rhs;
}
return lhs.type() > type::DOUBLE;
}
};
}

template< template< typename ... > class Traits >
Expand All @@ -162,8 +287,8 @@ namespace tao
internal::totally_ordered< value_base< Traits >, unsigned int, type::INTEGER >,
internal::totally_ordered< value_base< Traits >, signed long, type::INTEGER >,
internal::totally_ordered< value_base< Traits >, signed long long, type::INTEGER >,
internal::totally_ordered< value_base< Traits >, double, type::DOUBLE >,
internal::totally_ordered< value_base< Traits >, float, type::DOUBLE >,
internal::totally_ordered< value_base< Traits >, double, type::DOUBLE >,
internal::totally_ordered< value_base< Traits >, std::string, type::STRING >,
internal::totally_ordered< value_base< Traits >, const char*, type::STRING >,
internal::totally_ordered< value_base< Traits >, std::vector< value_base< Traits > >, type::ARRAY >,
Expand Down Expand Up @@ -923,6 +1048,12 @@ namespace tao
return lhs == null;
}
}
if ( lhs.type() == type::INTEGER && rhs.type() == type::DOUBLE ) {
return lhs.unsafe_get_integer() == rhs.unsafe_get_double();
}
if ( lhs.type() == type::DOUBLE && rhs.type() == type::INTEGER ) {
return lhs.unsafe_get_double() == rhs.unsafe_get_integer();
}
return false;
}
switch ( lhs.type() ) {
Expand Down Expand Up @@ -966,6 +1097,12 @@ namespace tao
return lhs < null;
}
}
if ( lhs.type() == type::INTEGER && rhs.type() == type::DOUBLE ) {
return lhs.unsafe_get_integer() < rhs.unsafe_get_double();
}
if ( lhs.type() == type::DOUBLE && rhs.type() == type::INTEGER ) {
return lhs.unsafe_get_double() < rhs.unsafe_get_integer();
}
return lhs.type() < rhs.type();
}
switch ( lhs.type() ) {
Expand Down
8 changes: 8 additions & 0 deletions src/test/json/create.cc
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,14 @@ namespace tao
const value a( "foo" );
const value b( a );
TEST_ASSERT( a.get_string() == b.get_string() );
} {
const value a = 4;
const value b = 4.0;
TEST_ASSERT( a == b );
TEST_ASSERT( a == 4 );
TEST_ASSERT( a == 4.0 );
TEST_ASSERT( b == 4 );
TEST_ASSERT( b == 4.0 );
}
}

Expand Down

0 comments on commit 0949d2b

Please sign in to comment.