Skip to content

Commit c5c9235

Browse files
committed
Define relative comparison operators for fly::Json
This allows fly::Json to be used as the template parameter of other containers like std::set which require a comparator function.
1 parent f95af44 commit c5c9235

File tree

3 files changed

+515
-0
lines changed

3 files changed

+515
-0
lines changed

fly/types/json/json.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,61 @@ bool operator!=(Json::const_reference json1, Json::const_reference json2)
777777
return !(json1 == json2);
778778
}
779779

780+
//==================================================================================================
781+
bool operator<(Json::const_reference json1, Json::const_reference json2)
782+
{
783+
auto visitor = [&json1, &json2](const auto &value1, const auto &value2) -> bool
784+
{
785+
using S1 = decltype(value1);
786+
using S2 = decltype(value2);
787+
788+
if constexpr (JsonNull<S1> && JsonNull<S2>)
789+
{
790+
return false;
791+
}
792+
else if constexpr (
793+
(JsonFloatingPoint<S1> && JsonNumber<S2>) || (JsonFloatingPoint<S2> && JsonNumber<S1>))
794+
{
795+
const auto fvalue1 = static_cast<json_floating_point_type>(value1);
796+
const auto fvalue2 = static_cast<json_floating_point_type>(value2);
797+
798+
return fvalue1 < fvalue2;
799+
}
800+
else if constexpr (JsonNumber<S1> && JsonNumber<S2>)
801+
{
802+
return value1 < static_cast<S1>(value2);
803+
}
804+
else if constexpr (fly::SameAsAny<S1, S2>)
805+
{
806+
return value1 < value2;
807+
}
808+
else
809+
{
810+
return json1.m_value.index() < json2.m_value.index();
811+
}
812+
};
813+
814+
return std::visit(std::move(visitor), json1.m_value, json2.m_value);
815+
}
816+
817+
//==================================================================================================
818+
bool operator<=(Json::const_reference json1, Json::const_reference json2)
819+
{
820+
return !(json2 < json1);
821+
}
822+
823+
//==================================================================================================
824+
bool operator>(Json::const_reference json1, Json::const_reference json2)
825+
{
826+
return !(json1 <= json2);
827+
}
828+
829+
//==================================================================================================
830+
bool operator>=(Json::const_reference json1, Json::const_reference json2)
831+
{
832+
return !(json1 < json2);
833+
}
834+
780835
//==================================================================================================
781836
json_string_type Json::validate_string(json_string_type &&value)
782837
{

fly/types/json/json.hpp

+45
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,51 @@ class Json
13851385
*/
13861386
friend bool operator!=(const_reference json1, const_reference json2);
13871387

1388+
/**
1389+
* Less-than operator. The first Json instance is less-than the second Json instance if one of
1390+
* the following is true:
1391+
*
1392+
* 1. One of the two JSON types are floating-point, the other is a numeric type (signed,
1393+
* unsigned, or float) and the default less-than operator returns true after converting both
1394+
* types to floating-point.
1395+
* 2. The two Json instances are an integer type (signed or unsigned) and have the default
1396+
* less-than operator returns true after converting the second Json value to the same type as
1397+
* the first Json value.
1398+
* 3. The two Json instances are of the same type and the default less-than operator returns
1399+
* true.
1400+
* 4. The two Json instances are of incompatible types and the type of the first Json instance
1401+
* is considered less-than the type of the second Json instance. The ordering is determined
1402+
* by the order of the types listed in the fly::json_type alias.
1403+
*
1404+
* @return True if the first Json instance is less-than the second Json instance.
1405+
*/
1406+
friend bool operator<(const_reference json1, const_reference json2);
1407+
1408+
/**
1409+
* Less-than-or-equal-to operator. The first Json instance is less-than-or-equal-to the second
1410+
* Json instance if any of the conditions of the equality or less-than operators are met.
1411+
*
1412+
* @return True if the first Json instance is less-than-or-equal-to the second Json instance.
1413+
*/
1414+
friend bool operator<=(const_reference json1, const_reference json2);
1415+
1416+
/**
1417+
* Greater-than operator. The first Json instance is greater-than the second Json instance if
1418+
* none of the conditions of the less-than-or-equal-to operator are met.
1419+
*
1420+
* @return True if the first Json instance is greater-than the second Json instance.
1421+
*/
1422+
friend bool operator>(const_reference json1, const_reference json2);
1423+
1424+
/**
1425+
* Greater-than-or-equal-to operator. The first Json instance is greater-than-or-equal-to the
1426+
* second Json instance if any of the conditions of the equality or greater-than operators are
1427+
* met.
1428+
*
1429+
* @return True if the first Json instance is greater-than-or-equal-to the second Json instance.
1430+
*/
1431+
friend bool operator>=(const_reference json1, const_reference json2);
1432+
13881433
private:
13891434
friend iterator;
13901435
friend const_iterator;

0 commit comments

Comments
 (0)