feat: compare.h header added with checks against zero

Resolves #487
This commit is contained in:
Mateusz Pusz
2023-09-13 08:55:06 +02:00
parent c4ad3039b3
commit cbcc6f4627
3 changed files with 156 additions and 6 deletions

View File

@@ -24,6 +24,7 @@
#include "ranged_representation.h"
#include <mp-units/bits/fmt_hacks.h>
#include <mp-units/compare.h>
#include <mp-units/format.h>
#include <mp-units/math.h>
#include <mp-units/quantity.h>
@@ -78,7 +79,7 @@ using longitude = mp_units::quantity_point<mp_units::si::degree, prime_meridian,
template<class CharT, class Traits, typename T>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const latitude<T>& lat)
{
if (lat > latitude<T>::zero())
if (is_gt_zero(lat))
return os << lat.quantity_from_origin() << " N";
else
return os << -lat.quantity_from_origin() << " S";
@@ -87,7 +88,7 @@ std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>&
template<class CharT, class Traits, typename T>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const longitude<T>& lon)
{
if (lon > longitude<T>::zero())
if (is_gt_zero(lon))
return os << lon.quantity_from_origin() << " E";
else
return os << -lon.quantity_from_origin() << " W";
@@ -137,8 +138,8 @@ struct MP_UNITS_STD_FMT::formatter<geographic::latitude<T>> :
auto format(geographic::latitude<T> lat, FormatContext& ctx)
{
formatter<typename geographic::latitude<T>::quantity_type>::format(
lat > geographic::latitude<T>::zero() ? lat.quantity_from_origin() : -lat.quantity_from_origin(), ctx);
MP_UNITS_STD_FMT::format_to(ctx.out(), "{}", lat > geographic::latitude<T>::zero() ? " N" : "S");
is_gt_zero(lat) ? lat.quantity_from_origin() : -lat.quantity_from_origin(), ctx);
MP_UNITS_STD_FMT::format_to(ctx.out(), "{}", is_gt_zero(lat) ? " N" : "S");
return ctx.out();
}
};
@@ -150,8 +151,8 @@ struct MP_UNITS_STD_FMT::formatter<geographic::longitude<T>> :
auto format(geographic::longitude<T> lon, FormatContext& ctx)
{
formatter<typename geographic::longitude<T>::quantity_type>::format(
lon > geographic::longitude<T>::zero() ? lon.quantity_from_origin() : -lon.quantity_from_origin(), ctx);
MP_UNITS_STD_FMT::format_to(ctx.out(), "{}", lon > geographic::longitude<T>::zero() ? " E" : " W");
is_gt_zero(lon) ? lon.quantity_from_origin() : -lon.quantity_from_origin(), ctx);
MP_UNITS_STD_FMT::format_to(ctx.out(), "{}", is_gt_zero(lon) ? " E" : " W");
return ctx.out();
}
};

View File

@@ -0,0 +1,95 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#pragma once
#include <compare>
namespace mp_units {
template<typename T>
requires requires {
{
T::zero()
} -> std::equality_comparable_with<T>;
}
[[nodiscard]] constexpr bool is_eq_zero(T v)
{
return v == T::zero();
}
template<typename T>
requires requires {
{
T::zero()
} -> std::equality_comparable_with<T>;
}
[[nodiscard]] constexpr bool is_neq_zero(T v)
{
return v != T::zero();
}
template<typename T>
requires requires {
{
T::zero()
} -> std::three_way_comparable_with<T>;
}
[[nodiscard]] constexpr bool is_lt_zero(T v)
{
return v < T::zero();
}
template<typename T>
requires requires {
{
T::zero()
} -> std::three_way_comparable_with<T>;
}
[[nodiscard]] constexpr bool is_gt_zero(T v)
{
return v > T::zero();
}
template<typename T>
requires requires {
{
T::zero()
} -> std::three_way_comparable_with<T>;
}
[[nodiscard]] constexpr bool is_lteq_zero(T v)
{
return v <= T::zero();
}
template<typename T>
requires requires {
{
T::zero()
} -> std::three_way_comparable_with<T>;
}
[[nodiscard]] constexpr bool is_gteq_zero(T v)
{
return v >= T::zero();
}
} // namespace mp_units

View File

@@ -0,0 +1,54 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <mp-units/compare.h>
#include <mp-units/systems/si/si.h>
namespace {
using namespace mp_units::si::unit_symbols;
static_assert(is_eq_zero(0 * m));
static_assert(!is_eq_zero(1 * m));
static_assert(!is_eq_zero(-1 * m));
static_assert(!is_neq_zero(0 * m));
static_assert(is_neq_zero(1 * m));
static_assert(is_neq_zero(-1 * m));
static_assert(!is_lt_zero(0 * m));
static_assert(!is_lt_zero(1 * m));
static_assert(is_lt_zero(-1 * m));
static_assert(!is_gt_zero(0 * m));
static_assert(is_gt_zero(1 * m));
static_assert(!is_gt_zero(-1 * m));
static_assert(is_lteq_zero(0 * m));
static_assert(!is_lteq_zero(1 * m));
static_assert(is_lteq_zero(-1 * m));
static_assert(is_gteq_zero(0 * m));
static_assert(is_gteq_zero(1 * m));
static_assert(!is_gteq_zero(-1 * m));
} // namespace