Skip to content

Commit 357cf58

Browse files
committed
Move comparison operators to their own header
1 parent bccff61 commit 357cf58

File tree

3 files changed

+164
-142
lines changed

3 files changed

+164
-142
lines changed

include/tao/json.hh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66

77
#include "json/value.hh"
88

9+
#include "json/comparison_operators.hh"
10+
#include "json/object_operators.hh"
11+
912
#include "json/stream.hh"
1013
#include "json/to_string.hh"
14+
1115
#include "json/from_string.hh"
1216
#include "json/parse_file.hh"
17+
1318
#include "json/self_contained.hh"
1419

1520
#include "json/patch.hh"
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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

include/tao/json/value.hh

Lines changed: 0 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -951,148 +951,6 @@ namespace tao
951951
json::type m_type = json::type::NULL_;
952952
};
953953

954-
template< template< typename ... > class Traits >
955-
bool operator== ( const basic_value< Traits > & lhs, const basic_value< Traits > & rhs ) noexcept
956-
{
957-
if ( lhs.type() == type::POINTER ) {
958-
if ( const auto * p = lhs.unsafe_get_pointer() ) {
959-
return * p == rhs;
960-
}
961-
else {
962-
return null == rhs;
963-
}
964-
}
965-
if ( lhs.type() != rhs.type() ) {
966-
if ( rhs.type() == type::POINTER ) {
967-
if ( const auto * p = rhs.unsafe_get_pointer() ) {
968-
return lhs == * p;
969-
}
970-
else {
971-
return lhs == null;
972-
}
973-
}
974-
if ( lhs.type() == type::SIGNED ) {
975-
if ( rhs.type() == type::UNSIGNED ) {
976-
const auto v = lhs.unsafe_get_signed();
977-
return ( v >= 0 ) && ( static_cast< std::uint64_t >( v ) == rhs.unsafe_get_unsigned() );
978-
}
979-
if ( rhs.type() == type::DOUBLE ) {
980-
return lhs.unsafe_get_signed() == rhs.unsafe_get_double();
981-
}
982-
}
983-
else if ( lhs.type() == type::UNSIGNED ) {
984-
if ( rhs.type() == type::SIGNED ) {
985-
const auto v = rhs.unsafe_get_signed();
986-
return ( v >= 0 ) && ( lhs.unsafe_get_unsigned() == static_cast< std::uint64_t >( v ) );
987-
}
988-
if ( rhs.type() == type::DOUBLE ) {
989-
return lhs.unsafe_get_unsigned() == rhs.unsafe_get_double();
990-
}
991-
}
992-
else if ( lhs.type() == type::DOUBLE ) {
993-
if ( rhs.type() == type::SIGNED ) {
994-
return lhs.unsafe_get_double() == rhs.unsafe_get_signed();
995-
}
996-
if ( rhs.type() == type::UNSIGNED ) {
997-
return lhs.unsafe_get_double() == rhs.unsafe_get_unsigned();
998-
}
999-
}
1000-
return false;
1001-
}
1002-
switch ( lhs.type() ) {
1003-
case type::NULL_:
1004-
return true;
1005-
case type::BOOL:
1006-
return lhs.unsafe_get_bool() == rhs.unsafe_get_bool();
1007-
case type::SIGNED:
1008-
return lhs.unsafe_get_signed() == rhs.unsafe_get_signed();
1009-
case type::UNSIGNED:
1010-
return lhs.unsafe_get_unsigned() == rhs.unsafe_get_unsigned();
1011-
case type::DOUBLE:
1012-
return lhs.unsafe_get_double() == rhs.unsafe_get_double();
1013-
case type::STRING:
1014-
return lhs.unsafe_get_string() == rhs.unsafe_get_string();
1015-
case type::ARRAY:
1016-
return lhs.unsafe_get_array() == rhs.unsafe_get_array();
1017-
case type::OBJECT:
1018-
return lhs.unsafe_get_object() == rhs.unsafe_get_object();
1019-
case type::POINTER:
1020-
break; // LCOV_EXCL_LINE
1021-
}
1022-
assert( false ); // LCOV_EXCL_LINE
1023-
}
1024-
1025-
template< template< typename ... > class Traits >
1026-
bool operator< ( const basic_value< Traits > & lhs, const basic_value< Traits > & rhs ) noexcept
1027-
{
1028-
if ( lhs.type() == type::POINTER ) {
1029-
if ( const auto * p = lhs.unsafe_get_pointer() ) {
1030-
return * p < rhs;
1031-
}
1032-
else {
1033-
return null < rhs;
1034-
}
1035-
}
1036-
if ( lhs.type() != rhs.type() ) {
1037-
if ( rhs.type() == type::POINTER ) {
1038-
if ( const auto * p = rhs.unsafe_get_pointer() ) {
1039-
return lhs < * p;
1040-
}
1041-
else {
1042-
return lhs < null;
1043-
}
1044-
}
1045-
if ( lhs.type() == type::SIGNED ) {
1046-
if ( rhs.type() == type::UNSIGNED ) {
1047-
const auto v = lhs.unsafe_get_signed();
1048-
return ( v < 0 ) || ( static_cast< std::uint64_t >( v ) < rhs.unsafe_get_unsigned() );
1049-
}
1050-
if ( rhs.type() == type::DOUBLE ) {
1051-
return lhs.unsafe_get_signed() < rhs.unsafe_get_double();
1052-
}
1053-
}
1054-
else if ( lhs.type() == type::UNSIGNED ) {
1055-
if ( rhs.type() == type::SIGNED ) {
1056-
const auto v = rhs.unsafe_get_signed();
1057-
return ( v >= 0 ) && ( lhs.unsafe_get_unsigned() < static_cast< std::uint64_t >( v ) );
1058-
}
1059-
if ( rhs.type() == type::DOUBLE ) {
1060-
return lhs.unsafe_get_unsigned() < rhs.unsafe_get_double();
1061-
}
1062-
}
1063-
else if ( lhs.type() == type::DOUBLE ) {
1064-
if ( rhs.type() == type::SIGNED ) {
1065-
return lhs.unsafe_get_double() < rhs.unsafe_get_signed();
1066-
}
1067-
if ( rhs.type() == type::UNSIGNED ) {
1068-
return lhs.unsafe_get_double() < rhs.unsafe_get_unsigned();
1069-
}
1070-
}
1071-
return lhs.type() < rhs.type();
1072-
}
1073-
switch ( lhs.type() ) {
1074-
case type::NULL_:
1075-
return false;
1076-
case type::BOOL:
1077-
return lhs.unsafe_get_bool() < rhs.unsafe_get_bool();
1078-
case type::SIGNED:
1079-
return lhs.unsafe_get_signed() < rhs.unsafe_get_signed();
1080-
case type::UNSIGNED:
1081-
return lhs.unsafe_get_unsigned() < rhs.unsafe_get_unsigned();
1082-
case type::DOUBLE:
1083-
return lhs.unsafe_get_double() < rhs.unsafe_get_double();
1084-
case type::STRING:
1085-
return lhs.unsafe_get_string() < rhs.unsafe_get_string();
1086-
case type::ARRAY:
1087-
return lhs.unsafe_get_array() < rhs.unsafe_get_array();
1088-
case type::OBJECT:
1089-
return lhs.unsafe_get_object() < rhs.unsafe_get_object();
1090-
case type::POINTER:
1091-
break; // LCOV_EXCL_LINE
1092-
}
1093-
assert( false ); // LCOV_EXCL_LINE
1094-
}
1095-
1096954
using value = basic_value< traits >;
1097955

1098956
} // json

0 commit comments

Comments
 (0)