|
| 1 | +// Copyright (c) 2016 Dr. Colin Hirsch and Daniel Frey |
| 2 | +// Please see LICENSE for license or visit https://github.com/taocpp/json/ |
| 3 | + |
| 4 | +#ifndef TAOCPP_JSON_INCLUDE_COMPARISON_OPERATORS_HH |
| 5 | +#define TAOCPP_JSON_INCLUDE_COMPARISON_OPERATORS_HH |
| 6 | + |
| 7 | +#include "value.hh" |
| 8 | + |
| 9 | +namespace tao |
| 10 | +{ |
| 11 | + namespace json |
| 12 | + { |
| 13 | + template< template< typename ... > class Traits > |
| 14 | + bool operator== ( const basic_value< Traits > & lhs, const basic_value< Traits > & rhs ) noexcept |
| 15 | + { |
| 16 | + if ( lhs.type() == type::POINTER ) { |
| 17 | + if ( const auto * p = lhs.unsafe_get_pointer() ) { |
| 18 | + return * p == rhs; |
| 19 | + } |
| 20 | + else { |
| 21 | + return null == rhs; |
| 22 | + } |
| 23 | + } |
| 24 | + if ( lhs.type() != rhs.type() ) { |
| 25 | + if ( rhs.type() == type::POINTER ) { |
| 26 | + if ( const auto * p = rhs.unsafe_get_pointer() ) { |
| 27 | + return lhs == * p; |
| 28 | + } |
| 29 | + else { |
| 30 | + return lhs == null; |
| 31 | + } |
| 32 | + } |
| 33 | + if ( lhs.type() == type::SIGNED ) { |
| 34 | + if ( rhs.type() == type::UNSIGNED ) { |
| 35 | + const auto v = lhs.unsafe_get_signed(); |
| 36 | + return ( v >= 0 ) && ( static_cast< std::uint64_t >( v ) == rhs.unsafe_get_unsigned() ); |
| 37 | + } |
| 38 | + if ( rhs.type() == type::DOUBLE ) { |
| 39 | + return lhs.unsafe_get_signed() == rhs.unsafe_get_double(); |
| 40 | + } |
| 41 | + } |
| 42 | + else if ( lhs.type() == type::UNSIGNED ) { |
| 43 | + if ( rhs.type() == type::SIGNED ) { |
| 44 | + const auto v = rhs.unsafe_get_signed(); |
| 45 | + return ( v >= 0 ) && ( lhs.unsafe_get_unsigned() == static_cast< std::uint64_t >( v ) ); |
| 46 | + } |
| 47 | + if ( rhs.type() == type::DOUBLE ) { |
| 48 | + return lhs.unsafe_get_unsigned() == rhs.unsafe_get_double(); |
| 49 | + } |
| 50 | + } |
| 51 | + else if ( lhs.type() == type::DOUBLE ) { |
| 52 | + if ( rhs.type() == type::SIGNED ) { |
| 53 | + return lhs.unsafe_get_double() == rhs.unsafe_get_signed(); |
| 54 | + } |
| 55 | + if ( rhs.type() == type::UNSIGNED ) { |
| 56 | + return lhs.unsafe_get_double() == rhs.unsafe_get_unsigned(); |
| 57 | + } |
| 58 | + } |
| 59 | + return false; |
| 60 | + } |
| 61 | + switch ( lhs.type() ) { |
| 62 | + case type::NULL_: |
| 63 | + return true; |
| 64 | + case type::BOOL: |
| 65 | + return lhs.unsafe_get_bool() == rhs.unsafe_get_bool(); |
| 66 | + case type::SIGNED: |
| 67 | + return lhs.unsafe_get_signed() == rhs.unsafe_get_signed(); |
| 68 | + case type::UNSIGNED: |
| 69 | + return lhs.unsafe_get_unsigned() == rhs.unsafe_get_unsigned(); |
| 70 | + case type::DOUBLE: |
| 71 | + return lhs.unsafe_get_double() == rhs.unsafe_get_double(); |
| 72 | + case type::STRING: |
| 73 | + return lhs.unsafe_get_string() == rhs.unsafe_get_string(); |
| 74 | + case type::ARRAY: |
| 75 | + return lhs.unsafe_get_array() == rhs.unsafe_get_array(); |
| 76 | + case type::OBJECT: |
| 77 | + return lhs.unsafe_get_object() == rhs.unsafe_get_object(); |
| 78 | + case type::POINTER: |
| 79 | + break; // LCOV_EXCL_LINE |
| 80 | + } |
| 81 | + assert( false ); // LCOV_EXCL_LINE |
| 82 | + } |
| 83 | + |
| 84 | + template< template< typename ... > class Traits > |
| 85 | + bool operator< ( const basic_value< Traits > & lhs, const basic_value< Traits > & rhs ) noexcept |
| 86 | + { |
| 87 | + if ( lhs.type() == type::POINTER ) { |
| 88 | + if ( const auto * p = lhs.unsafe_get_pointer() ) { |
| 89 | + return * p < rhs; |
| 90 | + } |
| 91 | + else { |
| 92 | + return null < rhs; |
| 93 | + } |
| 94 | + } |
| 95 | + if ( lhs.type() != rhs.type() ) { |
| 96 | + if ( rhs.type() == type::POINTER ) { |
| 97 | + if ( const auto * p = rhs.unsafe_get_pointer() ) { |
| 98 | + return lhs < * p; |
| 99 | + } |
| 100 | + else { |
| 101 | + return lhs < null; |
| 102 | + } |
| 103 | + } |
| 104 | + if ( lhs.type() == type::SIGNED ) { |
| 105 | + if ( rhs.type() == type::UNSIGNED ) { |
| 106 | + const auto v = lhs.unsafe_get_signed(); |
| 107 | + return ( v < 0 ) || ( static_cast< std::uint64_t >( v ) < rhs.unsafe_get_unsigned() ); |
| 108 | + } |
| 109 | + if ( rhs.type() == type::DOUBLE ) { |
| 110 | + return lhs.unsafe_get_signed() < rhs.unsafe_get_double(); |
| 111 | + } |
| 112 | + } |
| 113 | + else if ( lhs.type() == type::UNSIGNED ) { |
| 114 | + if ( rhs.type() == type::SIGNED ) { |
| 115 | + const auto v = rhs.unsafe_get_signed(); |
| 116 | + return ( v >= 0 ) && ( lhs.unsafe_get_unsigned() < static_cast< std::uint64_t >( v ) ); |
| 117 | + } |
| 118 | + if ( rhs.type() == type::DOUBLE ) { |
| 119 | + return lhs.unsafe_get_unsigned() < rhs.unsafe_get_double(); |
| 120 | + } |
| 121 | + } |
| 122 | + else if ( lhs.type() == type::DOUBLE ) { |
| 123 | + if ( rhs.type() == type::SIGNED ) { |
| 124 | + return lhs.unsafe_get_double() < rhs.unsafe_get_signed(); |
| 125 | + } |
| 126 | + if ( rhs.type() == type::UNSIGNED ) { |
| 127 | + return lhs.unsafe_get_double() < rhs.unsafe_get_unsigned(); |
| 128 | + } |
| 129 | + } |
| 130 | + return lhs.type() < rhs.type(); |
| 131 | + } |
| 132 | + switch ( lhs.type() ) { |
| 133 | + case type::NULL_: |
| 134 | + return false; |
| 135 | + case type::BOOL: |
| 136 | + return lhs.unsafe_get_bool() < rhs.unsafe_get_bool(); |
| 137 | + case type::SIGNED: |
| 138 | + return lhs.unsafe_get_signed() < rhs.unsafe_get_signed(); |
| 139 | + case type::UNSIGNED: |
| 140 | + return lhs.unsafe_get_unsigned() < rhs.unsafe_get_unsigned(); |
| 141 | + case type::DOUBLE: |
| 142 | + return lhs.unsafe_get_double() < rhs.unsafe_get_double(); |
| 143 | + case type::STRING: |
| 144 | + return lhs.unsafe_get_string() < rhs.unsafe_get_string(); |
| 145 | + case type::ARRAY: |
| 146 | + return lhs.unsafe_get_array() < rhs.unsafe_get_array(); |
| 147 | + case type::OBJECT: |
| 148 | + return lhs.unsafe_get_object() < rhs.unsafe_get_object(); |
| 149 | + case type::POINTER: |
| 150 | + break; // LCOV_EXCL_LINE |
| 151 | + } |
| 152 | + assert( false ); // LCOV_EXCL_LINE |
| 153 | + } |
| 154 | + |
| 155 | + } // json |
| 156 | + |
| 157 | +} // tao |
| 158 | + |
| 159 | +#endif |
0 commit comments